Re: [Vala] Type.from_name() / unregistered types



Thanks!

I've looked some more into it but went with a hardcoded name -> type map
for now. It's a nice-to-have to improve maintainability and I'll get
back to it once the project is bit further along the road.

Thanks again,
  Dennis

Am 23.08.2012 21:03, schrieb Evan Nemerson:
On Thu, 2012-08-23 at 12:20 +0200, Dennis Möhlmann wrote:
Hello,

I'm looking for a way to to create instances of a class (all subclasses
of a common parent) by its class-name (string, not identifier).
Type.from_name() + Object.new() seem to work well, but only if the type
has been used before (i.e. instanced or typeof()'ed). Otherwise the
result is Type.INVALID.
I have no previous knowledge of the GObject system, but apparently a
type needs to be registered and vala does that automatically once the
type used in some way. Is it possible to do this manually with only the
string name of the type available?

Maybe.  You need to call the *_get_type function which Vala generates
for each type.  If you have a string representing the type name you
could parse it to guess the right function name (e.g., FooBar ->
foo_bar_get_type), then use GLib.Module.open + GLib.Module.symbol to get
an address.  Keep in mind, though, that you need the type names at the C
level, not the Vala level, which may be completely different.  Also, if
you're integrating with code written in C the names of the get_type
functions may not be the same as they would have been had they been
generated by valac.

You could also try gobject-introspection.

It's hard to say for sure without more knowledge of what you're trying
to do, but my guess is that something like
https://live.gnome.org/Vala/TypeModules (or possibly libpeas) would be a
better way to go.


-Evan




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