Re: [Vala] Inspecting class hierarchy



Hello,

I do something similar in matrix-glib-sdk[1]: I get one specific key from a
JSON object, and based on that key I create different types of objects. For
that I have a type registry, which tells me what objects must be created
for what event type.

Your GtkBuilder approach also looks nice, although GTK would be a too heavy
dependency for my project.

Best,
Gergely

[1]
https://github.com/gergelypolonkai/matrix-glib-sdk/blob/develop/src/matrix-event-base.vala
On Mar 21, 2016 10:37 PM, "Alan Manuel Gloria" <almkglor gmail com> wrote:

Yes, that's what I thought too.

However, looking at GtkBuilder's source code, it has a function called
type_name_mangle which converts a name like "GtkButton" to
"gtk_button_get_type", then uses GModule to load that function.  Vala's C
generation seems to create a function of the same/similar naming
convention, so the equivalent of typeof(Weapon) in C is weapon_get_type().

As a sketch, I suppose something like this ought to work:

[CCode (has_target=false)]
private delegate Type TypeOf();
...
var module = Module.open(null, 0); // opens the program itself instead of
a different module
TypeOf get_type;
module.symbol("weapon_get_type", (void*) &get_type); // the real program
needs to generate the name and check the return value here
Type type = get_type();

Object obj = Object.create(type, "damage", 10);
...


On Tue, Mar 22, 2016 at 2:19 AM, Felipe Lavratti <felipelav gmail com>
wrote:

Hello, this is the minimum workaround I found to work, there might be a
simpler way of forcing the registration of non-literally used classes in
the GObject system.

public class Weapon : Object {
  public uint damage { get; construct; }
}

int main () {

    /* Force class registration here, comment this line to see the
execution failing. */
    var klass = typeof(Weapon);

    Type WeaponType = Type.from_name("Weapon");
    Object weapon = Object.new(WeaponType, damage: 10);

    GLib.message("weapon.damage = %u", (weapon as Weapon).damage);

    return 0;
}


On Mon, Mar 21, 2016 at 10:47 AM Gergely Polonkai <gergely polonkai eu>
wrote:

Hello,

for the first sight your format looks verybsimilar to key-value files
already supported by GLib:

https://developer.gnome.org/glib/stable/glib-Key-value-file-parser.html
http://valadoc.org/#!api=glib-2.0/GLib.KeyFile

Are you sure it won't work for your problem? If it would, you may want to
subclass it.

Best,
Gergely
On Mar 21, 2016 2:26 PM, "Alan Manuel Gloria" <almkglor gmail com>
wrote:

Hi Vala world,

I'm trying to make some kind of description loader.  I have a base
class
like so:

class Concept : Object {
  string name { get; construct; }
  string desc { get; construct; }
}

And one or more classes, e.g.:

class Component : Concept {
  uint size { get; construct; }
  uint cost { get; construct; }
}
class Weapon : Component {
  uint damage { get; construct; }
}

My goal is to parse some text file like so:

[Blaster I]
type = Weapon
desc = A simple blaster.
size = 20
cost = 200
damage = 1

Then the parser class will provide a collection of Concept objects
indexable by name and type.

My question is, how do I best do this?  Gtk.Buildable seems to
approximate
what I want, so I suppose I need to figure out how that works.

My initial research seems to suggest that I need to use GLib.Type
somehow,
but my understanding is that the exact type does not get created until
it
is used.  It seems to me that I'd need code like:

var p = new MyTextFileParser();
p.register(typeof(Component));
p.register(typeof(Weapon));
// ... and so on ...

Thanks in advance.

Sincerely,
AmkG
_______________________________________________
vala-list mailing list
vala-list gnome org
https://mail.gnome.org/mailman/listinfo/vala-list

_______________________________________________
vala-list mailing list
vala-list gnome org
https://mail.gnome.org/mailman/listinfo/vala-list





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