docs: Update code examples in GObject tutorial

Use G_DECLARE_FINAL_TYPE, simplify property handling, and remove some
unnecessary braces.

https://bugzilla.gnome.org/show_bug.cgi?id=744060
This commit is contained in:
Philip Withnall 2015-02-19 14:05:56 +00:00
parent 42baaa88cd
commit 2aade94fcc

View File

@ -48,28 +48,17 @@
</para> </para>
<para> <para>
Objects which inherit from GObject are allowed to override this Once all construction operations have been completed and constructor
constructor class method: they should however chain to their parent properties set, the constructed class method is called.
constructor method before doing so:
<informalexample><programlisting>
GObject *(* constructor) (GType gtype,
guint n_properties,
GObjectConstructParam *properties);
</programlisting></informalexample>
</para> </para>
<para> <para>
The example below shows how <type>MamanBar</type> overrides the parent's constructor: Objects which inherit from GObject are allowed to override this
constructed class method.
The example below shows how <type>MamanBar</type> overrides the parent's construction process:
<informalexample><programlisting> <informalexample><programlisting>
#define MAMAN_TYPE_BAR (maman_bar_get_type ()) #define MAMAN_TYPE_BAR maman_bar_get_type ()
#define MAMAN_BAR(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), MAMAN_TYPE_BAR, MamanBar)) G_DECLARE_FINAL_TYPE (MamanBar, maman_bar, MAMAN, BAR, GObject)
#define MAMAN_IS_BAR(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), MAMAN_TYPE_BAR))
#define MAMAN_BAR_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), MAMAN_TYPE_BAR, MamanBarClass))
#define MAMAN_IS_BAR_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), MAMAN_TYPE_BAR))
#define MAMAN_BAR_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), MAMAN_TYPE_BAR, MamanBarClass))
typedef struct _MamanBar MamanBar;
typedef struct _MamanBarClass MamanBarClass;
struct _MamanBar struct _MamanBar
{ {
@ -78,39 +67,25 @@ struct _MamanBar
/* instance members */ /* instance members */
}; };
struct _MamanBarClass
{
GObjectClass parent_class;
/* class members */
};
/* will create maman_bar_get_type and set maman_bar_parent_class */ /* will create maman_bar_get_type and set maman_bar_parent_class */
G_DEFINE_TYPE (MamanBar, maman_bar, G_TYPE_OBJECT); G_DEFINE_TYPE (MamanBar, maman_bar, G_TYPE_OBJECT);
static GObject * static void
maman_bar_constructor (GType gtype, maman_bar_constructed (GObject *obj)
guint n_properties,
GObjectConstructParam *properties)
{ {
GObject *obj;
{
/* Always chain up to the parent constructor */
obj = G_OBJECT_CLASS (maman_bar_parent_class)-&gt;constructor (gtype, n_properties, properties);
}
/* update the object state depending on constructor properties */ /* update the object state depending on constructor properties */
return obj; /* Always chain up to the parent constructed function to complete object
* initialisation. */
G_OBJECT_CLASS (maman_bar_parent_class)-&gt;constructed (obj);
} }
static void static void
maman_bar_class_init (MamanBarClass *klass) maman_bar_class_init (MamanBarClass *klass)
{ {
GObjectClass *gobject_class = G_OBJECT_CLASS (klass); GObjectClass *object_class = G_OBJECT_CLASS (klass);
gobject_class-&gt;constructor = maman_bar_constructor; object_class-&gt;constructed = maman_bar_constructed;
} }
static void static void
@ -509,11 +484,8 @@ void g_object_run_dispose (GObject *object);
enum enum
{ {
PROP_0, PROP_MAMAN_NAME = 1,
PROP_MAMAN_NAME,
PROP_PAPA_NUMBER, PROP_PAPA_NUMBER,
N_PROPERTIES N_PROPERTIES
}; };
@ -575,28 +547,28 @@ maman_bar_get_property (GObject *object,
static void static void
maman_bar_class_init (MamanBarClass *klass) maman_bar_class_init (MamanBarClass *klass)
{ {
GObjectClass *gobject_class = G_OBJECT_CLASS (klass); GObjectClass *object_class = G_OBJECT_CLASS (klass);
gobject_class-&gt;set_property = maman_bar_set_property; object_class-&gt;set_property = maman_bar_set_property;
gobject_class-&gt;get_property = maman_bar_get_property; object_class-&gt;get_property = maman_bar_get_property;
obj_properties[PROP_NAME] = obj_properties[PROP_MAMAN_NAME] =
g_param_spec_string ("maman-name", g_param_spec_string ("maman-name",
"Maman construct prop", "Maman construct prop",
"Set maman's name", "Set maman's name",
"no-name-set" /* default value */, "no-name-set" /* default value */,
G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE); G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE));
obj_properties[PROP_NUMBER] = obj_properties[PROP_PAPA_NUMBER] =
g_param_spec_uchar ("papa-number", g_param_spec_uchar ("papa-number",
"Number of current Papa", "Number of current Papa",
"Set/Get papa's number", "Set/Get papa's number",
0 /* minimum value */, 0 /* minimum value */,
10 /* maximum value */, 10 /* maximum value */,
2 /* default value */, 2 /* default value */,
G_PARAM_READWRITE); G_PARAM_READWRITE));
g_object_class_install_properties (gobject_class, g_object_class_install_properties (object_class,
N_PROPERTIES, N_PROPERTIES,
obj_properties); obj_properties);
} }