Re: [Vala] Inspecting class hierarchy



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]