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