Re: [GI] Generic array type



Have you explored Gee?

It uses Vala's generics and provides API to iterate on collections. It has
lot of implementations for generic collections you can use with virtually
any GTYPE object.

Just consider:

a) Generics returning values are not supported in Python and GJS but you
just need to add convenient methods returning actual GType values (GBoxed
ones for example).

b) You can implement Gee interfaces for yourself but requires to create
GObject classes implementing 2 or 3 interfaces. The result helps to avoid
using any Gee container overhead.

c) Vala generics are hard to use from C. You can add wrapper methods to
make your users lives easy (applies to Python and GJS too).

d ) You can use Vala to create your wrappers but add Vala compiler
dependency, and yes just use -C to convert Vala code to C and add resulting
file to your existing list of C code files.
El feb 13, 2015 12:57 PM, "Colomban Wendling" <lists ban herbesfolles org>
escribió:

Hi,

I'm trying to generate a gobject-introspection binding for a library,
and am facing some problems I couldn't find the answer to.

First, no, the library isn't really GObjec-based.  However that was not
the problem, I added appropriate boxed GTypes, and the GIR mostly works
[1][2].  So, my GIR works mostly as expected from Vala [3], Python and
GJS.  Great.

However, here comes the tricky problem:  my library has a custom generic
value type, which supports integers, reals, strings, and arrays.  So, I
wanted to add wrapping API that take GValues to integrate seamlessly
with the languages:

..._push_gvalue(..., GValue *value, ...);

so that the higher level language using the GIR could pass natural types:

...push(..., 42);
...push(..., "hello");
...push(..., [1, 2, "hello", [2, 3, 4]]);

It works just fine with integers, reals and strings.  But it doesn't do
anything good with arrays.
The problem is, I guess, because GI doesn't have a canonical generic
array type [4], so language bindings don't know what they should do when
trying to pass an array as a GValue.

Python passes in a boxed PyObject (what am I supposed to do with that?),
GJS basically errors out complaining it can't convert the array, and
Vala doesn't compile code passing in a native array (`int[]` for
example, nor even `GValue?[]`).

With Vala I can make it work by explicitly giving a `Array<Value?>`,
which properly gets boxed, and that I can read (of course, it's plain C
after all), but I can't even find these APIs in Python or GJS.

Of course, I could add separated API for arrays like this:

..._push_gvalue_array(..., GValue **values, gsize n_values, ...);

It works, but of course only for a single-level array (fails the same
with an array containing array members) -- and having separate API for
the array case is also not very nice.

I also gave GVariant a quick shot, but while it works quite well with
Vala (well, similarly to GValue, with the addition it knows about
arrays), it doesn't seem to have any kind of magic integration for
creating variants with Python or GJS [5] -- and then becomes extremely
verbose.
Also, I'm not quite sure GVariant is meant to be used that way, so maybe
it's abusing the API?


Anyway, my question is:  is there a way to pass generic arrays
implicitly as GValue (e.g. a GArray of GValue*, but I'm open to anything
usable from C), or is this a lost cause?

And if my only way is to manually build arrays, does Python and GJS have
a way of building a GArray, or some other similar GLib array type?
There is no constructor for GLib.Array in GJS apparently, and I didn't
find any in Python either.


BTW, I similarly need to be able to return an array and this array be
usable by the calling language.  Returning a GArray boxed in a GValue
"works", but that array is not really usable (not interface to access it
in Python, and GJS tells me without surprise that it is "Unable to
introspect element-type of container in GValue").  GVariant seems a
little more usable there.


Thanks for reading through :)

Regards,
Colomban


Notes:

[1] though, while it works with GJS and Python, apparently Seed doesn't
allow boxed types of unknown size and asserts out when calling the
constructor.

[2] but apparently boxed type constructors can't have arguments, which
is somewhat problematic -- but that's not important for me, at least not
now.

[3] Vala is a bit annoying because of
https://bugzilla.gnome.org/show_bug.cgi?id=670167, but I get it and it
can be worked around.

[4] there is GValueArray, but it's now deprecated.

[5] but Python seems to have some support for reading them, as they are
iterable, printable, and (to some extent) usable as a value
_______________________________________________
gtk-app-devel-list mailing list
gtk-app-devel-list gnome org
https://mail.gnome.org/mailman/listinfo/gtk-app-devel-list



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