23
23
<link rel="chapter" href="howto-interface.html" title="How to define and implement interfaces">
24
24
<link rel="chapter" href="howto-signals.html" title="How to create and use signals">
25
25
<link rel="part" href="pt03.html" title="Part V. Related Tools">
26
<link rel="chapter" href="tools-vala.html" title="Vala">
26
27
<link rel="chapter" href="tools-gob.html" title="GObject builder">
27
28
<link rel="chapter" href="tools-ginspector.html" title="Graphical inspection of GObjects">
28
29
<link rel="chapter" href="tools-refdb.html" title="Debugging reference count problems">
64
65
/* Implementation */
65
66
/************************************************/
68
MAMAN_BAR_CONSTRUCT_NAME = 1,
69
MAMAN_BAR_PAPA_NUMBER,
73
maman_bar_instance_init (GTypeInstance *instance,
76
MamanBar *self = (MamanBar *)instance;
80
77
maman_bar_set_property (GObject *object,
82
79
const GValue *value,
85
MamanBar *self = (MamanBar *) object;
87
switch (property_id) {
88
case MAMAN_BAR_CONSTRUCT_NAME: {
89
g_free (self->priv->name);
90
self->priv->name = g_value_dup_string (value);
91
g_print ("maman: %s\n",self->priv->name);
94
case MAMAN_BAR_PAPA_NUMBER: {
95
self->priv->papa_number = g_value_get_uchar (value);
96
g_print ("papa: %u\n",self->priv->papa_number);
100
/* We don't have any other property... */
101
G_OBJECT_WARN_INVALID_PROPERTY_ID(object,property_id,pspec);
82
MamanBar *self = MAMAN_BAR (object);
87
g_free (self->priv->name);
88
self->priv->name = g_value_dup_string (value);
89
g_print ("maman: %s\n", self->priv->name);
92
case PROP_PAPA_NUMBER:
93
self->priv->papa_number = g_value_get_uchar (value);
94
g_print ("papa: %u\n", self->priv->papa_number);
98
/* We don't have any other property... */
99
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
107
maman_bar_get_property (GObject *object,
105
maman_bar_get_property (GObject *object,
112
MamanBar *self = (MamanBar *) object;
114
switch (property_id) {
115
case MAMAN_BAR_CONSTRUCT_NAME: {
116
g_value_set_string (value, self->priv->name);
119
case MAMAN_BAR_PAPA_NUMBER: {
120
g_value_set_uchar (value, self->priv->papa_number);
124
/* We don't have any other property... */
125
G_OBJECT_WARN_INVALID_PROPERTY_ID(object,property_id,pspec);
110
MamanBar *self = MAMAN_BAR (object);
114
case PROP_MAMAN_NAME:
115
g_value_set_string (value, self->priv->name);
118
case PROP_PAPA_NUMBER:
119
g_value_set_uchar (value, self->priv->papa_number);
123
/* We don't have any other property... */
124
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
131
maman_bar_class_init (gpointer g_class,
132
gpointer g_class_data)
130
maman_bar_class_init (MamanBarClass *klass)
134
132
GObjectClass *gobject_class = G_OBJECT_CLASS (g_class);
135
MamanBarClass *klass = MAMAN_BAR_CLASS (g_class);
136
133
GParamSpec *pspec;
138
135
gobject_class->set_property = maman_bar_set_property;
164
161
/************************************************/
168
166
bar = g_object_new (MAMAN_TYPE_SUBBAR, NULL);
169
168
g_value_init (&val, G_TYPE_CHAR);
170
169
g_value_set_char (&val, 11);
171
171
g_object_set_property (G_OBJECT (bar), "papa-number", &val);
173
g_value_unset (&val);
174
176
The client code just above looks simple but a lot of things happen under the hood:
187
189
<code class="function"><a class="link" href="gobject-Generic-values.html#g-value-transform" title="g_value_transform ()">g_value_transform</a></code> will try to transform the input signed char into
188
190
an unsigned int. Of course, the success of the transformation depends on the availability
189
191
of the required transform function. In practice, there will almost always be a transformation
190
<sup>[<a name="id3075031" href="#ftn.id3075031" class="footnote">6</a>]</sup>
192
<sup>[<a name="id2569059" href="#ftn.id2569059" class="footnote">6</a>]</sup>
191
193
which matches and conversion will be carried out if needed.
207
209
implementation of Foo did override this method, the code path would jump to
208
210
<code class="function">foo_set_property</code> after having retrieved from the
209
211
<span class="type"><a class="link" href="gobject-GParamSpec.html#GParamSpec" title="GParamSpec">GParamSpec</a></span> the <span class="emphasis"><em>param_id</em></span>
210
<sup>[<a name="id3075133" href="#ftn.id3075133" class="footnote">7</a>]</sup>
212
<sup>[<a name="id2569173" href="#ftn.id2569173" class="footnote">7</a>]</sup>
211
213
which had been stored by
212
214
<code class="function"><a class="link" href="gobject-The-Base-Object-Type.html#g-object-class-install-property" title="g_object_class_install_property ()">g_object_class_install_property</a></code>.
277
279
<div class="footnotes">
278
280
<br><hr width="100" align="left">
279
<div class="footnote"><p><sup>[<a name="ftn.id3075031" href="#id3075031" class="para">6</a>] </sup>Its behaviour might not be what you expect but it is up to you to actually avoid
281
<div class="footnote"><p><sup>[<a name="ftn.id2569059" href="#id2569059" class="para">6</a>] </sup>Its behaviour might not be what you expect but it is up to you to actually avoid
280
282
relying on these transformations.
282
<div class="footnote"><p><sup>[<a name="ftn.id3075133" href="#id3075133" class="para">7</a>] </sup>
284
<div class="footnote"><p><sup>[<a name="ftn.id2569173" href="#id2569173" class="para">7</a>] </sup>
283
285
It should be noted that the param_id used here need only to uniquely identify each
284
286
<span class="type"><a class="link" href="gobject-GParamSpec.html#GParamSpec" title="GParamSpec">GParamSpec</a></span> within the <span class="type">FooClass</span> such that the switch
285
287
used in the set and get methods actually works. Of course, this locally-unique