1 | /* GObject - GLib Type, Object, Parameter and Signal Library |
2 | * Copyright (C) 1998-1999, 2000-2001 Tim Janik and Red Hat, Inc. |
3 | * |
4 | * This library is free software; you can redistribute it and/or |
5 | * modify it under the terms of the GNU Lesser General Public |
6 | * License as published by the Free Software Foundation; either |
7 | * version 2.1 of the License, or (at your option) any later version. |
8 | * |
9 | * This library is distributed in the hope that it will be useful, |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
12 | * Lesser General Public License for more details. |
13 | * |
14 | * You should have received a copy of the GNU Lesser General |
15 | * Public License along with this library; if not, see <http://www.gnu.org/licenses/>. |
16 | */ |
17 | #ifndef __G_OBJECT_H__ |
18 | #define __G_OBJECT_H__ |
19 | |
20 | #if !defined (__GLIB_GOBJECT_H_INSIDE__) && !defined (GOBJECT_COMPILATION) |
21 | #error "Only <glib-object.h> can be included directly." |
22 | #endif |
23 | |
24 | #include <gobject/gtype.h> |
25 | #include <gobject/gvalue.h> |
26 | #include <gobject/gparam.h> |
27 | #include <gobject/gclosure.h> |
28 | #include <gobject/gsignal.h> |
29 | #include <gobject/gboxed.h> |
30 | |
31 | #if defined(glib_typeof_2_68) && GLIB_VERSION_MIN_REQUIRED >= GLIB_VERSION_2_68 |
32 | /* for glib_typeof */ |
33 | #include <type_traits> |
34 | #endif |
35 | |
36 | G_BEGIN_DECLS |
37 | |
38 | /* --- type macros --- */ |
39 | /** |
40 | * G_TYPE_IS_OBJECT: |
41 | * @type: Type id to check |
42 | * |
43 | * Check if the passed in type id is a %G_TYPE_OBJECT or derived from it. |
44 | * |
45 | * Returns: %FALSE or %TRUE, indicating whether @type is a %G_TYPE_OBJECT. |
46 | */ |
47 | #define G_TYPE_IS_OBJECT(type) (G_TYPE_FUNDAMENTAL (type) == G_TYPE_OBJECT) |
48 | /** |
49 | * G_OBJECT: |
50 | * @object: Object which is subject to casting. |
51 | * |
52 | * Casts a #GObject or derived pointer into a (GObject*) pointer. |
53 | * Depending on the current debugging level, this function may invoke |
54 | * certain runtime checks to identify invalid casts. |
55 | */ |
56 | #define G_OBJECT(object) (G_TYPE_CHECK_INSTANCE_CAST ((object), G_TYPE_OBJECT, GObject)) |
57 | /** |
58 | * G_OBJECT_CLASS: |
59 | * @class: a valid #GObjectClass |
60 | * |
61 | * Casts a derived #GObjectClass structure into a #GObjectClass structure. |
62 | */ |
63 | #define G_OBJECT_CLASS(class) (G_TYPE_CHECK_CLASS_CAST ((class), G_TYPE_OBJECT, GObjectClass)) |
64 | /** |
65 | * G_IS_OBJECT: |
66 | * @object: Instance to check for being a %G_TYPE_OBJECT. |
67 | * |
68 | * Checks whether a valid #GTypeInstance pointer is of type %G_TYPE_OBJECT. |
69 | */ |
70 | #if GLIB_VERSION_MAX_ALLOWED >= GLIB_VERSION_2_42 |
71 | #define G_IS_OBJECT(object) (G_TYPE_CHECK_INSTANCE_FUNDAMENTAL_TYPE ((object), G_TYPE_OBJECT)) |
72 | #else |
73 | #define G_IS_OBJECT(object) (G_TYPE_CHECK_INSTANCE_TYPE ((object), G_TYPE_OBJECT)) |
74 | #endif |
75 | /** |
76 | * G_IS_OBJECT_CLASS: |
77 | * @class: a #GObjectClass |
78 | * |
79 | * Checks whether @class "is a" valid #GObjectClass structure of type |
80 | * %G_TYPE_OBJECT or derived. |
81 | */ |
82 | #define G_IS_OBJECT_CLASS(class) (G_TYPE_CHECK_CLASS_TYPE ((class), G_TYPE_OBJECT)) |
83 | /** |
84 | * G_OBJECT_GET_CLASS: |
85 | * @object: a #GObject instance. |
86 | * |
87 | * Get the class structure associated to a #GObject instance. |
88 | * |
89 | * Returns: pointer to object class structure. |
90 | */ |
91 | #define G_OBJECT_GET_CLASS(object) (G_TYPE_INSTANCE_GET_CLASS ((object), G_TYPE_OBJECT, GObjectClass)) |
92 | /** |
93 | * G_OBJECT_TYPE: |
94 | * @object: Object to return the type id for. |
95 | * |
96 | * Get the type id of an object. |
97 | * |
98 | * Returns: Type id of @object. |
99 | */ |
100 | #define G_OBJECT_TYPE(object) (G_TYPE_FROM_INSTANCE (object)) |
101 | /** |
102 | * G_OBJECT_TYPE_NAME: |
103 | * @object: Object to return the type name for. |
104 | * |
105 | * Get the name of an object's type. |
106 | * |
107 | * Returns: Type name of @object. The string is owned by the type system and |
108 | * should not be freed. |
109 | */ |
110 | #define G_OBJECT_TYPE_NAME(object) (g_type_name (G_OBJECT_TYPE (object))) |
111 | /** |
112 | * G_OBJECT_CLASS_TYPE: |
113 | * @class: a valid #GObjectClass |
114 | * |
115 | * Get the type id of a class structure. |
116 | * |
117 | * Returns: Type id of @class. |
118 | */ |
119 | #define G_OBJECT_CLASS_TYPE(class) (G_TYPE_FROM_CLASS (class)) |
120 | /** |
121 | * G_OBJECT_CLASS_NAME: |
122 | * @class: a valid #GObjectClass |
123 | * |
124 | * Return the name of a class structure's type. |
125 | * |
126 | * Returns: Type name of @class. The string is owned by the type system and |
127 | * should not be freed. |
128 | */ |
129 | #define G_OBJECT_CLASS_NAME(class) (g_type_name (G_OBJECT_CLASS_TYPE (class))) |
130 | /** |
131 | * G_VALUE_HOLDS_OBJECT: |
132 | * @value: a valid #GValue structure |
133 | * |
134 | * Checks whether the given #GValue can hold values derived from type %G_TYPE_OBJECT. |
135 | * |
136 | * Returns: %TRUE on success. |
137 | */ |
138 | #define G_VALUE_HOLDS_OBJECT(value) (G_TYPE_CHECK_VALUE_TYPE ((value), G_TYPE_OBJECT)) |
139 | |
140 | /* --- type macros --- */ |
141 | /** |
142 | * G_TYPE_INITIALLY_UNOWNED: |
143 | * |
144 | * The type for #GInitiallyUnowned. |
145 | */ |
146 | #define G_TYPE_INITIALLY_UNOWNED (g_initially_unowned_get_type()) |
147 | /** |
148 | * G_INITIALLY_UNOWNED: |
149 | * @object: Object which is subject to casting. |
150 | * |
151 | * Casts a #GInitiallyUnowned or derived pointer into a (GInitiallyUnowned*) |
152 | * pointer. Depending on the current debugging level, this function may invoke |
153 | * certain runtime checks to identify invalid casts. |
154 | */ |
155 | #define G_INITIALLY_UNOWNED(object) (G_TYPE_CHECK_INSTANCE_CAST ((object), G_TYPE_INITIALLY_UNOWNED, GInitiallyUnowned)) |
156 | /** |
157 | * G_INITIALLY_UNOWNED_CLASS: |
158 | * @class: a valid #GInitiallyUnownedClass |
159 | * |
160 | * Casts a derived #GInitiallyUnownedClass structure into a |
161 | * #GInitiallyUnownedClass structure. |
162 | */ |
163 | #define G_INITIALLY_UNOWNED_CLASS(class) (G_TYPE_CHECK_CLASS_CAST ((class), G_TYPE_INITIALLY_UNOWNED, GInitiallyUnownedClass)) |
164 | /** |
165 | * G_IS_INITIALLY_UNOWNED: |
166 | * @object: Instance to check for being a %G_TYPE_INITIALLY_UNOWNED. |
167 | * |
168 | * Checks whether a valid #GTypeInstance pointer is of type %G_TYPE_INITIALLY_UNOWNED. |
169 | */ |
170 | #define G_IS_INITIALLY_UNOWNED(object) (G_TYPE_CHECK_INSTANCE_TYPE ((object), G_TYPE_INITIALLY_UNOWNED)) |
171 | /** |
172 | * G_IS_INITIALLY_UNOWNED_CLASS: |
173 | * @class: a #GInitiallyUnownedClass |
174 | * |
175 | * Checks whether @class "is a" valid #GInitiallyUnownedClass structure of type |
176 | * %G_TYPE_INITIALLY_UNOWNED or derived. |
177 | */ |
178 | #define G_IS_INITIALLY_UNOWNED_CLASS(class) (G_TYPE_CHECK_CLASS_TYPE ((class), G_TYPE_INITIALLY_UNOWNED)) |
179 | /** |
180 | * G_INITIALLY_UNOWNED_GET_CLASS: |
181 | * @object: a #GInitiallyUnowned instance. |
182 | * |
183 | * Get the class structure associated to a #GInitiallyUnowned instance. |
184 | * |
185 | * Returns: pointer to object class structure. |
186 | */ |
187 | #define G_INITIALLY_UNOWNED_GET_CLASS(object) (G_TYPE_INSTANCE_GET_CLASS ((object), G_TYPE_INITIALLY_UNOWNED, GInitiallyUnownedClass)) |
188 | /* GInitiallyUnowned ia a GObject with initially floating reference count */ |
189 | |
190 | |
191 | /* --- typedefs & structures --- */ |
192 | typedef struct _GObject GObject; |
193 | typedef struct _GObjectClass GObjectClass; |
194 | typedef struct _GObject GInitiallyUnowned; |
195 | typedef struct _GObjectClass GInitiallyUnownedClass; |
196 | typedef struct _GObjectConstructParam GObjectConstructParam; |
197 | /** |
198 | * GObjectGetPropertyFunc: |
199 | * @object: a #GObject |
200 | * @property_id: the numeric id under which the property was registered with |
201 | * g_object_class_install_property(). |
202 | * @value: a #GValue to return the property value in |
203 | * @pspec: the #GParamSpec describing the property |
204 | * |
205 | * The type of the @get_property function of #GObjectClass. |
206 | */ |
207 | typedef void (*GObjectGetPropertyFunc) (GObject *object, |
208 | guint property_id, |
209 | GValue *value, |
210 | GParamSpec *pspec); |
211 | /** |
212 | * GObjectSetPropertyFunc: |
213 | * @object: a #GObject |
214 | * @property_id: the numeric id under which the property was registered with |
215 | * g_object_class_install_property(). |
216 | * @value: the new value for the property |
217 | * @pspec: the #GParamSpec describing the property |
218 | * |
219 | * The type of the @set_property function of #GObjectClass. |
220 | */ |
221 | typedef void (*GObjectSetPropertyFunc) (GObject *object, |
222 | guint property_id, |
223 | const GValue *value, |
224 | GParamSpec *pspec); |
225 | /** |
226 | * GObjectFinalizeFunc: |
227 | * @object: the #GObject being finalized |
228 | * |
229 | * The type of the @finalize function of #GObjectClass. |
230 | */ |
231 | typedef void (*GObjectFinalizeFunc) (GObject *object); |
232 | /** |
233 | * GWeakNotify: |
234 | * @data: data that was provided when the weak reference was established |
235 | * @where_the_object_was: the object being disposed |
236 | * |
237 | * A #GWeakNotify function can be added to an object as a callback that gets |
238 | * triggered when the object is finalized. Since the object is already being |
239 | * disposed when the #GWeakNotify is called, there's not much you could do |
240 | * with the object, apart from e.g. using its address as hash-index or the like. |
241 | */ |
242 | typedef void (*GWeakNotify) (gpointer data, |
243 | GObject *where_the_object_was); |
244 | /** |
245 | * GObject: |
246 | * |
247 | * All the fields in the GObject structure are private |
248 | * to the #GObject implementation and should never be accessed directly. |
249 | */ |
250 | struct _GObject |
251 | { |
252 | GTypeInstance g_type_instance; |
253 | |
254 | /*< private >*/ |
255 | guint ref_count; /* (atomic) */ |
256 | GData *qdata; |
257 | }; |
258 | /** |
259 | * GObjectClass: |
260 | * @g_type_class: the parent class |
261 | * @constructor: the @constructor function is called by g_object_new () to |
262 | * complete the object initialization after all the construction properties are |
263 | * set. The first thing a @constructor implementation must do is chain up to the |
264 | * @constructor of the parent class. Overriding @constructor should be rarely |
265 | * needed, e.g. to handle construct properties, or to implement singletons. |
266 | * @set_property: the generic setter for all properties of this type. Should be |
267 | * overridden for every type with properties. If implementations of |
268 | * @set_property don't emit property change notification explicitly, this will |
269 | * be done implicitly by the type system. However, if the notify signal is |
270 | * emitted explicitly, the type system will not emit it a second time. |
271 | * @get_property: the generic getter for all properties of this type. Should be |
272 | * overridden for every type with properties. |
273 | * @dispose: the @dispose function is supposed to drop all references to other |
274 | * objects, but keep the instance otherwise intact, so that client method |
275 | * invocations still work. It may be run multiple times (due to reference |
276 | * loops). Before returning, @dispose should chain up to the @dispose method |
277 | * of the parent class. |
278 | * @finalize: instance finalization function, should finish the finalization of |
279 | * the instance begun in @dispose and chain up to the @finalize method of the |
280 | * parent class. |
281 | * @dispatch_properties_changed: emits property change notification for a bunch |
282 | * of properties. Overriding @dispatch_properties_changed should be rarely |
283 | * needed. |
284 | * @notify: the class closure for the notify signal |
285 | * @constructed: the @constructed function is called by g_object_new() as the |
286 | * final step of the object creation process. At the point of the call, all |
287 | * construction properties have been set on the object. The purpose of this |
288 | * call is to allow for object initialisation steps that can only be performed |
289 | * after construction properties have been set. @constructed implementors |
290 | * should chain up to the @constructed call of their parent class to allow it |
291 | * to complete its initialisation. |
292 | * |
293 | * The class structure for the GObject type. |
294 | * |
295 | * |[<!-- language="C" --> |
296 | * // Example of implementing a singleton using a constructor. |
297 | * static MySingleton *the_singleton = NULL; |
298 | * |
299 | * static GObject* |
300 | * my_singleton_constructor (GType type, |
301 | * guint n_construct_params, |
302 | * GObjectConstructParam *construct_params) |
303 | * { |
304 | * GObject *object; |
305 | * |
306 | * if (!the_singleton) |
307 | * { |
308 | * object = G_OBJECT_CLASS (parent_class)->constructor (type, |
309 | * n_construct_params, |
310 | * construct_params); |
311 | * the_singleton = MY_SINGLETON (object); |
312 | * } |
313 | * else |
314 | * object = g_object_ref (G_OBJECT (the_singleton)); |
315 | * |
316 | * return object; |
317 | * } |
318 | * ]| |
319 | */ |
320 | struct _GObjectClass |
321 | { |
322 | GTypeClass g_type_class; |
323 | |
324 | /*< private >*/ |
325 | GSList *construct_properties; |
326 | |
327 | /*< public >*/ |
328 | /* seldom overridden */ |
329 | GObject* (*constructor) (GType type, |
330 | guint n_construct_properties, |
331 | GObjectConstructParam *construct_properties); |
332 | /* overridable methods */ |
333 | void (*set_property) (GObject *object, |
334 | guint property_id, |
335 | const GValue *value, |
336 | GParamSpec *pspec); |
337 | void (*get_property) (GObject *object, |
338 | guint property_id, |
339 | GValue *value, |
340 | GParamSpec *pspec); |
341 | void (*dispose) (GObject *object); |
342 | void (*finalize) (GObject *object); |
343 | /* seldom overridden */ |
344 | void (*dispatch_properties_changed) (GObject *object, |
345 | guint n_pspecs, |
346 | GParamSpec **pspecs); |
347 | /* signals */ |
348 | void (*notify) (GObject *object, |
349 | GParamSpec *pspec); |
350 | |
351 | /* called when done constructing */ |
352 | void (*constructed) (GObject *object); |
353 | |
354 | /*< private >*/ |
355 | gsize flags; |
356 | |
357 | /* padding */ |
358 | gpointer pdummy[6]; |
359 | }; |
360 | /** |
361 | * GObjectConstructParam: |
362 | * @pspec: the #GParamSpec of the construct parameter |
363 | * @value: the value to set the parameter to |
364 | * |
365 | * The GObjectConstructParam struct is an auxiliary |
366 | * structure used to hand #GParamSpec/#GValue pairs to the @constructor of |
367 | * a #GObjectClass. |
368 | */ |
369 | struct _GObjectConstructParam |
370 | { |
371 | GParamSpec *pspec; |
372 | GValue *value; |
373 | }; |
374 | |
375 | /** |
376 | * GInitiallyUnowned: |
377 | * |
378 | * All the fields in the GInitiallyUnowned structure |
379 | * are private to the #GInitiallyUnowned implementation and should never be |
380 | * accessed directly. |
381 | */ |
382 | /** |
383 | * GInitiallyUnownedClass: |
384 | * |
385 | * The class structure for the GInitiallyUnowned type. |
386 | */ |
387 | |
388 | |
389 | /* --- prototypes --- */ |
390 | GLIB_AVAILABLE_IN_ALL |
391 | GType g_initially_unowned_get_type (void); |
392 | GLIB_AVAILABLE_IN_ALL |
393 | void g_object_class_install_property (GObjectClass *oclass, |
394 | guint property_id, |
395 | GParamSpec *pspec); |
396 | GLIB_AVAILABLE_IN_ALL |
397 | GParamSpec* g_object_class_find_property (GObjectClass *oclass, |
398 | const gchar *property_name); |
399 | GLIB_AVAILABLE_IN_ALL |
400 | GParamSpec**g_object_class_list_properties (GObjectClass *oclass, |
401 | guint *n_properties); |
402 | GLIB_AVAILABLE_IN_ALL |
403 | void g_object_class_override_property (GObjectClass *oclass, |
404 | guint property_id, |
405 | const gchar *name); |
406 | GLIB_AVAILABLE_IN_ALL |
407 | void g_object_class_install_properties (GObjectClass *oclass, |
408 | guint n_pspecs, |
409 | GParamSpec **pspecs); |
410 | |
411 | GLIB_AVAILABLE_IN_ALL |
412 | void g_object_interface_install_property (gpointer g_iface, |
413 | GParamSpec *pspec); |
414 | GLIB_AVAILABLE_IN_ALL |
415 | GParamSpec* g_object_interface_find_property (gpointer g_iface, |
416 | const gchar *property_name); |
417 | GLIB_AVAILABLE_IN_ALL |
418 | GParamSpec**g_object_interface_list_properties (gpointer g_iface, |
419 | guint *n_properties_p); |
420 | |
421 | GLIB_AVAILABLE_IN_ALL |
422 | GType g_object_get_type (void) G_GNUC_CONST; |
423 | GLIB_AVAILABLE_IN_ALL |
424 | gpointer g_object_new (GType object_type, |
425 | const gchar *first_property_name, |
426 | ...); |
427 | GLIB_AVAILABLE_IN_2_54 |
428 | GObject* g_object_new_with_properties (GType object_type, |
429 | guint n_properties, |
430 | const char *names[], |
431 | const GValue values[]); |
432 | |
433 | G_GNUC_BEGIN_IGNORE_DEPRECATIONS |
434 | |
435 | GLIB_DEPRECATED_IN_2_54_FOR(g_object_new_with_properties) |
436 | gpointer g_object_newv (GType object_type, |
437 | guint n_parameters, |
438 | GParameter *parameters); |
439 | |
440 | G_GNUC_END_IGNORE_DEPRECATIONS |
441 | |
442 | GLIB_AVAILABLE_IN_ALL |
443 | GObject* g_object_new_valist (GType object_type, |
444 | const gchar *first_property_name, |
445 | va_list var_args); |
446 | GLIB_AVAILABLE_IN_ALL |
447 | void g_object_set (gpointer object, |
448 | const gchar *first_property_name, |
449 | ...) G_GNUC_NULL_TERMINATED; |
450 | GLIB_AVAILABLE_IN_ALL |
451 | void g_object_get (gpointer object, |
452 | const gchar *first_property_name, |
453 | ...) G_GNUC_NULL_TERMINATED; |
454 | GLIB_AVAILABLE_IN_ALL |
455 | gpointer g_object_connect (gpointer object, |
456 | const gchar *signal_spec, |
457 | ...) G_GNUC_NULL_TERMINATED; |
458 | GLIB_AVAILABLE_IN_ALL |
459 | void g_object_disconnect (gpointer object, |
460 | const gchar *signal_spec, |
461 | ...) G_GNUC_NULL_TERMINATED; |
462 | GLIB_AVAILABLE_IN_2_54 |
463 | void g_object_setv (GObject *object, |
464 | guint n_properties, |
465 | const gchar *names[], |
466 | const GValue values[]); |
467 | GLIB_AVAILABLE_IN_ALL |
468 | void g_object_set_valist (GObject *object, |
469 | const gchar *first_property_name, |
470 | va_list var_args); |
471 | GLIB_AVAILABLE_IN_2_54 |
472 | void g_object_getv (GObject *object, |
473 | guint n_properties, |
474 | const gchar *names[], |
475 | GValue values[]); |
476 | GLIB_AVAILABLE_IN_ALL |
477 | void g_object_get_valist (GObject *object, |
478 | const gchar *first_property_name, |
479 | va_list var_args); |
480 | GLIB_AVAILABLE_IN_ALL |
481 | void g_object_set_property (GObject *object, |
482 | const gchar *property_name, |
483 | const GValue *value); |
484 | GLIB_AVAILABLE_IN_ALL |
485 | void g_object_get_property (GObject *object, |
486 | const gchar *property_name, |
487 | GValue *value); |
488 | GLIB_AVAILABLE_IN_ALL |
489 | void g_object_freeze_notify (GObject *object); |
490 | GLIB_AVAILABLE_IN_ALL |
491 | void g_object_notify (GObject *object, |
492 | const gchar *property_name); |
493 | GLIB_AVAILABLE_IN_ALL |
494 | void g_object_notify_by_pspec (GObject *object, |
495 | GParamSpec *pspec); |
496 | GLIB_AVAILABLE_IN_ALL |
497 | void g_object_thaw_notify (GObject *object); |
498 | GLIB_AVAILABLE_IN_ALL |
499 | gboolean g_object_is_floating (gpointer object); |
500 | GLIB_AVAILABLE_IN_ALL |
501 | gpointer g_object_ref_sink (gpointer object); |
502 | GLIB_AVAILABLE_IN_ALL |
503 | gpointer g_object_ref (gpointer object); |
504 | GLIB_AVAILABLE_IN_ALL |
505 | void g_object_unref (gpointer object); |
506 | GLIB_AVAILABLE_IN_ALL |
507 | void g_object_weak_ref (GObject *object, |
508 | GWeakNotify notify, |
509 | gpointer data); |
510 | GLIB_AVAILABLE_IN_ALL |
511 | void g_object_weak_unref (GObject *object, |
512 | GWeakNotify notify, |
513 | gpointer data); |
514 | GLIB_AVAILABLE_IN_ALL |
515 | void g_object_add_weak_pointer (GObject *object, |
516 | gpointer *weak_pointer_location); |
517 | GLIB_AVAILABLE_IN_ALL |
518 | void g_object_remove_weak_pointer (GObject *object, |
519 | gpointer *weak_pointer_location); |
520 | |
521 | #if defined(glib_typeof) && GLIB_VERSION_MAX_ALLOWED >= GLIB_VERSION_2_56 && (!defined(glib_typeof_2_68) || GLIB_VERSION_MIN_REQUIRED >= GLIB_VERSION_2_68) |
522 | /* Make reference APIs type safe with macros */ |
523 | #define g_object_ref(Obj) ((glib_typeof (Obj)) (g_object_ref) (Obj)) |
524 | #define g_object_ref_sink(Obj) ((glib_typeof (Obj)) (g_object_ref_sink) (Obj)) |
525 | #endif |
526 | |
527 | /** |
528 | * GToggleNotify: |
529 | * @data: Callback data passed to g_object_add_toggle_ref() |
530 | * @object: The object on which g_object_add_toggle_ref() was called. |
531 | * @is_last_ref: %TRUE if the toggle reference is now the |
532 | * last reference to the object. %FALSE if the toggle |
533 | * reference was the last reference and there are now other |
534 | * references. |
535 | * |
536 | * A callback function used for notification when the state |
537 | * of a toggle reference changes. See g_object_add_toggle_ref(). |
538 | */ |
539 | typedef void (*GToggleNotify) (gpointer data, |
540 | GObject *object, |
541 | gboolean is_last_ref); |
542 | |
543 | GLIB_AVAILABLE_IN_ALL |
544 | void g_object_add_toggle_ref (GObject *object, |
545 | GToggleNotify notify, |
546 | gpointer data); |
547 | GLIB_AVAILABLE_IN_ALL |
548 | void g_object_remove_toggle_ref (GObject *object, |
549 | GToggleNotify notify, |
550 | gpointer data); |
551 | |
552 | GLIB_AVAILABLE_IN_ALL |
553 | gpointer g_object_get_qdata (GObject *object, |
554 | GQuark quark); |
555 | GLIB_AVAILABLE_IN_ALL |
556 | void g_object_set_qdata (GObject *object, |
557 | GQuark quark, |
558 | gpointer data); |
559 | GLIB_AVAILABLE_IN_ALL |
560 | void g_object_set_qdata_full (GObject *object, |
561 | GQuark quark, |
562 | gpointer data, |
563 | GDestroyNotify destroy); |
564 | GLIB_AVAILABLE_IN_ALL |
565 | gpointer g_object_steal_qdata (GObject *object, |
566 | GQuark quark); |
567 | |
568 | GLIB_AVAILABLE_IN_2_34 |
569 | gpointer g_object_dup_qdata (GObject *object, |
570 | GQuark quark, |
571 | GDuplicateFunc dup_func, |
572 | gpointer user_data); |
573 | GLIB_AVAILABLE_IN_2_34 |
574 | gboolean g_object_replace_qdata (GObject *object, |
575 | GQuark quark, |
576 | gpointer oldval, |
577 | gpointer newval, |
578 | GDestroyNotify destroy, |
579 | GDestroyNotify *old_destroy); |
580 | |
581 | GLIB_AVAILABLE_IN_ALL |
582 | gpointer g_object_get_data (GObject *object, |
583 | const gchar *key); |
584 | GLIB_AVAILABLE_IN_ALL |
585 | void g_object_set_data (GObject *object, |
586 | const gchar *key, |
587 | gpointer data); |
588 | GLIB_AVAILABLE_IN_ALL |
589 | void g_object_set_data_full (GObject *object, |
590 | const gchar *key, |
591 | gpointer data, |
592 | GDestroyNotify destroy); |
593 | GLIB_AVAILABLE_IN_ALL |
594 | gpointer g_object_steal_data (GObject *object, |
595 | const gchar *key); |
596 | |
597 | GLIB_AVAILABLE_IN_2_34 |
598 | gpointer g_object_dup_data (GObject *object, |
599 | const gchar *key, |
600 | GDuplicateFunc dup_func, |
601 | gpointer user_data); |
602 | GLIB_AVAILABLE_IN_2_34 |
603 | gboolean g_object_replace_data (GObject *object, |
604 | const gchar *key, |
605 | gpointer oldval, |
606 | gpointer newval, |
607 | GDestroyNotify destroy, |
608 | GDestroyNotify *old_destroy); |
609 | |
610 | |
611 | GLIB_AVAILABLE_IN_ALL |
612 | void g_object_watch_closure (GObject *object, |
613 | GClosure *closure); |
614 | GLIB_AVAILABLE_IN_ALL |
615 | GClosure* g_cclosure_new_object (GCallback callback_func, |
616 | GObject *object); |
617 | GLIB_AVAILABLE_IN_ALL |
618 | GClosure* g_cclosure_new_object_swap (GCallback callback_func, |
619 | GObject *object); |
620 | GLIB_AVAILABLE_IN_ALL |
621 | GClosure* g_closure_new_object (guint sizeof_closure, |
622 | GObject *object); |
623 | GLIB_AVAILABLE_IN_ALL |
624 | void g_value_set_object (GValue *value, |
625 | gpointer v_object); |
626 | GLIB_AVAILABLE_IN_ALL |
627 | gpointer g_value_get_object (const GValue *value); |
628 | GLIB_AVAILABLE_IN_ALL |
629 | gpointer g_value_dup_object (const GValue *value); |
630 | GLIB_AVAILABLE_IN_ALL |
631 | gulong g_signal_connect_object (gpointer instance, |
632 | const gchar *detailed_signal, |
633 | GCallback c_handler, |
634 | gpointer gobject, |
635 | GConnectFlags connect_flags); |
636 | |
637 | /*< protected >*/ |
638 | GLIB_AVAILABLE_IN_ALL |
639 | void g_object_force_floating (GObject *object); |
640 | GLIB_AVAILABLE_IN_ALL |
641 | void g_object_run_dispose (GObject *object); |
642 | |
643 | |
644 | GLIB_AVAILABLE_IN_ALL |
645 | void g_value_take_object (GValue *value, |
646 | gpointer v_object); |
647 | GLIB_DEPRECATED_FOR(g_value_take_object) |
648 | void g_value_set_object_take_ownership (GValue *value, |
649 | gpointer v_object); |
650 | |
651 | GLIB_DEPRECATED |
652 | gsize g_object_compat_control (gsize what, |
653 | gpointer data); |
654 | |
655 | /* --- implementation macros --- */ |
656 | #define G_OBJECT_WARN_INVALID_PSPEC(object, pname, property_id, pspec) \ |
657 | G_STMT_START { \ |
658 | GObject *_glib__object = (GObject*) (object); \ |
659 | GParamSpec *_glib__pspec = (GParamSpec*) (pspec); \ |
660 | guint _glib__property_id = (property_id); \ |
661 | g_warning ("%s:%d: invalid %s id %u for \"%s\" of type '%s' in '%s'", \ |
662 | __FILE__, __LINE__, \ |
663 | (pname), \ |
664 | _glib__property_id, \ |
665 | _glib__pspec->name, \ |
666 | g_type_name (G_PARAM_SPEC_TYPE (_glib__pspec)), \ |
667 | G_OBJECT_TYPE_NAME (_glib__object)); \ |
668 | } G_STMT_END |
669 | /** |
670 | * G_OBJECT_WARN_INVALID_PROPERTY_ID: |
671 | * @object: the #GObject on which set_property() or get_property() was called |
672 | * @property_id: the numeric id of the property |
673 | * @pspec: the #GParamSpec of the property |
674 | * |
675 | * This macro should be used to emit a standard warning about unexpected |
676 | * properties in set_property() and get_property() implementations. |
677 | */ |
678 | #define G_OBJECT_WARN_INVALID_PROPERTY_ID(object, property_id, pspec) \ |
679 | G_OBJECT_WARN_INVALID_PSPEC ((object), "property", (property_id), (pspec)) |
680 | |
681 | GLIB_AVAILABLE_IN_ALL |
682 | void g_clear_object (GObject **object_ptr); |
683 | #define g_clear_object(object_ptr) g_clear_pointer ((object_ptr), g_object_unref) |
684 | |
685 | /** |
686 | * g_set_object: (skip) |
687 | * @object_ptr: (inout) (not optional) (nullable): a pointer to a #GObject reference |
688 | * @new_object: (nullable) (transfer none): a pointer to the new #GObject to |
689 | * assign to @object_ptr, or %NULL to clear the pointer |
690 | * |
691 | * Updates a #GObject pointer to refer to @new_object. It increments the |
692 | * reference count of @new_object (if non-%NULL), decrements the reference |
693 | * count of the current value of @object_ptr (if non-%NULL), and assigns |
694 | * @new_object to @object_ptr. The assignment is not atomic. |
695 | * |
696 | * @object_ptr must not be %NULL, but can point to a %NULL value. |
697 | * |
698 | * A macro is also included that allows this function to be used without |
699 | * pointer casts. The function itself is static inline, so its address may vary |
700 | * between compilation units. |
701 | * |
702 | * One convenient usage of this function is in implementing property setters: |
703 | * |[ |
704 | * void |
705 | * foo_set_bar (Foo *foo, |
706 | * Bar *new_bar) |
707 | * { |
708 | * g_return_if_fail (IS_FOO (foo)); |
709 | * g_return_if_fail (new_bar == NULL || IS_BAR (new_bar)); |
710 | * |
711 | * if (g_set_object (&foo->bar, new_bar)) |
712 | * g_object_notify (foo, "bar"); |
713 | * } |
714 | * ]| |
715 | * |
716 | * Returns: %TRUE if the value of @object_ptr changed, %FALSE otherwise |
717 | * |
718 | * Since: 2.44 |
719 | */ |
720 | static inline gboolean |
721 | (g_set_object) (GObject **object_ptr, |
722 | GObject *new_object) |
723 | { |
724 | GObject *old_object = *object_ptr; |
725 | |
726 | /* rely on g_object_[un]ref() to check the pointers are actually GObjects; |
727 | * elide a (object_ptr != NULL) check because most of the time we will be |
728 | * operating on struct members with a constant offset, so a NULL check would |
729 | * not catch bugs |
730 | */ |
731 | |
732 | if (old_object == new_object) |
733 | return FALSE; |
734 | |
735 | if (new_object != NULL) |
736 | g_object_ref (new_object); |
737 | |
738 | *object_ptr = new_object; |
739 | |
740 | if (old_object != NULL) |
741 | g_object_unref (object: old_object); |
742 | |
743 | return TRUE; |
744 | } |
745 | |
746 | /* We need GCC for __extension__, which we need to sort out strict aliasing of @object_ptr */ |
747 | #if defined(__GNUC__) |
748 | |
749 | #define g_set_object(object_ptr, new_object) \ |
750 | (G_GNUC_EXTENSION ({ \ |
751 | G_STATIC_ASSERT (sizeof *(object_ptr) == sizeof (new_object)); \ |
752 | /* Only one access, please; work around type aliasing */ \ |
753 | union { char *in; GObject **out; } _object_ptr; \ |
754 | _object_ptr.in = (char *) (object_ptr); \ |
755 | /* Check types match */ \ |
756 | (void) (0 ? *(object_ptr) = (new_object), FALSE : FALSE); \ |
757 | (g_set_object) (_object_ptr.out, (GObject *) new_object); \ |
758 | })) \ |
759 | GLIB_AVAILABLE_MACRO_IN_2_44 |
760 | |
761 | #else /* if !defined(__GNUC__) */ |
762 | |
763 | #define g_set_object(object_ptr, new_object) \ |
764 | (/* Check types match. */ \ |
765 | 0 ? *(object_ptr) = (new_object), FALSE : \ |
766 | (g_set_object) ((GObject **) (object_ptr), (GObject *) (new_object)) \ |
767 | ) |
768 | |
769 | #endif /* !defined(__GNUC__) */ |
770 | |
771 | /** |
772 | * g_assert_finalize_object: (skip) |
773 | * @object: (transfer full) (type GObject.Object): an object |
774 | * |
775 | * Assert that @object is non-%NULL, then release one reference to it with |
776 | * g_object_unref() and assert that it has been finalized (i.e. that there |
777 | * are no more references). |
778 | * |
779 | * If assertions are disabled via `G_DISABLE_ASSERT`, |
780 | * this macro just calls g_object_unref() without any further checks. |
781 | * |
782 | * This macro should only be used in regression tests. |
783 | * |
784 | * Since: 2.62 |
785 | */ |
786 | static inline void |
787 | (g_assert_finalize_object) (GObject *object) |
788 | { |
789 | gpointer weak_pointer = object; |
790 | |
791 | g_assert_true (G_IS_OBJECT (weak_pointer)); |
792 | g_object_add_weak_pointer (object, weak_pointer_location: &weak_pointer); |
793 | g_object_unref (object: weak_pointer); |
794 | g_assert_null (weak_pointer); |
795 | } |
796 | |
797 | #ifdef G_DISABLE_ASSERT |
798 | #define g_assert_finalize_object(object) g_object_unref (object) |
799 | #else |
800 | #define g_assert_finalize_object(object) (g_assert_finalize_object ((GObject *) object)) |
801 | #endif |
802 | |
803 | /** |
804 | * g_clear_weak_pointer: (skip) |
805 | * @weak_pointer_location: The memory address of a pointer |
806 | * |
807 | * Clears a weak reference to a #GObject. |
808 | * |
809 | * @weak_pointer_location must not be %NULL. |
810 | * |
811 | * If the weak reference is %NULL then this function does nothing. |
812 | * Otherwise, the weak reference to the object is removed for that location |
813 | * and the pointer is set to %NULL. |
814 | * |
815 | * A macro is also included that allows this function to be used without |
816 | * pointer casts. The function itself is static inline, so its address may vary |
817 | * between compilation units. |
818 | * |
819 | * Since: 2.56 |
820 | */ |
821 | static inline void |
822 | (g_clear_weak_pointer) (gpointer *weak_pointer_location) |
823 | { |
824 | GObject *object = (GObject *) *weak_pointer_location; |
825 | |
826 | if (object != NULL) |
827 | { |
828 | g_object_remove_weak_pointer (object, weak_pointer_location); |
829 | *weak_pointer_location = NULL; |
830 | } |
831 | } |
832 | |
833 | #define g_clear_weak_pointer(weak_pointer_location) \ |
834 | (/* Check types match. */ \ |
835 | (g_clear_weak_pointer) ((gpointer *) (weak_pointer_location)) \ |
836 | ) |
837 | |
838 | /** |
839 | * g_set_weak_pointer: (skip) |
840 | * @weak_pointer_location: the memory address of a pointer |
841 | * @new_object: (nullable) (transfer none): a pointer to the new #GObject to |
842 | * assign to it, or %NULL to clear the pointer |
843 | * |
844 | * Updates a pointer to weakly refer to @new_object. It assigns @new_object |
845 | * to @weak_pointer_location and ensures that @weak_pointer_location will |
846 | * automatically be set to %NULL if @new_object gets destroyed. The assignment |
847 | * is not atomic. The weak reference is not thread-safe, see |
848 | * g_object_add_weak_pointer() for details. |
849 | * |
850 | * @weak_pointer_location must not be %NULL. |
851 | * |
852 | * A macro is also included that allows this function to be used without |
853 | * pointer casts. The function itself is static inline, so its address may vary |
854 | * between compilation units. |
855 | * |
856 | * One convenient usage of this function is in implementing property setters: |
857 | * |[ |
858 | * void |
859 | * foo_set_bar (Foo *foo, |
860 | * Bar *new_bar) |
861 | * { |
862 | * g_return_if_fail (IS_FOO (foo)); |
863 | * g_return_if_fail (new_bar == NULL || IS_BAR (new_bar)); |
864 | * |
865 | * if (g_set_weak_pointer (&foo->bar, new_bar)) |
866 | * g_object_notify (foo, "bar"); |
867 | * } |
868 | * ]| |
869 | * |
870 | * Returns: %TRUE if the value of @weak_pointer_location changed, %FALSE otherwise |
871 | * |
872 | * Since: 2.56 |
873 | */ |
874 | static inline gboolean |
875 | (g_set_weak_pointer) (gpointer *weak_pointer_location, |
876 | GObject *new_object) |
877 | { |
878 | GObject *old_object = (GObject *) *weak_pointer_location; |
879 | |
880 | /* elide a (weak_pointer_location != NULL) check because most of the time we |
881 | * will be operating on struct members with a constant offset, so a NULL |
882 | * check would not catch bugs |
883 | */ |
884 | |
885 | if (old_object == new_object) |
886 | return FALSE; |
887 | |
888 | if (old_object != NULL) |
889 | g_object_remove_weak_pointer (object: old_object, weak_pointer_location); |
890 | |
891 | *weak_pointer_location = new_object; |
892 | |
893 | if (new_object != NULL) |
894 | g_object_add_weak_pointer (object: new_object, weak_pointer_location); |
895 | |
896 | return TRUE; |
897 | } |
898 | |
899 | #define g_set_weak_pointer(weak_pointer_location, new_object) \ |
900 | (/* Check types match. */ \ |
901 | 0 ? *(weak_pointer_location) = (new_object), FALSE : \ |
902 | (g_set_weak_pointer) ((gpointer *) (weak_pointer_location), (GObject *) (new_object)) \ |
903 | ) |
904 | |
905 | typedef struct { |
906 | /*<private>*/ |
907 | union { gpointer p; } priv; |
908 | } GWeakRef; |
909 | |
910 | GLIB_AVAILABLE_IN_ALL |
911 | void g_weak_ref_init (GWeakRef *weak_ref, |
912 | gpointer object); |
913 | GLIB_AVAILABLE_IN_ALL |
914 | void g_weak_ref_clear (GWeakRef *weak_ref); |
915 | GLIB_AVAILABLE_IN_ALL |
916 | gpointer g_weak_ref_get (GWeakRef *weak_ref); |
917 | GLIB_AVAILABLE_IN_ALL |
918 | void g_weak_ref_set (GWeakRef *weak_ref, |
919 | gpointer object); |
920 | |
921 | G_END_DECLS |
922 | |
923 | #endif /* __G_OBJECT_H__ */ |
924 | |