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 typedef void (*GWeakNotify) (gpointer data,
243 GObject *where_the_object_was);
244 /**
245 * GObject:
246 *
247 * The base object type.
248 *
249 * All the fields in the `GObject` structure are private to the implementation
250 * and should never be accessed directly.
251 */
252 struct _GObject
253 {
254 GTypeInstance g_type_instance;
255
256 /*< private >*/
257 guint ref_count; /* (atomic) */
258 GData *qdata;
259 };
260 /**
261 * GObjectClass:
262 * @g_type_class: the parent class
263 * @constructor: the @constructor function is called by g_object_new () to
264 * complete the object initialization after all the construction properties are
265 * set. The first thing a @constructor implementation must do is chain up to the
266 * @constructor of the parent class. Overriding @constructor should be rarely
267 * needed, e.g. to handle construct properties, or to implement singletons.
268 * @set_property: the generic setter for all properties of this type. Should be
269 * overridden for every type with properties. If implementations of
270 * @set_property don't emit property change notification explicitly, this will
271 * be done implicitly by the type system. However, if the notify signal is
272 * emitted explicitly, the type system will not emit it a second time.
273 * @get_property: the generic getter for all properties of this type. Should be
274 * overridden for every type with properties.
275 * @dispose: the @dispose function is supposed to drop all references to other
276 * objects, but keep the instance otherwise intact, so that client method
277 * invocations still work. It may be run multiple times (due to reference
278 * loops). Before returning, @dispose should chain up to the @dispose method
279 * of the parent class.
280 * @finalize: instance finalization function, should finish the finalization of
281 * the instance begun in @dispose and chain up to the @finalize method of the
282 * parent class.
283 * @dispatch_properties_changed: emits property change notification for a bunch
284 * of properties. Overriding @dispatch_properties_changed should be rarely
285 * needed.
286 * @notify: the class closure for the notify signal
287 * @constructed: the @constructed function is called by g_object_new() as the
288 * final step of the object creation process. At the point of the call, all
289 * construction properties have been set on the object. The purpose of this
290 * call is to allow for object initialisation steps that can only be performed
291 * after construction properties have been set. @constructed implementors
292 * should chain up to the @constructed call of their parent class to allow it
293 * to complete its initialisation.
294 *
295 * The class structure for the GObject type.
296 *
297 * |[<!-- language="C" -->
298 * // Example of implementing a singleton using a constructor.
299 * static MySingleton *the_singleton = NULL;
300 *
301 * static GObject*
302 * my_singleton_constructor (GType type,
303 * guint n_construct_params,
304 * GObjectConstructParam *construct_params)
305 * {
306 * GObject *object;
307 *
308 * if (!the_singleton)
309 * {
310 * object = G_OBJECT_CLASS (parent_class)->constructor (type,
311 * n_construct_params,
312 * construct_params);
313 * the_singleton = MY_SINGLETON (object);
314 * }
315 * else
316 * object = g_object_ref (G_OBJECT (the_singleton));
317 *
318 * return object;
319 * }
320 * ]|
321 */
322 struct _GObjectClass
323 {
324 GTypeClass g_type_class;
325
326 /*< private >*/
327 GSList *construct_properties;
328
329 /*< public >*/
330 /* seldom overridden */
331 GObject* (*constructor) (GType type,
332 guint n_construct_properties,
333 GObjectConstructParam *construct_properties);
334 /* overridable methods */
335 void (*set_property) (GObject *object,
336 guint property_id,
337 const GValue *value,
338 GParamSpec *pspec);
339 void (*get_property) (GObject *object,
340 guint property_id,
341 GValue *value,
342 GParamSpec *pspec);
343 void (*dispose) (GObject *object);
344 void (*finalize) (GObject *object);
345 /* seldom overridden */
346 void (*dispatch_properties_changed) (GObject *object,
347 guint n_pspecs,
348 GParamSpec **pspecs);
349 /* signals */
350 void (*notify) (GObject *object,
351 GParamSpec *pspec);
352
353 /* called when done constructing */
354 void (*constructed) (GObject *object);
355
356 /*< private >*/
357 gsize flags;
358
359 /* padding */
360 gpointer pdummy[6];
361 };
362
363 /**
364 * GObjectConstructParam:
365 * @pspec: the #GParamSpec of the construct parameter
366 * @value: the value to set the parameter to
367 *
368 * The GObjectConstructParam struct is an auxiliary structure used to hand
369 * #GParamSpec/#GValue pairs to the @constructor of a #GObjectClass.
370 */
371 struct _GObjectConstructParam
372 {
373 GParamSpec *pspec;
374 GValue *value;
375 };
376
377 /**
378 * GInitiallyUnowned:
379 *
380 * A type for objects that have an initially floating reference.
381 *
382 * All the fields in the `GInitiallyUnowned` structure are private to the
383 * implementation and should never be accessed directly.
384 */
385 /**
386 * GInitiallyUnownedClass:
387 *
388 * The class structure for the GInitiallyUnowned type.
389 */
390
391
392 /* --- prototypes --- */
393 GLIB_AVAILABLE_IN_ALL
394 GType g_initially_unowned_get_type (void);
395 GLIB_AVAILABLE_IN_ALL
396 void g_object_class_install_property (GObjectClass *oclass,
397 guint property_id,
398 GParamSpec *pspec);
399 GLIB_AVAILABLE_IN_ALL
400 GParamSpec* g_object_class_find_property (GObjectClass *oclass,
401 const gchar *property_name);
402 GLIB_AVAILABLE_IN_ALL
403 GParamSpec**g_object_class_list_properties (GObjectClass *oclass,
404 guint *n_properties);
405 GLIB_AVAILABLE_IN_ALL
406 void g_object_class_override_property (GObjectClass *oclass,
407 guint property_id,
408 const gchar *name);
409 GLIB_AVAILABLE_IN_ALL
410 void g_object_class_install_properties (GObjectClass *oclass,
411 guint n_pspecs,
412 GParamSpec **pspecs);
413
414 GLIB_AVAILABLE_IN_ALL
415 void g_object_interface_install_property (gpointer g_iface,
416 GParamSpec *pspec);
417 GLIB_AVAILABLE_IN_ALL
418 GParamSpec* g_object_interface_find_property (gpointer g_iface,
419 const gchar *property_name);
420 GLIB_AVAILABLE_IN_ALL
421 GParamSpec**g_object_interface_list_properties (gpointer g_iface,
422 guint *n_properties_p);
423
424 GLIB_AVAILABLE_IN_ALL
425 GType g_object_get_type (void) G_GNUC_CONST;
426 GLIB_AVAILABLE_IN_ALL
427 gpointer g_object_new (GType object_type,
428 const gchar *first_property_name,
429 ...);
430 GLIB_AVAILABLE_IN_2_54
431 GObject* g_object_new_with_properties (GType object_type,
432 guint n_properties,
433 const char *names[],
434 const GValue values[]);
435
436 G_GNUC_BEGIN_IGNORE_DEPRECATIONS
437
438 GLIB_DEPRECATED_IN_2_54_FOR(g_object_new_with_properties)
439 gpointer g_object_newv (GType object_type,
440 guint n_parameters,
441 GParameter *parameters);
442
443 G_GNUC_END_IGNORE_DEPRECATIONS
444
445 GLIB_AVAILABLE_IN_ALL
446 GObject* g_object_new_valist (GType object_type,
447 const gchar *first_property_name,
448 va_list var_args);
449 GLIB_AVAILABLE_IN_ALL
450 void g_object_set (gpointer object,
451 const gchar *first_property_name,
452 ...) G_GNUC_NULL_TERMINATED;
453 GLIB_AVAILABLE_IN_ALL
454 void g_object_get (gpointer object,
455 const gchar *first_property_name,
456 ...) G_GNUC_NULL_TERMINATED;
457 GLIB_AVAILABLE_IN_ALL
458 gpointer g_object_connect (gpointer object,
459 const gchar *signal_spec,
460 ...) G_GNUC_NULL_TERMINATED;
461 GLIB_AVAILABLE_IN_ALL
462 void g_object_disconnect (gpointer object,
463 const gchar *signal_spec,
464 ...) G_GNUC_NULL_TERMINATED;
465 GLIB_AVAILABLE_IN_2_54
466 void g_object_setv (GObject *object,
467 guint n_properties,
468 const gchar *names[],
469 const GValue values[]);
470 GLIB_AVAILABLE_IN_ALL
471 void g_object_set_valist (GObject *object,
472 const gchar *first_property_name,
473 va_list var_args);
474 GLIB_AVAILABLE_IN_2_54
475 void g_object_getv (GObject *object,
476 guint n_properties,
477 const gchar *names[],
478 GValue values[]);
479 GLIB_AVAILABLE_IN_ALL
480 void g_object_get_valist (GObject *object,
481 const gchar *first_property_name,
482 va_list var_args);
483 GLIB_AVAILABLE_IN_ALL
484 void g_object_set_property (GObject *object,
485 const gchar *property_name,
486 const GValue *value);
487 GLIB_AVAILABLE_IN_ALL
488 void g_object_get_property (GObject *object,
489 const gchar *property_name,
490 GValue *value);
491 GLIB_AVAILABLE_IN_ALL
492 void g_object_freeze_notify (GObject *object);
493 GLIB_AVAILABLE_IN_ALL
494 void g_object_notify (GObject *object,
495 const gchar *property_name);
496 GLIB_AVAILABLE_IN_ALL
497 void g_object_notify_by_pspec (GObject *object,
498 GParamSpec *pspec);
499 GLIB_AVAILABLE_IN_ALL
500 void g_object_thaw_notify (GObject *object);
501 GLIB_AVAILABLE_IN_ALL
502 gboolean g_object_is_floating (gpointer object);
503 GLIB_AVAILABLE_IN_ALL
504 gpointer g_object_ref_sink (gpointer object);
505 GLIB_AVAILABLE_IN_2_70
506 gpointer g_object_take_ref (gpointer object);
507 GLIB_AVAILABLE_IN_ALL
508 gpointer g_object_ref (gpointer object);
509 GLIB_AVAILABLE_IN_ALL
510 void g_object_unref (gpointer object);
511 GLIB_AVAILABLE_IN_ALL
512 void g_object_weak_ref (GObject *object,
513 GWeakNotify notify,
514 gpointer data);
515 GLIB_AVAILABLE_IN_ALL
516 void g_object_weak_unref (GObject *object,
517 GWeakNotify notify,
518 gpointer data);
519 GLIB_AVAILABLE_IN_ALL
520 void g_object_add_weak_pointer (GObject *object,
521 gpointer *weak_pointer_location);
522 GLIB_AVAILABLE_IN_ALL
523 void g_object_remove_weak_pointer (GObject *object,
524 gpointer *weak_pointer_location);
525
526 #if defined(glib_typeof) && GLIB_VERSION_MAX_ALLOWED >= GLIB_VERSION_2_56
527 /* Make reference APIs type safe with macros */
528 #define g_object_ref(Obj) ((glib_typeof (Obj)) (g_object_ref) (Obj))
529 #define g_object_ref_sink(Obj) ((glib_typeof (Obj)) (g_object_ref_sink) (Obj))
530 #endif
531
532 /**
533 * GToggleNotify:
534 * @data: Callback data passed to g_object_add_toggle_ref()
535 * @object: The object on which g_object_add_toggle_ref() was called.
536 * @is_last_ref: %TRUE if the toggle reference is now the
537 * last reference to the object. %FALSE if the toggle
538 * reference was the last reference and there are now other
539 * references.
540 *
541 * A callback function used for notification when the state
542 * of a toggle reference changes.
543 *
544 * See also: g_object_add_toggle_ref()
545 */
546 typedef void (*GToggleNotify) (gpointer data,
547 GObject *object,
548 gboolean is_last_ref);
549
550 GLIB_AVAILABLE_IN_ALL
551 void g_object_add_toggle_ref (GObject *object,
552 GToggleNotify notify,
553 gpointer data);
554 GLIB_AVAILABLE_IN_ALL
555 void g_object_remove_toggle_ref (GObject *object,
556 GToggleNotify notify,
557 gpointer data);
558
559 GLIB_AVAILABLE_IN_ALL
560 gpointer g_object_get_qdata (GObject *object,
561 GQuark quark);
562 GLIB_AVAILABLE_IN_ALL
563 void g_object_set_qdata (GObject *object,
564 GQuark quark,
565 gpointer data);
566 GLIB_AVAILABLE_IN_ALL
567 void g_object_set_qdata_full (GObject *object,
568 GQuark quark,
569 gpointer data,
570 GDestroyNotify destroy);
571 GLIB_AVAILABLE_IN_ALL
572 gpointer g_object_steal_qdata (GObject *object,
573 GQuark quark);
574
575 GLIB_AVAILABLE_IN_2_34
576 gpointer g_object_dup_qdata (GObject *object,
577 GQuark quark,
578 GDuplicateFunc dup_func,
579 gpointer user_data);
580 GLIB_AVAILABLE_IN_2_34
581 gboolean g_object_replace_qdata (GObject *object,
582 GQuark quark,
583 gpointer oldval,
584 gpointer newval,
585 GDestroyNotify destroy,
586 GDestroyNotify *old_destroy);
587
588 GLIB_AVAILABLE_IN_ALL
589 gpointer g_object_get_data (GObject *object,
590 const gchar *key);
591 GLIB_AVAILABLE_IN_ALL
592 void g_object_set_data (GObject *object,
593 const gchar *key,
594 gpointer data);
595 GLIB_AVAILABLE_IN_ALL
596 void g_object_set_data_full (GObject *object,
597 const gchar *key,
598 gpointer data,
599 GDestroyNotify destroy);
600 GLIB_AVAILABLE_IN_ALL
601 gpointer g_object_steal_data (GObject *object,
602 const gchar *key);
603
604 GLIB_AVAILABLE_IN_2_34
605 gpointer g_object_dup_data (GObject *object,
606 const gchar *key,
607 GDuplicateFunc dup_func,
608 gpointer user_data);
609 GLIB_AVAILABLE_IN_2_34
610 gboolean g_object_replace_data (GObject *object,
611 const gchar *key,
612 gpointer oldval,
613 gpointer newval,
614 GDestroyNotify destroy,
615 GDestroyNotify *old_destroy);
616
617
618 GLIB_AVAILABLE_IN_ALL
619 void g_object_watch_closure (GObject *object,
620 GClosure *closure);
621 GLIB_AVAILABLE_IN_ALL
622 GClosure* g_cclosure_new_object (GCallback callback_func,
623 GObject *object);
624 GLIB_AVAILABLE_IN_ALL
625 GClosure* g_cclosure_new_object_swap (GCallback callback_func,
626 GObject *object);
627 GLIB_AVAILABLE_IN_ALL
628 GClosure* g_closure_new_object (guint sizeof_closure,
629 GObject *object);
630 GLIB_AVAILABLE_IN_ALL
631 void g_value_set_object (GValue *value,
632 gpointer v_object);
633 GLIB_AVAILABLE_IN_ALL
634 gpointer g_value_get_object (const GValue *value);
635 GLIB_AVAILABLE_IN_ALL
636 gpointer g_value_dup_object (const GValue *value);
637 GLIB_AVAILABLE_IN_ALL
638 gulong g_signal_connect_object (gpointer instance,
639 const gchar *detailed_signal,
640 GCallback c_handler,
641 gpointer gobject,
642 GConnectFlags connect_flags);
643
644 /*< protected >*/
645 GLIB_AVAILABLE_IN_ALL
646 void g_object_force_floating (GObject *object);
647 GLIB_AVAILABLE_IN_ALL
648 void g_object_run_dispose (GObject *object);
649
650
651 GLIB_AVAILABLE_IN_ALL
652 void g_value_take_object (GValue *value,
653 gpointer v_object);
654 GLIB_DEPRECATED_FOR(g_value_take_object)
655 void g_value_set_object_take_ownership (GValue *value,
656 gpointer v_object);
657
658 GLIB_DEPRECATED
659 gsize g_object_compat_control (gsize what,
660 gpointer data);
661
662 /* --- implementation macros --- */
663 #define G_OBJECT_WARN_INVALID_PSPEC(object, pname, property_id, pspec) \
664 G_STMT_START { \
665 GObject *_glib__object = (GObject*) (object); \
666 GParamSpec *_glib__pspec = (GParamSpec*) (pspec); \
667 guint _glib__property_id = (property_id); \
668 g_warning ("%s:%d: invalid %s id %u for \"%s\" of type '%s' in '%s'", \
669 __FILE__, __LINE__, \
670 (pname), \
671 _glib__property_id, \
672 _glib__pspec->name, \
673 g_type_name (G_PARAM_SPEC_TYPE (_glib__pspec)), \
674 G_OBJECT_TYPE_NAME (_glib__object)); \
675 } G_STMT_END
676 /**
677 * G_OBJECT_WARN_INVALID_PROPERTY_ID:
678 * @object: the #GObject on which set_property() or get_property() was called
679 * @property_id: the numeric id of the property
680 * @pspec: the #GParamSpec of the property
681 *
682 * This macro should be used to emit a standard warning about unexpected
683 * properties in set_property() and get_property() implementations.
684 */
685 #define G_OBJECT_WARN_INVALID_PROPERTY_ID(object, property_id, pspec) \
686 G_OBJECT_WARN_INVALID_PSPEC ((object), "property", (property_id), (pspec))
687
688 GLIB_AVAILABLE_IN_ALL
689 void g_clear_object (GObject **object_ptr);
690 #define g_clear_object(object_ptr) g_clear_pointer ((object_ptr), g_object_unref)
691
692 /**
693 * g_set_object: (skip)
694 * @object_ptr: (inout) (not optional) (nullable): a pointer to a #GObject reference
695 * @new_object: (nullable) (transfer none): a pointer to the new #GObject to
696 * assign to @object_ptr, or %NULL to clear the pointer
697 *
698 * Updates a #GObject pointer to refer to @new_object.
699 *
700 * It increments the reference count of @new_object (if non-%NULL), decrements
701 * the reference count of the current value of @object_ptr (if non-%NULL), and
702 * assigns @new_object to @object_ptr. The assignment is not atomic.
703 *
704 * @object_ptr must not be %NULL, but can point to a %NULL value.
705 *
706 * A macro is also included that allows this function to be used without
707 * pointer casts. The function itself is static inline, so its address may vary
708 * between compilation units.
709 *
710 * One convenient usage of this function is in implementing property setters:
711 * |[
712 * void
713 * foo_set_bar (Foo *foo,
714 * Bar *new_bar)
715 * {
716 * g_return_if_fail (IS_FOO (foo));
717 * g_return_if_fail (new_bar == NULL || IS_BAR (new_bar));
718 *
719 * if (g_set_object (&foo->bar, new_bar))
720 * g_object_notify (foo, "bar");
721 * }
722 * ]|
723 *
724 * Returns: %TRUE if the value of @object_ptr changed, %FALSE otherwise
725 *
726 * Since: 2.44
727 */
gboolean(g_set_object)728 static inline gboolean
729 (g_set_object) (GObject **object_ptr,
730 GObject *new_object)
731 {
732 GObject *old_object = *object_ptr;
733
734 /* rely on g_object_[un]ref() to check the pointers are actually GObjects;
735 * elide a (object_ptr != NULL) check because most of the time we will be
736 * operating on struct members with a constant offset, so a NULL check would
737 * not catch bugs
738 */
739
740 if (old_object == new_object)
741 return FALSE;
742
743 if (new_object != NULL)
744 g_object_ref (new_object);
745
746 *object_ptr = new_object;
747
748 if (old_object != NULL)
749 g_object_unref (old_object);
750
751 return TRUE;
752 }
753
754 /* We need GCC for __extension__, which we need to sort out strict aliasing of @object_ptr */
755 #if defined(__GNUC__)
756
757 #define g_set_object(object_ptr, new_object) \
758 (G_GNUC_EXTENSION ({ \
759 G_STATIC_ASSERT (sizeof *(object_ptr) == sizeof (new_object)); \
760 /* Only one access, please; work around type aliasing */ \
761 union { char *in; GObject **out; } _object_ptr; \
762 _object_ptr.in = (char *) (object_ptr); \
763 /* Check types match */ \
764 (void) (0 ? *(object_ptr) = (new_object), FALSE : FALSE); \
765 (g_set_object) (_object_ptr.out, (GObject *) new_object); \
766 })) \
767 GLIB_AVAILABLE_MACRO_IN_2_44
768
769 #else /* if !defined(__GNUC__) */
770
771 #define g_set_object(object_ptr, new_object) \
772 (/* Check types match. */ \
773 0 ? *(object_ptr) = (new_object), FALSE : \
774 (g_set_object) ((GObject **) (object_ptr), (GObject *) (new_object)) \
775 )
776
777 #endif /* !defined(__GNUC__) */
778
779 /**
780 * g_assert_finalize_object: (skip)
781 * @object: (transfer full) (type GObject.Object): an object
782 *
783 * Assert that @object is non-%NULL, then release one reference to it with
784 * g_object_unref() and assert that it has been finalized (i.e. that there
785 * are no more references).
786 *
787 * If assertions are disabled via `G_DISABLE_ASSERT`,
788 * this macro just calls g_object_unref() without any further checks.
789 *
790 * This macro should only be used in regression tests.
791 *
792 * Since: 2.62
793 */
794 static inline void
795 (g_assert_finalize_object) (GObject *object)
796 {
797 gpointer weak_pointer = object;
798
799 g_assert_true (G_IS_OBJECT (weak_pointer));
800 g_object_add_weak_pointer (object, &weak_pointer);
801 g_object_unref (weak_pointer);
802 g_assert_null (weak_pointer);
803 }
804
805 #ifdef G_DISABLE_ASSERT
806 #define g_assert_finalize_object(object) g_object_unref (object)
807 #else
808 #define g_assert_finalize_object(object) (g_assert_finalize_object ((GObject *) object))
809 #endif
810
811 /**
812 * g_clear_weak_pointer: (skip)
813 * @weak_pointer_location: The memory address of a pointer
814 *
815 * Clears a weak reference to a #GObject.
816 *
817 * @weak_pointer_location must not be %NULL.
818 *
819 * If the weak reference is %NULL then this function does nothing.
820 * Otherwise, the weak reference to the object is removed for that location
821 * and the pointer is set to %NULL.
822 *
823 * A macro is also included that allows this function to be used without
824 * pointer casts. The function itself is static inline, so its address may vary
825 * between compilation units.
826 *
827 * Since: 2.56
828 */
829 static inline void
830 (g_clear_weak_pointer) (gpointer *weak_pointer_location)
831 {
832 GObject *object = (GObject *) *weak_pointer_location;
833
834 if (object != NULL)
835 {
836 g_object_remove_weak_pointer (object, weak_pointer_location);
837 *weak_pointer_location = NULL;
838 }
839 }
840
841 #define g_clear_weak_pointer(weak_pointer_location) \
842 (/* Check types match. */ \
843 (g_clear_weak_pointer) ((gpointer *) (weak_pointer_location)) \
844 )
845
846 /**
847 * g_set_weak_pointer: (skip)
848 * @weak_pointer_location: the memory address of a pointer
849 * @new_object: (nullable) (transfer none): a pointer to the new #GObject to
850 * assign to it, or %NULL to clear the pointer
851 *
852 * Updates a pointer to weakly refer to @new_object.
853 *
854 * It assigns @new_object to @weak_pointer_location and ensures
855 * that @weak_pointer_location will automatically be set to %NULL
856 * if @new_object gets destroyed. The assignment is not atomic.
857 * The weak reference is not thread-safe, see g_object_add_weak_pointer()
858 * for details.
859 *
860 * The @weak_pointer_location argument must not be %NULL.
861 *
862 * A macro is also included that allows this function to be used without
863 * pointer casts. The function itself is static inline, so its address may vary
864 * between compilation units.
865 *
866 * One convenient usage of this function is in implementing property setters:
867 * |[
868 * void
869 * foo_set_bar (Foo *foo,
870 * Bar *new_bar)
871 * {
872 * g_return_if_fail (IS_FOO (foo));
873 * g_return_if_fail (new_bar == NULL || IS_BAR (new_bar));
874 *
875 * if (g_set_weak_pointer (&foo->bar, new_bar))
876 * g_object_notify (foo, "bar");
877 * }
878 * ]|
879 *
880 * Returns: %TRUE if the value of @weak_pointer_location changed, %FALSE otherwise
881 *
882 * Since: 2.56
883 */
gboolean(g_set_weak_pointer)884 static inline gboolean
885 (g_set_weak_pointer) (gpointer *weak_pointer_location,
886 GObject *new_object)
887 {
888 GObject *old_object = (GObject *) *weak_pointer_location;
889
890 /* elide a (weak_pointer_location != NULL) check because most of the time we
891 * will be operating on struct members with a constant offset, so a NULL
892 * check would not catch bugs
893 */
894
895 if (old_object == new_object)
896 return FALSE;
897
898 if (old_object != NULL)
899 g_object_remove_weak_pointer (old_object, weak_pointer_location);
900
901 *weak_pointer_location = new_object;
902
903 if (new_object != NULL)
904 g_object_add_weak_pointer (new_object, weak_pointer_location);
905
906 return TRUE;
907 }
908
909 #define g_set_weak_pointer(weak_pointer_location, new_object) \
910 (/* Check types match. */ \
911 0 ? *(weak_pointer_location) = (new_object), FALSE : \
912 (g_set_weak_pointer) ((gpointer *) (weak_pointer_location), (GObject *) (new_object)) \
913 )
914
915 typedef struct {
916 /*<private>*/
917 union { gpointer p; } priv;
918 } GWeakRef;
919
920 GLIB_AVAILABLE_IN_ALL
921 void g_weak_ref_init (GWeakRef *weak_ref,
922 gpointer object);
923 GLIB_AVAILABLE_IN_ALL
924 void g_weak_ref_clear (GWeakRef *weak_ref);
925 GLIB_AVAILABLE_IN_ALL
926 gpointer g_weak_ref_get (GWeakRef *weak_ref);
927 GLIB_AVAILABLE_IN_ALL
928 void g_weak_ref_set (GWeakRef *weak_ref,
929 gpointer object);
930
931 G_END_DECLS
932
933 #endif /* __G_OBJECT_H__ */
934