/src/rauc/subprojects/glib-2.76.5/gobject/gobject.c
Line | Count | Source |
1 | | /* GObject - GLib Type, Object, Parameter and Signal Library |
2 | | * Copyright (C) 1998-1999, 2000-2001 Tim Janik and Red Hat, Inc. |
3 | | * |
4 | | * SPDX-License-Identifier: LGPL-2.1-or-later |
5 | | * |
6 | | * This library is free software; you can redistribute it and/or |
7 | | * modify it under the terms of the GNU Lesser General Public |
8 | | * License as published by the Free Software Foundation; either |
9 | | * version 2.1 of the License, or (at your option) any later version. |
10 | | * |
11 | | * This library is distributed in the hope that it will be useful, |
12 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
14 | | * Lesser General Public License for more details. |
15 | | * |
16 | | * You should have received a copy of the GNU Lesser General |
17 | | * Public License along with this library; if not, see <http://www.gnu.org/licenses/>. |
18 | | */ |
19 | | |
20 | | /* |
21 | | * MT safe with regards to reference counting. |
22 | | */ |
23 | | |
24 | | #include "config.h" |
25 | | |
26 | | #include <string.h> |
27 | | #include <signal.h> |
28 | | |
29 | | #include "../glib/glib-private.h" |
30 | | |
31 | | #include "gobject.h" |
32 | | #include "gtype-private.h" |
33 | | #include "gvaluecollector.h" |
34 | | #include "gsignal.h" |
35 | | #include "gparamspecs.h" |
36 | | #include "gvaluetypes.h" |
37 | | #include "gobject_trace.h" |
38 | | #include "gconstructor.h" |
39 | | |
40 | | /** |
41 | | * SECTION:objects |
42 | | * @title: GObject |
43 | | * @short_description: The base object type |
44 | | * @see_also: #GParamSpecObject, g_param_spec_object() |
45 | | * |
46 | | * GObject is the fundamental type providing the common attributes and |
47 | | * methods for all object types in GTK+, Pango and other libraries |
48 | | * based on GObject. The GObject class provides methods for object |
49 | | * construction and destruction, property access methods, and signal |
50 | | * support. Signals are described in detail [here][gobject-Signals]. |
51 | | * |
52 | | * For a tutorial on implementing a new GObject class, see [How to define and |
53 | | * implement a new GObject][howto-gobject]. For a list of naming conventions for |
54 | | * GObjects and their methods, see the [GType conventions][gtype-conventions]. |
55 | | * For the high-level concepts behind GObject, read [Instantiatable classed types: |
56 | | * Objects][gtype-instantiatable-classed]. |
57 | | * |
58 | | * ## Floating references # {#floating-ref} |
59 | | * |
60 | | * **Note**: Floating references are a C convenience API and should not be |
61 | | * used in modern GObject code. Language bindings in particular find the |
62 | | * concept highly problematic, as floating references are not identifiable |
63 | | * through annotations, and neither are deviations from the floating reference |
64 | | * behavior, like types that inherit from #GInitiallyUnowned and still return |
65 | | * a full reference from g_object_new(). |
66 | | * |
67 | | * GInitiallyUnowned is derived from GObject. The only difference between |
68 | | * the two is that the initial reference of a GInitiallyUnowned is flagged |
69 | | * as a "floating" reference. This means that it is not specifically |
70 | | * claimed to be "owned" by any code portion. The main motivation for |
71 | | * providing floating references is C convenience. In particular, it |
72 | | * allows code to be written as: |
73 | | * |
74 | | * |[<!-- language="C" --> |
75 | | * container = create_container (); |
76 | | * container_add_child (container, create_child()); |
77 | | * ]| |
78 | | * |
79 | | * If container_add_child() calls g_object_ref_sink() on the passed-in child, |
80 | | * no reference of the newly created child is leaked. Without floating |
81 | | * references, container_add_child() can only g_object_ref() the new child, |
82 | | * so to implement this code without reference leaks, it would have to be |
83 | | * written as: |
84 | | * |
85 | | * |[<!-- language="C" --> |
86 | | * Child *child; |
87 | | * container = create_container (); |
88 | | * child = create_child (); |
89 | | * container_add_child (container, child); |
90 | | * g_object_unref (child); |
91 | | * ]| |
92 | | * |
93 | | * The floating reference can be converted into an ordinary reference by |
94 | | * calling g_object_ref_sink(). For already sunken objects (objects that |
95 | | * don't have a floating reference anymore), g_object_ref_sink() is equivalent |
96 | | * to g_object_ref() and returns a new reference. |
97 | | * |
98 | | * Since floating references are useful almost exclusively for C convenience, |
99 | | * language bindings that provide automated reference and memory ownership |
100 | | * maintenance (such as smart pointers or garbage collection) should not |
101 | | * expose floating references in their API. The best practice for handling |
102 | | * types that have initially floating references is to immediately sink those |
103 | | * references after g_object_new() returns, by checking if the #GType |
104 | | * inherits from #GInitiallyUnowned. For instance: |
105 | | * |
106 | | * |[<!-- language="C" --> |
107 | | * GObject *res = g_object_new_with_properties (gtype, |
108 | | * n_props, |
109 | | * prop_names, |
110 | | * prop_values); |
111 | | * |
112 | | * // or: if (g_type_is_a (gtype, G_TYPE_INITIALLY_UNOWNED)) |
113 | | * if (G_IS_INITIALLY_UNOWNED (res)) |
114 | | * g_object_ref_sink (res); |
115 | | * |
116 | | * return res; |
117 | | * ]| |
118 | | * |
119 | | * Some object implementations may need to save an objects floating state |
120 | | * across certain code portions (an example is #GtkMenu), to achieve this, |
121 | | * the following sequence can be used: |
122 | | * |
123 | | * |[<!-- language="C" --> |
124 | | * // save floating state |
125 | | * gboolean was_floating = g_object_is_floating (object); |
126 | | * g_object_ref_sink (object); |
127 | | * // protected code portion |
128 | | * |
129 | | * ... |
130 | | * |
131 | | * // restore floating state |
132 | | * if (was_floating) |
133 | | * g_object_force_floating (object); |
134 | | * else |
135 | | * g_object_unref (object); // release previously acquired reference |
136 | | * ]| |
137 | | */ |
138 | | |
139 | | |
140 | | /* --- macros --- */ |
141 | 0 | #define PARAM_SPEC_PARAM_ID(pspec) ((pspec)->param_id) |
142 | 0 | #define PARAM_SPEC_SET_PARAM_ID(pspec, id) ((pspec)->param_id = (id)) |
143 | | |
144 | 0 | #define OBJECT_HAS_TOGGLE_REF_FLAG 0x1 |
145 | | #define OBJECT_HAS_TOGGLE_REF(object) \ |
146 | 0 | ((g_datalist_get_flags (&(object)->qdata) & OBJECT_HAS_TOGGLE_REF_FLAG) != 0) |
147 | 0 | #define OBJECT_FLOATING_FLAG 0x2 |
148 | | |
149 | 28.3k | #define CLASS_HAS_PROPS_FLAG 0x1 |
150 | | #define CLASS_HAS_PROPS(class) \ |
151 | 42.4k | ((class)->flags & CLASS_HAS_PROPS_FLAG) |
152 | | #define CLASS_HAS_CUSTOM_CONSTRUCTOR(class) \ |
153 | | ((class)->constructor != g_object_constructor) |
154 | | #define CLASS_HAS_CUSTOM_CONSTRUCTED(class) \ |
155 | 14.1k | ((class)->constructed != g_object_constructed) |
156 | 0 | #define CLASS_HAS_NOTIFY(class) ((class)->notify != NULL) |
157 | | #define CLASS_HAS_CUSTOM_DISPATCH(class) \ |
158 | 0 | ((class)->dispatch_properties_changed != g_object_dispatch_properties_changed) |
159 | | #define CLASS_NEEDS_NOTIFY(class) \ |
160 | 0 | (CLASS_HAS_NOTIFY(class) || CLASS_HAS_CUSTOM_DISPATCH(class)) |
161 | | |
162 | 15 | #define CLASS_HAS_DERIVED_CLASS_FLAG 0x2 |
163 | | #define CLASS_HAS_DERIVED_CLASS(class) \ |
164 | 0 | ((class)->flags & CLASS_HAS_DERIVED_CLASS_FLAG) |
165 | | |
166 | | /* --- signals --- */ |
167 | | enum { |
168 | | NOTIFY, |
169 | | LAST_SIGNAL |
170 | | }; |
171 | | |
172 | | |
173 | | /* --- properties --- */ |
174 | | enum { |
175 | | PROP_NONE |
176 | | }; |
177 | | |
178 | 28.3k | #define OPTIONAL_FLAG_IN_CONSTRUCTION (1 << 0) |
179 | 0 | #define OPTIONAL_FLAG_HAS_SIGNAL_HANDLER (1 << 1) /* Set if object ever had a signal handler */ |
180 | 0 | #define OPTIONAL_FLAG_HAS_NOTIFY_HANDLER (1 << 2) /* Same, specifically for "notify" */ |
181 | | |
182 | | #if SIZEOF_INT == 4 && GLIB_SIZEOF_VOID_P == 8 |
183 | | #define HAVE_OPTIONAL_FLAGS |
184 | | #endif |
185 | | |
186 | | typedef struct |
187 | | { |
188 | | GTypeInstance g_type_instance; |
189 | | |
190 | | /*< private >*/ |
191 | | guint ref_count; /* (atomic) */ |
192 | | #ifdef HAVE_OPTIONAL_FLAGS |
193 | | guint optional_flags; /* (atomic) */ |
194 | | #endif |
195 | | GData *qdata; |
196 | | } GObjectReal; |
197 | | |
198 | | G_STATIC_ASSERT(sizeof(GObject) == sizeof(GObjectReal)); |
199 | | G_STATIC_ASSERT(G_STRUCT_OFFSET(GObject, ref_count) == G_STRUCT_OFFSET(GObjectReal, ref_count)); |
200 | | G_STATIC_ASSERT(G_STRUCT_OFFSET(GObject, qdata) == G_STRUCT_OFFSET(GObjectReal, qdata)); |
201 | | |
202 | | |
203 | | /* --- prototypes --- */ |
204 | | static void g_object_base_class_init (GObjectClass *class); |
205 | | static void g_object_base_class_finalize (GObjectClass *class); |
206 | | static void g_object_do_class_init (GObjectClass *class); |
207 | | static void g_object_init (GObject *object, |
208 | | GObjectClass *class); |
209 | | static GObject* g_object_constructor (GType type, |
210 | | guint n_construct_properties, |
211 | | GObjectConstructParam *construct_params); |
212 | | static void g_object_constructed (GObject *object); |
213 | | static void g_object_real_dispose (GObject *object); |
214 | | static void g_object_finalize (GObject *object); |
215 | | static void g_object_do_set_property (GObject *object, |
216 | | guint property_id, |
217 | | const GValue *value, |
218 | | GParamSpec *pspec); |
219 | | static void g_object_do_get_property (GObject *object, |
220 | | guint property_id, |
221 | | GValue *value, |
222 | | GParamSpec *pspec); |
223 | | static void g_value_object_init (GValue *value); |
224 | | static void g_value_object_free_value (GValue *value); |
225 | | static void g_value_object_copy_value (const GValue *src_value, |
226 | | GValue *dest_value); |
227 | | static void g_value_object_transform_value (const GValue *src_value, |
228 | | GValue *dest_value); |
229 | | static gpointer g_value_object_peek_pointer (const GValue *value); |
230 | | static gchar* g_value_object_collect_value (GValue *value, |
231 | | guint n_collect_values, |
232 | | GTypeCValue *collect_values, |
233 | | guint collect_flags); |
234 | | static gchar* g_value_object_lcopy_value (const GValue *value, |
235 | | guint n_collect_values, |
236 | | GTypeCValue *collect_values, |
237 | | guint collect_flags); |
238 | | static void g_object_dispatch_properties_changed (GObject *object, |
239 | | guint n_pspecs, |
240 | | GParamSpec **pspecs); |
241 | | static guint object_floating_flag_handler (GObject *object, |
242 | | gint job); |
243 | | |
244 | | static void object_interface_check_properties (gpointer check_data, |
245 | | gpointer g_iface); |
246 | | static void weak_locations_free_unlocked (GSList **weak_locations); |
247 | | |
248 | | /* --- typedefs --- */ |
249 | | typedef struct _GObjectNotifyQueue GObjectNotifyQueue; |
250 | | |
251 | | struct _GObjectNotifyQueue |
252 | | { |
253 | | GSList *pspecs; |
254 | | guint16 n_pspecs; |
255 | | guint16 freeze_count; |
256 | | }; |
257 | | |
258 | | /* --- variables --- */ |
259 | | G_LOCK_DEFINE_STATIC (closure_array_mutex); |
260 | | G_LOCK_DEFINE_STATIC (weak_refs_mutex); |
261 | | G_LOCK_DEFINE_STATIC (toggle_refs_mutex); |
262 | | static GQuark quark_closure_array = 0; |
263 | | static GQuark quark_weak_refs = 0; |
264 | | static GQuark quark_toggle_refs = 0; |
265 | | static GQuark quark_notify_queue; |
266 | | #ifndef HAVE_OPTIONAL_FLAGS |
267 | | static GQuark quark_in_construction; |
268 | | #endif |
269 | | static GParamSpecPool *pspec_pool = NULL; |
270 | | static gulong gobject_signals[LAST_SIGNAL] = { 0, }; |
271 | | static guint (*floating_flag_handler) (GObject*, gint) = object_floating_flag_handler; |
272 | | /* qdata pointing to GSList<GWeakRef *>, protected by weak_locations_lock */ |
273 | | static GQuark quark_weak_locations = 0; |
274 | | static GRWLock weak_locations_lock; |
275 | | |
276 | | G_LOCK_DEFINE_STATIC(notify_lock); |
277 | | |
278 | | /* --- functions --- */ |
279 | | static void |
280 | | g_object_notify_queue_free (gpointer data) |
281 | 14.1k | { |
282 | 14.1k | GObjectNotifyQueue *nqueue = data; |
283 | | |
284 | 14.1k | g_slist_free (nqueue->pspecs); |
285 | 14.1k | g_slice_free (GObjectNotifyQueue, nqueue); |
286 | 14.1k | } |
287 | | |
288 | | static GObjectNotifyQueue* |
289 | | g_object_notify_queue_freeze (GObject *object, |
290 | | gboolean conditional) |
291 | 14.1k | { |
292 | 14.1k | GObjectNotifyQueue *nqueue; |
293 | | |
294 | 14.1k | G_LOCK(notify_lock); |
295 | 14.1k | nqueue = g_datalist_id_get_data (&object->qdata, quark_notify_queue); |
296 | 14.1k | if (!nqueue) |
297 | 14.1k | { |
298 | 14.1k | if (conditional) |
299 | 0 | { |
300 | 0 | G_UNLOCK(notify_lock); |
301 | 0 | return NULL; |
302 | 0 | } |
303 | | |
304 | 14.1k | nqueue = g_slice_new0 (GObjectNotifyQueue); |
305 | 14.1k | g_datalist_id_set_data_full (&object->qdata, quark_notify_queue, |
306 | 14.1k | nqueue, g_object_notify_queue_free); |
307 | 14.1k | } |
308 | | |
309 | 14.1k | if (nqueue->freeze_count >= 65535) |
310 | 0 | g_critical("Free queue for %s (%p) is larger than 65535," |
311 | 14.1k | " called g_object_freeze_notify() too often." |
312 | 14.1k | " Forgot to call g_object_thaw_notify() or infinite loop", |
313 | 14.1k | G_OBJECT_TYPE_NAME (object), object); |
314 | 14.1k | else |
315 | 14.1k | nqueue->freeze_count++; |
316 | | |
317 | 14.1k | G_UNLOCK(notify_lock); |
318 | | |
319 | 14.1k | return nqueue; |
320 | 14.1k | } |
321 | | |
322 | | static void |
323 | | g_object_notify_queue_thaw (GObject *object, |
324 | | GObjectNotifyQueue *nqueue) |
325 | 0 | { |
326 | 0 | GParamSpec *pspecs_mem[16], **pspecs, **free_me = NULL; |
327 | 0 | GSList *slist; |
328 | 0 | guint n_pspecs = 0; |
329 | |
|
330 | 0 | G_LOCK(notify_lock); |
331 | | |
332 | | /* Just make sure we never get into some nasty race condition */ |
333 | 0 | if (G_UNLIKELY (nqueue->freeze_count == 0)) |
334 | 0 | { |
335 | 0 | G_UNLOCK (notify_lock); |
336 | 0 | g_critical ("%s: property-changed notification for %s(%p) is not frozen", |
337 | 0 | G_STRFUNC, G_OBJECT_TYPE_NAME (object), object); |
338 | 0 | return; |
339 | 0 | } |
340 | | |
341 | 0 | nqueue->freeze_count--; |
342 | 0 | if (nqueue->freeze_count) |
343 | 0 | { |
344 | 0 | G_UNLOCK (notify_lock); |
345 | 0 | return; |
346 | 0 | } |
347 | | |
348 | 0 | pspecs = nqueue->n_pspecs > 16 ? free_me = g_new (GParamSpec*, nqueue->n_pspecs) : pspecs_mem; |
349 | |
|
350 | 0 | for (slist = nqueue->pspecs; slist; slist = slist->next) |
351 | 0 | { |
352 | 0 | pspecs[n_pspecs++] = slist->data; |
353 | 0 | } |
354 | 0 | g_datalist_id_set_data (&object->qdata, quark_notify_queue, NULL); |
355 | |
|
356 | 0 | G_UNLOCK(notify_lock); |
357 | |
|
358 | 0 | if (n_pspecs) |
359 | 0 | G_OBJECT_GET_CLASS (object)->dispatch_properties_changed (object, n_pspecs, pspecs); |
360 | 0 | g_free (free_me); |
361 | 0 | } |
362 | | |
363 | | static void |
364 | | g_object_notify_queue_add (GObject *object, |
365 | | GObjectNotifyQueue *nqueue, |
366 | | GParamSpec *pspec) |
367 | 0 | { |
368 | 0 | G_LOCK(notify_lock); |
369 | |
|
370 | 0 | g_assert (nqueue->n_pspecs < 65535); |
371 | | |
372 | 0 | if (g_slist_find (nqueue->pspecs, pspec) == NULL) |
373 | 0 | { |
374 | 0 | nqueue->pspecs = g_slist_prepend (nqueue->pspecs, pspec); |
375 | 0 | nqueue->n_pspecs++; |
376 | 0 | } |
377 | |
|
378 | 0 | G_UNLOCK(notify_lock); |
379 | 0 | } |
380 | | |
381 | | #ifdef G_ENABLE_DEBUG |
382 | | G_LOCK_DEFINE_STATIC (debug_objects); |
383 | | static guint debug_objects_count = 0; |
384 | | static GHashTable *debug_objects_ht = NULL; |
385 | | |
386 | | static void |
387 | | debug_objects_foreach (gpointer key, |
388 | | gpointer value, |
389 | | gpointer user_data) |
390 | | { |
391 | | GObject *object = value; |
392 | | |
393 | | g_message ("[%p] stale %s\tref_count=%u", |
394 | | object, |
395 | | G_OBJECT_TYPE_NAME (object), |
396 | | object->ref_count); |
397 | | } |
398 | | |
399 | | #ifdef G_HAS_CONSTRUCTORS |
400 | | #ifdef G_DEFINE_DESTRUCTOR_NEEDS_PRAGMA |
401 | | #pragma G_DEFINE_DESTRUCTOR_PRAGMA_ARGS(debug_objects_atexit) |
402 | | #endif |
403 | | G_DEFINE_DESTRUCTOR(debug_objects_atexit) |
404 | | #endif /* G_HAS_CONSTRUCTORS */ |
405 | | |
406 | | static void |
407 | | debug_objects_atexit (void) |
408 | | { |
409 | | GOBJECT_IF_DEBUG (OBJECTS, |
410 | | { |
411 | | G_LOCK (debug_objects); |
412 | | g_message ("stale GObjects: %u", debug_objects_count); |
413 | | g_hash_table_foreach (debug_objects_ht, debug_objects_foreach, NULL); |
414 | | G_UNLOCK (debug_objects); |
415 | | }); |
416 | | } |
417 | | #endif /* G_ENABLE_DEBUG */ |
418 | | |
419 | | void |
420 | | _g_object_type_init (void) |
421 | 4 | { |
422 | 4 | static gboolean initialized = FALSE; |
423 | 4 | static const GTypeFundamentalInfo finfo = { |
424 | 4 | G_TYPE_FLAG_CLASSED | G_TYPE_FLAG_INSTANTIATABLE | G_TYPE_FLAG_DERIVABLE | G_TYPE_FLAG_DEEP_DERIVABLE, |
425 | 4 | }; |
426 | 4 | GTypeInfo info = { |
427 | 4 | sizeof (GObjectClass), |
428 | 4 | (GBaseInitFunc) g_object_base_class_init, |
429 | 4 | (GBaseFinalizeFunc) g_object_base_class_finalize, |
430 | 4 | (GClassInitFunc) g_object_do_class_init, |
431 | 4 | NULL /* class_destroy */, |
432 | 4 | NULL /* class_data */, |
433 | 4 | sizeof (GObject), |
434 | 4 | 0 /* n_preallocs */, |
435 | 4 | (GInstanceInitFunc) g_object_init, |
436 | 4 | NULL, /* value_table */ |
437 | 4 | }; |
438 | 4 | static const GTypeValueTable value_table = { |
439 | 4 | g_value_object_init, /* value_init */ |
440 | 4 | g_value_object_free_value, /* value_free */ |
441 | 4 | g_value_object_copy_value, /* value_copy */ |
442 | 4 | g_value_object_peek_pointer, /* value_peek_pointer */ |
443 | 4 | "p", /* collect_format */ |
444 | 4 | g_value_object_collect_value, /* collect_value */ |
445 | 4 | "p", /* lcopy_format */ |
446 | 4 | g_value_object_lcopy_value, /* lcopy_value */ |
447 | 4 | }; |
448 | 4 | GType type G_GNUC_UNUSED /* when compiling with G_DISABLE_ASSERT */; |
449 | | |
450 | 4 | g_return_if_fail (initialized == FALSE); |
451 | 4 | initialized = TRUE; |
452 | | |
453 | | /* G_TYPE_OBJECT |
454 | | */ |
455 | 4 | info.value_table = &value_table; |
456 | 4 | type = g_type_register_fundamental (G_TYPE_OBJECT, g_intern_static_string ("GObject"), &info, &finfo, 0); |
457 | 4 | g_assert (type == G_TYPE_OBJECT); |
458 | 4 | g_value_register_transform_func (G_TYPE_OBJECT, G_TYPE_OBJECT, g_value_object_transform_value); |
459 | | |
460 | | #if G_ENABLE_DEBUG |
461 | | /* We cannot use GOBJECT_IF_DEBUG here because of the G_HAS_CONSTRUCTORS |
462 | | * conditional in between, as the C spec leaves conditionals inside macro |
463 | | * expansions as undefined behavior. Only GCC and Clang are known to work |
464 | | * but compilation breaks on MSVC. |
465 | | * |
466 | | * See: https://bugzilla.gnome.org/show_bug.cgi?id=769504 |
467 | | */ |
468 | | if (_g_type_debug_flags & G_TYPE_DEBUG_OBJECTS) \ |
469 | | { |
470 | | debug_objects_ht = g_hash_table_new (g_direct_hash, NULL); |
471 | | # ifndef G_HAS_CONSTRUCTORS |
472 | | g_atexit (debug_objects_atexit); |
473 | | # endif /* G_HAS_CONSTRUCTORS */ |
474 | | } |
475 | | #endif /* G_ENABLE_DEBUG */ |
476 | 4 | } |
477 | | |
478 | | static void |
479 | | g_object_base_class_init (GObjectClass *class) |
480 | 8 | { |
481 | 8 | GObjectClass *pclass = g_type_class_peek_parent (class); |
482 | | |
483 | | /* Don't inherit HAS_DERIVED_CLASS flag from parent class */ |
484 | 8 | class->flags &= ~CLASS_HAS_DERIVED_CLASS_FLAG; |
485 | | |
486 | 8 | if (pclass) |
487 | 7 | pclass->flags |= CLASS_HAS_DERIVED_CLASS_FLAG; |
488 | | |
489 | | /* reset instance specific fields and methods that don't get inherited */ |
490 | 8 | class->construct_properties = pclass ? g_slist_copy (pclass->construct_properties) : NULL; |
491 | 8 | class->n_construct_properties = g_slist_length (class->construct_properties); |
492 | 8 | class->get_property = NULL; |
493 | 8 | class->set_property = NULL; |
494 | 8 | class->pspecs = NULL; |
495 | 8 | class->n_pspecs = 0; |
496 | 8 | } |
497 | | |
498 | | static void |
499 | | g_object_base_class_finalize (GObjectClass *class) |
500 | 0 | { |
501 | 0 | GList *list, *node; |
502 | | |
503 | 0 | _g_signals_destroy (G_OBJECT_CLASS_TYPE (class)); |
504 | |
|
505 | 0 | g_slist_free (class->construct_properties); |
506 | 0 | class->construct_properties = NULL; |
507 | 0 | class->n_construct_properties = 0; |
508 | 0 | list = g_param_spec_pool_list_owned (pspec_pool, G_OBJECT_CLASS_TYPE (class)); |
509 | 0 | for (node = list; node; node = node->next) |
510 | 0 | { |
511 | 0 | GParamSpec *pspec = node->data; |
512 | | |
513 | 0 | g_param_spec_pool_remove (pspec_pool, pspec); |
514 | 0 | PARAM_SPEC_SET_PARAM_ID (pspec, 0); |
515 | 0 | g_param_spec_unref (pspec); |
516 | 0 | } |
517 | 0 | g_list_free (list); |
518 | 0 | } |
519 | | |
520 | | static void |
521 | | g_object_do_class_init (GObjectClass *class) |
522 | 1 | { |
523 | | /* read the comment about typedef struct CArray; on why not to change this quark */ |
524 | 1 | quark_closure_array = g_quark_from_static_string ("GObject-closure-array"); |
525 | | |
526 | 1 | quark_weak_refs = g_quark_from_static_string ("GObject-weak-references"); |
527 | 1 | quark_weak_locations = g_quark_from_static_string ("GObject-weak-locations"); |
528 | 1 | quark_toggle_refs = g_quark_from_static_string ("GObject-toggle-references"); |
529 | 1 | quark_notify_queue = g_quark_from_static_string ("GObject-notify-queue"); |
530 | | #ifndef HAVE_OPTIONAL_FLAGS |
531 | | quark_in_construction = g_quark_from_static_string ("GObject-in-construction"); |
532 | | #endif |
533 | 1 | pspec_pool = g_param_spec_pool_new (TRUE); |
534 | | |
535 | 1 | class->constructor = g_object_constructor; |
536 | 1 | class->constructed = g_object_constructed; |
537 | 1 | class->set_property = g_object_do_set_property; |
538 | 1 | class->get_property = g_object_do_get_property; |
539 | 1 | class->dispose = g_object_real_dispose; |
540 | 1 | class->finalize = g_object_finalize; |
541 | 1 | class->dispatch_properties_changed = g_object_dispatch_properties_changed; |
542 | 1 | class->notify = NULL; |
543 | | |
544 | | /** |
545 | | * GObject::notify: |
546 | | * @gobject: the object which received the signal. |
547 | | * @pspec: the #GParamSpec of the property which changed. |
548 | | * |
549 | | * The notify signal is emitted on an object when one of its properties has |
550 | | * its value set through g_object_set_property(), g_object_set(), et al. |
551 | | * |
552 | | * Note that getting this signal doesn’t itself guarantee that the value of |
553 | | * the property has actually changed. When it is emitted is determined by the |
554 | | * derived GObject class. If the implementor did not create the property with |
555 | | * %G_PARAM_EXPLICIT_NOTIFY, then any call to g_object_set_property() results |
556 | | * in ::notify being emitted, even if the new value is the same as the old. |
557 | | * If they did pass %G_PARAM_EXPLICIT_NOTIFY, then this signal is emitted only |
558 | | * when they explicitly call g_object_notify() or g_object_notify_by_pspec(), |
559 | | * and common practice is to do that only when the value has actually changed. |
560 | | * |
561 | | * This signal is typically used to obtain change notification for a |
562 | | * single property, by specifying the property name as a detail in the |
563 | | * g_signal_connect() call, like this: |
564 | | * |
565 | | * |[<!-- language="C" --> |
566 | | * g_signal_connect (text_view->buffer, "notify::paste-target-list", |
567 | | * G_CALLBACK (gtk_text_view_target_list_notify), |
568 | | * text_view) |
569 | | * ]| |
570 | | * |
571 | | * It is important to note that you must use |
572 | | * [canonical parameter names][canonical-parameter-names] as |
573 | | * detail strings for the notify signal. |
574 | | */ |
575 | 1 | gobject_signals[NOTIFY] = |
576 | 1 | g_signal_new (g_intern_static_string ("notify"), |
577 | 1 | G_TYPE_FROM_CLASS (class), |
578 | 1 | G_SIGNAL_RUN_FIRST | G_SIGNAL_NO_RECURSE | G_SIGNAL_DETAILED | G_SIGNAL_NO_HOOKS | G_SIGNAL_ACTION, |
579 | 1 | G_STRUCT_OFFSET (GObjectClass, notify), |
580 | 1 | NULL, NULL, |
581 | 1 | NULL, |
582 | 1 | G_TYPE_NONE, |
583 | 1 | 1, G_TYPE_PARAM); |
584 | | |
585 | | /* Install a check function that we'll use to verify that classes that |
586 | | * implement an interface implement all properties for that interface |
587 | | */ |
588 | 1 | g_type_add_interface_check (NULL, object_interface_check_properties); |
589 | 1 | } |
590 | | |
591 | | /* Sinks @pspec if it’s a floating ref. */ |
592 | | static inline gboolean |
593 | | install_property_internal (GType g_type, |
594 | | guint property_id, |
595 | | GParamSpec *pspec) |
596 | 0 | { |
597 | 0 | g_param_spec_ref_sink (pspec); |
598 | |
|
599 | 0 | if (g_param_spec_pool_lookup (pspec_pool, pspec->name, g_type, FALSE)) |
600 | 0 | { |
601 | 0 | g_critical ("When installing property: type '%s' already has a property named '%s'", |
602 | 0 | g_type_name (g_type), |
603 | 0 | pspec->name); |
604 | 0 | g_param_spec_unref (pspec); |
605 | 0 | return FALSE; |
606 | 0 | } |
607 | | |
608 | 0 | PARAM_SPEC_SET_PARAM_ID (pspec, property_id); |
609 | 0 | g_param_spec_pool_insert (pspec_pool, g_steal_pointer (&pspec), g_type); |
610 | 0 | return TRUE; |
611 | 0 | } |
612 | | |
613 | | static gboolean |
614 | | validate_pspec_to_install (GParamSpec *pspec) |
615 | 0 | { |
616 | 0 | g_return_val_if_fail (G_IS_PARAM_SPEC (pspec), FALSE); |
617 | 0 | g_return_val_if_fail (PARAM_SPEC_PARAM_ID (pspec) == 0, FALSE); /* paranoid */ |
618 | | |
619 | 0 | g_return_val_if_fail (pspec->flags & (G_PARAM_READABLE | G_PARAM_WRITABLE), FALSE); |
620 | | |
621 | 0 | if (pspec->flags & G_PARAM_CONSTRUCT) |
622 | 0 | g_return_val_if_fail ((pspec->flags & G_PARAM_CONSTRUCT_ONLY) == 0, FALSE); |
623 | | |
624 | 0 | if (pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY)) |
625 | 0 | g_return_val_if_fail (pspec->flags & G_PARAM_WRITABLE, FALSE); |
626 | | |
627 | 0 | return TRUE; |
628 | 0 | } |
629 | | |
630 | | /* Sinks @pspec if it’s a floating ref. */ |
631 | | static gboolean |
632 | | validate_and_install_class_property (GObjectClass *class, |
633 | | GType oclass_type, |
634 | | GType parent_type, |
635 | | guint property_id, |
636 | | GParamSpec *pspec) |
637 | 0 | { |
638 | 0 | if (!validate_pspec_to_install (pspec)) |
639 | 0 | { |
640 | 0 | g_param_spec_ref_sink (pspec); |
641 | 0 | g_param_spec_unref (pspec); |
642 | 0 | return FALSE; |
643 | 0 | } |
644 | | |
645 | 0 | if (pspec->flags & G_PARAM_WRITABLE) |
646 | 0 | g_return_val_if_fail (class->set_property != NULL, FALSE); |
647 | 0 | if (pspec->flags & G_PARAM_READABLE) |
648 | 0 | g_return_val_if_fail (class->get_property != NULL, FALSE); |
649 | | |
650 | 0 | class->flags |= CLASS_HAS_PROPS_FLAG; |
651 | 0 | if (install_property_internal (oclass_type, property_id, pspec)) |
652 | 0 | { |
653 | 0 | if (pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY)) |
654 | 0 | { |
655 | 0 | class->construct_properties = g_slist_append (class->construct_properties, pspec); |
656 | 0 | class->n_construct_properties += 1; |
657 | 0 | } |
658 | | |
659 | | /* for property overrides of construct properties, we have to get rid |
660 | | * of the overridden inherited construct property |
661 | | */ |
662 | 0 | pspec = g_param_spec_pool_lookup (pspec_pool, pspec->name, parent_type, TRUE); |
663 | 0 | if (pspec && pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY)) |
664 | 0 | { |
665 | 0 | class->construct_properties = g_slist_remove (class->construct_properties, pspec); |
666 | 0 | class->n_construct_properties -= 1; |
667 | 0 | } |
668 | |
|
669 | 0 | return TRUE; |
670 | 0 | } |
671 | 0 | else |
672 | 0 | return FALSE; |
673 | 0 | } |
674 | | |
675 | | /** |
676 | | * g_object_class_install_property: |
677 | | * @oclass: a #GObjectClass |
678 | | * @property_id: the id for the new property |
679 | | * @pspec: the #GParamSpec for the new property |
680 | | * |
681 | | * Installs a new property. |
682 | | * |
683 | | * All properties should be installed during the class initializer. It |
684 | | * is possible to install properties after that, but doing so is not |
685 | | * recommend, and specifically, is not guaranteed to be thread-safe vs. |
686 | | * use of properties on the same type on other threads. |
687 | | * |
688 | | * Note that it is possible to redefine a property in a derived class, |
689 | | * by installing a property with the same name. This can be useful at times, |
690 | | * e.g. to change the range of allowed values or the default value. |
691 | | */ |
692 | | void |
693 | | g_object_class_install_property (GObjectClass *class, |
694 | | guint property_id, |
695 | | GParamSpec *pspec) |
696 | 0 | { |
697 | 0 | GType oclass_type, parent_type; |
698 | |
|
699 | 0 | g_return_if_fail (G_IS_OBJECT_CLASS (class)); |
700 | 0 | g_return_if_fail (property_id > 0); |
701 | | |
702 | 0 | oclass_type = G_OBJECT_CLASS_TYPE (class); |
703 | 0 | parent_type = g_type_parent (oclass_type); |
704 | |
|
705 | 0 | if (CLASS_HAS_DERIVED_CLASS (class)) |
706 | 0 | g_error ("Attempt to add property %s::%s to class after it was derived", G_OBJECT_CLASS_NAME (class), pspec->name); |
707 | |
|
708 | 0 | (void) validate_and_install_class_property (class, |
709 | 0 | oclass_type, |
710 | 0 | parent_type, |
711 | 0 | property_id, |
712 | 0 | pspec); |
713 | 0 | } |
714 | | |
715 | | typedef struct { |
716 | | const char *name; |
717 | | GParamSpec *pspec; |
718 | | } PspecEntry; |
719 | | |
720 | | static int |
721 | | compare_pspec_entry (const void *a, |
722 | | const void *b) |
723 | 0 | { |
724 | 0 | const PspecEntry *ae = a; |
725 | 0 | const PspecEntry *be = b; |
726 | |
|
727 | 0 | return ae->name < be->name ? -1 : (ae->name > be->name ? 1 : 0); |
728 | 0 | } |
729 | | |
730 | | /* This uses pointer comparisons with @property_name, so |
731 | | * will only work with string literals. */ |
732 | | static inline GParamSpec * |
733 | | find_pspec (GObjectClass *class, |
734 | | const char *property_name) |
735 | 0 | { |
736 | 0 | const PspecEntry *pspecs = (const PspecEntry *)class->pspecs; |
737 | 0 | gsize n_pspecs = class->n_pspecs; |
738 | |
|
739 | 0 | g_assert (n_pspecs <= G_MAXSSIZE); |
740 | | |
741 | | /* The limit for choosing between linear and binary search is |
742 | | * fairly arbitrary. |
743 | | * |
744 | | * Both searches use pointer comparisons against @property_name. |
745 | | * If this function is called with a non-static @property_name, |
746 | | * it will fall through to the g_param_spec_pool_lookup() case. |
747 | | * That’s OK; this is an opportunistic optimisation which relies |
748 | | * on the fact that *most* (but not all) property lookups use |
749 | | * static property names. |
750 | | */ |
751 | 0 | if (n_pspecs < 10) |
752 | 0 | { |
753 | 0 | for (gsize i = 0; i < n_pspecs; i++) |
754 | 0 | { |
755 | 0 | if (pspecs[i].name == property_name) |
756 | 0 | return pspecs[i].pspec; |
757 | 0 | } |
758 | 0 | } |
759 | 0 | else |
760 | 0 | { |
761 | 0 | gssize lower = 0; |
762 | 0 | gssize upper = (int)class->n_pspecs - 1; |
763 | 0 | gssize mid; |
764 | |
|
765 | 0 | while (lower <= upper) |
766 | 0 | { |
767 | 0 | mid = (lower + upper) / 2; |
768 | |
|
769 | 0 | if (property_name < pspecs[mid].name) |
770 | 0 | upper = mid - 1; |
771 | 0 | else if (property_name > pspecs[mid].name) |
772 | 0 | lower = mid + 1; |
773 | 0 | else |
774 | 0 | return pspecs[mid].pspec; |
775 | 0 | } |
776 | 0 | } |
777 | | |
778 | 0 | return g_param_spec_pool_lookup (pspec_pool, |
779 | 0 | property_name, |
780 | 0 | ((GTypeClass *)class)->g_type, |
781 | 0 | TRUE); |
782 | 0 | } |
783 | | |
784 | | /** |
785 | | * g_object_class_install_properties: |
786 | | * @oclass: a #GObjectClass |
787 | | * @n_pspecs: the length of the #GParamSpecs array |
788 | | * @pspecs: (array length=n_pspecs): the #GParamSpecs array |
789 | | * defining the new properties |
790 | | * |
791 | | * Installs new properties from an array of #GParamSpecs. |
792 | | * |
793 | | * All properties should be installed during the class initializer. It |
794 | | * is possible to install properties after that, but doing so is not |
795 | | * recommend, and specifically, is not guaranteed to be thread-safe vs. |
796 | | * use of properties on the same type on other threads. |
797 | | * |
798 | | * The property id of each property is the index of each #GParamSpec in |
799 | | * the @pspecs array. |
800 | | * |
801 | | * The property id of 0 is treated specially by #GObject and it should not |
802 | | * be used to store a #GParamSpec. |
803 | | * |
804 | | * This function should be used if you plan to use a static array of |
805 | | * #GParamSpecs and g_object_notify_by_pspec(). For instance, this |
806 | | * class initialization: |
807 | | * |
808 | | * |[<!-- language="C" --> |
809 | | * typedef enum { |
810 | | * PROP_FOO = 1, |
811 | | * PROP_BAR, |
812 | | * N_PROPERTIES |
813 | | * } MyObjectProperty; |
814 | | * |
815 | | * static GParamSpec *obj_properties[N_PROPERTIES] = { NULL, }; |
816 | | * |
817 | | * static void |
818 | | * my_object_class_init (MyObjectClass *klass) |
819 | | * { |
820 | | * GObjectClass *gobject_class = G_OBJECT_CLASS (klass); |
821 | | * |
822 | | * obj_properties[PROP_FOO] = |
823 | | * g_param_spec_int ("foo", "Foo", "Foo", |
824 | | * -1, G_MAXINT, |
825 | | * 0, |
826 | | * G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS); |
827 | | * |
828 | | * obj_properties[PROP_BAR] = |
829 | | * g_param_spec_string ("bar", "Bar", "Bar", |
830 | | * NULL, |
831 | | * G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS); |
832 | | * |
833 | | * gobject_class->set_property = my_object_set_property; |
834 | | * gobject_class->get_property = my_object_get_property; |
835 | | * g_object_class_install_properties (gobject_class, |
836 | | * G_N_ELEMENTS (obj_properties), |
837 | | * obj_properties); |
838 | | * } |
839 | | * ]| |
840 | | * |
841 | | * allows calling g_object_notify_by_pspec() to notify of property changes: |
842 | | * |
843 | | * |[<!-- language="C" --> |
844 | | * void |
845 | | * my_object_set_foo (MyObject *self, gint foo) |
846 | | * { |
847 | | * if (self->foo != foo) |
848 | | * { |
849 | | * self->foo = foo; |
850 | | * g_object_notify_by_pspec (G_OBJECT (self), obj_properties[PROP_FOO]); |
851 | | * } |
852 | | * } |
853 | | * ]| |
854 | | * |
855 | | * Since: 2.26 |
856 | | */ |
857 | | void |
858 | | g_object_class_install_properties (GObjectClass *oclass, |
859 | | guint n_pspecs, |
860 | | GParamSpec **pspecs) |
861 | 0 | { |
862 | 0 | GType oclass_type, parent_type; |
863 | 0 | guint i; |
864 | |
|
865 | 0 | g_return_if_fail (G_IS_OBJECT_CLASS (oclass)); |
866 | 0 | g_return_if_fail (n_pspecs > 1); |
867 | 0 | g_return_if_fail (pspecs[0] == NULL); |
868 | | |
869 | 0 | if (CLASS_HAS_DERIVED_CLASS (oclass)) |
870 | 0 | g_error ("Attempt to add properties to %s after it was derived", |
871 | 0 | G_OBJECT_CLASS_NAME (oclass)); |
872 | |
|
873 | 0 | oclass_type = G_OBJECT_CLASS_TYPE (oclass); |
874 | 0 | parent_type = g_type_parent (oclass_type); |
875 | | |
876 | | /* we skip the first element of the array as it would have a 0 prop_id */ |
877 | 0 | for (i = 1; i < n_pspecs; i++) |
878 | 0 | { |
879 | 0 | GParamSpec *pspec = pspecs[i]; |
880 | |
|
881 | 0 | if (!validate_and_install_class_property (oclass, |
882 | 0 | oclass_type, |
883 | 0 | parent_type, |
884 | 0 | i, |
885 | 0 | pspec)) |
886 | 0 | { |
887 | 0 | break; |
888 | 0 | } |
889 | 0 | } |
890 | | |
891 | | /* Save a copy of the pspec array inside the class struct. This |
892 | | * makes it faster to look up pspecs for the class in future when |
893 | | * acting on those properties. |
894 | | * |
895 | | * If a pspec is not in this cache array, calling code will fall |
896 | | * back to using g_param_spec_pool_lookup(), so a pspec not being |
897 | | * in this array is a (potential) performance problem but not a |
898 | | * correctness problem. */ |
899 | 0 | if (oclass->pspecs == NULL) |
900 | 0 | { |
901 | 0 | PspecEntry *entries; |
902 | |
|
903 | 0 | entries = g_new (PspecEntry, n_pspecs - 1); |
904 | |
|
905 | 0 | for (i = 1; i < n_pspecs; i++) |
906 | 0 | { |
907 | 0 | entries[i - 1].name = pspecs[i]->name; |
908 | 0 | entries[i - 1].pspec = pspecs[i]; |
909 | 0 | } |
910 | |
|
911 | 0 | qsort (entries, n_pspecs - 1, sizeof (PspecEntry), compare_pspec_entry); |
912 | |
|
913 | 0 | oclass->pspecs = entries; |
914 | 0 | oclass->n_pspecs = n_pspecs - 1; |
915 | 0 | } |
916 | 0 | } |
917 | | |
918 | | /** |
919 | | * g_object_interface_install_property: |
920 | | * @g_iface: (type GObject.TypeInterface): any interface vtable for the |
921 | | * interface, or the default |
922 | | * vtable for the interface. |
923 | | * @pspec: the #GParamSpec for the new property |
924 | | * |
925 | | * Add a property to an interface; this is only useful for interfaces |
926 | | * that are added to GObject-derived types. Adding a property to an |
927 | | * interface forces all objects classes with that interface to have a |
928 | | * compatible property. The compatible property could be a newly |
929 | | * created #GParamSpec, but normally |
930 | | * g_object_class_override_property() will be used so that the object |
931 | | * class only needs to provide an implementation and inherits the |
932 | | * property description, default value, bounds, and so forth from the |
933 | | * interface property. |
934 | | * |
935 | | * This function is meant to be called from the interface's default |
936 | | * vtable initialization function (the @class_init member of |
937 | | * #GTypeInfo.) It must not be called after after @class_init has |
938 | | * been called for any object types implementing this interface. |
939 | | * |
940 | | * If @pspec is a floating reference, it will be consumed. |
941 | | * |
942 | | * Since: 2.4 |
943 | | */ |
944 | | void |
945 | | g_object_interface_install_property (gpointer g_iface, |
946 | | GParamSpec *pspec) |
947 | 0 | { |
948 | 0 | GTypeInterface *iface_class = g_iface; |
949 | | |
950 | 0 | g_return_if_fail (G_TYPE_IS_INTERFACE (iface_class->g_type)); |
951 | 0 | g_return_if_fail (!G_IS_PARAM_SPEC_OVERRIDE (pspec)); /* paranoid */ |
952 | | |
953 | 0 | if (!validate_pspec_to_install (pspec)) |
954 | 0 | { |
955 | 0 | g_param_spec_ref_sink (pspec); |
956 | 0 | g_param_spec_unref (pspec); |
957 | 0 | return; |
958 | 0 | } |
959 | | |
960 | 0 | (void) install_property_internal (iface_class->g_type, 0, pspec); |
961 | 0 | } |
962 | | |
963 | | /* Inlined version of g_param_spec_get_redirect_target(), for speed */ |
964 | | static inline void |
965 | | param_spec_follow_override (GParamSpec **pspec) |
966 | 0 | { |
967 | 0 | if (((GTypeInstance *) (*pspec))->g_class->g_type == G_TYPE_PARAM_OVERRIDE) |
968 | 0 | *pspec = ((GParamSpecOverride *) (*pspec))->overridden; |
969 | 0 | } |
970 | | |
971 | | /** |
972 | | * g_object_class_find_property: |
973 | | * @oclass: a #GObjectClass |
974 | | * @property_name: the name of the property to look up |
975 | | * |
976 | | * Looks up the #GParamSpec for a property of a class. |
977 | | * |
978 | | * Returns: (transfer none): the #GParamSpec for the property, or |
979 | | * %NULL if the class doesn't have a property of that name |
980 | | */ |
981 | | GParamSpec* |
982 | | g_object_class_find_property (GObjectClass *class, |
983 | | const gchar *property_name) |
984 | 0 | { |
985 | 0 | GParamSpec *pspec; |
986 | |
|
987 | 0 | g_return_val_if_fail (G_IS_OBJECT_CLASS (class), NULL); |
988 | 0 | g_return_val_if_fail (property_name != NULL, NULL); |
989 | | |
990 | 0 | pspec = find_pspec (class, property_name); |
991 | |
|
992 | 0 | if (pspec) |
993 | 0 | param_spec_follow_override (&pspec); |
994 | |
|
995 | 0 | return pspec; |
996 | 0 | } |
997 | | |
998 | | /** |
999 | | * g_object_interface_find_property: |
1000 | | * @g_iface: (type GObject.TypeInterface): any interface vtable for the |
1001 | | * interface, or the default vtable for the interface |
1002 | | * @property_name: name of a property to look up. |
1003 | | * |
1004 | | * Find the #GParamSpec with the given name for an |
1005 | | * interface. Generally, the interface vtable passed in as @g_iface |
1006 | | * will be the default vtable from g_type_default_interface_ref(), or, |
1007 | | * if you know the interface has already been loaded, |
1008 | | * g_type_default_interface_peek(). |
1009 | | * |
1010 | | * Since: 2.4 |
1011 | | * |
1012 | | * Returns: (transfer none): the #GParamSpec for the property of the |
1013 | | * interface with the name @property_name, or %NULL if no |
1014 | | * such property exists. |
1015 | | */ |
1016 | | GParamSpec* |
1017 | | g_object_interface_find_property (gpointer g_iface, |
1018 | | const gchar *property_name) |
1019 | 0 | { |
1020 | 0 | GTypeInterface *iface_class = g_iface; |
1021 | | |
1022 | 0 | g_return_val_if_fail (G_TYPE_IS_INTERFACE (iface_class->g_type), NULL); |
1023 | 0 | g_return_val_if_fail (property_name != NULL, NULL); |
1024 | | |
1025 | 0 | return g_param_spec_pool_lookup (pspec_pool, |
1026 | 0 | property_name, |
1027 | 0 | iface_class->g_type, |
1028 | 0 | FALSE); |
1029 | 0 | } |
1030 | | |
1031 | | /** |
1032 | | * g_object_class_override_property: |
1033 | | * @oclass: a #GObjectClass |
1034 | | * @property_id: the new property ID |
1035 | | * @name: the name of a property registered in a parent class or |
1036 | | * in an interface of this class. |
1037 | | * |
1038 | | * Registers @property_id as referring to a property with the name |
1039 | | * @name in a parent class or in an interface implemented by @oclass. |
1040 | | * This allows this class to "override" a property implementation in |
1041 | | * a parent class or to provide the implementation of a property from |
1042 | | * an interface. |
1043 | | * |
1044 | | * Internally, overriding is implemented by creating a property of type |
1045 | | * #GParamSpecOverride; generally operations that query the properties of |
1046 | | * the object class, such as g_object_class_find_property() or |
1047 | | * g_object_class_list_properties() will return the overridden |
1048 | | * property. However, in one case, the @construct_properties argument of |
1049 | | * the @constructor virtual function, the #GParamSpecOverride is passed |
1050 | | * instead, so that the @param_id field of the #GParamSpec will be |
1051 | | * correct. For virtually all uses, this makes no difference. If you |
1052 | | * need to get the overridden property, you can call |
1053 | | * g_param_spec_get_redirect_target(). |
1054 | | * |
1055 | | * Since: 2.4 |
1056 | | */ |
1057 | | void |
1058 | | g_object_class_override_property (GObjectClass *oclass, |
1059 | | guint property_id, |
1060 | | const gchar *name) |
1061 | 0 | { |
1062 | 0 | GParamSpec *overridden = NULL; |
1063 | 0 | GParamSpec *new; |
1064 | 0 | GType parent_type; |
1065 | | |
1066 | 0 | g_return_if_fail (G_IS_OBJECT_CLASS (oclass)); |
1067 | 0 | g_return_if_fail (property_id > 0); |
1068 | 0 | g_return_if_fail (name != NULL); |
1069 | | |
1070 | | /* Find the overridden property; first check parent types |
1071 | | */ |
1072 | 0 | parent_type = g_type_parent (G_OBJECT_CLASS_TYPE (oclass)); |
1073 | 0 | if (parent_type != G_TYPE_NONE) |
1074 | 0 | overridden = g_param_spec_pool_lookup (pspec_pool, |
1075 | 0 | name, |
1076 | 0 | parent_type, |
1077 | 0 | TRUE); |
1078 | 0 | if (!overridden) |
1079 | 0 | { |
1080 | 0 | GType *ifaces; |
1081 | 0 | guint n_ifaces; |
1082 | | |
1083 | | /* Now check interfaces |
1084 | | */ |
1085 | 0 | ifaces = g_type_interfaces (G_OBJECT_CLASS_TYPE (oclass), &n_ifaces); |
1086 | 0 | while (n_ifaces-- && !overridden) |
1087 | 0 | { |
1088 | 0 | overridden = g_param_spec_pool_lookup (pspec_pool, |
1089 | 0 | name, |
1090 | 0 | ifaces[n_ifaces], |
1091 | 0 | FALSE); |
1092 | 0 | } |
1093 | | |
1094 | 0 | g_free (ifaces); |
1095 | 0 | } |
1096 | |
|
1097 | 0 | if (!overridden) |
1098 | 0 | { |
1099 | 0 | g_critical ("%s: Can't find property to override for '%s::%s'", |
1100 | 0 | G_STRFUNC, G_OBJECT_CLASS_NAME (oclass), name); |
1101 | 0 | return; |
1102 | 0 | } |
1103 | | |
1104 | 0 | new = g_param_spec_override (name, overridden); |
1105 | 0 | g_object_class_install_property (oclass, property_id, new); |
1106 | 0 | } |
1107 | | |
1108 | | /** |
1109 | | * g_object_class_list_properties: |
1110 | | * @oclass: a #GObjectClass |
1111 | | * @n_properties: (out): return location for the length of the returned array |
1112 | | * |
1113 | | * Get an array of #GParamSpec* for all properties of a class. |
1114 | | * |
1115 | | * Returns: (array length=n_properties) (transfer container): an array of |
1116 | | * #GParamSpec* which should be freed after use |
1117 | | */ |
1118 | | GParamSpec** /* free result */ |
1119 | | g_object_class_list_properties (GObjectClass *class, |
1120 | | guint *n_properties_p) |
1121 | 0 | { |
1122 | 0 | GParamSpec **pspecs; |
1123 | 0 | guint n; |
1124 | |
|
1125 | 0 | g_return_val_if_fail (G_IS_OBJECT_CLASS (class), NULL); |
1126 | | |
1127 | 0 | pspecs = g_param_spec_pool_list (pspec_pool, |
1128 | 0 | G_OBJECT_CLASS_TYPE (class), |
1129 | 0 | &n); |
1130 | 0 | if (n_properties_p) |
1131 | 0 | *n_properties_p = n; |
1132 | |
|
1133 | 0 | return pspecs; |
1134 | 0 | } |
1135 | | |
1136 | | /** |
1137 | | * g_object_interface_list_properties: |
1138 | | * @g_iface: (type GObject.TypeInterface): any interface vtable for the |
1139 | | * interface, or the default vtable for the interface |
1140 | | * @n_properties_p: (out): location to store number of properties returned. |
1141 | | * |
1142 | | * Lists the properties of an interface.Generally, the interface |
1143 | | * vtable passed in as @g_iface will be the default vtable from |
1144 | | * g_type_default_interface_ref(), or, if you know the interface has |
1145 | | * already been loaded, g_type_default_interface_peek(). |
1146 | | * |
1147 | | * Since: 2.4 |
1148 | | * |
1149 | | * Returns: (array length=n_properties_p) (transfer container): a |
1150 | | * pointer to an array of pointers to #GParamSpec |
1151 | | * structures. The paramspecs are owned by GLib, but the |
1152 | | * array should be freed with g_free() when you are done with |
1153 | | * it. |
1154 | | */ |
1155 | | GParamSpec** |
1156 | | g_object_interface_list_properties (gpointer g_iface, |
1157 | | guint *n_properties_p) |
1158 | 0 | { |
1159 | 0 | GTypeInterface *iface_class = g_iface; |
1160 | 0 | GParamSpec **pspecs; |
1161 | 0 | guint n; |
1162 | |
|
1163 | 0 | g_return_val_if_fail (G_TYPE_IS_INTERFACE (iface_class->g_type), NULL); |
1164 | | |
1165 | 0 | pspecs = g_param_spec_pool_list (pspec_pool, |
1166 | 0 | iface_class->g_type, |
1167 | 0 | &n); |
1168 | 0 | if (n_properties_p) |
1169 | 0 | *n_properties_p = n; |
1170 | |
|
1171 | 0 | return pspecs; |
1172 | 0 | } |
1173 | | |
1174 | | static inline guint |
1175 | | object_get_optional_flags (GObject *object) |
1176 | 0 | { |
1177 | 0 | #ifdef HAVE_OPTIONAL_FLAGS |
1178 | 0 | GObjectReal *real = (GObjectReal *)object; |
1179 | 0 | return (guint)g_atomic_int_get (&real->optional_flags); |
1180 | | #else |
1181 | | return 0; |
1182 | | #endif |
1183 | 0 | } |
1184 | | |
1185 | | /* Variant of object_get_optional_flags for when |
1186 | | * we know that we have exclusive access (during |
1187 | | * construction) |
1188 | | */ |
1189 | | static inline guint |
1190 | | object_get_optional_flags_X (GObject *object) |
1191 | 0 | { |
1192 | 0 | #ifdef HAVE_OPTIONAL_FLAGS |
1193 | 0 | GObjectReal *real = (GObjectReal *)object; |
1194 | 0 | return real->optional_flags; |
1195 | | #else |
1196 | | return 0; |
1197 | | #endif |
1198 | 0 | } |
1199 | | |
1200 | | #ifdef HAVE_OPTIONAL_FLAGS |
1201 | | static inline void |
1202 | | object_set_optional_flags (GObject *object, |
1203 | | guint flags) |
1204 | 0 | { |
1205 | 0 | GObjectReal *real = (GObjectReal *)object; |
1206 | 0 | g_atomic_int_or (&real->optional_flags, flags); |
1207 | 0 | } |
1208 | | |
1209 | | /* Variant for when we have exclusive access |
1210 | | * (during construction) |
1211 | | */ |
1212 | | static inline void |
1213 | | object_set_optional_flags_X (GObject *object, |
1214 | | guint flags) |
1215 | 14.1k | { |
1216 | 14.1k | GObjectReal *real = (GObjectReal *)object; |
1217 | 14.1k | real->optional_flags |= flags; |
1218 | 14.1k | } |
1219 | | |
1220 | | /* Variant for when we have exclusive access |
1221 | | * (during construction) |
1222 | | */ |
1223 | | static inline void |
1224 | | object_unset_optional_flags_X (GObject *object, |
1225 | | guint flags) |
1226 | 14.1k | { |
1227 | 14.1k | GObjectReal *real = (GObjectReal *)object; |
1228 | 14.1k | real->optional_flags &= ~flags; |
1229 | 14.1k | } |
1230 | | #endif |
1231 | | |
1232 | | gboolean |
1233 | | _g_object_has_signal_handler (GObject *object) |
1234 | 0 | { |
1235 | 0 | #ifdef HAVE_OPTIONAL_FLAGS |
1236 | 0 | return (object_get_optional_flags (object) & OPTIONAL_FLAG_HAS_SIGNAL_HANDLER) != 0; |
1237 | | #else |
1238 | | return TRUE; |
1239 | | #endif |
1240 | 0 | } |
1241 | | |
1242 | | static inline gboolean |
1243 | | _g_object_has_notify_handler (GObject *object) |
1244 | 0 | { |
1245 | 0 | #ifdef HAVE_OPTIONAL_FLAGS |
1246 | 0 | return CLASS_NEEDS_NOTIFY (G_OBJECT_GET_CLASS (object)) || |
1247 | 0 | (object_get_optional_flags (object) & OPTIONAL_FLAG_HAS_NOTIFY_HANDLER) != 0; |
1248 | | #else |
1249 | | return TRUE; |
1250 | | #endif |
1251 | 0 | } |
1252 | | |
1253 | | static inline gboolean |
1254 | | _g_object_has_notify_handler_X (GObject *object) |
1255 | 0 | { |
1256 | 0 | #ifdef HAVE_OPTIONAL_FLAGS |
1257 | 0 | return CLASS_NEEDS_NOTIFY (G_OBJECT_GET_CLASS (object)) || |
1258 | 0 | (object_get_optional_flags_X (object) & OPTIONAL_FLAG_HAS_NOTIFY_HANDLER) != 0; |
1259 | | #else |
1260 | | return TRUE; |
1261 | | #endif |
1262 | 0 | } |
1263 | | |
1264 | | void |
1265 | | _g_object_set_has_signal_handler (GObject *object, |
1266 | | guint signal_id) |
1267 | 0 | { |
1268 | 0 | #ifdef HAVE_OPTIONAL_FLAGS |
1269 | 0 | guint flags = OPTIONAL_FLAG_HAS_SIGNAL_HANDLER; |
1270 | 0 | if (signal_id == gobject_signals[NOTIFY]) |
1271 | 0 | flags |= OPTIONAL_FLAG_HAS_NOTIFY_HANDLER; |
1272 | 0 | object_set_optional_flags (object, flags); |
1273 | 0 | #endif |
1274 | 0 | } |
1275 | | |
1276 | | static inline gboolean |
1277 | | object_in_construction (GObject *object) |
1278 | 0 | { |
1279 | 0 | #ifdef HAVE_OPTIONAL_FLAGS |
1280 | 0 | return (object_get_optional_flags (object) & OPTIONAL_FLAG_IN_CONSTRUCTION) != 0; |
1281 | | #else |
1282 | | return g_datalist_id_get_data (&object->qdata, quark_in_construction) != NULL; |
1283 | | #endif |
1284 | 0 | } |
1285 | | |
1286 | | static inline void |
1287 | | set_object_in_construction (GObject *object) |
1288 | 14.1k | { |
1289 | 14.1k | #ifdef HAVE_OPTIONAL_FLAGS |
1290 | 14.1k | object_set_optional_flags_X (object, OPTIONAL_FLAG_IN_CONSTRUCTION); |
1291 | | #else |
1292 | | g_datalist_id_set_data (&object->qdata, quark_in_construction, object); |
1293 | | #endif |
1294 | 14.1k | } |
1295 | | |
1296 | | static inline void |
1297 | | unset_object_in_construction (GObject *object) |
1298 | 14.1k | { |
1299 | 14.1k | #ifdef HAVE_OPTIONAL_FLAGS |
1300 | 14.1k | object_unset_optional_flags_X (object, OPTIONAL_FLAG_IN_CONSTRUCTION); |
1301 | | #else |
1302 | | g_datalist_id_set_data (&object->qdata, quark_in_construction, NULL); |
1303 | | #endif |
1304 | 14.1k | } |
1305 | | |
1306 | | static void |
1307 | | g_object_init (GObject *object, |
1308 | | GObjectClass *class) |
1309 | 14.1k | { |
1310 | 14.1k | object->ref_count = 1; |
1311 | 14.1k | object->qdata = NULL; |
1312 | | |
1313 | 14.1k | if (CLASS_HAS_PROPS (class) && CLASS_NEEDS_NOTIFY (class)) |
1314 | 0 | { |
1315 | | /* freeze object's notification queue, g_object_new_internal() preserves pairedness */ |
1316 | 0 | g_object_notify_queue_freeze (object, FALSE); |
1317 | 0 | } |
1318 | | |
1319 | | /* mark object in-construction for notify_queue_thaw() and to allow construct-only properties */ |
1320 | 14.1k | set_object_in_construction (object); |
1321 | | |
1322 | 14.1k | GOBJECT_IF_DEBUG (OBJECTS, |
1323 | 14.1k | { |
1324 | 14.1k | G_LOCK (debug_objects); |
1325 | 14.1k | debug_objects_count++; |
1326 | 14.1k | g_hash_table_add (debug_objects_ht, object); |
1327 | 14.1k | G_UNLOCK (debug_objects); |
1328 | 14.1k | }); |
1329 | 14.1k | } |
1330 | | |
1331 | | static void |
1332 | | g_object_do_set_property (GObject *object, |
1333 | | guint property_id, |
1334 | | const GValue *value, |
1335 | | GParamSpec *pspec) |
1336 | 0 | { |
1337 | 0 | switch (property_id) |
1338 | 0 | { |
1339 | 0 | default: |
1340 | 0 | G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); |
1341 | 0 | break; |
1342 | 0 | } |
1343 | 0 | } |
1344 | | |
1345 | | static void |
1346 | | g_object_do_get_property (GObject *object, |
1347 | | guint property_id, |
1348 | | GValue *value, |
1349 | | GParamSpec *pspec) |
1350 | 0 | { |
1351 | 0 | switch (property_id) |
1352 | 0 | { |
1353 | 0 | default: |
1354 | 0 | G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); |
1355 | 0 | break; |
1356 | 0 | } |
1357 | 0 | } |
1358 | | |
1359 | | static void |
1360 | | g_object_real_dispose (GObject *object) |
1361 | 14.1k | { |
1362 | 14.1k | g_signal_handlers_destroy (object); |
1363 | 14.1k | g_datalist_id_set_data (&object->qdata, quark_closure_array, NULL); |
1364 | 14.1k | g_datalist_id_set_data (&object->qdata, quark_weak_refs, NULL); |
1365 | 14.1k | g_datalist_id_set_data (&object->qdata, quark_weak_locations, NULL); |
1366 | 14.1k | } |
1367 | | |
1368 | | #ifdef G_ENABLE_DEBUG |
1369 | | static gboolean |
1370 | | floating_check (GObject *object) |
1371 | | { |
1372 | | static const char *g_enable_diagnostic = NULL; |
1373 | | |
1374 | | if (G_UNLIKELY (g_enable_diagnostic == NULL)) |
1375 | | { |
1376 | | g_enable_diagnostic = g_getenv ("G_ENABLE_DIAGNOSTIC"); |
1377 | | if (g_enable_diagnostic == NULL) |
1378 | | g_enable_diagnostic = "0"; |
1379 | | } |
1380 | | |
1381 | | if (g_enable_diagnostic[0] == '1') |
1382 | | return g_object_is_floating (object); |
1383 | | |
1384 | | return FALSE; |
1385 | | } |
1386 | | #endif |
1387 | | |
1388 | | static void |
1389 | | g_object_finalize (GObject *object) |
1390 | 14.1k | { |
1391 | | #ifdef G_ENABLE_DEBUG |
1392 | | if (object_in_construction (object)) |
1393 | | { |
1394 | | g_critical ("object %s %p finalized while still in-construction", |
1395 | | G_OBJECT_TYPE_NAME (object), object); |
1396 | | } |
1397 | | |
1398 | | if (floating_check (object)) |
1399 | | { |
1400 | | g_critical ("A floating object %s %p was finalized. This means that someone\n" |
1401 | | "called g_object_unref() on an object that had only a floating\n" |
1402 | | "reference; the initial floating reference is not owned by anyone\n" |
1403 | | "and must be removed with g_object_ref_sink().", |
1404 | | G_OBJECT_TYPE_NAME (object), object); |
1405 | | } |
1406 | | #endif |
1407 | | |
1408 | 14.1k | g_datalist_clear (&object->qdata); |
1409 | | |
1410 | 14.1k | GOBJECT_IF_DEBUG (OBJECTS, |
1411 | 14.1k | { |
1412 | 14.1k | G_LOCK (debug_objects); |
1413 | 14.1k | g_assert (g_hash_table_contains (debug_objects_ht, object)); |
1414 | 14.1k | g_hash_table_remove (debug_objects_ht, object); |
1415 | 14.1k | debug_objects_count--; |
1416 | 14.1k | G_UNLOCK (debug_objects); |
1417 | 14.1k | }); |
1418 | 14.1k | } |
1419 | | |
1420 | | static void |
1421 | | g_object_dispatch_properties_changed (GObject *object, |
1422 | | guint n_pspecs, |
1423 | | GParamSpec **pspecs) |
1424 | 0 | { |
1425 | 0 | guint i; |
1426 | |
|
1427 | 0 | for (i = 0; i < n_pspecs; i++) |
1428 | 0 | g_signal_emit (object, gobject_signals[NOTIFY], g_param_spec_get_name_quark (pspecs[i]), pspecs[i]); |
1429 | 0 | } |
1430 | | |
1431 | | /** |
1432 | | * g_object_run_dispose: |
1433 | | * @object: a #GObject |
1434 | | * |
1435 | | * Releases all references to other objects. This can be used to break |
1436 | | * reference cycles. |
1437 | | * |
1438 | | * This function should only be called from object system implementations. |
1439 | | */ |
1440 | | void |
1441 | | g_object_run_dispose (GObject *object) |
1442 | 0 | { |
1443 | 0 | g_return_if_fail (G_IS_OBJECT (object)); |
1444 | 0 | g_return_if_fail (g_atomic_int_get (&object->ref_count) > 0); |
1445 | | |
1446 | 0 | g_object_ref (object); |
1447 | 0 | TRACE (GOBJECT_OBJECT_DISPOSE(object,G_TYPE_FROM_INSTANCE(object), 0)); |
1448 | 0 | G_OBJECT_GET_CLASS (object)->dispose (object); |
1449 | 0 | TRACE (GOBJECT_OBJECT_DISPOSE_END(object,G_TYPE_FROM_INSTANCE(object), 0)); |
1450 | 0 | g_object_unref (object); |
1451 | 0 | } |
1452 | | |
1453 | | /** |
1454 | | * g_object_freeze_notify: |
1455 | | * @object: a #GObject |
1456 | | * |
1457 | | * Increases the freeze count on @object. If the freeze count is |
1458 | | * non-zero, the emission of "notify" signals on @object is |
1459 | | * stopped. The signals are queued until the freeze count is decreased |
1460 | | * to zero. Duplicate notifications are squashed so that at most one |
1461 | | * #GObject::notify signal is emitted for each property modified while the |
1462 | | * object is frozen. |
1463 | | * |
1464 | | * This is necessary for accessors that modify multiple properties to prevent |
1465 | | * premature notification while the object is still being modified. |
1466 | | */ |
1467 | | void |
1468 | | g_object_freeze_notify (GObject *object) |
1469 | 0 | { |
1470 | 0 | g_return_if_fail (G_IS_OBJECT (object)); |
1471 | | |
1472 | 0 | #ifndef G_DISABLE_CHECKS |
1473 | 0 | if (G_UNLIKELY (g_atomic_int_get (&object->ref_count) == 0)) |
1474 | 0 | { |
1475 | 0 | g_critical ("Attempting to freeze the notification queue for object %s[%p]; " |
1476 | 0 | "Property notification does not work during instance finalization.", |
1477 | 0 | G_OBJECT_TYPE_NAME (object), |
1478 | 0 | object); |
1479 | 0 | return; |
1480 | 0 | } |
1481 | 0 | #endif |
1482 | | |
1483 | 0 | g_object_ref (object); |
1484 | 0 | g_object_notify_queue_freeze (object, FALSE); |
1485 | 0 | g_object_unref (object); |
1486 | 0 | } |
1487 | | |
1488 | | static inline void |
1489 | | g_object_notify_by_spec_internal (GObject *object, |
1490 | | GParamSpec *pspec) |
1491 | 0 | { |
1492 | 0 | #ifdef HAVE_OPTIONAL_FLAGS |
1493 | 0 | guint object_flags; |
1494 | 0 | #endif |
1495 | 0 | gboolean needs_notify; |
1496 | 0 | gboolean in_init; |
1497 | |
|
1498 | 0 | if (G_UNLIKELY (~pspec->flags & G_PARAM_READABLE)) |
1499 | 0 | return; |
1500 | | |
1501 | 0 | param_spec_follow_override (&pspec); |
1502 | |
|
1503 | 0 | #ifdef HAVE_OPTIONAL_FLAGS |
1504 | | /* get all flags we need with a single atomic read */ |
1505 | 0 | object_flags = object_get_optional_flags (object); |
1506 | 0 | needs_notify = ((object_flags & OPTIONAL_FLAG_HAS_NOTIFY_HANDLER) != 0) || |
1507 | 0 | CLASS_NEEDS_NOTIFY (G_OBJECT_GET_CLASS (object)); |
1508 | 0 | in_init = (object_flags & OPTIONAL_FLAG_IN_CONSTRUCTION) != 0; |
1509 | | #else |
1510 | | needs_notify = TRUE; |
1511 | | in_init = object_in_construction (object); |
1512 | | #endif |
1513 | |
|
1514 | 0 | if (pspec != NULL && needs_notify) |
1515 | 0 | { |
1516 | 0 | GObjectNotifyQueue *nqueue; |
1517 | 0 | gboolean need_thaw = TRUE; |
1518 | | |
1519 | | /* conditional freeze: only increase freeze count if already frozen */ |
1520 | 0 | nqueue = g_object_notify_queue_freeze (object, TRUE); |
1521 | 0 | if (in_init && !nqueue) |
1522 | 0 | { |
1523 | | /* We did not freeze the queue in g_object_init, but |
1524 | | * we gained a notify handler in instance init, so |
1525 | | * now we need to freeze just-in-time |
1526 | | */ |
1527 | 0 | nqueue = g_object_notify_queue_freeze (object, FALSE); |
1528 | 0 | need_thaw = FALSE; |
1529 | 0 | } |
1530 | |
|
1531 | 0 | if (nqueue != NULL) |
1532 | 0 | { |
1533 | | /* we're frozen, so add to the queue and release our freeze */ |
1534 | 0 | g_object_notify_queue_add (object, nqueue, pspec); |
1535 | 0 | if (need_thaw) |
1536 | 0 | g_object_notify_queue_thaw (object, nqueue); |
1537 | 0 | } |
1538 | 0 | else |
1539 | 0 | { |
1540 | | /* |
1541 | | * Coverity doesn’t understand the paired ref/unref here and seems to |
1542 | | * ignore the ref, thus reports every call to g_object_notify() as |
1543 | | * causing a double-free. That’s incorrect, but I can’t get a model |
1544 | | * file to work for avoiding the false positives, so instead comment |
1545 | | * out the ref/unref when doing static analysis. |
1546 | | */ |
1547 | 0 | #ifndef __COVERITY__ |
1548 | 0 | g_object_ref (object); |
1549 | 0 | #endif |
1550 | | |
1551 | | /* not frozen, so just dispatch the notification directly */ |
1552 | 0 | G_OBJECT_GET_CLASS (object) |
1553 | 0 | ->dispatch_properties_changed (object, 1, &pspec); |
1554 | |
|
1555 | 0 | #ifndef __COVERITY__ |
1556 | 0 | g_object_unref (object); |
1557 | 0 | #endif |
1558 | 0 | } |
1559 | 0 | } |
1560 | 0 | } |
1561 | | |
1562 | | /** |
1563 | | * g_object_notify: |
1564 | | * @object: a #GObject |
1565 | | * @property_name: the name of a property installed on the class of @object. |
1566 | | * |
1567 | | * Emits a "notify" signal for the property @property_name on @object. |
1568 | | * |
1569 | | * When possible, eg. when signaling a property change from within the class |
1570 | | * that registered the property, you should use g_object_notify_by_pspec() |
1571 | | * instead. |
1572 | | * |
1573 | | * Note that emission of the notify signal may be blocked with |
1574 | | * g_object_freeze_notify(). In this case, the signal emissions are queued |
1575 | | * and will be emitted (in reverse order) when g_object_thaw_notify() is |
1576 | | * called. |
1577 | | */ |
1578 | | void |
1579 | | g_object_notify (GObject *object, |
1580 | | const gchar *property_name) |
1581 | 0 | { |
1582 | 0 | GParamSpec *pspec; |
1583 | | |
1584 | 0 | g_return_if_fail (G_IS_OBJECT (object)); |
1585 | 0 | g_return_if_fail (property_name != NULL); |
1586 | | |
1587 | | /* We don't need to get the redirect target |
1588 | | * (by, e.g. calling g_object_class_find_property()) |
1589 | | * because g_object_notify_queue_add() does that |
1590 | | */ |
1591 | 0 | pspec = g_param_spec_pool_lookup (pspec_pool, |
1592 | 0 | property_name, |
1593 | 0 | G_OBJECT_TYPE (object), |
1594 | 0 | TRUE); |
1595 | |
|
1596 | 0 | if (!pspec) |
1597 | 0 | g_critical ("%s: object class '%s' has no property named '%s'", |
1598 | 0 | G_STRFUNC, |
1599 | 0 | G_OBJECT_TYPE_NAME (object), |
1600 | 0 | property_name); |
1601 | 0 | else |
1602 | 0 | g_object_notify_by_spec_internal (object, pspec); |
1603 | 0 | } |
1604 | | |
1605 | | /** |
1606 | | * g_object_notify_by_pspec: |
1607 | | * @object: a #GObject |
1608 | | * @pspec: the #GParamSpec of a property installed on the class of @object. |
1609 | | * |
1610 | | * Emits a "notify" signal for the property specified by @pspec on @object. |
1611 | | * |
1612 | | * This function omits the property name lookup, hence it is faster than |
1613 | | * g_object_notify(). |
1614 | | * |
1615 | | * One way to avoid using g_object_notify() from within the |
1616 | | * class that registered the properties, and using g_object_notify_by_pspec() |
1617 | | * instead, is to store the GParamSpec used with |
1618 | | * g_object_class_install_property() inside a static array, e.g.: |
1619 | | * |
1620 | | *|[<!-- language="C" --> |
1621 | | * typedef enum |
1622 | | * { |
1623 | | * PROP_FOO = 1, |
1624 | | * PROP_LAST |
1625 | | * } MyObjectProperty; |
1626 | | * |
1627 | | * static GParamSpec *properties[PROP_LAST]; |
1628 | | * |
1629 | | * static void |
1630 | | * my_object_class_init (MyObjectClass *klass) |
1631 | | * { |
1632 | | * properties[PROP_FOO] = g_param_spec_int ("foo", "Foo", "The foo", |
1633 | | * 0, 100, |
1634 | | * 50, |
1635 | | * G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS); |
1636 | | * g_object_class_install_property (gobject_class, |
1637 | | * PROP_FOO, |
1638 | | * properties[PROP_FOO]); |
1639 | | * } |
1640 | | * ]| |
1641 | | * |
1642 | | * and then notify a change on the "foo" property with: |
1643 | | * |
1644 | | * |[<!-- language="C" --> |
1645 | | * g_object_notify_by_pspec (self, properties[PROP_FOO]); |
1646 | | * ]| |
1647 | | * |
1648 | | * Since: 2.26 |
1649 | | */ |
1650 | | void |
1651 | | g_object_notify_by_pspec (GObject *object, |
1652 | | GParamSpec *pspec) |
1653 | 0 | { |
1654 | |
|
1655 | 0 | g_return_if_fail (G_IS_OBJECT (object)); |
1656 | 0 | g_return_if_fail (G_IS_PARAM_SPEC (pspec)); |
1657 | | |
1658 | 0 | g_object_notify_by_spec_internal (object, pspec); |
1659 | 0 | } |
1660 | | |
1661 | | /** |
1662 | | * g_object_thaw_notify: |
1663 | | * @object: a #GObject |
1664 | | * |
1665 | | * Reverts the effect of a previous call to |
1666 | | * g_object_freeze_notify(). The freeze count is decreased on @object |
1667 | | * and when it reaches zero, queued "notify" signals are emitted. |
1668 | | * |
1669 | | * Duplicate notifications for each property are squashed so that at most one |
1670 | | * #GObject::notify signal is emitted for each property, in the reverse order |
1671 | | * in which they have been queued. |
1672 | | * |
1673 | | * It is an error to call this function when the freeze count is zero. |
1674 | | */ |
1675 | | void |
1676 | | g_object_thaw_notify (GObject *object) |
1677 | 0 | { |
1678 | 0 | GObjectNotifyQueue *nqueue; |
1679 | | |
1680 | 0 | g_return_if_fail (G_IS_OBJECT (object)); |
1681 | | |
1682 | 0 | #ifndef G_DISABLE_CHECKS |
1683 | 0 | if (G_UNLIKELY (g_atomic_int_get (&object->ref_count) == 0)) |
1684 | 0 | { |
1685 | 0 | g_critical ("Attempting to thaw the notification queue for object %s[%p]; " |
1686 | 0 | "Property notification does not work during instance finalization.", |
1687 | 0 | G_OBJECT_TYPE_NAME (object), |
1688 | 0 | object); |
1689 | 0 | return; |
1690 | 0 | } |
1691 | 0 | #endif |
1692 | | |
1693 | | |
1694 | 0 | g_object_ref (object); |
1695 | | |
1696 | | /* FIXME: Freezing is the only way to get at the notify queue. |
1697 | | * So we freeze once and then thaw twice. |
1698 | | */ |
1699 | 0 | nqueue = g_object_notify_queue_freeze (object, FALSE); |
1700 | 0 | g_object_notify_queue_thaw (object, nqueue); |
1701 | 0 | g_object_notify_queue_thaw (object, nqueue); |
1702 | |
|
1703 | 0 | g_object_unref (object); |
1704 | 0 | } |
1705 | | |
1706 | | static void |
1707 | | maybe_issue_property_deprecation_warning (const GParamSpec *pspec) |
1708 | 0 | { |
1709 | 0 | static GHashTable *already_warned_table; |
1710 | 0 | static const gchar *enable_diagnostic; |
1711 | 0 | static GMutex already_warned_lock; |
1712 | 0 | gboolean already; |
1713 | |
|
1714 | 0 | if (g_once_init_enter (&enable_diagnostic)) |
1715 | 0 | { |
1716 | 0 | const gchar *value = g_getenv ("G_ENABLE_DIAGNOSTIC"); |
1717 | |
|
1718 | 0 | if (!value) |
1719 | 0 | value = "0"; |
1720 | |
|
1721 | 0 | g_once_init_leave (&enable_diagnostic, value); |
1722 | 0 | } |
1723 | |
|
1724 | 0 | if (enable_diagnostic[0] == '0') |
1725 | 0 | return; |
1726 | | |
1727 | | /* We hash only on property names: this means that we could end up in |
1728 | | * a situation where we fail to emit a warning about a pair of |
1729 | | * same-named deprecated properties used on two separate types. |
1730 | | * That's pretty unlikely to occur, and even if it does, you'll still |
1731 | | * have seen the warning for the first one... |
1732 | | * |
1733 | | * Doing it this way lets us hash directly on the (interned) property |
1734 | | * name pointers. |
1735 | | */ |
1736 | 0 | g_mutex_lock (&already_warned_lock); |
1737 | |
|
1738 | 0 | if (already_warned_table == NULL) |
1739 | 0 | already_warned_table = g_hash_table_new (NULL, NULL); |
1740 | |
|
1741 | 0 | already = g_hash_table_contains (already_warned_table, (gpointer) pspec->name); |
1742 | 0 | if (!already) |
1743 | 0 | g_hash_table_add (already_warned_table, (gpointer) pspec->name); |
1744 | |
|
1745 | 0 | g_mutex_unlock (&already_warned_lock); |
1746 | |
|
1747 | 0 | if (!already) |
1748 | 0 | g_warning ("The property %s:%s is deprecated and shouldn't be used " |
1749 | 0 | "anymore. It will be removed in a future version.", |
1750 | 0 | g_type_name (pspec->owner_type), pspec->name); |
1751 | 0 | } |
1752 | | |
1753 | | static inline void |
1754 | | consider_issuing_property_deprecation_warning (const GParamSpec *pspec) |
1755 | 0 | { |
1756 | 0 | if (G_UNLIKELY (pspec->flags & G_PARAM_DEPRECATED)) |
1757 | 0 | maybe_issue_property_deprecation_warning (pspec); |
1758 | 0 | } |
1759 | | |
1760 | | static inline void |
1761 | | object_get_property (GObject *object, |
1762 | | GParamSpec *pspec, |
1763 | | GValue *value) |
1764 | 0 | { |
1765 | 0 | GTypeInstance *inst = (GTypeInstance *) object; |
1766 | 0 | GObjectClass *class; |
1767 | 0 | guint param_id = PARAM_SPEC_PARAM_ID (pspec); |
1768 | |
|
1769 | 0 | if (G_LIKELY (inst->g_class->g_type == pspec->owner_type)) |
1770 | 0 | class = (GObjectClass *) inst->g_class; |
1771 | 0 | else |
1772 | 0 | class = g_type_class_peek (pspec->owner_type); |
1773 | |
|
1774 | 0 | g_assert (class != NULL); |
1775 | | |
1776 | 0 | param_spec_follow_override (&pspec); |
1777 | |
|
1778 | 0 | consider_issuing_property_deprecation_warning (pspec); |
1779 | |
|
1780 | 0 | class->get_property (object, param_id, value, pspec); |
1781 | 0 | } |
1782 | | |
1783 | | static inline void |
1784 | | object_set_property (GObject *object, |
1785 | | GParamSpec *pspec, |
1786 | | const GValue *value, |
1787 | | GObjectNotifyQueue *nqueue, |
1788 | | gboolean user_specified) |
1789 | 0 | { |
1790 | 0 | GTypeInstance *inst = (GTypeInstance *) object; |
1791 | 0 | GObjectClass *class; |
1792 | 0 | GParamSpecClass *pclass; |
1793 | 0 | guint param_id = PARAM_SPEC_PARAM_ID (pspec); |
1794 | |
|
1795 | 0 | if (G_LIKELY (inst->g_class->g_type == pspec->owner_type)) |
1796 | 0 | class = (GObjectClass *) inst->g_class; |
1797 | 0 | else |
1798 | 0 | class = g_type_class_peek (pspec->owner_type); |
1799 | |
|
1800 | 0 | g_assert (class != NULL); |
1801 | | |
1802 | 0 | param_spec_follow_override (&pspec); |
1803 | |
|
1804 | 0 | if (user_specified) |
1805 | 0 | consider_issuing_property_deprecation_warning (pspec); |
1806 | |
|
1807 | 0 | pclass = G_PARAM_SPEC_GET_CLASS (pspec); |
1808 | 0 | if (g_value_type_compatible (G_VALUE_TYPE (value), pspec->value_type) && |
1809 | 0 | (pclass->value_validate == NULL || |
1810 | 0 | (pclass->value_is_valid != NULL && pclass->value_is_valid (pspec, value)))) |
1811 | 0 | { |
1812 | 0 | class->set_property (object, param_id, value, pspec); |
1813 | 0 | } |
1814 | 0 | else |
1815 | 0 | { |
1816 | | /* provide a copy to work from, convert (if necessary) and validate */ |
1817 | 0 | GValue tmp_value = G_VALUE_INIT; |
1818 | |
|
1819 | 0 | g_value_init (&tmp_value, pspec->value_type); |
1820 | |
|
1821 | 0 | if (!g_value_transform (value, &tmp_value)) |
1822 | 0 | g_critical ("unable to set property '%s' of type '%s' from value of type '%s'", |
1823 | 0 | pspec->name, |
1824 | 0 | g_type_name (pspec->value_type), |
1825 | 0 | G_VALUE_TYPE_NAME (value)); |
1826 | 0 | else if (g_param_value_validate (pspec, &tmp_value) && !(pspec->flags & G_PARAM_LAX_VALIDATION)) |
1827 | 0 | { |
1828 | 0 | gchar *contents = g_strdup_value_contents (value); |
1829 | |
|
1830 | 0 | g_critical ("value \"%s\" of type '%s' is invalid or out of range for property '%s' of type '%s'", |
1831 | 0 | contents, |
1832 | 0 | G_VALUE_TYPE_NAME (value), |
1833 | 0 | pspec->name, |
1834 | 0 | g_type_name (pspec->value_type)); |
1835 | 0 | g_free (contents); |
1836 | 0 | } |
1837 | 0 | else |
1838 | 0 | { |
1839 | 0 | class->set_property (object, param_id, &tmp_value, pspec); |
1840 | 0 | } |
1841 | |
|
1842 | 0 | g_value_unset (&tmp_value); |
1843 | 0 | } |
1844 | |
|
1845 | 0 | if ((pspec->flags & (G_PARAM_EXPLICIT_NOTIFY | G_PARAM_READABLE)) == G_PARAM_READABLE && |
1846 | 0 | nqueue != NULL) |
1847 | 0 | g_object_notify_queue_add (object, nqueue, pspec); |
1848 | 0 | } |
1849 | | |
1850 | | static void |
1851 | | object_interface_check_properties (gpointer check_data, |
1852 | | gpointer g_iface) |
1853 | 3 | { |
1854 | 3 | GTypeInterface *iface_class = g_iface; |
1855 | 3 | GObjectClass *class; |
1856 | 3 | GType iface_type = iface_class->g_type; |
1857 | 3 | GParamSpec **pspecs; |
1858 | 3 | guint n; |
1859 | | |
1860 | 3 | class = g_type_class_ref (iface_class->g_instance_type); |
1861 | | |
1862 | 3 | if (class == NULL) |
1863 | 0 | return; |
1864 | | |
1865 | 3 | if (!G_IS_OBJECT_CLASS (class)) |
1866 | 0 | goto out; |
1867 | | |
1868 | 3 | pspecs = g_param_spec_pool_list (pspec_pool, iface_type, &n); |
1869 | | |
1870 | 3 | while (n--) |
1871 | 0 | { |
1872 | 0 | GParamSpec *class_pspec = g_param_spec_pool_lookup (pspec_pool, |
1873 | 0 | pspecs[n]->name, |
1874 | 0 | G_OBJECT_CLASS_TYPE (class), |
1875 | 0 | TRUE); |
1876 | |
|
1877 | 0 | if (!class_pspec) |
1878 | 0 | { |
1879 | 0 | g_critical ("Object class %s doesn't implement property " |
1880 | 0 | "'%s' from interface '%s'", |
1881 | 0 | g_type_name (G_OBJECT_CLASS_TYPE (class)), |
1882 | 0 | pspecs[n]->name, |
1883 | 0 | g_type_name (iface_type)); |
1884 | |
|
1885 | 0 | continue; |
1886 | 0 | } |
1887 | | |
1888 | | /* We do a number of checks on the properties of an interface to |
1889 | | * make sure that all classes implementing the interface are |
1890 | | * overriding the properties correctly. |
1891 | | * |
1892 | | * We do the checks in order of importance so that we can give |
1893 | | * more useful error messages first. |
1894 | | * |
1895 | | * First, we check that the implementation doesn't remove the |
1896 | | * basic functionality (readability, writability) advertised by |
1897 | | * the interface. Next, we check that it doesn't introduce |
1898 | | * additional restrictions (such as construct-only). Finally, we |
1899 | | * make sure the types are compatible. |
1900 | | */ |
1901 | | |
1902 | 0 | #define SUBSET(a,b,mask) (((a) & ~(b) & (mask)) == 0) |
1903 | | /* If the property on the interface is readable then the |
1904 | | * implementation must be readable. If the interface is writable |
1905 | | * then the implementation must be writable. |
1906 | | */ |
1907 | 0 | if (!SUBSET (pspecs[n]->flags, class_pspec->flags, G_PARAM_READABLE | G_PARAM_WRITABLE)) |
1908 | 0 | { |
1909 | 0 | g_critical ("Flags for property '%s' on class '%s' remove functionality compared with the " |
1910 | 0 | "property on interface '%s'\n", pspecs[n]->name, |
1911 | 0 | g_type_name (G_OBJECT_CLASS_TYPE (class)), g_type_name (iface_type)); |
1912 | 0 | continue; |
1913 | 0 | } |
1914 | | |
1915 | | /* If the property on the interface is writable then we need to |
1916 | | * make sure the implementation doesn't introduce new restrictions |
1917 | | * on that writability (ie: construct-only). |
1918 | | * |
1919 | | * If the interface was not writable to begin with then we don't |
1920 | | * really have any problems here because "writable at construct |
1921 | | * time only" is still more permissive than "read only". |
1922 | | */ |
1923 | 0 | if (pspecs[n]->flags & G_PARAM_WRITABLE) |
1924 | 0 | { |
1925 | 0 | if (!SUBSET (class_pspec->flags, pspecs[n]->flags, G_PARAM_CONSTRUCT_ONLY)) |
1926 | 0 | { |
1927 | 0 | g_critical ("Flags for property '%s' on class '%s' introduce additional restrictions on " |
1928 | 0 | "writability compared with the property on interface '%s'\n", pspecs[n]->name, |
1929 | 0 | g_type_name (G_OBJECT_CLASS_TYPE (class)), g_type_name (iface_type)); |
1930 | 0 | continue; |
1931 | 0 | } |
1932 | 0 | } |
1933 | 0 | #undef SUBSET |
1934 | | |
1935 | | /* If the property on the interface is readable then we are |
1936 | | * effectively advertising that reading the property will return a |
1937 | | * value of a specific type. All implementations of the interface |
1938 | | * need to return items of this type -- but may be more |
1939 | | * restrictive. For example, it is legal to have: |
1940 | | * |
1941 | | * GtkWidget *get_item(); |
1942 | | * |
1943 | | * that is implemented by a function that always returns a |
1944 | | * GtkEntry. In short: readability implies that the |
1945 | | * implementation value type must be equal or more restrictive. |
1946 | | * |
1947 | | * Similarly, if the property on the interface is writable then |
1948 | | * must be able to accept the property being set to any value of |
1949 | | * that type, including subclasses. In this case, we may also be |
1950 | | * less restrictive. For example, it is legal to have: |
1951 | | * |
1952 | | * set_item (GtkEntry *); |
1953 | | * |
1954 | | * that is implemented by a function that will actually work with |
1955 | | * any GtkWidget. In short: writability implies that the |
1956 | | * implementation value type must be equal or less restrictive. |
1957 | | * |
1958 | | * In the case that the property is both readable and writable |
1959 | | * then the only way that both of the above can be satisfied is |
1960 | | * with a type that is exactly equal. |
1961 | | */ |
1962 | 0 | switch (pspecs[n]->flags & (G_PARAM_READABLE | G_PARAM_WRITABLE)) |
1963 | 0 | { |
1964 | 0 | case G_PARAM_READABLE | G_PARAM_WRITABLE: |
1965 | | /* class pspec value type must have exact equality with interface */ |
1966 | 0 | if (pspecs[n]->value_type != class_pspec->value_type) |
1967 | 0 | g_critical ("Read/writable property '%s' on class '%s' has type '%s' which is not exactly equal to the " |
1968 | 0 | "type '%s' of the property on the interface '%s'\n", pspecs[n]->name, |
1969 | 0 | g_type_name (G_OBJECT_CLASS_TYPE (class)), g_type_name (G_PARAM_SPEC_VALUE_TYPE (class_pspec)), |
1970 | 0 | g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspecs[n])), g_type_name (iface_type)); |
1971 | 0 | break; |
1972 | | |
1973 | 0 | case G_PARAM_READABLE: |
1974 | | /* class pspec value type equal or more restrictive than interface */ |
1975 | 0 | if (!g_type_is_a (class_pspec->value_type, pspecs[n]->value_type)) |
1976 | 0 | g_critical ("Read-only property '%s' on class '%s' has type '%s' which is not equal to or more " |
1977 | 0 | "restrictive than the type '%s' of the property on the interface '%s'\n", pspecs[n]->name, |
1978 | 0 | g_type_name (G_OBJECT_CLASS_TYPE (class)), g_type_name (G_PARAM_SPEC_VALUE_TYPE (class_pspec)), |
1979 | 0 | g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspecs[n])), g_type_name (iface_type)); |
1980 | 0 | break; |
1981 | | |
1982 | 0 | case G_PARAM_WRITABLE: |
1983 | | /* class pspec value type equal or less restrictive than interface */ |
1984 | 0 | if (!g_type_is_a (pspecs[n]->value_type, class_pspec->value_type)) |
1985 | 0 | g_critical ("Write-only property '%s' on class '%s' has type '%s' which is not equal to or less " |
1986 | 0 | "restrictive than the type '%s' of the property on the interface '%s' \n", pspecs[n]->name, |
1987 | 0 | g_type_name (G_OBJECT_CLASS_TYPE (class)), g_type_name (G_PARAM_SPEC_VALUE_TYPE (class_pspec)), |
1988 | 0 | g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspecs[n])), g_type_name (iface_type)); |
1989 | 0 | break; |
1990 | | |
1991 | 0 | default: |
1992 | 0 | g_assert_not_reached (); |
1993 | 0 | } |
1994 | 0 | } |
1995 | | |
1996 | 3 | g_free (pspecs); |
1997 | | |
1998 | 3 | out: |
1999 | 3 | g_type_class_unref (class); |
2000 | 3 | } |
2001 | | |
2002 | | GType |
2003 | | g_object_get_type (void) |
2004 | 0 | { |
2005 | 0 | return G_TYPE_OBJECT; |
2006 | 0 | } |
2007 | | |
2008 | | /** |
2009 | | * g_object_new: (skip) |
2010 | | * @object_type: the type id of the #GObject subtype to instantiate |
2011 | | * @first_property_name: the name of the first property |
2012 | | * @...: the value of the first property, followed optionally by more |
2013 | | * name/value pairs, followed by %NULL |
2014 | | * |
2015 | | * Creates a new instance of a #GObject subtype and sets its properties. |
2016 | | * |
2017 | | * Construction parameters (see %G_PARAM_CONSTRUCT, %G_PARAM_CONSTRUCT_ONLY) |
2018 | | * which are not explicitly specified are set to their default values. Any |
2019 | | * private data for the object is guaranteed to be initialized with zeros, as |
2020 | | * per g_type_create_instance(). |
2021 | | * |
2022 | | * Note that in C, small integer types in variable argument lists are promoted |
2023 | | * up to `gint` or `guint` as appropriate, and read back accordingly. `gint` is |
2024 | | * 32 bits on every platform on which GLib is currently supported. This means that |
2025 | | * you can use C expressions of type `gint` with g_object_new() and properties of |
2026 | | * type `gint` or `guint` or smaller. Specifically, you can use integer literals |
2027 | | * with these property types. |
2028 | | * |
2029 | | * When using property types of `gint64` or `guint64`, you must ensure that the |
2030 | | * value that you provide is 64 bit. This means that you should use a cast or |
2031 | | * make use of the %G_GINT64_CONSTANT or %G_GUINT64_CONSTANT macros. |
2032 | | * |
2033 | | * Similarly, `gfloat` is promoted to `gdouble`, so you must ensure that the value |
2034 | | * you provide is a `gdouble`, even for a property of type `gfloat`. |
2035 | | * |
2036 | | * Since GLib 2.72, all #GObjects are guaranteed to be aligned to at least the |
2037 | | * alignment of the largest basic GLib type (typically this is `guint64` or |
2038 | | * `gdouble`). If you need larger alignment for an element in a #GObject, you |
2039 | | * should allocate it on the heap (aligned), or arrange for your #GObject to be |
2040 | | * appropriately padded. |
2041 | | * |
2042 | | * Returns: (transfer full) (type GObject.Object): a new instance of |
2043 | | * @object_type |
2044 | | */ |
2045 | | gpointer |
2046 | | g_object_new (GType object_type, |
2047 | | const gchar *first_property_name, |
2048 | | ...) |
2049 | 14.1k | { |
2050 | 14.1k | GObject *object; |
2051 | 14.1k | va_list var_args; |
2052 | | |
2053 | | /* short circuit for calls supplying no properties */ |
2054 | 14.1k | if (!first_property_name) |
2055 | 14.1k | return g_object_new_with_properties (object_type, 0, NULL, NULL); |
2056 | | |
2057 | 14.1k | va_start (var_args, first_property_name); |
2058 | 0 | object = g_object_new_valist (object_type, first_property_name, var_args); |
2059 | 0 | va_end (var_args); |
2060 | | |
2061 | 0 | return object; |
2062 | 14.1k | } |
2063 | | |
2064 | | /* Check alignment. (See https://gitlab.gnome.org/GNOME/glib/-/issues/1231.) |
2065 | | * This should never fail, since g_type_create_instance() uses g_slice_alloc0(). |
2066 | | * The GSlice allocator always aligns to the next power of 2 greater than the |
2067 | | * allocation size. The allocation size for a GObject is |
2068 | | * sizeof(GTypeInstance) + sizeof(guint) + sizeof(GData*) |
2069 | | * which is 12B on 32-bit platforms, and larger on 64-bit systems. In both |
2070 | | * cases, that’s larger than the 8B needed for a guint64 or gdouble. |
2071 | | * |
2072 | | * If GSlice falls back to malloc(), it’s documented to return something |
2073 | | * suitably aligned for any basic type. */ |
2074 | | static inline gboolean |
2075 | | g_object_is_aligned (GObject *object) |
2076 | 14.1k | { |
2077 | 14.1k | return ((((guintptr) (void *) object) % |
2078 | 14.1k | MAX (G_ALIGNOF (gdouble), |
2079 | 14.1k | MAX (G_ALIGNOF (guint64), |
2080 | 14.1k | MAX (G_ALIGNOF (gint), |
2081 | 14.1k | G_ALIGNOF (glong))))) == 0); |
2082 | 14.1k | } |
2083 | | |
2084 | | static gpointer |
2085 | | g_object_new_with_custom_constructor (GObjectClass *class, |
2086 | | GObjectConstructParam *params, |
2087 | | guint n_params) |
2088 | 0 | { |
2089 | 0 | GObjectNotifyQueue *nqueue = NULL; |
2090 | 0 | gboolean newly_constructed; |
2091 | 0 | GObjectConstructParam *cparams; |
2092 | 0 | gboolean free_cparams = FALSE; |
2093 | 0 | GObject *object; |
2094 | 0 | GValue *cvalues; |
2095 | 0 | gint cvals_used; |
2096 | 0 | GSList *node; |
2097 | 0 | guint i; |
2098 | | |
2099 | | /* If we have ->constructed() then we have to do a lot more work. |
2100 | | * It's possible that this is a singleton and it's also possible |
2101 | | * that the user's constructor() will attempt to modify the values |
2102 | | * that we pass in, so we'll need to allocate copies of them. |
2103 | | * It's also possible that the user may attempt to call |
2104 | | * g_object_set() from inside of their constructor, so we need to |
2105 | | * add ourselves to a list of objects for which that is allowed |
2106 | | * while their constructor() is running. |
2107 | | */ |
2108 | | |
2109 | | /* Create the array of GObjectConstructParams for constructor(), |
2110 | | * The 1024 here is an arbitrary, high limit that no sane code |
2111 | | * will ever hit, just to avoid the possibility of stack overflow. |
2112 | | */ |
2113 | 0 | if (G_LIKELY (class->n_construct_properties < 1024)) |
2114 | 0 | { |
2115 | 0 | cparams = g_newa0 (GObjectConstructParam, class->n_construct_properties); |
2116 | 0 | cvalues = g_newa0 (GValue, class->n_construct_properties); |
2117 | 0 | } |
2118 | 0 | else |
2119 | 0 | { |
2120 | 0 | cparams = g_new0 (GObjectConstructParam, class->n_construct_properties); |
2121 | 0 | cvalues = g_new0 (GValue, class->n_construct_properties); |
2122 | 0 | free_cparams = TRUE; |
2123 | 0 | } |
2124 | 0 | cvals_used = 0; |
2125 | 0 | i = 0; |
2126 | | |
2127 | | /* As above, we may find the value in the passed-in params list. |
2128 | | * |
2129 | | * If we have the value passed in then we can use the GValue from |
2130 | | * it directly because it is safe to modify. If we use the |
2131 | | * default value from the class, we had better not pass that in |
2132 | | * and risk it being modified, so we create a new one. |
2133 | | * */ |
2134 | 0 | for (node = class->construct_properties; node; node = node->next) |
2135 | 0 | { |
2136 | 0 | GParamSpec *pspec; |
2137 | 0 | GValue *value; |
2138 | 0 | guint j; |
2139 | |
|
2140 | 0 | pspec = node->data; |
2141 | 0 | value = NULL; /* to silence gcc... */ |
2142 | |
|
2143 | 0 | for (j = 0; j < n_params; j++) |
2144 | 0 | if (params[j].pspec == pspec) |
2145 | 0 | { |
2146 | 0 | consider_issuing_property_deprecation_warning (pspec); |
2147 | 0 | value = params[j].value; |
2148 | 0 | break; |
2149 | 0 | } |
2150 | |
|
2151 | 0 | if (value == NULL) |
2152 | 0 | { |
2153 | 0 | value = &cvalues[cvals_used++]; |
2154 | 0 | g_value_init (value, pspec->value_type); |
2155 | 0 | g_param_value_set_default (pspec, value); |
2156 | 0 | } |
2157 | |
|
2158 | 0 | cparams[i].pspec = pspec; |
2159 | 0 | cparams[i].value = value; |
2160 | 0 | i++; |
2161 | 0 | } |
2162 | | |
2163 | | /* construct object from construction parameters */ |
2164 | 0 | object = class->constructor (class->g_type_class.g_type, class->n_construct_properties, cparams); |
2165 | | /* free construction values */ |
2166 | 0 | while (cvals_used--) |
2167 | 0 | g_value_unset (&cvalues[cvals_used]); |
2168 | |
|
2169 | 0 | if (free_cparams) |
2170 | 0 | { |
2171 | 0 | g_free (cparams); |
2172 | 0 | g_free (cvalues); |
2173 | 0 | } |
2174 | | |
2175 | | /* There is code in the wild that relies on being able to return NULL |
2176 | | * from its custom constructor. This was never a supported operation, |
2177 | | * but since the code is already out there... |
2178 | | */ |
2179 | 0 | if (object == NULL) |
2180 | 0 | { |
2181 | 0 | g_critical ("Custom constructor for class %s returned NULL (which is invalid). " |
2182 | 0 | "Please use GInitable instead.", G_OBJECT_CLASS_NAME (class)); |
2183 | 0 | return NULL; |
2184 | 0 | } |
2185 | | |
2186 | 0 | if (!g_object_is_aligned (object)) |
2187 | 0 | { |
2188 | 0 | g_critical ("Custom constructor for class %s returned a non-aligned " |
2189 | 0 | "GObject (which is invalid since GLib 2.72). Assuming any " |
2190 | 0 | "code using this object doesn’t require it to be aligned. " |
2191 | 0 | "Please fix your constructor to align to the largest GLib " |
2192 | 0 | "basic type (typically gdouble or guint64).", |
2193 | 0 | G_OBJECT_CLASS_NAME (class)); |
2194 | 0 | } |
2195 | | |
2196 | | /* g_object_init() will have marked the object as being in-construction. |
2197 | | * Check if the returned object still is so marked, or if this is an |
2198 | | * already-existing singleton (in which case we should not do 'constructed'). |
2199 | | */ |
2200 | 0 | newly_constructed = object_in_construction (object); |
2201 | 0 | if (newly_constructed) |
2202 | 0 | unset_object_in_construction (object); |
2203 | |
|
2204 | 0 | if (CLASS_HAS_PROPS (class)) |
2205 | 0 | { |
2206 | 0 | if ((newly_constructed && _g_object_has_notify_handler_X (object)) || |
2207 | 0 | _g_object_has_notify_handler (object)) |
2208 | 0 | { |
2209 | | /* This may or may not have been setup in g_object_init(). |
2210 | | * If it hasn't, we do it now. |
2211 | | */ |
2212 | 0 | nqueue = g_datalist_id_get_data (&object->qdata, quark_notify_queue); |
2213 | 0 | if (!nqueue) |
2214 | 0 | nqueue = g_object_notify_queue_freeze (object, FALSE); |
2215 | 0 | } |
2216 | 0 | } |
2217 | | |
2218 | | /* run 'constructed' handler if there is a custom one */ |
2219 | 0 | if (newly_constructed && CLASS_HAS_CUSTOM_CONSTRUCTED (class)) |
2220 | 0 | class->constructed (object); |
2221 | | |
2222 | | /* set remaining properties */ |
2223 | 0 | for (i = 0; i < n_params; i++) |
2224 | 0 | if (!(params[i].pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))) |
2225 | 0 | object_set_property (object, params[i].pspec, params[i].value, nqueue, TRUE); |
2226 | | |
2227 | | /* If nqueue is non-NULL then we are frozen. Thaw it. */ |
2228 | 0 | if (nqueue) |
2229 | 0 | g_object_notify_queue_thaw (object, nqueue); |
2230 | |
|
2231 | 0 | return object; |
2232 | 0 | } |
2233 | | |
2234 | | static gpointer |
2235 | | g_object_new_internal (GObjectClass *class, |
2236 | | GObjectConstructParam *params, |
2237 | | guint n_params) |
2238 | 14.1k | { |
2239 | 14.1k | GObjectNotifyQueue *nqueue = NULL; |
2240 | 14.1k | GObject *object; |
2241 | 14.1k | guint i; |
2242 | | |
2243 | 14.1k | if G_UNLIKELY (CLASS_HAS_CUSTOM_CONSTRUCTOR (class)) |
2244 | 0 | return g_object_new_with_custom_constructor (class, params, n_params); |
2245 | | |
2246 | 14.1k | object = (GObject *) g_type_create_instance (class->g_type_class.g_type); |
2247 | | |
2248 | 14.1k | g_assert (g_object_is_aligned (object)); |
2249 | | |
2250 | 14.1k | unset_object_in_construction (object); |
2251 | | |
2252 | 14.1k | if (CLASS_HAS_PROPS (class)) |
2253 | 0 | { |
2254 | 0 | GSList *node; |
2255 | |
|
2256 | 0 | if (_g_object_has_notify_handler_X (object)) |
2257 | 0 | { |
2258 | | /* This may or may not have been setup in g_object_init(). |
2259 | | * If it hasn't, we do it now. |
2260 | | */ |
2261 | 0 | nqueue = g_datalist_id_get_data (&object->qdata, quark_notify_queue); |
2262 | 0 | if (!nqueue) |
2263 | 0 | nqueue = g_object_notify_queue_freeze (object, FALSE); |
2264 | 0 | } |
2265 | | |
2266 | | /* We will set exactly n_construct_properties construct |
2267 | | * properties, but they may come from either the class default |
2268 | | * values or the passed-in parameter list. |
2269 | | */ |
2270 | 0 | for (node = class->construct_properties; node; node = node->next) |
2271 | 0 | { |
2272 | 0 | const GValue *value; |
2273 | 0 | GParamSpec *pspec; |
2274 | 0 | guint j; |
2275 | 0 | gboolean user_specified = FALSE; |
2276 | |
|
2277 | 0 | pspec = node->data; |
2278 | 0 | value = NULL; /* to silence gcc... */ |
2279 | |
|
2280 | 0 | for (j = 0; j < n_params; j++) |
2281 | 0 | if (params[j].pspec == pspec) |
2282 | 0 | { |
2283 | 0 | value = params[j].value; |
2284 | 0 | user_specified = TRUE; |
2285 | 0 | break; |
2286 | 0 | } |
2287 | |
|
2288 | 0 | if (value == NULL) |
2289 | 0 | value = g_param_spec_get_default_value (pspec); |
2290 | |
|
2291 | 0 | object_set_property (object, pspec, value, nqueue, user_specified); |
2292 | 0 | } |
2293 | 0 | } |
2294 | | |
2295 | | /* run 'constructed' handler if there is a custom one */ |
2296 | 14.1k | if (CLASS_HAS_CUSTOM_CONSTRUCTED (class)) |
2297 | 0 | class->constructed (object); |
2298 | | |
2299 | | /* Set remaining properties. The construct properties will |
2300 | | * already have been taken, so set only the non-construct ones. |
2301 | | */ |
2302 | 14.1k | for (i = 0; i < n_params; i++) |
2303 | 0 | if (!(params[i].pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))) |
2304 | 0 | object_set_property (object, params[i].pspec, params[i].value, nqueue, TRUE); |
2305 | | |
2306 | 14.1k | if (nqueue) |
2307 | 0 | g_object_notify_queue_thaw (object, nqueue); |
2308 | | |
2309 | 14.1k | return object; |
2310 | 14.1k | } |
2311 | | |
2312 | | |
2313 | | static inline gboolean |
2314 | | g_object_new_is_valid_property (GType object_type, |
2315 | | GParamSpec *pspec, |
2316 | | const char *name, |
2317 | | GObjectConstructParam *params, |
2318 | | guint n_params) |
2319 | 0 | { |
2320 | 0 | guint i; |
2321 | |
|
2322 | 0 | if (G_UNLIKELY (pspec == NULL)) |
2323 | 0 | { |
2324 | 0 | g_critical ("%s: object class '%s' has no property named '%s'", |
2325 | 0 | G_STRFUNC, g_type_name (object_type), name); |
2326 | 0 | return FALSE; |
2327 | 0 | } |
2328 | | |
2329 | 0 | if (G_UNLIKELY (~pspec->flags & G_PARAM_WRITABLE)) |
2330 | 0 | { |
2331 | 0 | g_critical ("%s: property '%s' of object class '%s' is not writable", |
2332 | 0 | G_STRFUNC, pspec->name, g_type_name (object_type)); |
2333 | 0 | return FALSE; |
2334 | 0 | } |
2335 | | |
2336 | 0 | if (G_UNLIKELY (pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))) |
2337 | 0 | { |
2338 | 0 | for (i = 0; i < n_params; i++) |
2339 | 0 | if (params[i].pspec == pspec) |
2340 | 0 | break; |
2341 | 0 | if (G_UNLIKELY (i != n_params)) |
2342 | 0 | { |
2343 | 0 | g_critical ("%s: property '%s' for type '%s' cannot be set twice", |
2344 | 0 | G_STRFUNC, name, g_type_name (object_type)); |
2345 | 0 | return FALSE; |
2346 | 0 | } |
2347 | 0 | } |
2348 | 0 | return TRUE; |
2349 | 0 | } |
2350 | | |
2351 | | |
2352 | | /** |
2353 | | * g_object_new_with_properties: (skip) |
2354 | | * @object_type: the object type to instantiate |
2355 | | * @n_properties: the number of properties |
2356 | | * @names: (array length=n_properties): the names of each property to be set |
2357 | | * @values: (array length=n_properties): the values of each property to be set |
2358 | | * |
2359 | | * Creates a new instance of a #GObject subtype and sets its properties using |
2360 | | * the provided arrays. Both arrays must have exactly @n_properties elements, |
2361 | | * and the names and values correspond by index. |
2362 | | * |
2363 | | * Construction parameters (see %G_PARAM_CONSTRUCT, %G_PARAM_CONSTRUCT_ONLY) |
2364 | | * which are not explicitly specified are set to their default values. |
2365 | | * |
2366 | | * Returns: (type GObject.Object) (transfer full): a new instance of |
2367 | | * @object_type |
2368 | | * |
2369 | | * Since: 2.54 |
2370 | | */ |
2371 | | GObject * |
2372 | | g_object_new_with_properties (GType object_type, |
2373 | | guint n_properties, |
2374 | | const char *names[], |
2375 | | const GValue values[]) |
2376 | 14.1k | { |
2377 | 14.1k | GObjectClass *class, *unref_class = NULL; |
2378 | 14.1k | GObject *object; |
2379 | | |
2380 | 14.1k | g_return_val_if_fail (G_TYPE_IS_OBJECT (object_type), NULL); |
2381 | | |
2382 | | /* Try to avoid thrashing the ref_count if we don't need to (since |
2383 | | * it's a locked operation). |
2384 | | */ |
2385 | 14.1k | class = g_type_class_peek_static (object_type); |
2386 | | |
2387 | 14.1k | if (class == NULL) |
2388 | 4 | class = unref_class = g_type_class_ref (object_type); |
2389 | | |
2390 | 14.1k | if (n_properties > 0) |
2391 | 0 | { |
2392 | 0 | guint i, count = 0; |
2393 | 0 | GObjectConstructParam *params; |
2394 | |
|
2395 | 0 | params = g_newa (GObjectConstructParam, n_properties); |
2396 | 0 | for (i = 0; i < n_properties; i++) |
2397 | 0 | { |
2398 | 0 | GParamSpec *pspec = find_pspec (class, names[i]); |
2399 | |
|
2400 | 0 | if (!g_object_new_is_valid_property (object_type, pspec, names[i], params, count)) |
2401 | 0 | continue; |
2402 | 0 | params[count].pspec = pspec; |
2403 | 0 | params[count].value = (GValue *) &values[i]; |
2404 | 0 | count++; |
2405 | 0 | } |
2406 | 0 | object = g_object_new_internal (class, params, count); |
2407 | 0 | } |
2408 | 14.1k | else |
2409 | 14.1k | object = g_object_new_internal (class, NULL, 0); |
2410 | | |
2411 | 14.1k | if (unref_class != NULL) |
2412 | 4 | g_type_class_unref (unref_class); |
2413 | | |
2414 | 14.1k | return object; |
2415 | 14.1k | } |
2416 | | |
2417 | | /** |
2418 | | * g_object_newv: |
2419 | | * @object_type: the type id of the #GObject subtype to instantiate |
2420 | | * @n_parameters: the length of the @parameters array |
2421 | | * @parameters: (array length=n_parameters): an array of #GParameter |
2422 | | * |
2423 | | * Creates a new instance of a #GObject subtype and sets its properties. |
2424 | | * |
2425 | | * Construction parameters (see %G_PARAM_CONSTRUCT, %G_PARAM_CONSTRUCT_ONLY) |
2426 | | * which are not explicitly specified are set to their default values. |
2427 | | * |
2428 | | * Returns: (type GObject.Object) (transfer full): a new instance of |
2429 | | * @object_type |
2430 | | * |
2431 | | * Deprecated: 2.54: Use g_object_new_with_properties() instead. |
2432 | | * deprecated. See #GParameter for more information. |
2433 | | */ |
2434 | | G_GNUC_BEGIN_IGNORE_DEPRECATIONS |
2435 | | gpointer |
2436 | | g_object_newv (GType object_type, |
2437 | | guint n_parameters, |
2438 | | GParameter *parameters) |
2439 | 0 | { |
2440 | 0 | GObjectClass *class, *unref_class = NULL; |
2441 | 0 | GObject *object; |
2442 | |
|
2443 | 0 | g_return_val_if_fail (G_TYPE_IS_OBJECT (object_type), NULL); |
2444 | 0 | g_return_val_if_fail (n_parameters == 0 || parameters != NULL, NULL); |
2445 | | |
2446 | | /* Try to avoid thrashing the ref_count if we don't need to (since |
2447 | | * it's a locked operation). |
2448 | | */ |
2449 | 0 | class = g_type_class_peek_static (object_type); |
2450 | |
|
2451 | 0 | if (!class) |
2452 | 0 | class = unref_class = g_type_class_ref (object_type); |
2453 | |
|
2454 | 0 | if (n_parameters) |
2455 | 0 | { |
2456 | 0 | GObjectConstructParam *cparams; |
2457 | 0 | guint i, j; |
2458 | |
|
2459 | 0 | cparams = g_newa (GObjectConstructParam, n_parameters); |
2460 | 0 | j = 0; |
2461 | |
|
2462 | 0 | for (i = 0; i < n_parameters; i++) |
2463 | 0 | { |
2464 | 0 | GParamSpec *pspec = find_pspec (class, parameters[i].name); |
2465 | |
|
2466 | 0 | if (!g_object_new_is_valid_property (object_type, pspec, parameters[i].name, cparams, j)) |
2467 | 0 | continue; |
2468 | | |
2469 | 0 | cparams[j].pspec = pspec; |
2470 | 0 | cparams[j].value = ¶meters[i].value; |
2471 | 0 | j++; |
2472 | 0 | } |
2473 | |
|
2474 | 0 | object = g_object_new_internal (class, cparams, j); |
2475 | 0 | } |
2476 | 0 | else |
2477 | | /* Fast case: no properties passed in. */ |
2478 | 0 | object = g_object_new_internal (class, NULL, 0); |
2479 | |
|
2480 | 0 | if (unref_class) |
2481 | 0 | g_type_class_unref (unref_class); |
2482 | |
|
2483 | 0 | return object; |
2484 | 0 | } |
2485 | | G_GNUC_END_IGNORE_DEPRECATIONS |
2486 | | |
2487 | | /** |
2488 | | * g_object_new_valist: (skip) |
2489 | | * @object_type: the type id of the #GObject subtype to instantiate |
2490 | | * @first_property_name: the name of the first property |
2491 | | * @var_args: the value of the first property, followed optionally by more |
2492 | | * name/value pairs, followed by %NULL |
2493 | | * |
2494 | | * Creates a new instance of a #GObject subtype and sets its properties. |
2495 | | * |
2496 | | * Construction parameters (see %G_PARAM_CONSTRUCT, %G_PARAM_CONSTRUCT_ONLY) |
2497 | | * which are not explicitly specified are set to their default values. |
2498 | | * |
2499 | | * Returns: a new instance of @object_type |
2500 | | */ |
2501 | | GObject* |
2502 | | g_object_new_valist (GType object_type, |
2503 | | const gchar *first_property_name, |
2504 | | va_list var_args) |
2505 | 0 | { |
2506 | 0 | GObjectClass *class, *unref_class = NULL; |
2507 | 0 | GObject *object; |
2508 | |
|
2509 | 0 | g_return_val_if_fail (G_TYPE_IS_OBJECT (object_type), NULL); |
2510 | | |
2511 | | /* Try to avoid thrashing the ref_count if we don't need to (since |
2512 | | * it's a locked operation). |
2513 | | */ |
2514 | 0 | class = g_type_class_peek_static (object_type); |
2515 | |
|
2516 | 0 | if (!class) |
2517 | 0 | class = unref_class = g_type_class_ref (object_type); |
2518 | |
|
2519 | 0 | if (first_property_name) |
2520 | 0 | { |
2521 | 0 | GObjectConstructParam params_stack[16]; |
2522 | 0 | GValue values_stack[G_N_ELEMENTS (params_stack)]; |
2523 | 0 | GTypeValueTable *vtabs_stack[G_N_ELEMENTS (params_stack)]; |
2524 | 0 | const gchar *name; |
2525 | 0 | GObjectConstructParam *params = params_stack; |
2526 | 0 | GValue *values = values_stack; |
2527 | 0 | GTypeValueTable **vtabs = vtabs_stack; |
2528 | 0 | guint n_params = 0; |
2529 | 0 | guint n_params_alloc = G_N_ELEMENTS (params_stack); |
2530 | |
|
2531 | 0 | name = first_property_name; |
2532 | |
|
2533 | 0 | do |
2534 | 0 | { |
2535 | 0 | gchar *error = NULL; |
2536 | 0 | GParamSpec *pspec = find_pspec (class, name); |
2537 | |
|
2538 | 0 | if (!g_object_new_is_valid_property (object_type, pspec, name, params, n_params)) |
2539 | 0 | break; |
2540 | | |
2541 | 0 | if (G_UNLIKELY (n_params == n_params_alloc)) |
2542 | 0 | { |
2543 | 0 | guint i; |
2544 | |
|
2545 | 0 | if (n_params_alloc == G_N_ELEMENTS (params_stack)) |
2546 | 0 | { |
2547 | 0 | n_params_alloc = G_N_ELEMENTS (params_stack) * 2u; |
2548 | 0 | params = g_new (GObjectConstructParam, n_params_alloc); |
2549 | 0 | values = g_new (GValue, n_params_alloc); |
2550 | 0 | vtabs = g_new (GTypeValueTable *, n_params_alloc); |
2551 | 0 | memcpy (params, params_stack, sizeof (GObjectConstructParam) * n_params); |
2552 | 0 | memcpy (values, values_stack, sizeof (GValue) * n_params); |
2553 | 0 | memcpy (vtabs, vtabs_stack, sizeof (GTypeValueTable *) * n_params); |
2554 | 0 | } |
2555 | 0 | else |
2556 | 0 | { |
2557 | 0 | n_params_alloc *= 2u; |
2558 | 0 | params = g_realloc (params, sizeof (GObjectConstructParam) * n_params_alloc); |
2559 | 0 | values = g_realloc (values, sizeof (GValue) * n_params_alloc); |
2560 | 0 | vtabs = g_realloc (vtabs, sizeof (GTypeValueTable *) * n_params_alloc); |
2561 | 0 | } |
2562 | |
|
2563 | 0 | for (i = 0; i < n_params; i++) |
2564 | 0 | params[i].value = &values[i]; |
2565 | 0 | } |
2566 | |
|
2567 | 0 | params[n_params].pspec = pspec; |
2568 | 0 | params[n_params].value = &values[n_params]; |
2569 | 0 | memset (&values[n_params], 0, sizeof (GValue)); |
2570 | |
|
2571 | 0 | G_VALUE_COLLECT_INIT2 (&values[n_params], vtabs[n_params], pspec->value_type, var_args, G_VALUE_NOCOPY_CONTENTS, &error); |
2572 | | |
2573 | 0 | if (error) |
2574 | 0 | { |
2575 | 0 | g_critical ("%s: %s", G_STRFUNC, error); |
2576 | 0 | g_value_unset (&values[n_params]); |
2577 | 0 | g_free (error); |
2578 | 0 | break; |
2579 | 0 | } |
2580 | | |
2581 | 0 | n_params++; |
2582 | 0 | } |
2583 | 0 | while ((name = va_arg (var_args, const gchar *))); |
2584 | | |
2585 | 0 | object = g_object_new_internal (class, params, n_params); |
2586 | |
|
2587 | 0 | while (n_params--) |
2588 | 0 | { |
2589 | | /* We open-code g_value_unset() here to avoid the |
2590 | | * cost of looking up the GTypeValueTable again. |
2591 | | */ |
2592 | 0 | if (vtabs[n_params]->value_free) |
2593 | 0 | vtabs[n_params]->value_free (params[n_params].value); |
2594 | 0 | } |
2595 | |
|
2596 | 0 | if (G_UNLIKELY (n_params_alloc != G_N_ELEMENTS (params_stack))) |
2597 | 0 | { |
2598 | 0 | g_free (params); |
2599 | 0 | g_free (values); |
2600 | 0 | g_free (vtabs); |
2601 | 0 | } |
2602 | 0 | } |
2603 | 0 | else |
2604 | | /* Fast case: no properties passed in. */ |
2605 | 0 | object = g_object_new_internal (class, NULL, 0); |
2606 | | |
2607 | 0 | if (unref_class) |
2608 | 0 | g_type_class_unref (unref_class); |
2609 | |
|
2610 | 0 | return object; |
2611 | 0 | } |
2612 | | |
2613 | | static GObject* |
2614 | | g_object_constructor (GType type, |
2615 | | guint n_construct_properties, |
2616 | | GObjectConstructParam *construct_params) |
2617 | 0 | { |
2618 | 0 | GObject *object; |
2619 | | |
2620 | | /* create object */ |
2621 | 0 | object = (GObject*) g_type_create_instance (type); |
2622 | | |
2623 | | /* set construction parameters */ |
2624 | 0 | if (n_construct_properties) |
2625 | 0 | { |
2626 | 0 | GObjectNotifyQueue *nqueue = g_object_notify_queue_freeze (object, FALSE); |
2627 | | |
2628 | | /* set construct properties */ |
2629 | 0 | while (n_construct_properties--) |
2630 | 0 | { |
2631 | 0 | GValue *value = construct_params->value; |
2632 | 0 | GParamSpec *pspec = construct_params->pspec; |
2633 | |
|
2634 | 0 | construct_params++; |
2635 | 0 | object_set_property (object, pspec, value, nqueue, TRUE); |
2636 | 0 | } |
2637 | 0 | g_object_notify_queue_thaw (object, nqueue); |
2638 | | /* the notification queue is still frozen from g_object_init(), so |
2639 | | * we don't need to handle it here, g_object_newv() takes |
2640 | | * care of that |
2641 | | */ |
2642 | 0 | } |
2643 | |
|
2644 | 0 | return object; |
2645 | 0 | } |
2646 | | |
2647 | | static void |
2648 | | g_object_constructed (GObject *object) |
2649 | 0 | { |
2650 | | /* empty default impl to allow unconditional upchaining */ |
2651 | 0 | } |
2652 | | |
2653 | | static inline gboolean |
2654 | | g_object_set_is_valid_property (GObject *object, |
2655 | | GParamSpec *pspec, |
2656 | | const char *property_name) |
2657 | 0 | { |
2658 | 0 | if (G_UNLIKELY (pspec == NULL)) |
2659 | 0 | { |
2660 | 0 | g_critical ("%s: object class '%s' has no property named '%s'", |
2661 | 0 | G_STRFUNC, G_OBJECT_TYPE_NAME (object), property_name); |
2662 | 0 | return FALSE; |
2663 | 0 | } |
2664 | 0 | if (G_UNLIKELY (!(pspec->flags & G_PARAM_WRITABLE))) |
2665 | 0 | { |
2666 | 0 | g_critical ("%s: property '%s' of object class '%s' is not writable", |
2667 | 0 | G_STRFUNC, pspec->name, G_OBJECT_TYPE_NAME (object)); |
2668 | 0 | return FALSE; |
2669 | 0 | } |
2670 | 0 | if (G_UNLIKELY (((pspec->flags & G_PARAM_CONSTRUCT_ONLY) && !object_in_construction (object)))) |
2671 | 0 | { |
2672 | 0 | g_critical ("%s: construct property \"%s\" for object '%s' can't be set after construction", |
2673 | 0 | G_STRFUNC, pspec->name, G_OBJECT_TYPE_NAME (object)); |
2674 | 0 | return FALSE; |
2675 | 0 | } |
2676 | 0 | return TRUE; |
2677 | 0 | } |
2678 | | |
2679 | | /** |
2680 | | * g_object_setv: (skip) |
2681 | | * @object: a #GObject |
2682 | | * @n_properties: the number of properties |
2683 | | * @names: (array length=n_properties): the names of each property to be set |
2684 | | * @values: (array length=n_properties): the values of each property to be set |
2685 | | * |
2686 | | * Sets @n_properties properties for an @object. |
2687 | | * Properties to be set will be taken from @values. All properties must be |
2688 | | * valid. Warnings will be emitted and undefined behaviour may result if invalid |
2689 | | * properties are passed in. |
2690 | | * |
2691 | | * Since: 2.54 |
2692 | | */ |
2693 | | void |
2694 | | g_object_setv (GObject *object, |
2695 | | guint n_properties, |
2696 | | const gchar *names[], |
2697 | | const GValue values[]) |
2698 | 0 | { |
2699 | 0 | guint i; |
2700 | 0 | GObjectNotifyQueue *nqueue = NULL; |
2701 | 0 | GParamSpec *pspec; |
2702 | 0 | GObjectClass *class; |
2703 | |
|
2704 | 0 | g_return_if_fail (G_IS_OBJECT (object)); |
2705 | | |
2706 | 0 | if (n_properties == 0) |
2707 | 0 | return; |
2708 | | |
2709 | 0 | g_object_ref (object); |
2710 | |
|
2711 | 0 | class = G_OBJECT_GET_CLASS (object); |
2712 | |
|
2713 | 0 | if (_g_object_has_notify_handler (object)) |
2714 | 0 | nqueue = g_object_notify_queue_freeze (object, FALSE); |
2715 | |
|
2716 | 0 | for (i = 0; i < n_properties; i++) |
2717 | 0 | { |
2718 | 0 | pspec = find_pspec (class, names[i]); |
2719 | |
|
2720 | 0 | if (!g_object_set_is_valid_property (object, pspec, names[i])) |
2721 | 0 | break; |
2722 | | |
2723 | 0 | object_set_property (object, pspec, &values[i], nqueue, TRUE); |
2724 | 0 | } |
2725 | |
|
2726 | 0 | if (nqueue) |
2727 | 0 | g_object_notify_queue_thaw (object, nqueue); |
2728 | |
|
2729 | 0 | g_object_unref (object); |
2730 | 0 | } |
2731 | | |
2732 | | /** |
2733 | | * g_object_set_valist: (skip) |
2734 | | * @object: a #GObject |
2735 | | * @first_property_name: name of the first property to set |
2736 | | * @var_args: value for the first property, followed optionally by more |
2737 | | * name/value pairs, followed by %NULL |
2738 | | * |
2739 | | * Sets properties on an object. |
2740 | | */ |
2741 | | void |
2742 | | g_object_set_valist (GObject *object, |
2743 | | const gchar *first_property_name, |
2744 | | va_list var_args) |
2745 | 0 | { |
2746 | 0 | GObjectNotifyQueue *nqueue = NULL; |
2747 | 0 | const gchar *name; |
2748 | 0 | GObjectClass *class; |
2749 | | |
2750 | 0 | g_return_if_fail (G_IS_OBJECT (object)); |
2751 | | |
2752 | 0 | g_object_ref (object); |
2753 | |
|
2754 | 0 | if (_g_object_has_notify_handler (object)) |
2755 | 0 | nqueue = g_object_notify_queue_freeze (object, FALSE); |
2756 | |
|
2757 | 0 | class = G_OBJECT_GET_CLASS (object); |
2758 | |
|
2759 | 0 | name = first_property_name; |
2760 | 0 | while (name) |
2761 | 0 | { |
2762 | 0 | GValue value = G_VALUE_INIT; |
2763 | 0 | GParamSpec *pspec; |
2764 | 0 | gchar *error = NULL; |
2765 | 0 | GTypeValueTable *vtab; |
2766 | | |
2767 | 0 | pspec = find_pspec (class, name); |
2768 | |
|
2769 | 0 | if (!g_object_set_is_valid_property (object, pspec, name)) |
2770 | 0 | break; |
2771 | | |
2772 | 0 | G_VALUE_COLLECT_INIT2 (&value, vtab, pspec->value_type, var_args, G_VALUE_NOCOPY_CONTENTS, &error); |
2773 | 0 | if (error) |
2774 | 0 | { |
2775 | 0 | g_critical ("%s: %s", G_STRFUNC, error); |
2776 | 0 | g_free (error); |
2777 | 0 | g_value_unset (&value); |
2778 | 0 | break; |
2779 | 0 | } |
2780 | | |
2781 | 0 | object_set_property (object, pspec, &value, nqueue, TRUE); |
2782 | | |
2783 | | /* We open-code g_value_unset() here to avoid the |
2784 | | * cost of looking up the GTypeValueTable again. |
2785 | | */ |
2786 | 0 | if (vtab->value_free) |
2787 | 0 | vtab->value_free (&value); |
2788 | |
|
2789 | 0 | name = va_arg (var_args, gchar*); |
2790 | 0 | } |
2791 | | |
2792 | 0 | if (nqueue) |
2793 | 0 | g_object_notify_queue_thaw (object, nqueue); |
2794 | |
|
2795 | 0 | g_object_unref (object); |
2796 | 0 | } |
2797 | | |
2798 | | static inline gboolean |
2799 | | g_object_get_is_valid_property (GObject *object, |
2800 | | GParamSpec *pspec, |
2801 | | const char *property_name) |
2802 | 0 | { |
2803 | 0 | if (G_UNLIKELY (pspec == NULL)) |
2804 | 0 | { |
2805 | 0 | g_critical ("%s: object class '%s' has no property named '%s'", |
2806 | 0 | G_STRFUNC, G_OBJECT_TYPE_NAME (object), property_name); |
2807 | 0 | return FALSE; |
2808 | 0 | } |
2809 | 0 | if (G_UNLIKELY (!(pspec->flags & G_PARAM_READABLE))) |
2810 | 0 | { |
2811 | 0 | g_critical ("%s: property '%s' of object class '%s' is not readable", |
2812 | 0 | G_STRFUNC, pspec->name, G_OBJECT_TYPE_NAME (object)); |
2813 | 0 | return FALSE; |
2814 | 0 | } |
2815 | 0 | return TRUE; |
2816 | 0 | } |
2817 | | |
2818 | | /** |
2819 | | * g_object_getv: |
2820 | | * @object: a #GObject |
2821 | | * @n_properties: the number of properties |
2822 | | * @names: (array length=n_properties): the names of each property to get |
2823 | | * @values: (array length=n_properties): the values of each property to get |
2824 | | * |
2825 | | * Gets @n_properties properties for an @object. |
2826 | | * Obtained properties will be set to @values. All properties must be valid. |
2827 | | * Warnings will be emitted and undefined behaviour may result if invalid |
2828 | | * properties are passed in. |
2829 | | * |
2830 | | * Since: 2.54 |
2831 | | */ |
2832 | | void |
2833 | | g_object_getv (GObject *object, |
2834 | | guint n_properties, |
2835 | | const gchar *names[], |
2836 | | GValue values[]) |
2837 | 0 | { |
2838 | 0 | guint i; |
2839 | 0 | GParamSpec *pspec; |
2840 | 0 | GObjectClass *class; |
2841 | |
|
2842 | 0 | g_return_if_fail (G_IS_OBJECT (object)); |
2843 | | |
2844 | 0 | if (n_properties == 0) |
2845 | 0 | return; |
2846 | | |
2847 | 0 | g_object_ref (object); |
2848 | |
|
2849 | 0 | class = G_OBJECT_GET_CLASS (object); |
2850 | |
|
2851 | 0 | memset (values, 0, n_properties * sizeof (GValue)); |
2852 | |
|
2853 | 0 | for (i = 0; i < n_properties; i++) |
2854 | 0 | { |
2855 | 0 | pspec = find_pspec (class, names[i]); |
2856 | |
|
2857 | 0 | if (!g_object_get_is_valid_property (object, pspec, names[i])) |
2858 | 0 | break; |
2859 | 0 | g_value_init (&values[i], pspec->value_type); |
2860 | 0 | object_get_property (object, pspec, &values[i]); |
2861 | 0 | } |
2862 | 0 | g_object_unref (object); |
2863 | 0 | } |
2864 | | |
2865 | | /** |
2866 | | * g_object_get_valist: (skip) |
2867 | | * @object: a #GObject |
2868 | | * @first_property_name: name of the first property to get |
2869 | | * @var_args: return location for the first property, followed optionally by more |
2870 | | * name/return location pairs, followed by %NULL |
2871 | | * |
2872 | | * Gets properties of an object. |
2873 | | * |
2874 | | * In general, a copy is made of the property contents and the caller |
2875 | | * is responsible for freeing the memory in the appropriate manner for |
2876 | | * the type, for instance by calling g_free() or g_object_unref(). |
2877 | | * |
2878 | | * See g_object_get(). |
2879 | | */ |
2880 | | void |
2881 | | g_object_get_valist (GObject *object, |
2882 | | const gchar *first_property_name, |
2883 | | va_list var_args) |
2884 | 0 | { |
2885 | 0 | const gchar *name; |
2886 | 0 | GObjectClass *class; |
2887 | | |
2888 | 0 | g_return_if_fail (G_IS_OBJECT (object)); |
2889 | | |
2890 | 0 | g_object_ref (object); |
2891 | |
|
2892 | 0 | class = G_OBJECT_GET_CLASS (object); |
2893 | |
|
2894 | 0 | name = first_property_name; |
2895 | |
|
2896 | 0 | while (name) |
2897 | 0 | { |
2898 | 0 | GValue value = G_VALUE_INIT; |
2899 | 0 | GParamSpec *pspec; |
2900 | 0 | gchar *error; |
2901 | |
|
2902 | 0 | pspec = find_pspec (class, name); |
2903 | |
|
2904 | 0 | if (!g_object_get_is_valid_property (object, pspec, name)) |
2905 | 0 | break; |
2906 | | |
2907 | 0 | g_value_init (&value, pspec->value_type); |
2908 | | |
2909 | 0 | object_get_property (object, pspec, &value); |
2910 | | |
2911 | 0 | G_VALUE_LCOPY (&value, var_args, 0, &error); |
2912 | 0 | if (error) |
2913 | 0 | { |
2914 | 0 | g_critical ("%s: %s", G_STRFUNC, error); |
2915 | 0 | g_free (error); |
2916 | 0 | g_value_unset (&value); |
2917 | 0 | break; |
2918 | 0 | } |
2919 | | |
2920 | 0 | g_value_unset (&value); |
2921 | | |
2922 | 0 | name = va_arg (var_args, gchar*); |
2923 | 0 | } |
2924 | | |
2925 | 0 | g_object_unref (object); |
2926 | 0 | } |
2927 | | |
2928 | | /** |
2929 | | * g_object_set: (skip) |
2930 | | * @object: (type GObject.Object): a #GObject |
2931 | | * @first_property_name: name of the first property to set |
2932 | | * @...: value for the first property, followed optionally by more |
2933 | | * name/value pairs, followed by %NULL |
2934 | | * |
2935 | | * Sets properties on an object. |
2936 | | * |
2937 | | * The same caveats about passing integer literals as varargs apply as with |
2938 | | * g_object_new(). In particular, any integer literals set as the values for |
2939 | | * properties of type #gint64 or #guint64 must be 64 bits wide, using the |
2940 | | * %G_GINT64_CONSTANT or %G_GUINT64_CONSTANT macros. |
2941 | | * |
2942 | | * Note that the "notify" signals are queued and only emitted (in |
2943 | | * reverse order) after all properties have been set. See |
2944 | | * g_object_freeze_notify(). |
2945 | | */ |
2946 | | void |
2947 | | g_object_set (gpointer _object, |
2948 | | const gchar *first_property_name, |
2949 | | ...) |
2950 | 0 | { |
2951 | 0 | GObject *object = _object; |
2952 | 0 | va_list var_args; |
2953 | | |
2954 | 0 | g_return_if_fail (G_IS_OBJECT (object)); |
2955 | | |
2956 | 0 | va_start (var_args, first_property_name); |
2957 | 0 | g_object_set_valist (object, first_property_name, var_args); |
2958 | 0 | va_end (var_args); |
2959 | 0 | } |
2960 | | |
2961 | | /** |
2962 | | * g_object_get: (skip) |
2963 | | * @object: (type GObject.Object): a #GObject |
2964 | | * @first_property_name: name of the first property to get |
2965 | | * @...: return location for the first property, followed optionally by more |
2966 | | * name/return location pairs, followed by %NULL |
2967 | | * |
2968 | | * Gets properties of an object. |
2969 | | * |
2970 | | * In general, a copy is made of the property contents and the caller |
2971 | | * is responsible for freeing the memory in the appropriate manner for |
2972 | | * the type, for instance by calling g_free() or g_object_unref(). |
2973 | | * |
2974 | | * Here is an example of using g_object_get() to get the contents |
2975 | | * of three properties: an integer, a string and an object: |
2976 | | * |[<!-- language="C" --> |
2977 | | * gint intval; |
2978 | | * guint64 uint64val; |
2979 | | * gchar *strval; |
2980 | | * GObject *objval; |
2981 | | * |
2982 | | * g_object_get (my_object, |
2983 | | * "int-property", &intval, |
2984 | | * "uint64-property", &uint64val, |
2985 | | * "str-property", &strval, |
2986 | | * "obj-property", &objval, |
2987 | | * NULL); |
2988 | | * |
2989 | | * // Do something with intval, uint64val, strval, objval |
2990 | | * |
2991 | | * g_free (strval); |
2992 | | * g_object_unref (objval); |
2993 | | * ]| |
2994 | | */ |
2995 | | void |
2996 | | g_object_get (gpointer _object, |
2997 | | const gchar *first_property_name, |
2998 | | ...) |
2999 | 0 | { |
3000 | 0 | GObject *object = _object; |
3001 | 0 | va_list var_args; |
3002 | | |
3003 | 0 | g_return_if_fail (G_IS_OBJECT (object)); |
3004 | | |
3005 | 0 | va_start (var_args, first_property_name); |
3006 | 0 | g_object_get_valist (object, first_property_name, var_args); |
3007 | 0 | va_end (var_args); |
3008 | 0 | } |
3009 | | |
3010 | | /** |
3011 | | * g_object_set_property: |
3012 | | * @object: a #GObject |
3013 | | * @property_name: the name of the property to set |
3014 | | * @value: the value |
3015 | | * |
3016 | | * Sets a property on an object. |
3017 | | */ |
3018 | | void |
3019 | | g_object_set_property (GObject *object, |
3020 | | const gchar *property_name, |
3021 | | const GValue *value) |
3022 | 0 | { |
3023 | 0 | g_object_setv (object, 1, &property_name, value); |
3024 | 0 | } |
3025 | | |
3026 | | /** |
3027 | | * g_object_get_property: |
3028 | | * @object: a #GObject |
3029 | | * @property_name: the name of the property to get |
3030 | | * @value: return location for the property value |
3031 | | * |
3032 | | * Gets a property of an object. |
3033 | | * |
3034 | | * The @value can be: |
3035 | | * |
3036 | | * - an empty #GValue initialized by %G_VALUE_INIT, which will be |
3037 | | * automatically initialized with the expected type of the property |
3038 | | * (since GLib 2.60) |
3039 | | * - a #GValue initialized with the expected type of the property |
3040 | | * - a #GValue initialized with a type to which the expected type |
3041 | | * of the property can be transformed |
3042 | | * |
3043 | | * In general, a copy is made of the property contents and the caller is |
3044 | | * responsible for freeing the memory by calling g_value_unset(). |
3045 | | * |
3046 | | * Note that g_object_get_property() is really intended for language |
3047 | | * bindings, g_object_get() is much more convenient for C programming. |
3048 | | */ |
3049 | | void |
3050 | | g_object_get_property (GObject *object, |
3051 | | const gchar *property_name, |
3052 | | GValue *value) |
3053 | 0 | { |
3054 | 0 | GParamSpec *pspec; |
3055 | | |
3056 | 0 | g_return_if_fail (G_IS_OBJECT (object)); |
3057 | 0 | g_return_if_fail (property_name != NULL); |
3058 | 0 | g_return_if_fail (value != NULL); |
3059 | | |
3060 | 0 | g_object_ref (object); |
3061 | | |
3062 | 0 | pspec = find_pspec (G_OBJECT_GET_CLASS (object), property_name); |
3063 | |
|
3064 | 0 | if (g_object_get_is_valid_property (object, pspec, property_name)) |
3065 | 0 | { |
3066 | 0 | GValue *prop_value, tmp_value = G_VALUE_INIT; |
3067 | | |
3068 | 0 | if (G_VALUE_TYPE (value) == G_TYPE_INVALID) |
3069 | 0 | { |
3070 | | /* zero-initialized value */ |
3071 | 0 | g_value_init (value, pspec->value_type); |
3072 | 0 | prop_value = value; |
3073 | 0 | } |
3074 | 0 | else if (G_VALUE_TYPE (value) == pspec->value_type) |
3075 | 0 | { |
3076 | | /* auto-conversion of the callers value type */ |
3077 | 0 | g_value_reset (value); |
3078 | 0 | prop_value = value; |
3079 | 0 | } |
3080 | 0 | else if (!g_value_type_transformable (pspec->value_type, G_VALUE_TYPE (value))) |
3081 | 0 | { |
3082 | 0 | g_critical ("%s: can't retrieve property '%s' of type '%s' as value of type '%s'", |
3083 | 0 | G_STRFUNC, pspec->name, |
3084 | 0 | g_type_name (pspec->value_type), |
3085 | 0 | G_VALUE_TYPE_NAME (value)); |
3086 | 0 | g_object_unref (object); |
3087 | 0 | return; |
3088 | 0 | } |
3089 | 0 | else |
3090 | 0 | { |
3091 | 0 | g_value_init (&tmp_value, pspec->value_type); |
3092 | 0 | prop_value = &tmp_value; |
3093 | 0 | } |
3094 | 0 | object_get_property (object, pspec, prop_value); |
3095 | 0 | if (prop_value != value) |
3096 | 0 | { |
3097 | 0 | g_value_transform (prop_value, value); |
3098 | 0 | g_value_unset (&tmp_value); |
3099 | 0 | } |
3100 | 0 | } |
3101 | | |
3102 | 0 | g_object_unref (object); |
3103 | 0 | } |
3104 | | |
3105 | | /** |
3106 | | * g_object_connect: (skip) |
3107 | | * @object: (type GObject.Object): a #GObject |
3108 | | * @signal_spec: the spec for the first signal |
3109 | | * @...: #GCallback for the first signal, followed by data for the |
3110 | | * first signal, followed optionally by more signal |
3111 | | * spec/callback/data triples, followed by %NULL |
3112 | | * |
3113 | | * A convenience function to connect multiple signals at once. |
3114 | | * |
3115 | | * The signal specs expected by this function have the form |
3116 | | * "modifier::signal_name", where modifier can be one of the following: |
3117 | | * - signal: equivalent to g_signal_connect_data (..., NULL, G_CONNECT_DEFAULT) |
3118 | | * - object-signal, object_signal: equivalent to g_signal_connect_object (..., G_CONNECT_DEFAULT) |
3119 | | * - swapped-signal, swapped_signal: equivalent to g_signal_connect_data (..., NULL, G_CONNECT_SWAPPED) |
3120 | | * - swapped_object_signal, swapped-object-signal: equivalent to g_signal_connect_object (..., G_CONNECT_SWAPPED) |
3121 | | * - signal_after, signal-after: equivalent to g_signal_connect_data (..., NULL, G_CONNECT_AFTER) |
3122 | | * - object_signal_after, object-signal-after: equivalent to g_signal_connect_object (..., G_CONNECT_AFTER) |
3123 | | * - swapped_signal_after, swapped-signal-after: equivalent to g_signal_connect_data (..., NULL, G_CONNECT_SWAPPED | G_CONNECT_AFTER) |
3124 | | * - swapped_object_signal_after, swapped-object-signal-after: equivalent to g_signal_connect_object (..., G_CONNECT_SWAPPED | G_CONNECT_AFTER) |
3125 | | * |
3126 | | * |[<!-- language="C" --> |
3127 | | * menu->toplevel = g_object_connect (g_object_new (GTK_TYPE_WINDOW, |
3128 | | * "type", GTK_WINDOW_POPUP, |
3129 | | * "child", menu, |
3130 | | * NULL), |
3131 | | * "signal::event", gtk_menu_window_event, menu, |
3132 | | * "signal::size_request", gtk_menu_window_size_request, menu, |
3133 | | * "signal::destroy", gtk_widget_destroyed, &menu->toplevel, |
3134 | | * NULL); |
3135 | | * ]| |
3136 | | * |
3137 | | * Returns: (transfer none) (type GObject.Object): @object |
3138 | | */ |
3139 | | gpointer |
3140 | | g_object_connect (gpointer _object, |
3141 | | const gchar *signal_spec, |
3142 | | ...) |
3143 | 0 | { |
3144 | 0 | GObject *object = _object; |
3145 | 0 | va_list var_args; |
3146 | |
|
3147 | 0 | g_return_val_if_fail (G_IS_OBJECT (object), NULL); |
3148 | 0 | g_return_val_if_fail (object->ref_count > 0, object); |
3149 | | |
3150 | 0 | va_start (var_args, signal_spec); |
3151 | 0 | while (signal_spec) |
3152 | 0 | { |
3153 | 0 | GCallback callback = va_arg (var_args, GCallback); |
3154 | 0 | gpointer data = va_arg (var_args, gpointer); |
3155 | |
|
3156 | 0 | if (strncmp (signal_spec, "signal::", 8) == 0) |
3157 | 0 | g_signal_connect_data (object, signal_spec + 8, |
3158 | 0 | callback, data, NULL, |
3159 | 0 | G_CONNECT_DEFAULT); |
3160 | 0 | else if (strncmp (signal_spec, "object_signal::", 15) == 0 || |
3161 | 0 | strncmp (signal_spec, "object-signal::", 15) == 0) |
3162 | 0 | g_signal_connect_object (object, signal_spec + 15, |
3163 | 0 | callback, data, |
3164 | 0 | G_CONNECT_DEFAULT); |
3165 | 0 | else if (strncmp (signal_spec, "swapped_signal::", 16) == 0 || |
3166 | 0 | strncmp (signal_spec, "swapped-signal::", 16) == 0) |
3167 | 0 | g_signal_connect_data (object, signal_spec + 16, |
3168 | 0 | callback, data, NULL, |
3169 | 0 | G_CONNECT_SWAPPED); |
3170 | 0 | else if (strncmp (signal_spec, "swapped_object_signal::", 23) == 0 || |
3171 | 0 | strncmp (signal_spec, "swapped-object-signal::", 23) == 0) |
3172 | 0 | g_signal_connect_object (object, signal_spec + 23, |
3173 | 0 | callback, data, |
3174 | 0 | G_CONNECT_SWAPPED); |
3175 | 0 | else if (strncmp (signal_spec, "signal_after::", 14) == 0 || |
3176 | 0 | strncmp (signal_spec, "signal-after::", 14) == 0) |
3177 | 0 | g_signal_connect_data (object, signal_spec + 14, |
3178 | 0 | callback, data, NULL, |
3179 | 0 | G_CONNECT_AFTER); |
3180 | 0 | else if (strncmp (signal_spec, "object_signal_after::", 21) == 0 || |
3181 | 0 | strncmp (signal_spec, "object-signal-after::", 21) == 0) |
3182 | 0 | g_signal_connect_object (object, signal_spec + 21, |
3183 | 0 | callback, data, |
3184 | 0 | G_CONNECT_AFTER); |
3185 | 0 | else if (strncmp (signal_spec, "swapped_signal_after::", 22) == 0 || |
3186 | 0 | strncmp (signal_spec, "swapped-signal-after::", 22) == 0) |
3187 | 0 | g_signal_connect_data (object, signal_spec + 22, |
3188 | 0 | callback, data, NULL, |
3189 | 0 | G_CONNECT_SWAPPED | G_CONNECT_AFTER); |
3190 | 0 | else if (strncmp (signal_spec, "swapped_object_signal_after::", 29) == 0 || |
3191 | 0 | strncmp (signal_spec, "swapped-object-signal-after::", 29) == 0) |
3192 | 0 | g_signal_connect_object (object, signal_spec + 29, |
3193 | 0 | callback, data, |
3194 | 0 | G_CONNECT_SWAPPED | G_CONNECT_AFTER); |
3195 | 0 | else |
3196 | 0 | { |
3197 | 0 | g_critical ("%s: invalid signal spec \"%s\"", G_STRFUNC, signal_spec); |
3198 | 0 | break; |
3199 | 0 | } |
3200 | 0 | signal_spec = va_arg (var_args, gchar*); |
3201 | 0 | } |
3202 | 0 | va_end (var_args); |
3203 | |
|
3204 | 0 | return object; |
3205 | 0 | } |
3206 | | |
3207 | | /** |
3208 | | * g_object_disconnect: (skip) |
3209 | | * @object: (type GObject.Object): a #GObject |
3210 | | * @signal_spec: the spec for the first signal |
3211 | | * @...: #GCallback for the first signal, followed by data for the first signal, |
3212 | | * followed optionally by more signal spec/callback/data triples, |
3213 | | * followed by %NULL |
3214 | | * |
3215 | | * A convenience function to disconnect multiple signals at once. |
3216 | | * |
3217 | | * The signal specs expected by this function have the form |
3218 | | * "any_signal", which means to disconnect any signal with matching |
3219 | | * callback and data, or "any_signal::signal_name", which only |
3220 | | * disconnects the signal named "signal_name". |
3221 | | */ |
3222 | | void |
3223 | | g_object_disconnect (gpointer _object, |
3224 | | const gchar *signal_spec, |
3225 | | ...) |
3226 | 0 | { |
3227 | 0 | GObject *object = _object; |
3228 | 0 | va_list var_args; |
3229 | |
|
3230 | 0 | g_return_if_fail (G_IS_OBJECT (object)); |
3231 | 0 | g_return_if_fail (object->ref_count > 0); |
3232 | | |
3233 | 0 | va_start (var_args, signal_spec); |
3234 | 0 | while (signal_spec) |
3235 | 0 | { |
3236 | 0 | GCallback callback = va_arg (var_args, GCallback); |
3237 | 0 | gpointer data = va_arg (var_args, gpointer); |
3238 | 0 | guint sid = 0, detail = 0, mask = 0; |
3239 | |
|
3240 | 0 | if (strncmp (signal_spec, "any_signal::", 12) == 0 || |
3241 | 0 | strncmp (signal_spec, "any-signal::", 12) == 0) |
3242 | 0 | { |
3243 | 0 | signal_spec += 12; |
3244 | 0 | mask = G_SIGNAL_MATCH_ID | G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA; |
3245 | 0 | } |
3246 | 0 | else if (strcmp (signal_spec, "any_signal") == 0 || |
3247 | 0 | strcmp (signal_spec, "any-signal") == 0) |
3248 | 0 | { |
3249 | 0 | signal_spec += 10; |
3250 | 0 | mask = G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA; |
3251 | 0 | } |
3252 | 0 | else |
3253 | 0 | { |
3254 | 0 | g_critical ("%s: invalid signal spec \"%s\"", G_STRFUNC, signal_spec); |
3255 | 0 | break; |
3256 | 0 | } |
3257 | | |
3258 | 0 | if ((mask & G_SIGNAL_MATCH_ID) && |
3259 | 0 | !g_signal_parse_name (signal_spec, G_OBJECT_TYPE (object), &sid, &detail, FALSE)) |
3260 | 0 | g_critical ("%s: invalid signal name \"%s\"", G_STRFUNC, signal_spec); |
3261 | 0 | else if (!g_signal_handlers_disconnect_matched (object, mask | (detail ? G_SIGNAL_MATCH_DETAIL : 0), |
3262 | 0 | sid, detail, |
3263 | 0 | NULL, (gpointer)callback, data)) |
3264 | 0 | g_critical ("%s: signal handler %p(%p) is not connected", G_STRFUNC, callback, data); |
3265 | 0 | signal_spec = va_arg (var_args, gchar*); |
3266 | 0 | } |
3267 | 0 | va_end (var_args); |
3268 | 0 | } |
3269 | | |
3270 | | typedef struct { |
3271 | | GObject *object; |
3272 | | guint n_weak_refs; |
3273 | | struct { |
3274 | | GWeakNotify notify; |
3275 | | gpointer data; |
3276 | | } weak_refs[1]; /* flexible array */ |
3277 | | } WeakRefStack; |
3278 | | |
3279 | | static void |
3280 | | weak_refs_notify (gpointer data) |
3281 | 0 | { |
3282 | 0 | WeakRefStack *wstack = data; |
3283 | 0 | guint i; |
3284 | |
|
3285 | 0 | for (i = 0; i < wstack->n_weak_refs; i++) |
3286 | 0 | wstack->weak_refs[i].notify (wstack->weak_refs[i].data, wstack->object); |
3287 | 0 | g_free (wstack); |
3288 | 0 | } |
3289 | | |
3290 | | /** |
3291 | | * g_object_weak_ref: (skip) |
3292 | | * @object: #GObject to reference weakly |
3293 | | * @notify: callback to invoke before the object is freed |
3294 | | * @data: extra data to pass to notify |
3295 | | * |
3296 | | * Adds a weak reference callback to an object. Weak references are |
3297 | | * used for notification when an object is disposed. They are called |
3298 | | * "weak references" because they allow you to safely hold a pointer |
3299 | | * to an object without calling g_object_ref() (g_object_ref() adds a |
3300 | | * strong reference, that is, forces the object to stay alive). |
3301 | | * |
3302 | | * Note that the weak references created by this method are not |
3303 | | * thread-safe: they cannot safely be used in one thread if the |
3304 | | * object's last g_object_unref() might happen in another thread. |
3305 | | * Use #GWeakRef if thread-safety is required. |
3306 | | */ |
3307 | | void |
3308 | | g_object_weak_ref (GObject *object, |
3309 | | GWeakNotify notify, |
3310 | | gpointer data) |
3311 | 0 | { |
3312 | 0 | WeakRefStack *wstack; |
3313 | 0 | guint i; |
3314 | | |
3315 | 0 | g_return_if_fail (G_IS_OBJECT (object)); |
3316 | 0 | g_return_if_fail (notify != NULL); |
3317 | 0 | g_return_if_fail (g_atomic_int_get (&object->ref_count) >= 1); |
3318 | | |
3319 | 0 | G_LOCK (weak_refs_mutex); |
3320 | 0 | wstack = g_datalist_id_remove_no_notify (&object->qdata, quark_weak_refs); |
3321 | 0 | if (wstack) |
3322 | 0 | { |
3323 | 0 | i = wstack->n_weak_refs++; |
3324 | 0 | wstack = g_realloc (wstack, sizeof (*wstack) + sizeof (wstack->weak_refs[0]) * i); |
3325 | 0 | } |
3326 | 0 | else |
3327 | 0 | { |
3328 | 0 | wstack = g_renew (WeakRefStack, NULL, 1); |
3329 | 0 | wstack->object = object; |
3330 | 0 | wstack->n_weak_refs = 1; |
3331 | 0 | i = 0; |
3332 | 0 | } |
3333 | 0 | wstack->weak_refs[i].notify = notify; |
3334 | 0 | wstack->weak_refs[i].data = data; |
3335 | 0 | g_datalist_id_set_data_full (&object->qdata, quark_weak_refs, wstack, weak_refs_notify); |
3336 | 0 | G_UNLOCK (weak_refs_mutex); |
3337 | 0 | } |
3338 | | |
3339 | | /** |
3340 | | * g_object_weak_unref: (skip) |
3341 | | * @object: #GObject to remove a weak reference from |
3342 | | * @notify: callback to search for |
3343 | | * @data: data to search for |
3344 | | * |
3345 | | * Removes a weak reference callback to an object. |
3346 | | */ |
3347 | | void |
3348 | | g_object_weak_unref (GObject *object, |
3349 | | GWeakNotify notify, |
3350 | | gpointer data) |
3351 | 0 | { |
3352 | 0 | WeakRefStack *wstack; |
3353 | 0 | gboolean found_one = FALSE; |
3354 | |
|
3355 | 0 | g_return_if_fail (G_IS_OBJECT (object)); |
3356 | 0 | g_return_if_fail (notify != NULL); |
3357 | | |
3358 | 0 | G_LOCK (weak_refs_mutex); |
3359 | 0 | wstack = g_datalist_id_get_data (&object->qdata, quark_weak_refs); |
3360 | 0 | if (wstack) |
3361 | 0 | { |
3362 | 0 | guint i; |
3363 | |
|
3364 | 0 | for (i = 0; i < wstack->n_weak_refs; i++) |
3365 | 0 | if (wstack->weak_refs[i].notify == notify && |
3366 | 0 | wstack->weak_refs[i].data == data) |
3367 | 0 | { |
3368 | 0 | found_one = TRUE; |
3369 | 0 | wstack->n_weak_refs -= 1; |
3370 | 0 | if (i != wstack->n_weak_refs) |
3371 | 0 | wstack->weak_refs[i] = wstack->weak_refs[wstack->n_weak_refs]; |
3372 | |
|
3373 | 0 | break; |
3374 | 0 | } |
3375 | 0 | } |
3376 | 0 | G_UNLOCK (weak_refs_mutex); |
3377 | 0 | if (!found_one) |
3378 | 0 | g_critical ("%s: couldn't find weak ref %p(%p)", G_STRFUNC, notify, data); |
3379 | 0 | } |
3380 | | |
3381 | | /** |
3382 | | * g_object_add_weak_pointer: (skip) |
3383 | | * @object: The object that should be weak referenced. |
3384 | | * @weak_pointer_location: (inout) (not optional): The memory address |
3385 | | * of a pointer. |
3386 | | * |
3387 | | * Adds a weak reference from weak_pointer to @object to indicate that |
3388 | | * the pointer located at @weak_pointer_location is only valid during |
3389 | | * the lifetime of @object. When the @object is finalized, |
3390 | | * @weak_pointer will be set to %NULL. |
3391 | | * |
3392 | | * Note that as with g_object_weak_ref(), the weak references created by |
3393 | | * this method are not thread-safe: they cannot safely be used in one |
3394 | | * thread if the object's last g_object_unref() might happen in another |
3395 | | * thread. Use #GWeakRef if thread-safety is required. |
3396 | | */ |
3397 | | void |
3398 | | g_object_add_weak_pointer (GObject *object, |
3399 | | gpointer *weak_pointer_location) |
3400 | 0 | { |
3401 | 0 | g_return_if_fail (G_IS_OBJECT (object)); |
3402 | 0 | g_return_if_fail (weak_pointer_location != NULL); |
3403 | | |
3404 | 0 | g_object_weak_ref (object, |
3405 | 0 | (GWeakNotify) g_nullify_pointer, |
3406 | 0 | weak_pointer_location); |
3407 | 0 | } |
3408 | | |
3409 | | /** |
3410 | | * g_object_remove_weak_pointer: (skip) |
3411 | | * @object: The object that is weak referenced. |
3412 | | * @weak_pointer_location: (inout) (not optional): The memory address |
3413 | | * of a pointer. |
3414 | | * |
3415 | | * Removes a weak reference from @object that was previously added |
3416 | | * using g_object_add_weak_pointer(). The @weak_pointer_location has |
3417 | | * to match the one used with g_object_add_weak_pointer(). |
3418 | | */ |
3419 | | void |
3420 | | g_object_remove_weak_pointer (GObject *object, |
3421 | | gpointer *weak_pointer_location) |
3422 | 0 | { |
3423 | 0 | g_return_if_fail (G_IS_OBJECT (object)); |
3424 | 0 | g_return_if_fail (weak_pointer_location != NULL); |
3425 | | |
3426 | 0 | g_object_weak_unref (object, |
3427 | 0 | (GWeakNotify) g_nullify_pointer, |
3428 | 0 | weak_pointer_location); |
3429 | 0 | } |
3430 | | |
3431 | | static guint |
3432 | | object_floating_flag_handler (GObject *object, |
3433 | | gint job) |
3434 | 0 | { |
3435 | 0 | switch (job) |
3436 | 0 | { |
3437 | 0 | gpointer oldvalue; |
3438 | 0 | case +1: /* force floating if possible */ |
3439 | 0 | do |
3440 | 0 | oldvalue = g_atomic_pointer_get (&object->qdata); |
3441 | 0 | while (!g_atomic_pointer_compare_and_exchange ((void**) &object->qdata, oldvalue, |
3442 | 0 | (gpointer) ((gsize) oldvalue | OBJECT_FLOATING_FLAG))); |
3443 | 0 | return (gsize) oldvalue & OBJECT_FLOATING_FLAG; |
3444 | 0 | case -1: /* sink if possible */ |
3445 | 0 | do |
3446 | 0 | oldvalue = g_atomic_pointer_get (&object->qdata); |
3447 | 0 | while (!g_atomic_pointer_compare_and_exchange ((void**) &object->qdata, oldvalue, |
3448 | 0 | (gpointer) ((gsize) oldvalue & ~(gsize) OBJECT_FLOATING_FLAG))); |
3449 | 0 | return (gsize) oldvalue & OBJECT_FLOATING_FLAG; |
3450 | 0 | default: /* check floating */ |
3451 | 0 | return 0 != ((gsize) g_atomic_pointer_get (&object->qdata) & OBJECT_FLOATING_FLAG); |
3452 | 0 | } |
3453 | 0 | } |
3454 | | |
3455 | | /** |
3456 | | * g_object_is_floating: |
3457 | | * @object: (type GObject.Object): a #GObject |
3458 | | * |
3459 | | * Checks whether @object has a [floating][floating-ref] reference. |
3460 | | * |
3461 | | * Since: 2.10 |
3462 | | * |
3463 | | * Returns: %TRUE if @object has a floating reference |
3464 | | */ |
3465 | | gboolean |
3466 | | g_object_is_floating (gpointer _object) |
3467 | 0 | { |
3468 | 0 | GObject *object = _object; |
3469 | 0 | g_return_val_if_fail (G_IS_OBJECT (object), FALSE); |
3470 | 0 | return floating_flag_handler (object, 0); |
3471 | 0 | } |
3472 | | |
3473 | | /** |
3474 | | * g_object_ref_sink: |
3475 | | * @object: (type GObject.Object): a #GObject |
3476 | | * |
3477 | | * Increase the reference count of @object, and possibly remove the |
3478 | | * [floating][floating-ref] reference, if @object has a floating reference. |
3479 | | * |
3480 | | * In other words, if the object is floating, then this call "assumes |
3481 | | * ownership" of the floating reference, converting it to a normal |
3482 | | * reference by clearing the floating flag while leaving the reference |
3483 | | * count unchanged. If the object is not floating, then this call |
3484 | | * adds a new normal reference increasing the reference count by one. |
3485 | | * |
3486 | | * Since GLib 2.56, the type of @object will be propagated to the return type |
3487 | | * under the same conditions as for g_object_ref(). |
3488 | | * |
3489 | | * Since: 2.10 |
3490 | | * |
3491 | | * Returns: (type GObject.Object) (transfer none): @object |
3492 | | */ |
3493 | | gpointer |
3494 | | (g_object_ref_sink) (gpointer _object) |
3495 | 0 | { |
3496 | 0 | GObject *object = _object; |
3497 | 0 | gboolean was_floating; |
3498 | 0 | g_return_val_if_fail (G_IS_OBJECT (object), object); |
3499 | 0 | g_return_val_if_fail (g_atomic_int_get (&object->ref_count) >= 1, object); |
3500 | 0 | g_object_ref (object); |
3501 | 0 | was_floating = floating_flag_handler (object, -1); |
3502 | 0 | if (was_floating) |
3503 | 0 | g_object_unref (object); |
3504 | 0 | return object; |
3505 | 0 | } |
3506 | | |
3507 | | /** |
3508 | | * g_object_take_ref: (skip) |
3509 | | * @object: (type GObject.Object): a #GObject |
3510 | | * |
3511 | | * If @object is floating, sink it. Otherwise, do nothing. |
3512 | | * |
3513 | | * In other words, this function will convert a floating reference (if |
3514 | | * present) into a full reference. |
3515 | | * |
3516 | | * Typically you want to use g_object_ref_sink() in order to |
3517 | | * automatically do the correct thing with respect to floating or |
3518 | | * non-floating references, but there is one specific scenario where |
3519 | | * this function is helpful. |
3520 | | * |
3521 | | * The situation where this function is helpful is when creating an API |
3522 | | * that allows the user to provide a callback function that returns a |
3523 | | * GObject. We certainly want to allow the user the flexibility to |
3524 | | * return a non-floating reference from this callback (for the case |
3525 | | * where the object that is being returned already exists). |
3526 | | * |
3527 | | * At the same time, the API style of some popular GObject-based |
3528 | | * libraries (such as Gtk) make it likely that for newly-created GObject |
3529 | | * instances, the user can be saved some typing if they are allowed to |
3530 | | * return a floating reference. |
3531 | | * |
3532 | | * Using this function on the return value of the user's callback allows |
3533 | | * the user to do whichever is more convenient for them. The caller will |
3534 | | * alway receives exactly one full reference to the value: either the |
3535 | | * one that was returned in the first place, or a floating reference |
3536 | | * that has been converted to a full reference. |
3537 | | * |
3538 | | * This function has an odd interaction when combined with |
3539 | | * g_object_ref_sink() running at the same time in another thread on |
3540 | | * the same #GObject instance. If g_object_ref_sink() runs first then |
3541 | | * the result will be that the floating reference is converted to a hard |
3542 | | * reference. If g_object_take_ref() runs first then the result will be |
3543 | | * that the floating reference is converted to a hard reference and an |
3544 | | * additional reference on top of that one is added. It is best to avoid |
3545 | | * this situation. |
3546 | | * |
3547 | | * Since: 2.70 |
3548 | | * |
3549 | | * Returns: (type GObject.Object) (transfer full): @object |
3550 | | */ |
3551 | | gpointer |
3552 | | g_object_take_ref (gpointer _object) |
3553 | 0 | { |
3554 | 0 | GObject *object = _object; |
3555 | 0 | g_return_val_if_fail (G_IS_OBJECT (object), object); |
3556 | 0 | g_return_val_if_fail (g_atomic_int_get (&object->ref_count) >= 1, object); |
3557 | | |
3558 | 0 | floating_flag_handler (object, -1); |
3559 | |
|
3560 | 0 | return object; |
3561 | 0 | } |
3562 | | |
3563 | | /** |
3564 | | * g_object_force_floating: |
3565 | | * @object: a #GObject |
3566 | | * |
3567 | | * This function is intended for #GObject implementations to re-enforce |
3568 | | * a [floating][floating-ref] object reference. Doing this is seldom |
3569 | | * required: all #GInitiallyUnowneds are created with a floating reference |
3570 | | * which usually just needs to be sunken by calling g_object_ref_sink(). |
3571 | | * |
3572 | | * Since: 2.10 |
3573 | | */ |
3574 | | void |
3575 | | g_object_force_floating (GObject *object) |
3576 | 0 | { |
3577 | 0 | g_return_if_fail (G_IS_OBJECT (object)); |
3578 | 0 | g_return_if_fail (g_atomic_int_get (&object->ref_count) >= 1); |
3579 | | |
3580 | 0 | floating_flag_handler (object, +1); |
3581 | 0 | } |
3582 | | |
3583 | | typedef struct { |
3584 | | GObject *object; |
3585 | | guint n_toggle_refs; |
3586 | | struct { |
3587 | | GToggleNotify notify; |
3588 | | gpointer data; |
3589 | | } toggle_refs[1]; /* flexible array */ |
3590 | | } ToggleRefStack; |
3591 | | |
3592 | | static void |
3593 | | toggle_refs_notify (GObject *object, |
3594 | | gboolean is_last_ref) |
3595 | 0 | { |
3596 | 0 | ToggleRefStack tstack, *tstackptr; |
3597 | |
|
3598 | 0 | G_LOCK (toggle_refs_mutex); |
3599 | | /* If another thread removed the toggle reference on the object, while |
3600 | | * we were waiting here, there's nothing to notify. |
3601 | | * So let's check again if the object has toggle reference and in case return. |
3602 | | */ |
3603 | 0 | if (!OBJECT_HAS_TOGGLE_REF (object)) |
3604 | 0 | { |
3605 | 0 | G_UNLOCK (toggle_refs_mutex); |
3606 | 0 | return; |
3607 | 0 | } |
3608 | | |
3609 | 0 | tstackptr = g_datalist_id_get_data (&object->qdata, quark_toggle_refs); |
3610 | 0 | tstack = *tstackptr; |
3611 | 0 | G_UNLOCK (toggle_refs_mutex); |
3612 | | |
3613 | | /* Reentrancy here is not as tricky as it seems, because a toggle reference |
3614 | | * will only be notified when there is exactly one of them. |
3615 | | */ |
3616 | 0 | g_assert (tstack.n_toggle_refs == 1); |
3617 | 0 | tstack.toggle_refs[0].notify (tstack.toggle_refs[0].data, tstack.object, is_last_ref); |
3618 | 0 | } |
3619 | | |
3620 | | /** |
3621 | | * g_object_add_toggle_ref: (skip) |
3622 | | * @object: a #GObject |
3623 | | * @notify: a function to call when this reference is the |
3624 | | * last reference to the object, or is no longer |
3625 | | * the last reference. |
3626 | | * @data: data to pass to @notify |
3627 | | * |
3628 | | * Increases the reference count of the object by one and sets a |
3629 | | * callback to be called when all other references to the object are |
3630 | | * dropped, or when this is already the last reference to the object |
3631 | | * and another reference is established. |
3632 | | * |
3633 | | * This functionality is intended for binding @object to a proxy |
3634 | | * object managed by another memory manager. This is done with two |
3635 | | * paired references: the strong reference added by |
3636 | | * g_object_add_toggle_ref() and a reverse reference to the proxy |
3637 | | * object which is either a strong reference or weak reference. |
3638 | | * |
3639 | | * The setup is that when there are no other references to @object, |
3640 | | * only a weak reference is held in the reverse direction from @object |
3641 | | * to the proxy object, but when there are other references held to |
3642 | | * @object, a strong reference is held. The @notify callback is called |
3643 | | * when the reference from @object to the proxy object should be |
3644 | | * "toggled" from strong to weak (@is_last_ref true) or weak to strong |
3645 | | * (@is_last_ref false). |
3646 | | * |
3647 | | * Since a (normal) reference must be held to the object before |
3648 | | * calling g_object_add_toggle_ref(), the initial state of the reverse |
3649 | | * link is always strong. |
3650 | | * |
3651 | | * Multiple toggle references may be added to the same gobject, |
3652 | | * however if there are multiple toggle references to an object, none |
3653 | | * of them will ever be notified until all but one are removed. For |
3654 | | * this reason, you should only ever use a toggle reference if there |
3655 | | * is important state in the proxy object. |
3656 | | * |
3657 | | * Since: 2.8 |
3658 | | */ |
3659 | | void |
3660 | | g_object_add_toggle_ref (GObject *object, |
3661 | | GToggleNotify notify, |
3662 | | gpointer data) |
3663 | 0 | { |
3664 | 0 | ToggleRefStack *tstack; |
3665 | 0 | guint i; |
3666 | | |
3667 | 0 | g_return_if_fail (G_IS_OBJECT (object)); |
3668 | 0 | g_return_if_fail (notify != NULL); |
3669 | 0 | g_return_if_fail (g_atomic_int_get (&object->ref_count) >= 1); |
3670 | | |
3671 | 0 | g_object_ref (object); |
3672 | |
|
3673 | 0 | G_LOCK (toggle_refs_mutex); |
3674 | 0 | tstack = g_datalist_id_remove_no_notify (&object->qdata, quark_toggle_refs); |
3675 | 0 | if (tstack) |
3676 | 0 | { |
3677 | 0 | i = tstack->n_toggle_refs++; |
3678 | | /* allocate i = tstate->n_toggle_refs - 1 positions beyond the 1 declared |
3679 | | * in tstate->toggle_refs */ |
3680 | 0 | tstack = g_realloc (tstack, sizeof (*tstack) + sizeof (tstack->toggle_refs[0]) * i); |
3681 | 0 | } |
3682 | 0 | else |
3683 | 0 | { |
3684 | 0 | tstack = g_renew (ToggleRefStack, NULL, 1); |
3685 | 0 | tstack->object = object; |
3686 | 0 | tstack->n_toggle_refs = 1; |
3687 | 0 | i = 0; |
3688 | 0 | } |
3689 | | |
3690 | | /* Set a flag for fast lookup after adding the first toggle reference */ |
3691 | 0 | if (tstack->n_toggle_refs == 1) |
3692 | 0 | g_datalist_set_flags (&object->qdata, OBJECT_HAS_TOGGLE_REF_FLAG); |
3693 | | |
3694 | 0 | tstack->toggle_refs[i].notify = notify; |
3695 | 0 | tstack->toggle_refs[i].data = data; |
3696 | 0 | g_datalist_id_set_data_full (&object->qdata, quark_toggle_refs, tstack, |
3697 | 0 | (GDestroyNotify)g_free); |
3698 | 0 | G_UNLOCK (toggle_refs_mutex); |
3699 | 0 | } |
3700 | | |
3701 | | /** |
3702 | | * g_object_remove_toggle_ref: (skip) |
3703 | | * @object: a #GObject |
3704 | | * @notify: a function to call when this reference is the |
3705 | | * last reference to the object, or is no longer |
3706 | | * the last reference. |
3707 | | * @data: (nullable): data to pass to @notify, or %NULL to |
3708 | | * match any toggle refs with the @notify argument. |
3709 | | * |
3710 | | * Removes a reference added with g_object_add_toggle_ref(). The |
3711 | | * reference count of the object is decreased by one. |
3712 | | * |
3713 | | * Since: 2.8 |
3714 | | */ |
3715 | | void |
3716 | | g_object_remove_toggle_ref (GObject *object, |
3717 | | GToggleNotify notify, |
3718 | | gpointer data) |
3719 | 0 | { |
3720 | 0 | ToggleRefStack *tstack; |
3721 | 0 | gboolean found_one = FALSE; |
3722 | |
|
3723 | 0 | g_return_if_fail (G_IS_OBJECT (object)); |
3724 | 0 | g_return_if_fail (notify != NULL); |
3725 | | |
3726 | 0 | G_LOCK (toggle_refs_mutex); |
3727 | 0 | tstack = g_datalist_id_get_data (&object->qdata, quark_toggle_refs); |
3728 | 0 | if (tstack) |
3729 | 0 | { |
3730 | 0 | guint i; |
3731 | |
|
3732 | 0 | for (i = 0; i < tstack->n_toggle_refs; i++) |
3733 | 0 | if (tstack->toggle_refs[i].notify == notify && |
3734 | 0 | (tstack->toggle_refs[i].data == data || data == NULL)) |
3735 | 0 | { |
3736 | 0 | found_one = TRUE; |
3737 | 0 | tstack->n_toggle_refs -= 1; |
3738 | 0 | if (i != tstack->n_toggle_refs) |
3739 | 0 | tstack->toggle_refs[i] = tstack->toggle_refs[tstack->n_toggle_refs]; |
3740 | |
|
3741 | 0 | if (tstack->n_toggle_refs == 0) |
3742 | 0 | g_datalist_unset_flags (&object->qdata, OBJECT_HAS_TOGGLE_REF_FLAG); |
3743 | |
|
3744 | 0 | break; |
3745 | 0 | } |
3746 | 0 | } |
3747 | 0 | G_UNLOCK (toggle_refs_mutex); |
3748 | |
|
3749 | 0 | if (found_one) |
3750 | 0 | g_object_unref (object); |
3751 | 0 | else |
3752 | 0 | g_critical ("%s: couldn't find toggle ref %p(%p)", G_STRFUNC, notify, data); |
3753 | 0 | } |
3754 | | |
3755 | | /** |
3756 | | * g_object_ref: |
3757 | | * @object: (type GObject.Object): a #GObject |
3758 | | * |
3759 | | * Increases the reference count of @object. |
3760 | | * |
3761 | | * Since GLib 2.56, if `GLIB_VERSION_MAX_ALLOWED` is 2.56 or greater, the type |
3762 | | * of @object will be propagated to the return type (using the GCC typeof() |
3763 | | * extension), so any casting the caller needs to do on the return type must be |
3764 | | * explicit. |
3765 | | * |
3766 | | * Returns: (type GObject.Object) (transfer none): the same @object |
3767 | | */ |
3768 | | gpointer |
3769 | | (g_object_ref) (gpointer _object) |
3770 | 0 | { |
3771 | 0 | GObject *object = _object; |
3772 | 0 | gint old_val; |
3773 | 0 | gboolean object_already_finalized; |
3774 | |
|
3775 | 0 | g_return_val_if_fail (G_IS_OBJECT (object), NULL); |
3776 | | |
3777 | 0 | old_val = g_atomic_int_add (&object->ref_count, 1); |
3778 | 0 | object_already_finalized = (old_val <= 0); |
3779 | 0 | g_return_val_if_fail (!object_already_finalized, NULL); |
3780 | | |
3781 | 0 | if (old_val == 1 && OBJECT_HAS_TOGGLE_REF (object)) |
3782 | 0 | toggle_refs_notify (object, FALSE); |
3783 | |
|
3784 | 0 | TRACE (GOBJECT_OBJECT_REF(object,G_TYPE_FROM_INSTANCE(object),old_val)); |
3785 | |
|
3786 | 0 | return object; |
3787 | 0 | } |
3788 | | |
3789 | | /** |
3790 | | * g_object_unref: |
3791 | | * @object: (type GObject.Object): a #GObject |
3792 | | * |
3793 | | * Decreases the reference count of @object. When its reference count |
3794 | | * drops to 0, the object is finalized (i.e. its memory is freed). |
3795 | | * |
3796 | | * If the pointer to the #GObject may be reused in future (for example, if it is |
3797 | | * an instance variable of another object), it is recommended to clear the |
3798 | | * pointer to %NULL rather than retain a dangling pointer to a potentially |
3799 | | * invalid #GObject instance. Use g_clear_object() for this. |
3800 | | */ |
3801 | | void |
3802 | | g_object_unref (gpointer _object) |
3803 | 14.1k | { |
3804 | 14.1k | GObject *object = _object; |
3805 | 14.1k | gint old_ref; |
3806 | | |
3807 | 14.1k | g_return_if_fail (G_IS_OBJECT (object)); |
3808 | | |
3809 | | /* here we want to atomically do: if (ref_count>1) { ref_count--; return; } */ |
3810 | 14.1k | old_ref = g_atomic_int_get (&object->ref_count); |
3811 | 14.1k | retry_atomic_decrement1: |
3812 | 14.1k | while (old_ref > 1) |
3813 | 0 | { |
3814 | | /* valid if last 2 refs are owned by this call to unref and the toggle_ref */ |
3815 | |
|
3816 | 0 | if (!g_atomic_int_compare_and_exchange_full ((int *)&object->ref_count, |
3817 | 0 | old_ref, old_ref - 1, |
3818 | 0 | &old_ref)) |
3819 | 0 | continue; |
3820 | | |
3821 | 0 | TRACE (GOBJECT_OBJECT_UNREF(object,G_TYPE_FROM_INSTANCE(object),old_ref)); |
3822 | | |
3823 | | /* if we went from 2->1 we need to notify toggle refs if any */ |
3824 | 0 | if (old_ref == 2 && OBJECT_HAS_TOGGLE_REF (object)) |
3825 | 0 | { |
3826 | | /* The last ref being held in this case is owned by the toggle_ref */ |
3827 | 0 | toggle_refs_notify (object, TRUE); |
3828 | 0 | } |
3829 | |
|
3830 | 0 | return; |
3831 | 0 | } |
3832 | | |
3833 | 14.1k | { |
3834 | 14.1k | GSList **weak_locations; |
3835 | 14.1k | GObjectNotifyQueue *nqueue; |
3836 | | |
3837 | | /* The only way that this object can live at this point is if |
3838 | | * there are outstanding weak references already established |
3839 | | * before we got here. |
3840 | | * |
3841 | | * If there were not already weak references then no more can be |
3842 | | * established at this time, because the other thread would have |
3843 | | * to hold a strong ref in order to call |
3844 | | * g_object_add_weak_pointer() and then we wouldn't be here. |
3845 | | * |
3846 | | * Other GWeakRef's (weak locations) instead may still be added |
3847 | | * before the object is finalized, but in such case we'll unset |
3848 | | * them as part of the qdata removal. |
3849 | | */ |
3850 | 14.1k | weak_locations = g_datalist_id_get_data (&object->qdata, quark_weak_locations); |
3851 | | |
3852 | 14.1k | if (weak_locations != NULL) |
3853 | 0 | { |
3854 | 0 | g_rw_lock_writer_lock (&weak_locations_lock); |
3855 | | |
3856 | | /* It is possible that one of the weak references beat us to |
3857 | | * the lock. Make sure the refcount is still what we expected |
3858 | | * it to be. |
3859 | | */ |
3860 | 0 | old_ref = g_atomic_int_get (&object->ref_count); |
3861 | 0 | if (old_ref != 1) |
3862 | 0 | { |
3863 | 0 | g_rw_lock_writer_unlock (&weak_locations_lock); |
3864 | 0 | goto retry_atomic_decrement1; |
3865 | 0 | } |
3866 | | |
3867 | | /* We got the lock first, so the object will definitely die |
3868 | | * now. Clear out all the weak references, if they're still set. |
3869 | | */ |
3870 | 0 | weak_locations = g_datalist_id_remove_no_notify (&object->qdata, |
3871 | 0 | quark_weak_locations); |
3872 | 0 | g_clear_pointer (&weak_locations, weak_locations_free_unlocked); |
3873 | |
|
3874 | 0 | g_rw_lock_writer_unlock (&weak_locations_lock); |
3875 | 0 | } |
3876 | | |
3877 | | /* freeze the notification queue, so we don't accidentally emit |
3878 | | * notifications during dispose() and finalize(). |
3879 | | * |
3880 | | * The notification queue stays frozen unless the instance acquires |
3881 | | * a reference during dispose(), in which case we thaw it and |
3882 | | * dispatch all the notifications. If the instance gets through |
3883 | | * to finalize(), the notification queue gets automatically |
3884 | | * drained when g_object_finalize() is reached and |
3885 | | * the qdata is cleared. |
3886 | | */ |
3887 | 14.1k | nqueue = g_object_notify_queue_freeze (object, FALSE); |
3888 | | |
3889 | | /* we are about to remove the last reference */ |
3890 | 14.1k | TRACE (GOBJECT_OBJECT_DISPOSE(object,G_TYPE_FROM_INSTANCE(object), 1)); |
3891 | 14.1k | G_OBJECT_GET_CLASS (object)->dispose (object); |
3892 | 14.1k | TRACE (GOBJECT_OBJECT_DISPOSE_END(object,G_TYPE_FROM_INSTANCE(object), 1)); |
3893 | | |
3894 | | /* may have been re-referenced meanwhile */ |
3895 | 14.1k | old_ref = g_atomic_int_get ((int *)&object->ref_count); |
3896 | | |
3897 | 14.1k | while (old_ref > 1) |
3898 | 0 | { |
3899 | | /* valid if last 2 refs are owned by this call to unref and the toggle_ref */ |
3900 | |
|
3901 | 0 | if (!g_atomic_int_compare_and_exchange_full ((int *)&object->ref_count, |
3902 | 0 | old_ref, old_ref - 1, |
3903 | 0 | &old_ref)) |
3904 | 0 | continue; |
3905 | | |
3906 | 0 | TRACE (GOBJECT_OBJECT_UNREF (object, G_TYPE_FROM_INSTANCE (object), old_ref)); |
3907 | | |
3908 | | /* emit all notifications that have been queued during dispose() */ |
3909 | 0 | g_object_notify_queue_thaw (object, nqueue); |
3910 | | |
3911 | | /* if we went from 2->1 we need to notify toggle refs if any */ |
3912 | 0 | if (old_ref == 2 && OBJECT_HAS_TOGGLE_REF (object) && |
3913 | 0 | g_atomic_int_get ((int *)&object->ref_count) == 1) |
3914 | 0 | { |
3915 | | /* The last ref being held in this case is owned by the toggle_ref */ |
3916 | 0 | toggle_refs_notify (object, TRUE); |
3917 | 0 | } |
3918 | |
|
3919 | 0 | return; |
3920 | 0 | } |
3921 | | |
3922 | | /* we are still in the process of taking away the last ref */ |
3923 | 14.1k | g_datalist_id_set_data (&object->qdata, quark_closure_array, NULL); |
3924 | 14.1k | g_signal_handlers_destroy (object); |
3925 | 14.1k | g_datalist_id_set_data (&object->qdata, quark_weak_refs, NULL); |
3926 | 14.1k | g_datalist_id_set_data (&object->qdata, quark_weak_locations, NULL); |
3927 | | |
3928 | | /* decrement the last reference */ |
3929 | 14.1k | old_ref = g_atomic_int_add (&object->ref_count, -1); |
3930 | 14.1k | g_return_if_fail (old_ref > 0); |
3931 | | |
3932 | 14.1k | TRACE (GOBJECT_OBJECT_UNREF(object,G_TYPE_FROM_INSTANCE(object),old_ref)); |
3933 | | |
3934 | | /* may have been re-referenced meanwhile */ |
3935 | 14.1k | if (G_LIKELY (old_ref == 1)) |
3936 | 14.1k | { |
3937 | 14.1k | TRACE (GOBJECT_OBJECT_FINALIZE(object,G_TYPE_FROM_INSTANCE(object))); |
3938 | 14.1k | G_OBJECT_GET_CLASS (object)->finalize (object); |
3939 | 14.1k | TRACE (GOBJECT_OBJECT_FINALIZE_END(object,G_TYPE_FROM_INSTANCE(object))); |
3940 | | |
3941 | 14.1k | GOBJECT_IF_DEBUG (OBJECTS, |
3942 | 14.1k | { |
3943 | 14.1k | gboolean was_present; |
3944 | | |
3945 | | /* catch objects not chaining finalize handlers */ |
3946 | 14.1k | G_LOCK (debug_objects); |
3947 | 14.1k | was_present = g_hash_table_remove (debug_objects_ht, object); |
3948 | 14.1k | G_UNLOCK (debug_objects); |
3949 | | |
3950 | 14.1k | if (was_present) |
3951 | 14.1k | g_critical ("Object %p of type %s not finalized correctly.", |
3952 | 14.1k | object, G_OBJECT_TYPE_NAME (object)); |
3953 | 14.1k | }); |
3954 | 14.1k | g_type_free_instance ((GTypeInstance*) object); |
3955 | 14.1k | } |
3956 | 0 | else |
3957 | 0 | { |
3958 | | /* The instance acquired a reference between dispose() and |
3959 | | * finalize(), so we need to thaw the notification queue |
3960 | | */ |
3961 | 0 | g_object_notify_queue_thaw (object, nqueue); |
3962 | 0 | } |
3963 | 14.1k | } |
3964 | 14.1k | } |
3965 | | |
3966 | | /** |
3967 | | * g_clear_object: (skip) |
3968 | | * @object_ptr: a pointer to a #GObject reference |
3969 | | * |
3970 | | * Clears a reference to a #GObject. |
3971 | | * |
3972 | | * @object_ptr must not be %NULL. |
3973 | | * |
3974 | | * If the reference is %NULL then this function does nothing. |
3975 | | * Otherwise, the reference count of the object is decreased and the |
3976 | | * pointer is set to %NULL. |
3977 | | * |
3978 | | * A macro is also included that allows this function to be used without |
3979 | | * pointer casts. |
3980 | | * |
3981 | | * Since: 2.28 |
3982 | | **/ |
3983 | | #undef g_clear_object |
3984 | | void |
3985 | | g_clear_object (GObject **object_ptr) |
3986 | 0 | { |
3987 | 0 | g_clear_pointer (object_ptr, g_object_unref); |
3988 | 0 | } |
3989 | | |
3990 | | /** |
3991 | | * g_object_get_qdata: |
3992 | | * @object: The GObject to get a stored user data pointer from |
3993 | | * @quark: A #GQuark, naming the user data pointer |
3994 | | * |
3995 | | * This function gets back user data pointers stored via |
3996 | | * g_object_set_qdata(). |
3997 | | * |
3998 | | * Returns: (transfer none) (nullable): The user data pointer set, or %NULL |
3999 | | */ |
4000 | | gpointer |
4001 | | g_object_get_qdata (GObject *object, |
4002 | | GQuark quark) |
4003 | 0 | { |
4004 | 0 | g_return_val_if_fail (G_IS_OBJECT (object), NULL); |
4005 | | |
4006 | 0 | return quark ? g_datalist_id_get_data (&object->qdata, quark) : NULL; |
4007 | 0 | } |
4008 | | |
4009 | | /** |
4010 | | * g_object_set_qdata: (skip) |
4011 | | * @object: The GObject to set store a user data pointer |
4012 | | * @quark: A #GQuark, naming the user data pointer |
4013 | | * @data: (nullable): An opaque user data pointer |
4014 | | * |
4015 | | * This sets an opaque, named pointer on an object. |
4016 | | * The name is specified through a #GQuark (retrieved e.g. via |
4017 | | * g_quark_from_static_string()), and the pointer |
4018 | | * can be gotten back from the @object with g_object_get_qdata() |
4019 | | * until the @object is finalized. |
4020 | | * Setting a previously set user data pointer, overrides (frees) |
4021 | | * the old pointer set, using #NULL as pointer essentially |
4022 | | * removes the data stored. |
4023 | | */ |
4024 | | void |
4025 | | g_object_set_qdata (GObject *object, |
4026 | | GQuark quark, |
4027 | | gpointer data) |
4028 | 0 | { |
4029 | 0 | g_return_if_fail (G_IS_OBJECT (object)); |
4030 | 0 | g_return_if_fail (quark > 0); |
4031 | | |
4032 | 0 | g_datalist_id_set_data (&object->qdata, quark, data); |
4033 | 0 | } |
4034 | | |
4035 | | /** |
4036 | | * g_object_dup_qdata: (skip) |
4037 | | * @object: the #GObject to store user data on |
4038 | | * @quark: a #GQuark, naming the user data pointer |
4039 | | * @dup_func: (nullable): function to dup the value |
4040 | | * @user_data: (nullable): passed as user_data to @dup_func |
4041 | | * |
4042 | | * This is a variant of g_object_get_qdata() which returns |
4043 | | * a 'duplicate' of the value. @dup_func defines the |
4044 | | * meaning of 'duplicate' in this context, it could e.g. |
4045 | | * take a reference on a ref-counted object. |
4046 | | * |
4047 | | * If the @quark is not set on the object then @dup_func |
4048 | | * will be called with a %NULL argument. |
4049 | | * |
4050 | | * Note that @dup_func is called while user data of @object |
4051 | | * is locked. |
4052 | | * |
4053 | | * This function can be useful to avoid races when multiple |
4054 | | * threads are using object data on the same key on the same |
4055 | | * object. |
4056 | | * |
4057 | | * Returns: the result of calling @dup_func on the value |
4058 | | * associated with @quark on @object, or %NULL if not set. |
4059 | | * If @dup_func is %NULL, the value is returned |
4060 | | * unmodified. |
4061 | | * |
4062 | | * Since: 2.34 |
4063 | | */ |
4064 | | gpointer |
4065 | | g_object_dup_qdata (GObject *object, |
4066 | | GQuark quark, |
4067 | | GDuplicateFunc dup_func, |
4068 | | gpointer user_data) |
4069 | 0 | { |
4070 | 0 | g_return_val_if_fail (G_IS_OBJECT (object), NULL); |
4071 | 0 | g_return_val_if_fail (quark > 0, NULL); |
4072 | | |
4073 | 0 | return g_datalist_id_dup_data (&object->qdata, quark, dup_func, user_data); |
4074 | 0 | } |
4075 | | |
4076 | | /** |
4077 | | * g_object_replace_qdata: (skip) |
4078 | | * @object: the #GObject to store user data on |
4079 | | * @quark: a #GQuark, naming the user data pointer |
4080 | | * @oldval: (nullable): the old value to compare against |
4081 | | * @newval: (nullable): the new value |
4082 | | * @destroy: (nullable): a destroy notify for the new value |
4083 | | * @old_destroy: (out) (optional): destroy notify for the existing value |
4084 | | * |
4085 | | * Compares the user data for the key @quark on @object with |
4086 | | * @oldval, and if they are the same, replaces @oldval with |
4087 | | * @newval. |
4088 | | * |
4089 | | * This is like a typical atomic compare-and-exchange |
4090 | | * operation, for user data on an object. |
4091 | | * |
4092 | | * If the previous value was replaced then ownership of the |
4093 | | * old value (@oldval) is passed to the caller, including |
4094 | | * the registered destroy notify for it (passed out in @old_destroy). |
4095 | | * It’s up to the caller to free this as needed, which may |
4096 | | * or may not include using @old_destroy as sometimes replacement |
4097 | | * should not destroy the object in the normal way. |
4098 | | * |
4099 | | * Returns: %TRUE if the existing value for @quark was replaced |
4100 | | * by @newval, %FALSE otherwise. |
4101 | | * |
4102 | | * Since: 2.34 |
4103 | | */ |
4104 | | gboolean |
4105 | | g_object_replace_qdata (GObject *object, |
4106 | | GQuark quark, |
4107 | | gpointer oldval, |
4108 | | gpointer newval, |
4109 | | GDestroyNotify destroy, |
4110 | | GDestroyNotify *old_destroy) |
4111 | 0 | { |
4112 | 0 | g_return_val_if_fail (G_IS_OBJECT (object), FALSE); |
4113 | 0 | g_return_val_if_fail (quark > 0, FALSE); |
4114 | | |
4115 | 0 | return g_datalist_id_replace_data (&object->qdata, quark, |
4116 | 0 | oldval, newval, destroy, |
4117 | 0 | old_destroy); |
4118 | 0 | } |
4119 | | |
4120 | | /** |
4121 | | * g_object_set_qdata_full: (skip) |
4122 | | * @object: The GObject to set store a user data pointer |
4123 | | * @quark: A #GQuark, naming the user data pointer |
4124 | | * @data: (nullable): An opaque user data pointer |
4125 | | * @destroy: (nullable): Function to invoke with @data as argument, when @data |
4126 | | * needs to be freed |
4127 | | * |
4128 | | * This function works like g_object_set_qdata(), but in addition, |
4129 | | * a void (*destroy) (gpointer) function may be specified which is |
4130 | | * called with @data as argument when the @object is finalized, or |
4131 | | * the data is being overwritten by a call to g_object_set_qdata() |
4132 | | * with the same @quark. |
4133 | | */ |
4134 | | void |
4135 | | g_object_set_qdata_full (GObject *object, |
4136 | | GQuark quark, |
4137 | | gpointer data, |
4138 | | GDestroyNotify destroy) |
4139 | 0 | { |
4140 | 0 | g_return_if_fail (G_IS_OBJECT (object)); |
4141 | 0 | g_return_if_fail (quark > 0); |
4142 | | |
4143 | 0 | g_datalist_id_set_data_full (&object->qdata, quark, data, |
4144 | 0 | data ? destroy : (GDestroyNotify) NULL); |
4145 | 0 | } |
4146 | | |
4147 | | /** |
4148 | | * g_object_steal_qdata: |
4149 | | * @object: The GObject to get a stored user data pointer from |
4150 | | * @quark: A #GQuark, naming the user data pointer |
4151 | | * |
4152 | | * This function gets back user data pointers stored via |
4153 | | * g_object_set_qdata() and removes the @data from object |
4154 | | * without invoking its destroy() function (if any was |
4155 | | * set). |
4156 | | * Usually, calling this function is only required to update |
4157 | | * user data pointers with a destroy notifier, for example: |
4158 | | * |[<!-- language="C" --> |
4159 | | * void |
4160 | | * object_add_to_user_list (GObject *object, |
4161 | | * const gchar *new_string) |
4162 | | * { |
4163 | | * // the quark, naming the object data |
4164 | | * GQuark quark_string_list = g_quark_from_static_string ("my-string-list"); |
4165 | | * // retrieve the old string list |
4166 | | * GList *list = g_object_steal_qdata (object, quark_string_list); |
4167 | | * |
4168 | | * // prepend new string |
4169 | | * list = g_list_prepend (list, g_strdup (new_string)); |
4170 | | * // this changed 'list', so we need to set it again |
4171 | | * g_object_set_qdata_full (object, quark_string_list, list, free_string_list); |
4172 | | * } |
4173 | | * static void |
4174 | | * free_string_list (gpointer data) |
4175 | | * { |
4176 | | * GList *node, *list = data; |
4177 | | * |
4178 | | * for (node = list; node; node = node->next) |
4179 | | * g_free (node->data); |
4180 | | * g_list_free (list); |
4181 | | * } |
4182 | | * ]| |
4183 | | * Using g_object_get_qdata() in the above example, instead of |
4184 | | * g_object_steal_qdata() would have left the destroy function set, |
4185 | | * and thus the partial string list would have been freed upon |
4186 | | * g_object_set_qdata_full(). |
4187 | | * |
4188 | | * Returns: (transfer full) (nullable): The user data pointer set, or %NULL |
4189 | | */ |
4190 | | gpointer |
4191 | | g_object_steal_qdata (GObject *object, |
4192 | | GQuark quark) |
4193 | 0 | { |
4194 | 0 | g_return_val_if_fail (G_IS_OBJECT (object), NULL); |
4195 | 0 | g_return_val_if_fail (quark > 0, NULL); |
4196 | | |
4197 | 0 | return g_datalist_id_remove_no_notify (&object->qdata, quark); |
4198 | 0 | } |
4199 | | |
4200 | | /** |
4201 | | * g_object_get_data: |
4202 | | * @object: #GObject containing the associations |
4203 | | * @key: name of the key for that association |
4204 | | * |
4205 | | * Gets a named field from the objects table of associations (see g_object_set_data()). |
4206 | | * |
4207 | | * Returns: (transfer none) (nullable): the data if found, |
4208 | | * or %NULL if no such data exists. |
4209 | | */ |
4210 | | gpointer |
4211 | | g_object_get_data (GObject *object, |
4212 | | const gchar *key) |
4213 | 0 | { |
4214 | 0 | g_return_val_if_fail (G_IS_OBJECT (object), NULL); |
4215 | 0 | g_return_val_if_fail (key != NULL, NULL); |
4216 | | |
4217 | 0 | return g_datalist_get_data (&object->qdata, key); |
4218 | 0 | } |
4219 | | |
4220 | | /** |
4221 | | * g_object_set_data: |
4222 | | * @object: #GObject containing the associations. |
4223 | | * @key: name of the key |
4224 | | * @data: (nullable): data to associate with that key |
4225 | | * |
4226 | | * Each object carries around a table of associations from |
4227 | | * strings to pointers. This function lets you set an association. |
4228 | | * |
4229 | | * If the object already had an association with that name, |
4230 | | * the old association will be destroyed. |
4231 | | * |
4232 | | * Internally, the @key is converted to a #GQuark using g_quark_from_string(). |
4233 | | * This means a copy of @key is kept permanently (even after @object has been |
4234 | | * finalized) — so it is recommended to only use a small, bounded set of values |
4235 | | * for @key in your program, to avoid the #GQuark storage growing unbounded. |
4236 | | */ |
4237 | | void |
4238 | | g_object_set_data (GObject *object, |
4239 | | const gchar *key, |
4240 | | gpointer data) |
4241 | 0 | { |
4242 | 0 | g_return_if_fail (G_IS_OBJECT (object)); |
4243 | 0 | g_return_if_fail (key != NULL); |
4244 | | |
4245 | 0 | g_datalist_id_set_data (&object->qdata, g_quark_from_string (key), data); |
4246 | 0 | } |
4247 | | |
4248 | | /** |
4249 | | * g_object_dup_data: (skip) |
4250 | | * @object: the #GObject to store user data on |
4251 | | * @key: a string, naming the user data pointer |
4252 | | * @dup_func: (nullable): function to dup the value |
4253 | | * @user_data: (nullable): passed as user_data to @dup_func |
4254 | | * |
4255 | | * This is a variant of g_object_get_data() which returns |
4256 | | * a 'duplicate' of the value. @dup_func defines the |
4257 | | * meaning of 'duplicate' in this context, it could e.g. |
4258 | | * take a reference on a ref-counted object. |
4259 | | * |
4260 | | * If the @key is not set on the object then @dup_func |
4261 | | * will be called with a %NULL argument. |
4262 | | * |
4263 | | * Note that @dup_func is called while user data of @object |
4264 | | * is locked. |
4265 | | * |
4266 | | * This function can be useful to avoid races when multiple |
4267 | | * threads are using object data on the same key on the same |
4268 | | * object. |
4269 | | * |
4270 | | * Returns: the result of calling @dup_func on the value |
4271 | | * associated with @key on @object, or %NULL if not set. |
4272 | | * If @dup_func is %NULL, the value is returned |
4273 | | * unmodified. |
4274 | | * |
4275 | | * Since: 2.34 |
4276 | | */ |
4277 | | gpointer |
4278 | | g_object_dup_data (GObject *object, |
4279 | | const gchar *key, |
4280 | | GDuplicateFunc dup_func, |
4281 | | gpointer user_data) |
4282 | 0 | { |
4283 | 0 | g_return_val_if_fail (G_IS_OBJECT (object), NULL); |
4284 | 0 | g_return_val_if_fail (key != NULL, NULL); |
4285 | | |
4286 | 0 | return g_datalist_id_dup_data (&object->qdata, |
4287 | 0 | g_quark_from_string (key), |
4288 | 0 | dup_func, user_data); |
4289 | 0 | } |
4290 | | |
4291 | | /** |
4292 | | * g_object_replace_data: (skip) |
4293 | | * @object: the #GObject to store user data on |
4294 | | * @key: a string, naming the user data pointer |
4295 | | * @oldval: (nullable): the old value to compare against |
4296 | | * @newval: (nullable): the new value |
4297 | | * @destroy: (nullable): a destroy notify for the new value |
4298 | | * @old_destroy: (out) (optional): destroy notify for the existing value |
4299 | | * |
4300 | | * Compares the user data for the key @key on @object with |
4301 | | * @oldval, and if they are the same, replaces @oldval with |
4302 | | * @newval. |
4303 | | * |
4304 | | * This is like a typical atomic compare-and-exchange |
4305 | | * operation, for user data on an object. |
4306 | | * |
4307 | | * If the previous value was replaced then ownership of the |
4308 | | * old value (@oldval) is passed to the caller, including |
4309 | | * the registered destroy notify for it (passed out in @old_destroy). |
4310 | | * It’s up to the caller to free this as needed, which may |
4311 | | * or may not include using @old_destroy as sometimes replacement |
4312 | | * should not destroy the object in the normal way. |
4313 | | * |
4314 | | * See g_object_set_data() for guidance on using a small, bounded set of values |
4315 | | * for @key. |
4316 | | * |
4317 | | * Returns: %TRUE if the existing value for @key was replaced |
4318 | | * by @newval, %FALSE otherwise. |
4319 | | * |
4320 | | * Since: 2.34 |
4321 | | */ |
4322 | | gboolean |
4323 | | g_object_replace_data (GObject *object, |
4324 | | const gchar *key, |
4325 | | gpointer oldval, |
4326 | | gpointer newval, |
4327 | | GDestroyNotify destroy, |
4328 | | GDestroyNotify *old_destroy) |
4329 | 0 | { |
4330 | 0 | g_return_val_if_fail (G_IS_OBJECT (object), FALSE); |
4331 | 0 | g_return_val_if_fail (key != NULL, FALSE); |
4332 | | |
4333 | 0 | return g_datalist_id_replace_data (&object->qdata, |
4334 | 0 | g_quark_from_string (key), |
4335 | 0 | oldval, newval, destroy, |
4336 | 0 | old_destroy); |
4337 | 0 | } |
4338 | | |
4339 | | /** |
4340 | | * g_object_set_data_full: (skip) |
4341 | | * @object: #GObject containing the associations |
4342 | | * @key: name of the key |
4343 | | * @data: (nullable): data to associate with that key |
4344 | | * @destroy: (nullable): function to call when the association is destroyed |
4345 | | * |
4346 | | * Like g_object_set_data() except it adds notification |
4347 | | * for when the association is destroyed, either by setting it |
4348 | | * to a different value or when the object is destroyed. |
4349 | | * |
4350 | | * Note that the @destroy callback is not called if @data is %NULL. |
4351 | | */ |
4352 | | void |
4353 | | g_object_set_data_full (GObject *object, |
4354 | | const gchar *key, |
4355 | | gpointer data, |
4356 | | GDestroyNotify destroy) |
4357 | 0 | { |
4358 | 0 | g_return_if_fail (G_IS_OBJECT (object)); |
4359 | 0 | g_return_if_fail (key != NULL); |
4360 | | |
4361 | 0 | g_datalist_id_set_data_full (&object->qdata, g_quark_from_string (key), data, |
4362 | 0 | data ? destroy : (GDestroyNotify) NULL); |
4363 | 0 | } |
4364 | | |
4365 | | /** |
4366 | | * g_object_steal_data: |
4367 | | * @object: #GObject containing the associations |
4368 | | * @key: name of the key |
4369 | | * |
4370 | | * Remove a specified datum from the object's data associations, |
4371 | | * without invoking the association's destroy handler. |
4372 | | * |
4373 | | * Returns: (transfer full) (nullable): the data if found, or %NULL |
4374 | | * if no such data exists. |
4375 | | */ |
4376 | | gpointer |
4377 | | g_object_steal_data (GObject *object, |
4378 | | const gchar *key) |
4379 | 0 | { |
4380 | 0 | GQuark quark; |
4381 | |
|
4382 | 0 | g_return_val_if_fail (G_IS_OBJECT (object), NULL); |
4383 | 0 | g_return_val_if_fail (key != NULL, NULL); |
4384 | | |
4385 | 0 | quark = g_quark_try_string (key); |
4386 | |
|
4387 | 0 | return quark ? g_datalist_id_remove_no_notify (&object->qdata, quark) : NULL; |
4388 | 0 | } |
4389 | | |
4390 | | static void |
4391 | | g_value_object_init (GValue *value) |
4392 | 0 | { |
4393 | 0 | value->data[0].v_pointer = NULL; |
4394 | 0 | } |
4395 | | |
4396 | | static void |
4397 | | g_value_object_free_value (GValue *value) |
4398 | 0 | { |
4399 | 0 | g_clear_object ((GObject**) &value->data[0].v_pointer); |
4400 | 0 | } |
4401 | | |
4402 | | static void |
4403 | | g_value_object_copy_value (const GValue *src_value, |
4404 | | GValue *dest_value) |
4405 | 0 | { |
4406 | 0 | g_set_object ((GObject**) &dest_value->data[0].v_pointer, |
4407 | 0 | src_value->data[0].v_pointer); |
4408 | 0 | } |
4409 | | |
4410 | | static void |
4411 | | g_value_object_transform_value (const GValue *src_value, |
4412 | | GValue *dest_value) |
4413 | 0 | { |
4414 | 0 | if (src_value->data[0].v_pointer && g_type_is_a (G_OBJECT_TYPE (src_value->data[0].v_pointer), G_VALUE_TYPE (dest_value))) |
4415 | 0 | dest_value->data[0].v_pointer = g_object_ref (src_value->data[0].v_pointer); |
4416 | 0 | else |
4417 | 0 | dest_value->data[0].v_pointer = NULL; |
4418 | 0 | } |
4419 | | |
4420 | | static gpointer |
4421 | | g_value_object_peek_pointer (const GValue *value) |
4422 | 0 | { |
4423 | 0 | return value->data[0].v_pointer; |
4424 | 0 | } |
4425 | | |
4426 | | static gchar* |
4427 | | g_value_object_collect_value (GValue *value, |
4428 | | guint n_collect_values, |
4429 | | GTypeCValue *collect_values, |
4430 | | guint collect_flags) |
4431 | 0 | { |
4432 | 0 | if (collect_values[0].v_pointer) |
4433 | 0 | { |
4434 | 0 | GObject *object = collect_values[0].v_pointer; |
4435 | | |
4436 | 0 | if (object->g_type_instance.g_class == NULL) |
4437 | 0 | return g_strconcat ("invalid unclassed object pointer for value type '", |
4438 | 0 | G_VALUE_TYPE_NAME (value), |
4439 | 0 | "'", |
4440 | 0 | NULL); |
4441 | 0 | else if (!g_value_type_compatible (G_OBJECT_TYPE (object), G_VALUE_TYPE (value))) |
4442 | 0 | return g_strconcat ("invalid object type '", |
4443 | 0 | G_OBJECT_TYPE_NAME (object), |
4444 | 0 | "' for value type '", |
4445 | 0 | G_VALUE_TYPE_NAME (value), |
4446 | 0 | "'", |
4447 | 0 | NULL); |
4448 | | /* never honour G_VALUE_NOCOPY_CONTENTS for ref-counted types */ |
4449 | 0 | value->data[0].v_pointer = g_object_ref (object); |
4450 | 0 | } |
4451 | 0 | else |
4452 | 0 | value->data[0].v_pointer = NULL; |
4453 | | |
4454 | 0 | return NULL; |
4455 | 0 | } |
4456 | | |
4457 | | static gchar* |
4458 | | g_value_object_lcopy_value (const GValue *value, |
4459 | | guint n_collect_values, |
4460 | | GTypeCValue *collect_values, |
4461 | | guint collect_flags) |
4462 | 0 | { |
4463 | 0 | GObject **object_p = collect_values[0].v_pointer; |
4464 | |
|
4465 | 0 | g_return_val_if_fail (object_p != NULL, g_strdup_printf ("value location for '%s' passed as NULL", G_VALUE_TYPE_NAME (value))); |
4466 | | |
4467 | 0 | if (!value->data[0].v_pointer) |
4468 | 0 | *object_p = NULL; |
4469 | 0 | else if (collect_flags & G_VALUE_NOCOPY_CONTENTS) |
4470 | 0 | *object_p = value->data[0].v_pointer; |
4471 | 0 | else |
4472 | 0 | *object_p = g_object_ref (value->data[0].v_pointer); |
4473 | | |
4474 | 0 | return NULL; |
4475 | 0 | } |
4476 | | |
4477 | | /** |
4478 | | * g_value_set_object: |
4479 | | * @value: a valid #GValue of %G_TYPE_OBJECT derived type |
4480 | | * @v_object: (type GObject.Object) (nullable): object value to be set |
4481 | | * |
4482 | | * Set the contents of a %G_TYPE_OBJECT derived #GValue to @v_object. |
4483 | | * |
4484 | | * g_value_set_object() increases the reference count of @v_object |
4485 | | * (the #GValue holds a reference to @v_object). If you do not wish |
4486 | | * to increase the reference count of the object (i.e. you wish to |
4487 | | * pass your current reference to the #GValue because you no longer |
4488 | | * need it), use g_value_take_object() instead. |
4489 | | * |
4490 | | * It is important that your #GValue holds a reference to @v_object (either its |
4491 | | * own, or one it has taken) to ensure that the object won't be destroyed while |
4492 | | * the #GValue still exists). |
4493 | | */ |
4494 | | void |
4495 | | g_value_set_object (GValue *value, |
4496 | | gpointer v_object) |
4497 | 0 | { |
4498 | 0 | GObject *old; |
4499 | |
|
4500 | 0 | g_return_if_fail (G_VALUE_HOLDS_OBJECT (value)); |
4501 | | |
4502 | 0 | if G_UNLIKELY (value->data[0].v_pointer == v_object) |
4503 | 0 | return; |
4504 | | |
4505 | 0 | old = g_steal_pointer (&value->data[0].v_pointer); |
4506 | |
|
4507 | 0 | if (v_object) |
4508 | 0 | { |
4509 | 0 | g_return_if_fail (G_IS_OBJECT (v_object)); |
4510 | 0 | g_return_if_fail (g_value_type_compatible (G_OBJECT_TYPE (v_object), G_VALUE_TYPE (value))); |
4511 | | |
4512 | 0 | value->data[0].v_pointer = g_object_ref (v_object); |
4513 | 0 | } |
4514 | | |
4515 | 0 | g_clear_object (&old); |
4516 | 0 | } |
4517 | | |
4518 | | /** |
4519 | | * g_value_set_object_take_ownership: (skip) |
4520 | | * @value: a valid #GValue of %G_TYPE_OBJECT derived type |
4521 | | * @v_object: (nullable): object value to be set |
4522 | | * |
4523 | | * This is an internal function introduced mainly for C marshallers. |
4524 | | * |
4525 | | * Deprecated: 2.4: Use g_value_take_object() instead. |
4526 | | */ |
4527 | | void |
4528 | | g_value_set_object_take_ownership (GValue *value, |
4529 | | gpointer v_object) |
4530 | 0 | { |
4531 | 0 | g_value_take_object (value, v_object); |
4532 | 0 | } |
4533 | | |
4534 | | /** |
4535 | | * g_value_take_object: (skip) |
4536 | | * @value: a valid #GValue of %G_TYPE_OBJECT derived type |
4537 | | * @v_object: (nullable): object value to be set |
4538 | | * |
4539 | | * Sets the contents of a %G_TYPE_OBJECT derived #GValue to @v_object |
4540 | | * and takes over the ownership of the caller’s reference to @v_object; |
4541 | | * the caller doesn’t have to unref it any more (i.e. the reference |
4542 | | * count of the object is not increased). |
4543 | | * |
4544 | | * If you want the #GValue to hold its own reference to @v_object, use |
4545 | | * g_value_set_object() instead. |
4546 | | * |
4547 | | * Since: 2.4 |
4548 | | */ |
4549 | | void |
4550 | | g_value_take_object (GValue *value, |
4551 | | gpointer v_object) |
4552 | 0 | { |
4553 | 0 | g_return_if_fail (G_VALUE_HOLDS_OBJECT (value)); |
4554 | | |
4555 | 0 | g_clear_object ((GObject **) &value->data[0].v_pointer); |
4556 | |
|
4557 | 0 | if (v_object) |
4558 | 0 | { |
4559 | 0 | g_return_if_fail (G_IS_OBJECT (v_object)); |
4560 | 0 | g_return_if_fail (g_value_type_compatible (G_OBJECT_TYPE (v_object), G_VALUE_TYPE (value))); |
4561 | | |
4562 | 0 | value->data[0].v_pointer = g_steal_pointer (&v_object); |
4563 | 0 | } |
4564 | 0 | } |
4565 | | |
4566 | | /** |
4567 | | * g_value_get_object: |
4568 | | * @value: a valid #GValue of %G_TYPE_OBJECT derived type |
4569 | | * |
4570 | | * Get the contents of a %G_TYPE_OBJECT derived #GValue. |
4571 | | * |
4572 | | * Returns: (type GObject.Object) (transfer none) (nullable): object contents of @value |
4573 | | */ |
4574 | | gpointer |
4575 | | g_value_get_object (const GValue *value) |
4576 | 0 | { |
4577 | 0 | g_return_val_if_fail (G_VALUE_HOLDS_OBJECT (value), NULL); |
4578 | | |
4579 | 0 | return value->data[0].v_pointer; |
4580 | 0 | } |
4581 | | |
4582 | | /** |
4583 | | * g_value_dup_object: |
4584 | | * @value: a valid #GValue whose type is derived from %G_TYPE_OBJECT |
4585 | | * |
4586 | | * Get the contents of a %G_TYPE_OBJECT derived #GValue, increasing |
4587 | | * its reference count. If the contents of the #GValue are %NULL, then |
4588 | | * %NULL will be returned. |
4589 | | * |
4590 | | * Returns: (type GObject.Object) (transfer full) (nullable): object content of @value, |
4591 | | * should be unreferenced when no longer needed. |
4592 | | */ |
4593 | | gpointer |
4594 | | g_value_dup_object (const GValue *value) |
4595 | 0 | { |
4596 | 0 | g_return_val_if_fail (G_VALUE_HOLDS_OBJECT (value), NULL); |
4597 | | |
4598 | 0 | return value->data[0].v_pointer ? g_object_ref (value->data[0].v_pointer) : NULL; |
4599 | 0 | } |
4600 | | |
4601 | | /** |
4602 | | * g_signal_connect_object: (skip) |
4603 | | * @instance: (type GObject.TypeInstance): the instance to connect to. |
4604 | | * @detailed_signal: a string of the form "signal-name::detail". |
4605 | | * @c_handler: the #GCallback to connect. |
4606 | | * @gobject: (type GObject.Object) (nullable): the object to pass as data |
4607 | | * to @c_handler. |
4608 | | * @connect_flags: a combination of #GConnectFlags. |
4609 | | * |
4610 | | * This is similar to g_signal_connect_data(), but uses a closure which |
4611 | | * ensures that the @gobject stays alive during the call to @c_handler |
4612 | | * by temporarily adding a reference count to @gobject. |
4613 | | * |
4614 | | * When the @gobject is destroyed the signal handler will be automatically |
4615 | | * disconnected. Note that this is not currently threadsafe (ie: |
4616 | | * emitting a signal while @gobject is being destroyed in another thread |
4617 | | * is not safe). |
4618 | | * |
4619 | | * Returns: the handler id. |
4620 | | */ |
4621 | | gulong |
4622 | | g_signal_connect_object (gpointer instance, |
4623 | | const gchar *detailed_signal, |
4624 | | GCallback c_handler, |
4625 | | gpointer gobject, |
4626 | | GConnectFlags connect_flags) |
4627 | 0 | { |
4628 | 0 | g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0); |
4629 | 0 | g_return_val_if_fail (detailed_signal != NULL, 0); |
4630 | 0 | g_return_val_if_fail (c_handler != NULL, 0); |
4631 | | |
4632 | 0 | if (gobject) |
4633 | 0 | { |
4634 | 0 | GClosure *closure; |
4635 | |
|
4636 | 0 | g_return_val_if_fail (G_IS_OBJECT (gobject), 0); |
4637 | | |
4638 | 0 | closure = ((connect_flags & G_CONNECT_SWAPPED) ? g_cclosure_new_object_swap : g_cclosure_new_object) (c_handler, gobject); |
4639 | |
|
4640 | 0 | return g_signal_connect_closure (instance, detailed_signal, closure, connect_flags & G_CONNECT_AFTER); |
4641 | 0 | } |
4642 | 0 | else |
4643 | 0 | return g_signal_connect_data (instance, detailed_signal, c_handler, NULL, NULL, connect_flags); |
4644 | 0 | } |
4645 | | |
4646 | | typedef struct { |
4647 | | GObject *object; |
4648 | | guint n_closures; |
4649 | | GClosure *closures[1]; /* flexible array */ |
4650 | | } CArray; |
4651 | | /* don't change this structure without supplying an accessor for |
4652 | | * watched closures, e.g.: |
4653 | | * GSList* g_object_list_watched_closures (GObject *object) |
4654 | | * { |
4655 | | * CArray *carray; |
4656 | | * g_return_val_if_fail (G_IS_OBJECT (object), NULL); |
4657 | | * carray = g_object_get_data (object, "GObject-closure-array"); |
4658 | | * if (carray) |
4659 | | * { |
4660 | | * GSList *slist = NULL; |
4661 | | * guint i; |
4662 | | * for (i = 0; i < carray->n_closures; i++) |
4663 | | * slist = g_slist_prepend (slist, carray->closures[i]); |
4664 | | * return slist; |
4665 | | * } |
4666 | | * return NULL; |
4667 | | * } |
4668 | | */ |
4669 | | |
4670 | | static void |
4671 | | object_remove_closure (gpointer data, |
4672 | | GClosure *closure) |
4673 | 0 | { |
4674 | 0 | GObject *object = data; |
4675 | 0 | CArray *carray; |
4676 | 0 | guint i; |
4677 | | |
4678 | 0 | G_LOCK (closure_array_mutex); |
4679 | 0 | carray = g_object_get_qdata (object, quark_closure_array); |
4680 | 0 | for (i = 0; i < carray->n_closures; i++) |
4681 | 0 | if (carray->closures[i] == closure) |
4682 | 0 | { |
4683 | 0 | carray->n_closures--; |
4684 | 0 | if (i < carray->n_closures) |
4685 | 0 | carray->closures[i] = carray->closures[carray->n_closures]; |
4686 | 0 | G_UNLOCK (closure_array_mutex); |
4687 | 0 | return; |
4688 | 0 | } |
4689 | 0 | G_UNLOCK (closure_array_mutex); |
4690 | 0 | g_assert_not_reached (); |
4691 | 0 | } |
4692 | | |
4693 | | static void |
4694 | | destroy_closure_array (gpointer data) |
4695 | 0 | { |
4696 | 0 | CArray *carray = data; |
4697 | 0 | GObject *object = carray->object; |
4698 | 0 | guint i, n = carray->n_closures; |
4699 | | |
4700 | 0 | for (i = 0; i < n; i++) |
4701 | 0 | { |
4702 | 0 | GClosure *closure = carray->closures[i]; |
4703 | | |
4704 | | /* removing object_remove_closure() upfront is probably faster than |
4705 | | * letting it fiddle with quark_closure_array which is empty anyways |
4706 | | */ |
4707 | 0 | g_closure_remove_invalidate_notifier (closure, object, object_remove_closure); |
4708 | 0 | g_closure_invalidate (closure); |
4709 | 0 | } |
4710 | 0 | g_free (carray); |
4711 | 0 | } |
4712 | | |
4713 | | /** |
4714 | | * g_object_watch_closure: |
4715 | | * @object: #GObject restricting lifetime of @closure |
4716 | | * @closure: #GClosure to watch |
4717 | | * |
4718 | | * This function essentially limits the life time of the @closure to |
4719 | | * the life time of the object. That is, when the object is finalized, |
4720 | | * the @closure is invalidated by calling g_closure_invalidate() on |
4721 | | * it, in order to prevent invocations of the closure with a finalized |
4722 | | * (nonexisting) object. Also, g_object_ref() and g_object_unref() are |
4723 | | * added as marshal guards to the @closure, to ensure that an extra |
4724 | | * reference count is held on @object during invocation of the |
4725 | | * @closure. Usually, this function will be called on closures that |
4726 | | * use this @object as closure data. |
4727 | | */ |
4728 | | void |
4729 | | g_object_watch_closure (GObject *object, |
4730 | | GClosure *closure) |
4731 | 0 | { |
4732 | 0 | CArray *carray; |
4733 | 0 | guint i; |
4734 | | |
4735 | 0 | g_return_if_fail (G_IS_OBJECT (object)); |
4736 | 0 | g_return_if_fail (closure != NULL); |
4737 | 0 | g_return_if_fail (closure->is_invalid == FALSE); |
4738 | 0 | g_return_if_fail (closure->in_marshal == FALSE); |
4739 | 0 | g_return_if_fail (g_atomic_int_get (&object->ref_count) > 0); /* this doesn't work on finalizing objects */ |
4740 | | |
4741 | 0 | g_closure_add_invalidate_notifier (closure, object, object_remove_closure); |
4742 | 0 | g_closure_add_marshal_guards (closure, |
4743 | 0 | object, (GClosureNotify) g_object_ref, |
4744 | 0 | object, (GClosureNotify) g_object_unref); |
4745 | 0 | G_LOCK (closure_array_mutex); |
4746 | 0 | carray = g_datalist_id_remove_no_notify (&object->qdata, quark_closure_array); |
4747 | 0 | if (!carray) |
4748 | 0 | { |
4749 | 0 | carray = g_renew (CArray, NULL, 1); |
4750 | 0 | carray->object = object; |
4751 | 0 | carray->n_closures = 1; |
4752 | 0 | i = 0; |
4753 | 0 | } |
4754 | 0 | else |
4755 | 0 | { |
4756 | 0 | i = carray->n_closures++; |
4757 | 0 | carray = g_realloc (carray, sizeof (*carray) + sizeof (carray->closures[0]) * i); |
4758 | 0 | } |
4759 | 0 | carray->closures[i] = closure; |
4760 | 0 | g_datalist_id_set_data_full (&object->qdata, quark_closure_array, carray, destroy_closure_array); |
4761 | 0 | G_UNLOCK (closure_array_mutex); |
4762 | 0 | } |
4763 | | |
4764 | | /** |
4765 | | * g_closure_new_object: |
4766 | | * @sizeof_closure: the size of the structure to allocate, must be at least |
4767 | | * `sizeof (GClosure)` |
4768 | | * @object: a #GObject pointer to store in the @data field of the newly |
4769 | | * allocated #GClosure |
4770 | | * |
4771 | | * A variant of g_closure_new_simple() which stores @object in the |
4772 | | * @data field of the closure and calls g_object_watch_closure() on |
4773 | | * @object and the created closure. This function is mainly useful |
4774 | | * when implementing new types of closures. |
4775 | | * |
4776 | | * Returns: (transfer floating): a newly allocated #GClosure |
4777 | | */ |
4778 | | GClosure * |
4779 | | g_closure_new_object (guint sizeof_closure, |
4780 | | GObject *object) |
4781 | 0 | { |
4782 | 0 | GClosure *closure; |
4783 | |
|
4784 | 0 | g_return_val_if_fail (G_IS_OBJECT (object), NULL); |
4785 | 0 | g_return_val_if_fail (g_atomic_int_get (&object->ref_count) > 0, NULL); /* this doesn't work on finalizing objects */ |
4786 | | |
4787 | 0 | closure = g_closure_new_simple (sizeof_closure, object); |
4788 | 0 | g_object_watch_closure (object, closure); |
4789 | |
|
4790 | 0 | return closure; |
4791 | 0 | } |
4792 | | |
4793 | | /** |
4794 | | * g_cclosure_new_object: (skip) |
4795 | | * @callback_func: the function to invoke |
4796 | | * @object: a #GObject pointer to pass to @callback_func |
4797 | | * |
4798 | | * A variant of g_cclosure_new() which uses @object as @user_data and |
4799 | | * calls g_object_watch_closure() on @object and the created |
4800 | | * closure. This function is useful when you have a callback closely |
4801 | | * associated with a #GObject, and want the callback to no longer run |
4802 | | * after the object is is freed. |
4803 | | * |
4804 | | * Returns: (transfer floating): a new #GCClosure |
4805 | | */ |
4806 | | GClosure * |
4807 | | g_cclosure_new_object (GCallback callback_func, |
4808 | | GObject *object) |
4809 | 0 | { |
4810 | 0 | GClosure *closure; |
4811 | |
|
4812 | 0 | g_return_val_if_fail (G_IS_OBJECT (object), NULL); |
4813 | 0 | g_return_val_if_fail (g_atomic_int_get (&object->ref_count) > 0, NULL); /* this doesn't work on finalizing objects */ |
4814 | 0 | g_return_val_if_fail (callback_func != NULL, NULL); |
4815 | | |
4816 | 0 | closure = g_cclosure_new (callback_func, object, NULL); |
4817 | 0 | g_object_watch_closure (object, closure); |
4818 | |
|
4819 | 0 | return closure; |
4820 | 0 | } |
4821 | | |
4822 | | /** |
4823 | | * g_cclosure_new_object_swap: (skip) |
4824 | | * @callback_func: the function to invoke |
4825 | | * @object: a #GObject pointer to pass to @callback_func |
4826 | | * |
4827 | | * A variant of g_cclosure_new_swap() which uses @object as @user_data |
4828 | | * and calls g_object_watch_closure() on @object and the created |
4829 | | * closure. This function is useful when you have a callback closely |
4830 | | * associated with a #GObject, and want the callback to no longer run |
4831 | | * after the object is is freed. |
4832 | | * |
4833 | | * Returns: (transfer floating): a new #GCClosure |
4834 | | */ |
4835 | | GClosure * |
4836 | | g_cclosure_new_object_swap (GCallback callback_func, |
4837 | | GObject *object) |
4838 | 0 | { |
4839 | 0 | GClosure *closure; |
4840 | |
|
4841 | 0 | g_return_val_if_fail (G_IS_OBJECT (object), NULL); |
4842 | 0 | g_return_val_if_fail (g_atomic_int_get (&object->ref_count) > 0, NULL); /* this doesn't work on finalizing objects */ |
4843 | 0 | g_return_val_if_fail (callback_func != NULL, NULL); |
4844 | | |
4845 | 0 | closure = g_cclosure_new_swap (callback_func, object, NULL); |
4846 | 0 | g_object_watch_closure (object, closure); |
4847 | |
|
4848 | 0 | return closure; |
4849 | 0 | } |
4850 | | |
4851 | | gsize |
4852 | | g_object_compat_control (gsize what, |
4853 | | gpointer data) |
4854 | 0 | { |
4855 | 0 | switch (what) |
4856 | 0 | { |
4857 | 0 | gpointer *pp; |
4858 | 0 | case 1: /* floating base type */ |
4859 | 0 | return G_TYPE_INITIALLY_UNOWNED; |
4860 | 0 | case 2: /* FIXME: remove this once GLib/Gtk+ break ABI again */ |
4861 | 0 | floating_flag_handler = (guint(*)(GObject*,gint)) data; |
4862 | 0 | return 1; |
4863 | 0 | case 3: /* FIXME: remove this once GLib/Gtk+ break ABI again */ |
4864 | 0 | pp = data; |
4865 | 0 | *pp = floating_flag_handler; |
4866 | 0 | return 1; |
4867 | 0 | default: |
4868 | 0 | return 0; |
4869 | 0 | } |
4870 | 0 | } |
4871 | | |
4872 | 0 | G_DEFINE_TYPE (GInitiallyUnowned, g_initially_unowned, G_TYPE_OBJECT) |
4873 | 0 |
|
4874 | 0 | static void |
4875 | 0 | g_initially_unowned_init (GInitiallyUnowned *object) |
4876 | 0 | { |
4877 | 0 | g_object_force_floating (object); |
4878 | 0 | } |
4879 | | |
4880 | | static void |
4881 | | g_initially_unowned_class_init (GInitiallyUnownedClass *klass) |
4882 | 0 | { |
4883 | 0 | } |
4884 | | |
4885 | | /** |
4886 | | * GWeakRef: |
4887 | | * |
4888 | | * A structure containing a weak reference to a #GObject. |
4889 | | * |
4890 | | * A `GWeakRef` can either be empty (i.e. point to %NULL), or point to an |
4891 | | * object for as long as at least one "strong" reference to that object |
4892 | | * exists. Before the object's #GObjectClass.dispose method is called, |
4893 | | * every #GWeakRef associated with becomes empty (i.e. points to %NULL). |
4894 | | * |
4895 | | * Like #GValue, #GWeakRef can be statically allocated, stack- or |
4896 | | * heap-allocated, or embedded in larger structures. |
4897 | | * |
4898 | | * Unlike g_object_weak_ref() and g_object_add_weak_pointer(), this weak |
4899 | | * reference is thread-safe: converting a weak pointer to a reference is |
4900 | | * atomic with respect to invalidation of weak pointers to destroyed |
4901 | | * objects. |
4902 | | * |
4903 | | * If the object's #GObjectClass.dispose method results in additional |
4904 | | * references to the object being held (‘re-referencing’), any #GWeakRefs taken |
4905 | | * before it was disposed will continue to point to %NULL. Any #GWeakRefs taken |
4906 | | * during disposal and after re-referencing, or after disposal has returned due |
4907 | | * to the re-referencing, will continue to point to the object until its refcount |
4908 | | * goes back to zero, at which point they too will be invalidated. |
4909 | | * |
4910 | | * It is invalid to take a #GWeakRef on an object during #GObjectClass.dispose |
4911 | | * without first having or creating a strong reference to the object. |
4912 | | */ |
4913 | | |
4914 | | /** |
4915 | | * g_weak_ref_init: (skip) |
4916 | | * @weak_ref: (inout): uninitialized or empty location for a weak |
4917 | | * reference |
4918 | | * @object: (type GObject.Object) (nullable): a #GObject or %NULL |
4919 | | * |
4920 | | * Initialise a non-statically-allocated #GWeakRef. |
4921 | | * |
4922 | | * This function also calls g_weak_ref_set() with @object on the |
4923 | | * freshly-initialised weak reference. |
4924 | | * |
4925 | | * This function should always be matched with a call to |
4926 | | * g_weak_ref_clear(). It is not necessary to use this function for a |
4927 | | * #GWeakRef in static storage because it will already be |
4928 | | * properly initialised. Just use g_weak_ref_set() directly. |
4929 | | * |
4930 | | * Since: 2.32 |
4931 | | */ |
4932 | | void |
4933 | | g_weak_ref_init (GWeakRef *weak_ref, |
4934 | | gpointer object) |
4935 | 1 | { |
4936 | 1 | weak_ref->priv.p = NULL; |
4937 | | |
4938 | 1 | g_weak_ref_set (weak_ref, object); |
4939 | 1 | } |
4940 | | |
4941 | | /** |
4942 | | * g_weak_ref_clear: (skip) |
4943 | | * @weak_ref: (inout): location of a weak reference, which |
4944 | | * may be empty |
4945 | | * |
4946 | | * Frees resources associated with a non-statically-allocated #GWeakRef. |
4947 | | * After this call, the #GWeakRef is left in an undefined state. |
4948 | | * |
4949 | | * You should only call this on a #GWeakRef that previously had |
4950 | | * g_weak_ref_init() called on it. |
4951 | | * |
4952 | | * Since: 2.32 |
4953 | | */ |
4954 | | void |
4955 | | g_weak_ref_clear (GWeakRef *weak_ref) |
4956 | 0 | { |
4957 | 0 | g_weak_ref_set (weak_ref, NULL); |
4958 | | |
4959 | | /* be unkind */ |
4960 | 0 | weak_ref->priv.p = (void *) 0xccccccccu; |
4961 | 0 | } |
4962 | | |
4963 | | /** |
4964 | | * g_weak_ref_get: (skip) |
4965 | | * @weak_ref: (inout): location of a weak reference to a #GObject |
4966 | | * |
4967 | | * If @weak_ref is not empty, atomically acquire a strong |
4968 | | * reference to the object it points to, and return that reference. |
4969 | | * |
4970 | | * This function is needed because of the potential race between taking |
4971 | | * the pointer value and g_object_ref() on it, if the object was losing |
4972 | | * its last reference at the same time in a different thread. |
4973 | | * |
4974 | | * The caller should release the resulting reference in the usual way, |
4975 | | * by using g_object_unref(). |
4976 | | * |
4977 | | * Returns: (transfer full) (type GObject.Object): the object pointed to |
4978 | | * by @weak_ref, or %NULL if it was empty |
4979 | | * |
4980 | | * Since: 2.32 |
4981 | | */ |
4982 | | gpointer |
4983 | | g_weak_ref_get (GWeakRef *weak_ref) |
4984 | 0 | { |
4985 | 0 | gpointer object_or_null; |
4986 | |
|
4987 | 0 | g_return_val_if_fail (weak_ref!= NULL, NULL); |
4988 | | |
4989 | 0 | g_rw_lock_reader_lock (&weak_locations_lock); |
4990 | |
|
4991 | 0 | object_or_null = weak_ref->priv.p; |
4992 | |
|
4993 | 0 | if (object_or_null != NULL) |
4994 | 0 | g_object_ref (object_or_null); |
4995 | |
|
4996 | 0 | g_rw_lock_reader_unlock (&weak_locations_lock); |
4997 | |
|
4998 | 0 | return object_or_null; |
4999 | 0 | } |
5000 | | |
5001 | | static void |
5002 | | weak_locations_free_unlocked (GSList **weak_locations) |
5003 | 0 | { |
5004 | 0 | if (*weak_locations) |
5005 | 0 | { |
5006 | 0 | GSList *weak_location; |
5007 | |
|
5008 | 0 | for (weak_location = *weak_locations; weak_location;) |
5009 | 0 | { |
5010 | 0 | GWeakRef *weak_ref_location = weak_location->data; |
5011 | |
|
5012 | 0 | weak_ref_location->priv.p = NULL; |
5013 | 0 | weak_location = g_slist_delete_link (weak_location, weak_location); |
5014 | 0 | } |
5015 | 0 | } |
5016 | |
|
5017 | 0 | g_free (weak_locations); |
5018 | 0 | } |
5019 | | |
5020 | | static void |
5021 | | weak_locations_free (gpointer data) |
5022 | 0 | { |
5023 | 0 | GSList **weak_locations = data; |
5024 | |
|
5025 | 0 | g_rw_lock_writer_lock (&weak_locations_lock); |
5026 | 0 | weak_locations_free_unlocked (weak_locations); |
5027 | 0 | g_rw_lock_writer_unlock (&weak_locations_lock); |
5028 | 0 | } |
5029 | | |
5030 | | /** |
5031 | | * g_weak_ref_set: (skip) |
5032 | | * @weak_ref: location for a weak reference |
5033 | | * @object: (type GObject.Object) (nullable): a #GObject or %NULL |
5034 | | * |
5035 | | * Change the object to which @weak_ref points, or set it to |
5036 | | * %NULL. |
5037 | | * |
5038 | | * You must own a strong reference on @object while calling this |
5039 | | * function. |
5040 | | * |
5041 | | * Since: 2.32 |
5042 | | */ |
5043 | | void |
5044 | | g_weak_ref_set (GWeakRef *weak_ref, |
5045 | | gpointer object) |
5046 | 1 | { |
5047 | 1 | GSList **weak_locations; |
5048 | 1 | GObject *new_object; |
5049 | 1 | GObject *old_object; |
5050 | | |
5051 | 1 | g_return_if_fail (weak_ref != NULL); |
5052 | 1 | g_return_if_fail (object == NULL || G_IS_OBJECT (object)); |
5053 | | |
5054 | 1 | new_object = object; |
5055 | | |
5056 | 1 | g_rw_lock_writer_lock (&weak_locations_lock); |
5057 | | |
5058 | | /* We use the extra level of indirection here so that if we have ever |
5059 | | * had a weak pointer installed at any point in time on this object, |
5060 | | * we can see that there is a non-NULL value associated with the |
5061 | | * weak-pointer quark and know that this value will not change at any |
5062 | | * point in the object's lifetime. |
5063 | | * |
5064 | | * Both properties are important for reducing the amount of times we |
5065 | | * need to acquire locks and for decreasing the duration of time the |
5066 | | * lock is held while avoiding some rather tricky races. |
5067 | | * |
5068 | | * Specifically: we can avoid having to do an extra unconditional lock |
5069 | | * in g_object_unref() without worrying about some extremely tricky |
5070 | | * races. |
5071 | | */ |
5072 | | |
5073 | 1 | old_object = weak_ref->priv.p; |
5074 | 1 | if (new_object != old_object) |
5075 | 1 | { |
5076 | 1 | weak_ref->priv.p = new_object; |
5077 | | |
5078 | | /* Remove the weak ref from the old object */ |
5079 | 1 | if (old_object != NULL) |
5080 | 0 | { |
5081 | 0 | weak_locations = g_datalist_id_get_data (&old_object->qdata, quark_weak_locations); |
5082 | 0 | if (weak_locations == NULL) |
5083 | 0 | { |
5084 | 0 | #ifndef G_DISABLE_ASSERT |
5085 | 0 | gboolean in_weak_refs_notify = |
5086 | 0 | g_datalist_id_get_data (&old_object->qdata, quark_weak_refs) == NULL; |
5087 | 0 | g_assert (in_weak_refs_notify); |
5088 | 0 | #endif /* G_DISABLE_ASSERT */ |
5089 | 0 | } |
5090 | 0 | else |
5091 | 0 | { |
5092 | 0 | *weak_locations = g_slist_remove (*weak_locations, weak_ref); |
5093 | |
|
5094 | 0 | if (!*weak_locations) |
5095 | 0 | { |
5096 | 0 | weak_locations_free_unlocked (weak_locations); |
5097 | 0 | g_datalist_id_remove_no_notify (&old_object->qdata, quark_weak_locations); |
5098 | 0 | } |
5099 | 0 | } |
5100 | 0 | } |
5101 | | |
5102 | | /* Add the weak ref to the new object */ |
5103 | 1 | if (new_object != NULL) |
5104 | 1 | { |
5105 | 1 | weak_locations = g_datalist_id_get_data (&new_object->qdata, quark_weak_locations); |
5106 | | |
5107 | 1 | if (weak_locations == NULL) |
5108 | 1 | { |
5109 | 1 | weak_locations = g_new0 (GSList *, 1); |
5110 | 1 | g_datalist_id_set_data_full (&new_object->qdata, quark_weak_locations, |
5111 | 1 | weak_locations, weak_locations_free); |
5112 | 1 | } |
5113 | | |
5114 | 1 | *weak_locations = g_slist_prepend (*weak_locations, weak_ref); |
5115 | 1 | } |
5116 | 1 | } |
5117 | | |
5118 | 1 | g_rw_lock_writer_unlock (&weak_locations_lock); |
5119 | 1 | } |