Am Thu, 05 Jun 2008 12:25:02 +0200 schrieb Xavier Bestel <[EMAIL PROTECTED]>:
> On Thu, 2008-06-05 at 08:59 +0200, Jean-Yves Lefort wrote: > > Likewise, you can implement a class "Foo" containing an int property > > "bar" using the GObject way: > > > > #define G_TYPE_FOO (g_foo_get_type()) > > #define G_FOO(obj) > > (G_TYPE_CHECK_INSTANCE_CAST((obj), G_TYPE_FOO, GFoo)) #define > > G_FOO_CLASS(class) (G_TYPE_CHECK_CLASS_CAST((class), > > G_TYPE_FOO, GFooClass)) #define G_IS_FOO(obj) > > (G_TYPE_CHECK_INSTANE_TYPE((obj), G_TYPE_FOO)) #define > > G_IS_FOO_CLASS(class) (G_TYPE_CHECK_CLASS_TYPE((class), > > G_TYPE_FOO)) #define G_FOO_GET_CLASS(obj) > > (G_TYPE_INSTANCE_GET_CLASS((obj), G_TYPE_FOO, GFooClass)) > > > > typedef struct > > { > > GObject base; > > } GFoo; > > > > typedef struct > > { > > GObjectClass base; > > } GFooClass; > > > > GType g_foo_get_type (void); > > > > int g_foo_get_bar (GFoo *foo); > > void g_foo_set_bar (GFoo *foo, int value); > > > > enum { > > PROP_0, > > PROP_BAR > > }; > > > > G_DEFINE_TYPE(GFoo, g_foo, G_TYPE_OBJECT) > > > > static void g_foo_get_property (GObject *object, > > guint prop_id, > > GValue *value, > > GParamSpec *pspec) > > { > > GFoo *foo = G_FOO(object); > > > > switch (prop_id) > > { > > case PROP_BAR: > > g_value_set_int(value, g_foo_get_bar(foo)); > > break; > > default: > > G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, > > pspec); break; > > } > > } > > > > static void g_foo_set_property (GObject *object, > > guint prop_id, > > const GValue *value, > > GParamSpec *pspec) > > { > > GFoo *foo = G_FOO(object); > > > > switch (prop_id) > > { > > case PROP_BAR: > > g_foo_set_bar(foo, g_value_get_int(value)); > > break; > > default: > > G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, > > pspec); break; > > } > > } > > > > void g_foo_class_init (GFooClass *class) > > { > > GObjectClass *object_class = G_OBJECT_CLASS(foo); > > > > object_class->get_property = g_foo_get_property; > > object_class->set_property = g_foo_set_property; > > > > g_object_class_install_property(class, > > PROP_BAR, > > g_param_spec_int("bar", > > NULL, > > NULL, > > 0, > > G_MAXINT, > > 0, > > > > G_PARAM_READWRITE)); > > } > > > > void g_foo_init (GFoo *foo) > > { > > } > > > > int g_foo_get_bar (GFoo *foo) > > { > > /* ... */ > > } > > > > void g_foo_set_bar (GFoo *foo, int value) > > { > > /* ... */ > > } > > > > or using the Qt way: > > > > class QFoo : public QObject > > { > > Q_OBJECT > > > > Q_PROPERTY(int bar READ bar WRITE setBar) > > > > public: > > void setBar (int value); > > int bar () const; > > }; > > > > void QFoo::setBar (int value) > > { > > // ... > > } > > > > int QFoo::bar () > > { > > // ... > > } > > > > Which way do you prefer? > > BTW here is how it would look in Vala: > > using GLib; > > public class Foo : Object { > public int bar { get; set; } > } What about Genie even? [indent=4] uses Glib class Foo : Object init var bar = 0 Yours, Christian _______________________________________________ gtk-devel-list mailing list gtk-devel-list@gnome.org http://mail.gnome.org/mailman/listinfo/gtk-devel-list