After an evaluation, GNOME has moved from Bugzilla to GitLab. Learn more about GitLab.
No new issues can be reported in GNOME Bugzilla anymore.
To report an issue in a GNOME project, go to GNOME GitLab.
Do not go to GNOME Gitlab for: Bluefish, Doxygen, GnuCash, GStreamer, java-gnome, LDTP, NetworkManager, Tomboy.
Bug 669586 - Use GLib.Value to cast between values/GObjects
Use GLib.Value to cast between values/GObjects
Status: RESOLVED OBSOLETE
Product: vala
Classification: Core
Component: Semantic Analyzer
0.15.x
Other Linux
: Normal enhancement
: ---
Assigned To: Vala maintainers
Vala maintainers
Depends on:
Blocks:
 
 
Reported: 2012-02-07 16:51 UTC by Daniel Espinosa
Modified: 2018-05-22 14:21 UTC
See Also:
GNOME target: ---
GNOME version: ---



Description Daniel Espinosa 2012-02-07 16:51:53 UTC
GValue, AFAIK, is used intensively in Vala to manage values in diferent ways, allowing to pass "any kind of values" to functions that requires GLib.Value parameters, Vala converts this values to GValue and then pass to the function, at least this is for most GDA functions, is very simple and convenient, excusing you to create/init/set a Value before to pass to a function.

Python and pygobject, allows you the same and "cast" returned GLib.Value to its actual value (string, gobject, or any it holds) you don't need to handle the convertion or know what type of value it holds, python does it for you.

If this is possible for GDA and Python, why is not possible to:

a) Use registered functions to transform a GLib.Value to other value types, this is done when you use g_value_register_transform_func in GType your_type_get_type declaration

b) Detect code like var str = (string) MyObject, and use above registered functions to "try" to cast from the given object to another, of course this means that this kind of cast will fail on runtime when g_value_type_transformable () returns false.

c) Declare in a GBoxed/GObject derived type the compatible types to transform to, using a sintax like:

[Compact]
class MyType : Object {
   public name { get; set; }
   public string cast<MyType> { return name; } // From MyType to string
   public MyType cast<string>  // From string to MyType
   {
      var obj = new MyType ();
      obj.name = str;
      return obj;
   }
}

The above code could help compiler to detect at compilate time incompatible casts and could generate a C code like:

static void
my_type_to_string (const GValue *src, GValue *dest)
{
    g_return_if_fail (G_VALUE_HOLDS_STRING (dest) && GDA_VALUE_HOLDS_MY_TYPE (src));
    MyType *t = (MyType) g_value_get_boxed (src); 
    g_value_set_string (dest, t->name);
}


static void
my_type_from_string (const GValue *src, GValue *dest)
{
    g_return_if_fail (G_VALUE_HOLDS_STRING (src) && GDA_VALUE_HOLDS_MY_TYPE (dest));
    MyType *t = g_new0 (1, MyType);
    my_type_set_name (t, g_value_get_string (src));
    g_value_take_boxed (dest, t);
}

GType
my_type_get_type (void)
{
       static GType type = 0;

       if (G_UNLIKELY (type == 0)) {
               type = g_boxed_type_register_static ("MyType",
                                                    (GBoxedCopyFunc) my_type_copy,
                                                    (GBoxedFreeFunc) my_type_free);

               g_value_register_transform_func (G_TYPE_STRING,
                                                type,
                                                string_to_my_type);

               g_value_register_transform_func (type,
                                                G_TYPE_STRING,
                                                my_type_to_string);
       }

       return type;
}
Comment 1 GNOME Infrastructure Team 2018-05-22 14:21:29 UTC
-- GitLab Migration Automatic Message --

This bug has been migrated to GNOME's GitLab instance and has been closed from further activity.

You can subscribe and participate further through the new bug through this link to our GitLab instance: https://gitlab.gnome.org/GNOME/vala/issues/281.