Re: GLib/perl and GArray, GType




On Dec 9, 2008, at 5:09 PM, Wouter Verhelst wrote:

I've been using GObject and the GLib perl module to write some
object/relational mapper. Mostly for fun (stop looking at me weird!),
but it's working out quite well.

Hey, some of us like writing language bindings, so you're in good company for weird.


One thing I'm not very happy about is the way I have to work with
GArrays and GTypes in the XS API. Every time my API returns more than
one object, it uses GArrays to do this (although I'm thinking of
changing that to GLists). As far as I can see in "perldoc Glib::xsapi", the perl module doesn't do any black magic to make sure that GArrays (or GLists, or GSLists, or whatever) are automatically converted from and to
perl lists. Is this correct? If so, it's not a huge deal--I can live
with having to allocate an array and deallocate after having converted
it to a perl list, but it would be nice to not have to do this *every
time*. It gets boring :-)

The GArray and GList do not specify the type of what they contain. Therefore, there's no generic way to convert them. You're always open to somebody deciding to be cute and have the first element be an object, the second be an int cast as a pointer, and the third be a boxed type. You'll crash on the second one, and probably the third one, too.

Now, it's fairly easy for you to create a function that knows how to convert an array of whatever, and create a typemap for that. Then you can use that all over the place in your own code.

By the way, the glib folks have discussed this before, in the context of the introspection api, for all the same reasons.



Secondly, "perldoc Glib" suggests that every time you want to use a
GType, the Perl version will be that you use a package name. A
particular part of my API has a property that is a GType; it uses
g_param_spec_gtype(), and this works from C. Unfortunately, the Glib
module doesn't see this; I get errors about "Foo::Bar" not being a valid
integer (despite it having been registered as a GObject package). Is
there any way to make this work, other than an ugly hack of adding a
perl-specific 'foo_bar_get_type_property' sub in the XS file?

Hey, GParamSpecGType looks damned handy. It's new in 2.10, and appears to be unsupported in trunk of perl-Glib. It looks like it uses GValues that hold G_TYPE_GTYPE, so we should be able to set up marshaling logic in GValue.xs that calls gperl_package_from_type() and gperl_type_from_package() to do the magical conversions.


I had knocked together a patch that implements this, but working the kinks out of value coercion in the unit test expanded to fill all my available time for the evening, which was about half an hour. :- ( I'll try to get you a working patch later this week.


--
The one difference between Dali and a crazy man is very simple: Dali is not crazy at all.
  -- Salvador Dali





[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]