8
8
<link rel="up" href="howto-gobject.html" title="How to define and implement a new GObject">
9
9
<link rel="prev" href="howto-gobject-code.html" title="Boilerplate code">
10
10
<link rel="next" href="howto-gobject-destruction.html" title="Object Destruction">
11
<meta name="generator" content="GTK-Doc V1.18.1 (XML mode)">
11
<meta name="generator" content="GTK-Doc V1.19 (XML mode)">
12
12
<link rel="stylesheet" href="style.css" type="text/css">
14
14
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
42
42
until all the construction properties have been set.
45
It is important to note that object construction cannot <span class="emphasis"><em>ever</em></span>
46
fail. If you require a fallible GObject construction, you can use the
47
GInitable and GAsyncInitable interfaces provided by the GIO library
45
50
As such, I would recommend writing the following code first:
47
52
<pre class="programlisting">
53
G_DEFINE_TYPE_WITH_PRIVATE (MamanBar, maman_bar, G_TYPE_OBJECT)
56
maman_bar_class_init (MamanBarClass *klass)
49
61
maman_bar_init (MamanBar *self)
51
self->priv = MAMAN_BAR_GET_PRIVATE (self);
63
self->priv = maman_bar_get_instance_private (self);
53
65
/* initialize all public and private members to reasonable default values. */
55
/* If you need specific construction properties to complete initialization,
56
* delay initialization completion until the property is set.
63
Now, if you need special construction properties, install the properties in the class_init function,
64
override the set and get methods and implement the get and set methods as described in
65
<a class="xref" href="gobject-properties.html" title="Object properties">the section called “Object properties”</a>. Make sure that these properties use a construct only
66
<a class="link" href="gobject-GParamSpec.html#GParamSpec" title="struct GParamSpec"><span class="type">GParamSpec</span></a> by setting the param spec's flag field to G_PARAM_CONSTRUCT_ONLY: this helps
67
GType ensure that these properties are not set again later by malicious user code.
71
If you need special construction properties, install the properties in
72
the <code class="function">class_init()</code> function, override the <code class="function">set_property()</code>
73
and <code class="function">get_property()</code> methods of the GObject class,
74
and implement them as described by <a class="xref" href="gobject-properties.html" title="Object properties">the section called “Object properties”</a>.
69
76
<div class="informalexample">
70
77
<table class="listing_frame" border="0" cellpadding="0" cellspacing="0">
107
117
<span class="normal"> N_PROPERTIES</span>
108
118
<span class="cbracket">}</span><span class="symbol">;</span>
120
<span class="comment">/* Keep a pointer to the properties definition */</span>
110
121
<span class="keyword">static</span><span class="normal"> </span><span class="usertype">GParamSpec</span><span class="normal"> </span><span class="symbol">*</span><span class="normal">obj_properties</span><span class="symbol">[</span><span class="normal">N_PROPERTIES</span><span class="symbol">]</span><span class="normal"> </span><span class="symbol">=</span><span class="normal"> </span><span class="cbracket">{</span><span class="normal"> <a href="http://library.gnome.org/devel/glib/unstable/glib-Standard-Macros.html#NULL:CAPS">NULL</a></span><span class="symbol">,</span><span class="normal"> </span><span class="cbracket">}</span><span class="symbol">;</span>
112
123
<span class="keyword">static</span><span class="normal"> </span><span class="type">void</span>
122
133
<span class="normal"> </span><span class="string">"Maman construct prop"</span><span class="symbol">,</span>
123
134
<span class="normal"> </span><span class="string">"Set maman's name"</span><span class="symbol">,</span>
124
135
<span class="normal"> </span><span class="string">"no-name-set"</span><span class="normal"> </span><span class="comment">/* default value */</span><span class="symbol">,</span>
125
<span class="normal"> <a href="gobject-GParamSpec.html#G-PARAM-CONSTRUCT-ONLY:CAPS">G_PARAM_CONSTRUCT_ONLY</a> </span><span class="symbol">|</span><span class="normal"> <a href="gobject-GParamSpec.html#G-PARAM-READWRITE:CAPS">G_PARAM_READWRITE</a></span><span class="symbol">);</span>
136
<span class="normal"> <a href="gobject-GParamSpec.html#G-PARAM-CONSTRUCT-ONLY:CAPS">G_PARAM_CONSTRUCT_ONLY</a> </span><span class="symbol">|</span>
137
<span class="normal"> <a href="gobject-GParamSpec.html#G-PARAM-READWRITE:CAPS">G_PARAM_READWRITE</a> </span><span class="symbol">|</span>
138
<span class="normal"> <a href="gobject-GParamSpec.html#G-PARAM-STATIC-STRINGS:CAPS">G_PARAM_STATIC_STRINGS</a></span><span class="symbol">);</span>
127
140
<span class="normal"> </span><span class="function"><a href="gobject-The-Base-Object-Type.html#g-object-class-install-properties">g_object_class_install_properties</a></span><span class="normal"> </span><span class="symbol">(</span><span class="normal">gobject_class</span><span class="symbol">,</span>
128
141
<span class="normal"> N_PROPERTIES</span><span class="symbol">,</span>
137
If you need this, make sure you can build and run code similar to the code shown above. Make sure
138
your construct properties can set correctly during construction, make sure you cannot set them
139
afterwards and make sure that if your users do not call <code class="function"><a class="link" href="gobject-The-Base-Object-Type.html#g-object-new" title="g_object_new ()">g_object_new</a></code>
140
with the required construction properties, these will be initialized with the default values.
143
I consider good taste to halt program execution if a construction property is set its
144
default value. This allows you to catch client code which does not give a reasonable
145
value to the construction properties. Of course, you are free to disagree but you
146
should have a good reason to do so.
149
Some people sometimes need to construct their object but only after
150
the construction properties have been set. This is possible through
151
the use of the constructor class method as described in
152
<a class="xref" href="chapter-gobject.html#gobject-instantiation" title="Object instantiation">the section called “Object instantiation”</a> or, more simply, using
153
the constructed class method available since GLib 2.12.
150
If you need this, make sure you can build and run code similar to the
151
code shown above. Also, make sure your construct properties can be set
152
without side effects during construction.
155
Some people sometimes need to complete the initialization of a instance
156
of a type only after the properties passed to the constructors have been
157
set. This is possible through the use of the <code class="function">constructor()</code>
158
class method as described in <a class="xref" href="chapter-gobject.html#gobject-instantiation" title="Object instantiation">the section called “Object instantiation”</a> or,
159
more simply, using the <code class="function">constructed()</code> class method
160
available since GLib 2.12. Note that the <code class="function">constructed()</code>
161
virtual function will only be invoked after the properties marked as
162
G_PARAM_CONSTRUCT_ONLY or G_PARAM_CONSTRUCT have been consumed, but
163
before the regular properties passed to <code class="function">g_object_new()</code>
156
167
<div class="footer">
158
Generated by GTK-Doc V1.18.1</div>
169
Generated by GTK-Doc V1.19</div>
b'\\ No newline at end of file'