1 /* GObject - GLib Type, Object, Parameter and Signal Library 2 * Copyright (C) 1998-1999, 2000-2001 Tim Janik and Red Hat, Inc. 3 * 4 * This library is free software; you can redistribute it and/or 5 * modify it under the terms of the GNU Lesser General Public 6 * License as published by the Free Software Foundation; either 7 * version 2 of the License, or (at your option) any later version. 8 * 9 * This library is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 * Lesser General Public License for more details. 13 * 14 * You should have received a copy of the GNU Lesser General 15 * Public License along with this library; if not, write to the 16 * Free Software Foundation, Inc., 59 Temple Place, Suite 330, 17 * Boston, MA 02111-1307, USA. 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/gdatasetprivate.h" 30 31 #include "gobject.h" 32 #include "gvaluecollector.h" 33 #include "gsignal.h" 34 #include "gparamspecs.h" 35 #include "gvaluetypes.h" 36 #include "gobjectalias.h" 37 38 /* This should be included after gobjectalias.h (or pltcheck.sh will fail) */ 39 #include "gobjectnotifyqueue.c" 40 41 42 /** 43 * SECTION:objects 44 * @short_description: The base object type 45 * @see_also: #GParamSpecObject, g_param_spec_object() 46 * @title: The Base Object Type 47 * 48 * GObject is the fundamental type providing the common attributes and 49 * methods for all object types in GTK+, Pango and other libraries 50 * based on GObject. The GObject class provides methods for object 51 * construction and destruction, property access methods, and signal 52 * support. Signals are described in detail in <xref 53 * linkend="gobject-Signals"/>. 54 * 55 * <para id="floating-ref"> 56 * #GInitiallyUnowned is derived from #GObject. The only difference between 57 * the two is that the initial reference of a #GInitiallyUnowned is flagged 58 * as a <firstterm>floating</firstterm> reference. 59 * This means that it is not specifically claimed to be "owned" by 60 * any code portion. The main motivation for providing floating references is 61 * C convenience. In particular, it allows code to be written as: 62 * |[ 63 * container = create_container(); 64 * container_add_child (container, create_child()); 65 * ]| 66 * If <function>container_add_child()</function> will g_object_ref_sink() the 67 * passed in child, no reference of the newly created child is leaked. 68 * Without floating references, <function>container_add_child()</function> 69 * can only g_object_ref() the new child, so to implement this code without 70 * reference leaks, it would have to be written as: 71 * |[ 72 * Child *child; 73 * container = create_container(); 74 * child = create_child(); 75 * container_add_child (container, child); 76 * g_object_unref (child); 77 * ]| 78 * The floating reference can be converted into 79 * an ordinary reference by calling g_object_ref_sink(). 80 * For already sunken objects (objects that don't have a floating reference 81 * anymore), g_object_ref_sink() is equivalent to g_object_ref() and returns 82 * a new reference. 83 * Since floating references are useful almost exclusively for C convenience, 84 * language bindings that provide automated reference and memory ownership 85 * maintenance (such as smart pointers or garbage collection) therefore don't 86 * need to expose floating references in their API. 87 * </para> 88 * 89 * Some object implementations may need to save an objects floating state 90 * across certain code portions (an example is #GtkMenu), to achive this, the 91 * following sequence can be used: 92 * 93 * |[ 94 * // save floating state 95 * gboolean was_floating = g_object_is_floating (object); 96 * g_object_ref_sink (object); 97 * // protected code portion 98 * ...; 99 * // restore floating state 100 * if (was_floating) 101 * g_object_force_floating (object); 102 * g_obejct_unref (object); // release previously acquired reference 103 * ]| 104 */ 105 106 107 /* --- macros --- */ 108 #define PARAM_SPEC_PARAM_ID(pspec) ((pspec)->param_id) 109 #define PARAM_SPEC_SET_PARAM_ID(pspec, id) ((pspec)->param_id = (id)) 110 111 #define OBJECT_HAS_TOGGLE_REF_FLAG 0x1 112 #define OBJECT_HAS_TOGGLE_REF(object) \ 113 ((G_DATALIST_GET_FLAGS (&(object)->qdata) & OBJECT_HAS_TOGGLE_REF_FLAG) != 0) 114 #define OBJECT_FLOATING_FLAG 0x2 115 116 117 /* --- signals --- */ 118 enum { 119 NOTIFY, 120 LAST_SIGNAL 121 }; 122 123 124 /* --- properties --- */ 125 enum { 126 PROP_NONE 127 }; 128 129 130 /* --- prototypes --- */ 131 static void g_object_base_class_init (GObjectClass *class); 132 static void g_object_base_class_finalize (GObjectClass *class); 133 static void g_object_do_class_init (GObjectClass *class); 134 static void g_object_init (GObject *object); 135 static GObject* g_object_constructor (GType type, 136 guint n_construct_properties, 137 GObjectConstructParam *construct_params); 138 static void g_object_real_dispose (GObject *object); 139 static void g_object_finalize (GObject *object); 140 static void g_object_do_set_property (GObject *object, 141 guint property_id, 142 const GValue *value, 143 GParamSpec *pspec); 144 static void g_object_do_get_property (GObject *object, 145 guint property_id, 146 GValue *value, 147 GParamSpec *pspec); 148 static void g_value_object_init (GValue *value); 149 static void g_value_object_free_value (GValue *value); 150 static void g_value_object_copy_value (const GValue *src_value, 151 GValue *dest_value); 152 static void g_value_object_transform_value (const GValue *src_value, 153 GValue *dest_value); 154 static gpointer g_value_object_peek_pointer (const GValue *value); 155 static gchar* g_value_object_collect_value (GValue *value, 156 guint n_collect_values, 157 GTypeCValue *collect_values, 158 guint collect_flags); 159 static gchar* g_value_object_lcopy_value (const GValue *value, 160 guint n_collect_values, 161 GTypeCValue *collect_values, 162 guint collect_flags); 163 static void g_object_dispatch_properties_changed (GObject *object, 164 guint n_pspecs, 165 GParamSpec **pspecs); 166 static inline void object_get_property (GObject *object, 167 GParamSpec *pspec, 168 GValue *value); 169 static inline void object_set_property (GObject *object, 170 GParamSpec *pspec, 171 const GValue *value, 172 GObjectNotifyQueue *nqueue); 173 static guint object_floating_flag_handler (GObject *object, 174 gint job); 175 176 static void object_interface_check_properties (gpointer func_data, 177 gpointer g_iface); 178 179 180 /* --- variables --- */ 181 static GQuark quark_closure_array = 0; 182 static GQuark quark_weak_refs = 0; 183 static GQuark quark_toggle_refs = 0; 184 static GParamSpecPool *pspec_pool = NULL; 185 static GObjectNotifyContext property_notify_context = { 0, }; 186 static gulong gobject_signals[LAST_SIGNAL] = { 0, }; 187 static guint (*floating_flag_handler) (GObject*, gint) = object_floating_flag_handler; 188 G_LOCK_DEFINE_STATIC (construction_mutex); 189 static GSList *construction_objects = NULL; 190 191 /* --- functions --- */ 192 #ifdef G_ENABLE_DEBUG 193 #define IF_DEBUG(debug_type) if (_g_type_debug_flags & G_TYPE_DEBUG_ ## debug_type) 194 G_LOCK_DEFINE_STATIC (debug_objects); 195 static volatile GObject *g_trap_object_ref = NULL; 196 static guint debug_objects_count = 0; 197 static GHashTable *debug_objects_ht = NULL; 198 199 static void 200 debug_objects_foreach (gpointer key, 201 gpointer value, 202 gpointer user_data) 203 { 204 GObject *object = value; 205 206 g_message ("[%p] stale %s\tref_count=%u", 207 object, 208 G_OBJECT_TYPE_NAME (object), 209 object->ref_count); 210 } 211 212 static void 213 debug_objects_atexit (void) 214 { 215 IF_DEBUG (OBJECTS) 216 { 217 G_LOCK (debug_objects); 218 g_message ("stale GObjects: %u", debug_objects_count); 219 g_hash_table_foreach (debug_objects_ht, debug_objects_foreach, NULL); 220 G_UNLOCK (debug_objects); 221 } 222 } 223 #endif /* G_ENABLE_DEBUG */ 224 225 void 226 g_object_type_init (void) 227 { 228 static gboolean initialized = FALSE; 229 static const GTypeFundamentalInfo finfo = { 230 G_TYPE_FLAG_CLASSED | G_TYPE_FLAG_INSTANTIATABLE | G_TYPE_FLAG_DERIVABLE | G_TYPE_FLAG_DEEP_DERIVABLE, 231 }; 232 static GTypeInfo info = { 233 sizeof (GObjectClass), 234 (GBaseInitFunc) g_object_base_class_init, 235 (GBaseFinalizeFunc) g_object_base_class_finalize, 236 (GClassInitFunc) g_object_do_class_init, 237 NULL /* class_destroy */, 238 NULL /* class_data */, 239 sizeof (GObject), 240 0 /* n_preallocs */, 241 (GInstanceInitFunc) g_object_init, 242 NULL, /* value_table */ 243 }; 244 static const GTypeValueTable value_table = { 245 g_value_object_init, /* value_init */ 246 g_value_object_free_value, /* value_free */ 247 g_value_object_copy_value, /* value_copy */ 248 g_value_object_peek_pointer, /* value_peek_pointer */ 249 "p", /* collect_format */ 250 g_value_object_collect_value, /* collect_value */ 251 "p", /* lcopy_format */ 252 g_value_object_lcopy_value, /* lcopy_value */ 253 }; 254 GType type; 255 256 g_return_if_fail (initialized == FALSE); 257 initialized = TRUE; 258 259 /* G_TYPE_OBJECT 260 */ 261 info.value_table = &value_table; 262 type = g_type_register_fundamental (G_TYPE_OBJECT, g_intern_static_string ("GObject"), &info, &finfo, 0); 263 g_assert (type == G_TYPE_OBJECT); 264 g_value_register_transform_func (G_TYPE_OBJECT, G_TYPE_OBJECT, g_value_object_transform_value); 265 266 #ifdef G_ENABLE_DEBUG 267 IF_DEBUG (OBJECTS) 268 { 269 debug_objects_ht = g_hash_table_new (g_direct_hash, NULL); 270 g_atexit (debug_objects_atexit); 271 } 272 #endif /* G_ENABLE_DEBUG */ 273 } 274 275 static void 276 g_object_base_class_init (GObjectClass *class) 277 { 278 GObjectClass *pclass = g_type_class_peek_parent (class); 279 280 /* reset instance specific fields and methods that don't get inherited */ 281 class->construct_properties = pclass ? g_slist_copy (pclass->construct_properties) : NULL; 282 class->get_property = NULL; 283 class->set_property = NULL; 284 } 285 286 static void 287 g_object_base_class_finalize (GObjectClass *class) 288 { 289 GList *list, *node; 290 291 _g_signals_destroy (G_OBJECT_CLASS_TYPE (class)); 292 293 g_slist_free (class->construct_properties); 294 class->construct_properties = NULL; 295 list = g_param_spec_pool_list_owned (pspec_pool, G_OBJECT_CLASS_TYPE (class)); 296 for (node = list; node; node = node->next) 297 { 298 GParamSpec *pspec = node->data; 299 300 g_param_spec_pool_remove (pspec_pool, pspec); 301 PARAM_SPEC_SET_PARAM_ID (pspec, 0); 302 g_param_spec_unref (pspec); 303 } 304 g_list_free (list); 305 } 306 307 static void 308 g_object_notify_dispatcher (GObject *object, 309 guint n_pspecs, 310 GParamSpec **pspecs) 311 { 312 G_OBJECT_GET_CLASS (object)->dispatch_properties_changed (object, n_pspecs, pspecs); 313 } 314 315 static void 316 g_object_do_class_init (GObjectClass *class) 317 { 318 /* read the comment about typedef struct CArray; on why not to change this quark */ 319 quark_closure_array = g_quark_from_static_string ("GObject-closure-array"); 320 321 quark_weak_refs = g_quark_from_static_string ("GObject-weak-references"); 322 quark_toggle_refs = g_quark_from_static_string ("GObject-toggle-references"); 323 pspec_pool = g_param_spec_pool_new (TRUE); 324 property_notify_context.quark_notify_queue = g_quark_from_static_string ("GObject-notify-queue"); 325 property_notify_context.dispatcher = g_object_notify_dispatcher; 326 327 class->constructor = g_object_constructor; 328 class->set_property = g_object_do_set_property; 329 class->get_property = g_object_do_get_property; 330 class->dispose = g_object_real_dispose; 331 class->finalize = g_object_finalize; 332 class->dispatch_properties_changed = g_object_dispatch_properties_changed; 333 class->notify = NULL; 334 335 /** 336 * GObject::notify: 337 * @gobject: the object which received the signal. 338 * @pspec: the #GParamSpec of the property which changed. 339 * 340 * The notify signal is emitted on an object when one of its 341 * properties has been changed. Note that getting this signal 342 * doesn't guarantee that the value of the property has actually 343 * changed, it may also be emitted when the setter for the property 344 * is called to reinstate the previous value. 345 * 346 * This signal is typically used to obtain change notification for a 347 * single property, by specifying the property name as a detail in the 348 * g_signal_connect() call, like this: 349 * |[ 350 * g_signal_connect (text_view->buffer, "notify::paste-target-list", 351 * G_CALLBACK (gtk_text_view_target_list_notify), 352 * text_view) 353 * ]| 354 * It is important to note that you must use 355 * <link linkend="canonical-parameter-name">canonical</link> parameter names as 356 * detail strings for the notify signal. 357 */ 358 gobject_signals[NOTIFY] = 359 g_signal_new (g_intern_static_string ("notify"), 360 G_TYPE_FROM_CLASS (class), 361 G_SIGNAL_RUN_FIRST | G_SIGNAL_NO_RECURSE | G_SIGNAL_DETAILED | G_SIGNAL_NO_HOOKS | G_SIGNAL_ACTION, 362 G_STRUCT_OFFSET (GObjectClass, notify), 363 NULL, NULL, 364 g_cclosure_marshal_VOID__PARAM, 365 G_TYPE_NONE, 366 1, G_TYPE_PARAM); 367 368 /* Install a check function that we'll use to verify that classes that 369 * implement an interface implement all properties for that interface 370 */ 371 g_type_add_interface_check (NULL, object_interface_check_properties); 372 } 373 374 static void 375 install_property_internal (GType g_type, 376 guint property_id, 377 GParamSpec *pspec) 378 { 379 if (g_param_spec_pool_lookup (pspec_pool, pspec->name, g_type, FALSE)) 380 { 381 g_warning ("When installing property: type `%s' already has a property named `%s'", 382 g_type_name (g_type), 383 pspec->name); 384 return; 385 } 386 387 g_param_spec_ref (pspec); 388 g_param_spec_sink (pspec); 389 PARAM_SPEC_SET_PARAM_ID (pspec, property_id); 390 g_param_spec_pool_insert (pspec_pool, pspec, g_type); 391 } 392 393 /** 394 * g_object_class_install_property: 395 * @oclass: a #GObjectClass 396 * @property_id: the id for the new property 397 * @pspec: the #GParamSpec for the new property 398 * 399 * Installs a new property. This is usually done in the class initializer. 400 * 401 * Note that it is possible to redefine a property in a derived class, 402 * by installing a property with the same name. This can be useful at times, 403 * e.g. to change the range of allowed values or the default value. 404 */ 405 void 406 g_object_class_install_property (GObjectClass *class, 407 guint property_id, 408 GParamSpec *pspec) 409 { 410 g_return_if_fail (G_IS_OBJECT_CLASS (class)); 411 g_return_if_fail (G_IS_PARAM_SPEC (pspec)); 412 if (pspec->flags & G_PARAM_WRITABLE) 413 g_return_if_fail (class->set_property != NULL); 414 if (pspec->flags & G_PARAM_READABLE) 415 g_return_if_fail (class->get_property != NULL); 416 g_return_if_fail (property_id > 0); 417 g_return_if_fail (PARAM_SPEC_PARAM_ID (pspec) == 0); /* paranoid */ 418 if (pspec->flags & G_PARAM_CONSTRUCT) 419 g_return_if_fail ((pspec->flags & G_PARAM_CONSTRUCT_ONLY) == 0); 420 if (pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY)) 421 g_return_if_fail (pspec->flags & G_PARAM_WRITABLE); 422 423 install_property_internal (G_OBJECT_CLASS_TYPE (class), property_id, pspec); 424 425 if (pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY)) 426 class->construct_properties = g_slist_prepend (class->construct_properties, pspec); 427 428 /* for property overrides of construct poperties, we have to get rid 429 * of the overidden inherited construct property 430 */ 431 pspec = g_param_spec_pool_lookup (pspec_pool, pspec->name, g_type_parent (G_OBJECT_CLASS_TYPE (class)), TRUE); 432 if (pspec && pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY)) 433 class->construct_properties = g_slist_remove (class->construct_properties, pspec); 434 } 435 436 /** 437 * g_object_interface_install_property: 438 * @g_iface: any interface vtable for the interface, or the default 439 * vtable for the interface. 440 * @pspec: the #GParamSpec for the new property 441 * 442 * Add a property to an interface; this is only useful for interfaces 443 * that are added to GObject-derived types. Adding a property to an 444 * interface forces all objects classes with that interface to have a 445 * compatible property. The compatible property could be a newly 446 * created #GParamSpec, but normally 447 * g_object_class_override_property() will be used so that the object 448 * class only needs to provide an implementation and inherits the 449 * property description, default value, bounds, and so forth from the 450 * interface property. 451 * 452 * This function is meant to be called from the interface's default 453 * vtable initialization function (the @class_init member of 454 * #GTypeInfo.) It must not be called after after @class_init has 455 * been called for any object types implementing this interface. 456 * 457 * Since: 2.4 458 */ 459 void 460 g_object_interface_install_property (gpointer g_iface, 461 GParamSpec *pspec) 462 { 463 GTypeInterface *iface_class = g_iface; 464 465 g_return_if_fail (G_TYPE_IS_INTERFACE (iface_class->g_type)); 466 g_return_if_fail (G_IS_PARAM_SPEC (pspec)); 467 g_return_if_fail (!G_IS_PARAM_SPEC_OVERRIDE (pspec)); /* paranoid */ 468 g_return_if_fail (PARAM_SPEC_PARAM_ID (pspec) == 0); /* paranoid */ 469 470 install_property_internal (iface_class->g_type, 0, pspec); 471 } 472 473 /** 474 * g_object_class_find_property: 475 * @oclass: a #GObjectClass 476 * @property_name: the name of the property to look up 477 * 478 * Looks up the #GParamSpec for a property of a class. 479 * 480 * Returns: the #GParamSpec for the property, or %NULL if the class 481 * doesn't have a property of that name 482 */ 483 GParamSpec* 484 g_object_class_find_property (GObjectClass *class, 485 const gchar *property_name) 486 { 487 GParamSpec *pspec; 488 GParamSpec *redirect; 489 490 g_return_val_if_fail (G_IS_OBJECT_CLASS (class), NULL); 491 g_return_val_if_fail (property_name != NULL, NULL); 492 493 pspec = g_param_spec_pool_lookup (pspec_pool, 494 property_name, 495 G_OBJECT_CLASS_TYPE (class), 496 TRUE); 497 if (pspec) 498 { 499 redirect = g_param_spec_get_redirect_target (pspec); 500 if (redirect) 501 return redirect; 502 else 503 return pspec; 504 } 505 else 506 return NULL; 507 } 508 509 /** 510 * g_object_interface_find_property: 511 * @g_iface: any interface vtable for the interface, or the default 512 * vtable for the interface 513 * @property_name: name of a property to lookup. 514 * 515 * Find the #GParamSpec with the given name for an 516 * interface. Generally, the interface vtable passed in as @g_iface 517 * will be the default vtable from g_type_default_interface_ref(), or, 518 * if you know the interface has already been loaded, 519 * g_type_default_interface_peek(). 520 * 521 * Since: 2.4 522 * 523 * Returns: the #GParamSpec for the property of the interface with the 524 * name @property_name, or %NULL if no such property exists. 525 */ 526 GParamSpec* 527 g_object_interface_find_property (gpointer g_iface, 528 const gchar *property_name) 529 { 530 GTypeInterface *iface_class = g_iface; 531 532 g_return_val_if_fail (G_TYPE_IS_INTERFACE (iface_class->g_type), NULL); 533 g_return_val_if_fail (property_name != NULL, NULL); 534 535 return g_param_spec_pool_lookup (pspec_pool, 536 property_name, 537 iface_class->g_type, 538 FALSE); 539 } 540 541 /** 542 * g_object_class_override_property: 543 * @oclass: a #GObjectClass 544 * @property_id: the new property ID 545 * @name: the name of a property registered in a parent class or 546 * in an interface of this class. 547 * 548 * Registers @property_id as referring to a property with the 549 * name @name in a parent class or in an interface implemented 550 * by @oclass. This allows this class to <firstterm>override</firstterm> 551 * a property implementation in a parent class or to provide 552 * the implementation of a property from an interface. 553 * 554 * <note> 555 * Internally, overriding is implemented by creating a property of type 556 * #GParamSpecOverride; generally operations that query the properties of 557 * the object class, such as g_object_class_find_property() or 558 * g_object_class_list_properties() will return the overridden 559 * property. However, in one case, the @construct_properties argument of 560 * the @constructor virtual function, the #GParamSpecOverride is passed 561 * instead, so that the @param_id field of the #GParamSpec will be 562 * correct. For virtually all uses, this makes no difference. If you 563 * need to get the overridden property, you can call 564 * g_param_spec_get_redirect_target(). 565 * </note> 566 * 567 * Since: 2.4 568 */ 569 void 570 g_object_class_override_property (GObjectClass *oclass, 571 guint property_id, 572 const gchar *name) 573 { 574 GParamSpec *overridden = NULL; 575 GParamSpec *new; 576 GType parent_type; 577 578 g_return_if_fail (G_IS_OBJECT_CLASS (oclass)); 579 g_return_if_fail (property_id > 0); 580 g_return_if_fail (name != NULL); 581 582 /* Find the overridden property; first check parent types 583 */ 584 parent_type = g_type_parent (G_OBJECT_CLASS_TYPE (oclass)); 585 if (parent_type != G_TYPE_NONE) 586 overridden = g_param_spec_pool_lookup (pspec_pool, 587 name, 588 parent_type, 589 TRUE); 590 if (!overridden) 591 { 592 GType *ifaces; 593 guint n_ifaces; 594 595 /* Now check interfaces 596 */ 597 ifaces = g_type_interfaces (G_OBJECT_CLASS_TYPE (oclass), &n_ifaces); 598 while (n_ifaces-- && !overridden) 599 { 600 overridden = g_param_spec_pool_lookup (pspec_pool, 601 name, 602 ifaces[n_ifaces], 603 FALSE); 604 } 605 606 g_free (ifaces); 607 } 608 609 if (!overridden) 610 { 611 g_warning ("%s: Can't find property to override for '%s::%s'", 612 G_STRFUNC, G_OBJECT_CLASS_NAME (oclass), name); 613 return; 614 } 615 616 new = g_param_spec_override (name, overridden); 617 g_object_class_install_property (oclass, property_id, new); 618 } 619 620 /** 621 * g_object_class_list_properties: 622 * @oclass: a #GObjectClass 623 * @n_properties: return location for the length of the returned array 624 * 625 * Get an array of #GParamSpec* for all properties of a class. 626 * 627 * Returns: an array of #GParamSpec* which should be freed after use 628 */ 629 GParamSpec** /* free result */ 630 g_object_class_list_properties (GObjectClass *class, 631 guint *n_properties_p) 632 { 633 GParamSpec **pspecs; 634 guint n; 635 636 g_return_val_if_fail (G_IS_OBJECT_CLASS (class), NULL); 637 638 pspecs = g_param_spec_pool_list (pspec_pool, 639 G_OBJECT_CLASS_TYPE (class), 640 &n); 641 if (n_properties_p) 642 *n_properties_p = n; 643 644 return pspecs; 645 } 646 647 /** 648 * g_object_interface_list_properties: 649 * @g_iface: any interface vtable for the interface, or the default 650 * vtable for the interface 651 * @n_properties_p: location to store number of properties returned. 652 * 653 * Lists the properties of an interface.Generally, the interface 654 * vtable passed in as @g_iface will be the default vtable from 655 * g_type_default_interface_ref(), or, if you know the interface has 656 * already been loaded, g_type_default_interface_peek(). 657 * 658 * Since: 2.4 659 * 660 * Returns: a pointer to an array of pointers to #GParamSpec 661 * structures. The paramspecs are owned by GLib, but the 662 * array should be freed with g_free() when you are done with 663 * it. 664 */ 665 GParamSpec** 666 g_object_interface_list_properties (gpointer g_iface, 667 guint *n_properties_p) 668 { 669 GTypeInterface *iface_class = g_iface; 670 GParamSpec **pspecs; 671 guint n; 672 673 g_return_val_if_fail (G_TYPE_IS_INTERFACE (iface_class->g_type), NULL); 674 675 pspecs = g_param_spec_pool_list (pspec_pool, 676 iface_class->g_type, 677 &n); 678 if (n_properties_p) 679 *n_properties_p = n; 680 681 return pspecs; 682 } 683 684 static void 685 g_object_init (GObject *object) 686 { 687 object->ref_count = 1; 688 g_datalist_init (&object->qdata); 689 690 /* freeze object's notification queue, g_object_newv() preserves pairedness */ 691 g_object_notify_queue_freeze (object, &property_notify_context); 692 /* enter construction list for notify_queue_thaw() and to allow construct-only properties */ 693 G_LOCK (construction_mutex); 694 construction_objects = g_slist_prepend (construction_objects, object); 695 G_UNLOCK (construction_mutex); 696 697 #ifdef G_ENABLE_DEBUG 698 IF_DEBUG (OBJECTS) 699 { 700 G_LOCK (debug_objects); 701 debug_objects_count++; 702 g_hash_table_insert (debug_objects_ht, object, object); 703 G_UNLOCK (debug_objects); 704 } 705 #endif /* G_ENABLE_DEBUG */ 706 } 707 708 static void 709 g_object_do_set_property (GObject *object, 710 guint property_id, 711 const GValue *value, 712 GParamSpec *pspec) 713 { 714 switch (property_id) 715 { 716 default: 717 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); 718 break; 719 } 720 } 721 722 static void 723 g_object_do_get_property (GObject *object, 724 guint property_id, 725 GValue *value, 726 GParamSpec *pspec) 727 { 728 switch (property_id) 729 { 730 default: 731 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); 732 break; 733 } 734 } 735 736 static void 737 g_object_real_dispose (GObject *object) 738 { 739 g_signal_handlers_destroy (object); 740 g_datalist_id_set_data (&object->qdata, quark_closure_array, NULL); 741 g_datalist_id_set_data (&object->qdata, quark_weak_refs, NULL); 742 } 743 744 static void 745 g_object_finalize (GObject *object) 746 { 747 g_datalist_clear (&object->qdata); 748 749 #ifdef G_ENABLE_DEBUG 750 IF_DEBUG (OBJECTS) 751 { 752 G_LOCK (debug_objects); 753 g_assert (g_hash_table_lookup (debug_objects_ht, object) == object); 754 g_hash_table_remove (debug_objects_ht, object); 755 debug_objects_count--; 756 G_UNLOCK (debug_objects); 757 } 758 #endif /* G_ENABLE_DEBUG */ 759 } 760 761 762 static void 763 g_object_dispatch_properties_changed (GObject *object, 764 guint n_pspecs, 765 GParamSpec **pspecs) 766 { 767 guint i; 768 769 for (i = 0; i < n_pspecs; i++) 770 g_signal_emit (object, gobject_signals[NOTIFY], g_quark_from_string (pspecs[i]->name), pspecs[i]); 771 } 772 773 /** 774 * g_object_run_dispose: 775 * @object: a #GObject 776 * 777 * Releases all references to other objects. This can be used to break 778 * reference cycles. 779 * 780 * This functions should only be called from object system implementations. 781 */ 782 void 783 g_object_run_dispose (GObject *object) 784 { 785 g_return_if_fail (G_IS_OBJECT (object)); 786 g_return_if_fail (object->ref_count > 0); 787 788 g_object_ref (object); 789 G_OBJECT_GET_CLASS (object)->dispose (object); 790 g_object_unref (object); 791 } 792 793 /** 794 * g_object_freeze_notify: 795 * @object: a #GObject 796 * 797 * Increases the freeze count on @object. If the freeze count is 798 * non-zero, the emission of "notify" signals on @object is 799 * stopped. The signals are queued until the freeze count is decreased 800 * to zero. 801 * 802 * This is necessary for accessors that modify multiple properties to prevent 803 * premature notification while the object is still being modified. 804 */ 805 void 806 g_object_freeze_notify (GObject *object) 807 { 808 g_return_if_fail (G_IS_OBJECT (object)); 809 810 if (g_atomic_int_get (&object->ref_count) == 0) 811 return; 812 813 g_object_ref (object); 814 g_object_notify_queue_freeze (object, &property_notify_context); 815 g_object_unref (object); 816 } 817 818 /** 819 * g_object_notify: 820 * @object: a #GObject 821 * @property_name: the name of a property installed on the class of @object. 822 * 823 * Emits a "notify" signal for the property @property_name on @object. 824 */ 825 void 826 g_object_notify (GObject *object, 827 const gchar *property_name) 828 { 829 GParamSpec *pspec; 830 831 g_return_if_fail (G_IS_OBJECT (object)); 832 g_return_if_fail (property_name != NULL); 833 if (g_atomic_int_get (&object->ref_count) == 0) 834 return; 835 836 g_object_ref (object); 837 /* We don't need to get the redirect target 838 * (by, e.g. calling g_object_class_find_property()) 839 * because g_object_notify_queue_add() does that 840 */ 841 pspec = g_param_spec_pool_lookup (pspec_pool, 842 property_name, 843 G_OBJECT_TYPE (object), 844 TRUE); 845 846 if (!pspec) 847 g_warning ("%s: object class `%s' has no property named `%s'", 848 G_STRFUNC, 849 G_OBJECT_TYPE_NAME (object), 850 property_name); 851 else 852 { 853 GObjectNotifyQueue *nqueue; 854 855 nqueue = g_object_notify_queue_freeze (object, &property_notify_context); 856 g_object_notify_queue_add (object, nqueue, pspec); 857 g_object_notify_queue_thaw (object, nqueue); 858 } 859 g_object_unref (object); 860 } 861 862 /** 863 * g_object_thaw_notify: 864 * @object: a #GObject 865 * 866 * Reverts the effect of a previous call to 867 * g_object_freeze_notify(). The freeze count is decreased on @object 868 * and when it reaches zero, all queued "notify" signals are emitted. 869 * 870 * It is an error to call this function when the freeze count is zero. 871 */ 872 void 873 g_object_thaw_notify (GObject *object) 874 { 875 GObjectNotifyQueue *nqueue; 876 877 g_return_if_fail (G_IS_OBJECT (object)); 878 if (g_atomic_int_get (&object->ref_count) == 0) 879 return; 880 881 g_object_ref (object); 882 nqueue = g_object_notify_queue_from_object (object, &property_notify_context); 883 if (!nqueue || !nqueue->freeze_count) 884 g_warning ("%s: property-changed notification for %s(%p) is not frozen", 885 G_STRFUNC, G_OBJECT_TYPE_NAME (object), object); 886 else 887 g_object_notify_queue_thaw (object, nqueue); 888 g_object_unref (object); 889 } 890 891 static inline void 892 object_get_property (GObject *object, 893 GParamSpec *pspec, 894 GValue *value) 895 { 896 GObjectClass *class = g_type_class_peek (pspec->owner_type); 897 guint param_id = PARAM_SPEC_PARAM_ID (pspec); 898 GParamSpec *redirect; 899 900 redirect = g_param_spec_get_redirect_target (pspec); 901 if (redirect) 902 pspec = redirect; 903 904 class->get_property (object, param_id, value, pspec); 905 } 906 907 static inline void 908 object_set_property (GObject *object, 909 GParamSpec *pspec, 910 const GValue *value, 911 GObjectNotifyQueue *nqueue) 912 { 913 GValue tmp_value = { 0, }; 914 GObjectClass *class = g_type_class_peek (pspec->owner_type); 915 guint param_id = PARAM_SPEC_PARAM_ID (pspec); 916 GParamSpec *redirect; 917 918 redirect = g_param_spec_get_redirect_target (pspec); 919 if (redirect) 920 pspec = redirect; 921 922 /* provide a copy to work from, convert (if necessary) and validate */ 923 g_value_init (&tmp_value, G_PARAM_SPEC_VALUE_TYPE (pspec)); 924 if (!g_value_transform (value, &tmp_value)) 925 g_warning ("unable to set property `%s' of type `%s' from value of type `%s'", 926 pspec->name, 927 g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspec)), 928 G_VALUE_TYPE_NAME (value)); 929 else if (g_param_value_validate (pspec, &tmp_value) && !(pspec->flags & G_PARAM_LAX_VALIDATION)) 930 { 931 gchar *contents = g_strdup_value_contents (value); 932 933 g_warning ("value \"%s\" of type `%s' is invalid or out of range for property `%s' of type `%s'", 934 contents, 935 G_VALUE_TYPE_NAME (value), 936 pspec->name, 937 g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspec))); 938 g_free (contents); 939 } 940 else 941 { 942 class->set_property (object, param_id, &tmp_value, pspec); 943 g_object_notify_queue_add (object, nqueue, pspec); 944 } 945 g_value_unset (&tmp_value); 946 } 947 948 static void 949 object_interface_check_properties (gpointer func_data, 950 gpointer g_iface) 951 { 952 GTypeInterface *iface_class = g_iface; 953 GObjectClass *class = g_type_class_peek (iface_class->g_instance_type); 954 GType iface_type = iface_class->g_type; 955 GParamSpec **pspecs; 956 guint n; 957 958 if (!G_IS_OBJECT_CLASS (class)) 959 return; 960 961 pspecs = g_param_spec_pool_list (pspec_pool, iface_type, &n); 962 963 while (n--) 964 { 965 GParamSpec *class_pspec = g_param_spec_pool_lookup (pspec_pool, 966 pspecs[n]->name, 967 G_OBJECT_CLASS_TYPE (class), 968 TRUE); 969 970 if (!class_pspec) 971 { 972 g_critical ("Object class %s doesn't implement property " 973 "'%s' from interface '%s'", 974 g_type_name (G_OBJECT_CLASS_TYPE (class)), 975 pspecs[n]->name, 976 g_type_name (iface_type)); 977 978 continue; 979 } 980 981 /* The implementation paramspec must have a less restrictive 982 * type than the interface parameter spec for set() and a 983 * more restrictive type for get(). We just require equality, 984 * rather than doing something more complicated checking 985 * the READABLE and WRITABLE flags. We also simplify here 986 * by only checking the value type, not the G_PARAM_SPEC_TYPE. 987 */ 988 if (class_pspec && 989 !g_type_is_a (G_PARAM_SPEC_VALUE_TYPE (pspecs[n]), 990 G_PARAM_SPEC_VALUE_TYPE (class_pspec))) 991 { 992 g_critical ("Property '%s' on class '%s' has type '%s' " 993 "which is different from the type '%s', " 994 "of the property on interface '%s'\n", 995 pspecs[n]->name, 996 g_type_name (G_OBJECT_CLASS_TYPE (class)), 997 g_type_name (G_PARAM_SPEC_VALUE_TYPE (class_pspec)), 998 g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspecs[n])), 999 g_type_name (iface_type)); 1000 } 1001 1002 #define SUBSET(a,b,mask) (((a) & ~(b) & (mask)) == 0) 1003 1004 /* CONSTRUCT and CONSTRUCT_ONLY add restrictions. 1005 * READABLE and WRITABLE remove restrictions. The implementation 1006 * paramspec must have less restrictive flags. 1007 */ 1008 if (class_pspec && 1009 (!SUBSET (class_pspec->flags, 1010 pspecs[n]->flags, 1011 G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY) || 1012 !SUBSET (pspecs[n]->flags, 1013 class_pspec->flags, 1014 G_PARAM_READABLE | G_PARAM_WRITABLE))) 1015 { 1016 g_critical ("Flags for property '%s' on class '%s' " 1017 "are not compatible with the property on" 1018 "interface '%s'\n", 1019 pspecs[n]->name, 1020 g_type_name (G_OBJECT_CLASS_TYPE (class)), 1021 g_type_name (iface_type)); 1022 } 1023 #undef SUBSET 1024 } 1025 1026 g_free (pspecs); 1027 } 1028 1029 GType 1030 g_object_get_type (void) 1031 { 1032 return G_TYPE_OBJECT; 1033 } 1034 1035 /** 1036 * g_object_new: 1037 * @object_type: the type id of the #GObject subtype to instantiate 1038 * @first_property_name: the name of the first property 1039 * @...: the value of the first property, followed optionally by more 1040 * name/value pairs, followed by %NULL 1041 * 1042 * Creates a new instance of a #GObject subtype and sets its properties. 1043 * 1044 * Construction parameters (see #G_PARAM_CONSTRUCT, #G_PARAM_CONSTRUCT_ONLY) 1045 * which are not explicitly specified are set to their default values. 1046 * 1047 * Returns: a new instance of @object_type 1048 */ 1049 gpointer 1050 g_object_new (GType object_type, 1051 const gchar *first_property_name, 1052 ...) 1053 { 1054 GObject *object; 1055 va_list var_args; 1056 1057 g_return_val_if_fail (G_TYPE_IS_OBJECT (object_type), NULL); 1058 1059 va_start (var_args, first_property_name); 1060 object = g_object_new_valist (object_type, first_property_name, var_args); 1061 va_end (var_args); 1062 1063 return object; 1064 } 1065 1066 static gboolean 1067 slist_maybe_remove (GSList **slist, 1068 gconstpointer data) 1069 { 1070 GSList *last = NULL, *node = *slist; 1071 while (node) 1072 { 1073 if (node->data == data) 1074 { 1075 if (last) 1076 last->next = node->next; 1077 else 1078 *slist = node->next; 1079 g_slist_free_1 (node); 1080 return TRUE; 1081 } 1082 last = node; 1083 node = last->next; 1084 } 1085 return FALSE; 1086 } 1087 1088 static inline gboolean 1089 object_in_construction_list (GObject *object) 1090 { 1091 gboolean in_construction; 1092 G_LOCK (construction_mutex); 1093 in_construction = g_slist_find (construction_objects, object) != NULL; 1094 G_UNLOCK (construction_mutex); 1095 return in_construction; 1096 } 1097 1098 /** 1099 * g_object_newv: 1100 * @object_type: the type id of the #GObject subtype to instantiate 1101 * @n_parameters: the length of the @parameters array 1102 * @parameters: an array of #GParameter 1103 * 1104 * Creates a new instance of a #GObject subtype and sets its properties. 1105 * 1106 * Construction parameters (see #G_PARAM_CONSTRUCT, #G_PARAM_CONSTRUCT_ONLY) 1107 * which are not explicitly specified are set to their default values. 1108 * 1109 * Returns: a new instance of @object_type 1110 */ 1111 gpointer 1112 g_object_newv (GType object_type, 1113 guint n_parameters, 1114 GParameter *parameters) 1115 { 1116 GObjectConstructParam *cparams, *oparams; 1117 GObjectNotifyQueue *nqueue = NULL; /* shouldn't be initialized, just to silence compiler */ 1118 GObject *object; 1119 GObjectClass *class, *unref_class = NULL; 1120 GSList *slist; 1121 guint n_total_cparams = 0, n_cparams = 0, n_oparams = 0, n_cvalues; 1122 GValue *cvalues; 1123 GList *clist = NULL; 1124 gboolean newly_constructed; 1125 guint i; 1126 1127 g_return_val_if_fail (G_TYPE_IS_OBJECT (object_type), NULL); 1128 1129 class = g_type_class_peek_static (object_type); 1130 if (!class) 1131 class = unref_class = g_type_class_ref (object_type); 1132 for (slist = class->construct_properties; slist; slist = slist->next) 1133 { 1134 clist = g_list_prepend (clist, slist->data); 1135 n_total_cparams += 1; 1136 } 1137 1138 /* collect parameters, sort into construction and normal ones */ 1139 oparams = g_new (GObjectConstructParam, n_parameters); 1140 cparams = g_new (GObjectConstructParam, n_total_cparams); 1141 for (i = 0; i < n_parameters; i++) 1142 { 1143 GValue *value = ¶meters[i].value; 1144 GParamSpec *pspec = g_param_spec_pool_lookup (pspec_pool, 1145 parameters[i].name, 1146 object_type, 1147 TRUE); 1148 if (!pspec) 1149 { 1150 g_warning ("%s: object class `%s' has no property named `%s'", 1151 G_STRFUNC, 1152 g_type_name (object_type), 1153 parameters[i].name); 1154 continue; 1155 } 1156 if (!(pspec->flags & G_PARAM_WRITABLE)) 1157 { 1158 g_warning ("%s: property `%s' of object class `%s' is not writable", 1159 G_STRFUNC, 1160 pspec->name, 1161 g_type_name (object_type)); 1162 continue; 1163 } 1164 if (pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY)) 1165 { 1166 GList *list = g_list_find (clist, pspec); 1167 1168 if (!list) 1169 { 1170 g_warning ("%s: construct property \"%s\" for object `%s' can't be set twice", 1171 G_STRFUNC, pspec->name, g_type_name (object_type)); 1172 continue; 1173 } 1174 cparams[n_cparams].pspec = pspec; 1175 cparams[n_cparams].value = value; 1176 n_cparams++; 1177 if (!list->prev) 1178 clist = list->next; 1179 else 1180 list->prev->next = list->next; 1181 if (list->next) 1182 list->next->prev = list->prev; 1183 g_list_free_1 (list); 1184 } 1185 else 1186 { 1187 oparams[n_oparams].pspec = pspec; 1188 oparams[n_oparams].value = value; 1189 n_oparams++; 1190 } 1191 } 1192 1193 /* set remaining construction properties to default values */ 1194 n_cvalues = n_total_cparams - n_cparams; 1195 cvalues = g_new (GValue, n_cvalues); 1196 while (clist) 1197 { 1198 GList *tmp = clist->next; 1199 GParamSpec *pspec = clist->data; 1200 GValue *value = cvalues + n_total_cparams - n_cparams - 1; 1201 1202 value->g_type = 0; 1203 g_value_init (value, G_PARAM_SPEC_VALUE_TYPE (pspec)); 1204 g_param_value_set_default (pspec, value); 1205 1206 cparams[n_cparams].pspec = pspec; 1207 cparams[n_cparams].value = value; 1208 n_cparams++; 1209 1210 g_list_free_1 (clist); 1211 clist = tmp; 1212 } 1213 1214 /* construct object from construction parameters */ 1215 object = class->constructor (object_type, n_total_cparams, cparams); 1216 /* free construction values */ 1217 g_free (cparams); 1218 while (n_cvalues--) 1219 g_value_unset (cvalues + n_cvalues); 1220 g_free (cvalues); 1221 1222 /* adjust freeze_count according to g_object_init() and remaining properties */ 1223 G_LOCK (construction_mutex); 1224 newly_constructed = slist_maybe_remove (&construction_objects, object); 1225 G_UNLOCK (construction_mutex); 1226 if (newly_constructed || n_oparams) 1227 nqueue = g_object_notify_queue_freeze (object, &property_notify_context); 1228 if (newly_constructed) 1229 g_object_notify_queue_thaw (object, nqueue); 1230 1231 /* run 'constructed' handler if there is one */ 1232 if (newly_constructed && class->constructed) 1233 class->constructed (object); 1234 1235 /* set remaining properties */ 1236 for (i = 0; i < n_oparams; i++) 1237 object_set_property (object, oparams[i].pspec, oparams[i].value, nqueue); 1238 g_free (oparams); 1239 1240 /* release our own freeze count and handle notifications */ 1241 if (newly_constructed || n_oparams) 1242 g_object_notify_queue_thaw (object, nqueue); 1243 1244 if (unref_class) 1245 g_type_class_unref (unref_class); 1246 1247 return object; 1248 } 1249 1250 /** 1251 * g_object_new_valist: 1252 * @object_type: the type id of the #GObject subtype to instantiate 1253 * @first_property_name: the name of the first property 1254 * @var_args: the value of the first property, followed optionally by more 1255 * name/value pairs, followed by %NULL 1256 * 1257 * Creates a new instance of a #GObject subtype and sets its properties. 1258 * 1259 * Construction parameters (see #G_PARAM_CONSTRUCT, #G_PARAM_CONSTRUCT_ONLY) 1260 * which are not explicitly specified are set to their default values. 1261 * 1262 * Returns: a new instance of @object_type 1263 */ 1264 GObject* 1265 g_object_new_valist (GType object_type, 1266 const gchar *first_property_name, 1267 va_list var_args) 1268 { 1269 GObjectClass *class; 1270 GParameter *params; 1271 const gchar *name; 1272 GObject *object; 1273 guint n_params = 0, n_alloced_params = 16; 1274 1275 g_return_val_if_fail (G_TYPE_IS_OBJECT (object_type), NULL); 1276 1277 if (!first_property_name) 1278 return g_object_newv (object_type, 0, NULL); 1279 1280 class = g_type_class_ref (object_type); 1281 1282 params = g_new (GParameter, n_alloced_params); 1283 name = first_property_name; 1284 while (name) 1285 { 1286 gchar *error = NULL; 1287 GParamSpec *pspec = g_param_spec_pool_lookup (pspec_pool, 1288 name, 1289 object_type, 1290 TRUE); 1291 if (!pspec) 1292 { 1293 g_warning ("%s: object class `%s' has no property named `%s'", 1294 G_STRFUNC, 1295 g_type_name (object_type), 1296 name); 1297 break; 1298 } 1299 if (n_params >= n_alloced_params) 1300 { 1301 n_alloced_params += 16; 1302 params = g_renew (GParameter, params, n_alloced_params); 1303 } 1304 params[n_params].name = name; 1305 params[n_params].value.g_type = 0; 1306 g_value_init (¶ms[n_params].value, G_PARAM_SPEC_VALUE_TYPE (pspec)); 1307 G_VALUE_COLLECT (¶ms[n_params].value, var_args, 0, &error); 1308 if (error) 1309 { 1310 g_warning ("%s: %s", G_STRFUNC, error); 1311 g_free (error); 1312 g_value_unset (¶ms[n_params].value); 1313 break; 1314 } 1315 n_params++; 1316 name = va_arg (var_args, gchar*); 1317 } 1318 1319 object = g_object_newv (object_type, n_params, params); 1320 1321 while (n_params--) 1322 g_value_unset (¶ms[n_params].value); 1323 g_free (params); 1324 1325 g_type_class_unref (class); 1326 1327 return object; 1328 } 1329 1330 static GObject* 1331 g_object_constructor (GType type, 1332 guint n_construct_properties, 1333 GObjectConstructParam *construct_params) 1334 { 1335 GObject *object; 1336 1337 /* create object */ 1338 object = (GObject*) g_type_create_instance (type); 1339 1340 /* set construction parameters */ 1341 if (n_construct_properties) 1342 { 1343 GObjectNotifyQueue *nqueue = g_object_notify_queue_freeze (object, &property_notify_context); 1344 1345 /* set construct properties */ 1346 while (n_construct_properties--) 1347 { 1348 GValue *value = construct_params->value; 1349 GParamSpec *pspec = construct_params->pspec; 1350 1351 construct_params++; 1352 object_set_property (object, pspec, value, nqueue); 1353 } 1354 g_object_notify_queue_thaw (object, nqueue); 1355 /* the notification queue is still frozen from g_object_init(), so 1356 * we don't need to handle it here, g_object_newv() takes 1357 * care of that 1358 */ 1359 } 1360 1361 return object; 1362 } 1363 1364 /** 1365 * g_object_set_valist: 1366 * @object: a #GObject 1367 * @first_property_name: name of the first property to set 1368 * @var_args: value for the first property, followed optionally by more 1369 * name/value pairs, followed by %NULL 1370 * 1371 * Sets properties on an object. 1372 */ 1373 void 1374 g_object_set_valist (GObject *object, 1375 const gchar *first_property_name, 1376 va_list var_args) 1377 { 1378 GObjectNotifyQueue *nqueue; 1379 const gchar *name; 1380 1381 g_return_if_fail (G_IS_OBJECT (object)); 1382 1383 g_object_ref (object); 1384 nqueue = g_object_notify_queue_freeze (object, &property_notify_context); 1385 1386 name = first_property_name; 1387 while (name) 1388 { 1389 GValue value = { 0, }; 1390 GParamSpec *pspec; 1391 gchar *error = NULL; 1392 1393 pspec = g_param_spec_pool_lookup (pspec_pool, 1394 name, 1395 G_OBJECT_TYPE (object), 1396 TRUE); 1397 if (!pspec) 1398 { 1399 g_warning ("%s: object class `%s' has no property named `%s'", 1400 G_STRFUNC, 1401 G_OBJECT_TYPE_NAME (object), 1402 name); 1403 break; 1404 } 1405 if (!(pspec->flags & G_PARAM_WRITABLE)) 1406 { 1407 g_warning ("%s: property `%s' of object class `%s' is not writable", 1408 G_STRFUNC, 1409 pspec->name, 1410 G_OBJECT_TYPE_NAME (object)); 1411 break; 1412 } 1413 if ((pspec->flags & G_PARAM_CONSTRUCT_ONLY) && !object_in_construction_list (object)) 1414 { 1415 g_warning ("%s: construct property \"%s\" for object `%s' can't be set after construction", 1416 G_STRFUNC, pspec->name, G_OBJECT_TYPE_NAME (object)); 1417 break; 1418 } 1419 1420 g_value_init (&value, G_PARAM_SPEC_VALUE_TYPE (pspec)); 1421 1422 G_VALUE_COLLECT (&value, var_args, 0, &error); 1423 if (error) 1424 { 1425 g_warning ("%s: %s", G_STRFUNC, error); 1426 g_free (error); 1427 g_value_unset (&value); 1428 break; 1429 } 1430 1431 object_set_property (object, pspec, &value, nqueue); 1432 g_value_unset (&value); 1433 1434 name = va_arg (var_args, gchar*); 1435 } 1436 1437 g_object_notify_queue_thaw (object, nqueue); 1438 g_object_unref (object); 1439 } 1440 1441 /** 1442 * g_object_get_valist: 1443 * @object: a #GObject 1444 * @first_property_name: name of the first property to get 1445 * @var_args: return location for the first property, followed optionally by more 1446 * name/return location pairs, followed by %NULL 1447 * 1448 * Gets properties of an object. 1449 * 1450 * In general, a copy is made of the property contents and the caller 1451 * is responsible for freeing the memory in the appropriate manner for 1452 * the type, for instance by calling g_free() or g_object_unref(). 1453 * 1454 * See g_object_get(). 1455 */ 1456 void 1457 g_object_get_valist (GObject *object, 1458 const gchar *first_property_name, 1459 va_list var_args) 1460 { 1461 const gchar *name; 1462 1463 g_return_if_fail (G_IS_OBJECT (object)); 1464 1465 g_object_ref (object); 1466 1467 name = first_property_name; 1468 1469 while (name) 1470 { 1471 GValue value = { 0, }; 1472 GParamSpec *pspec; 1473 gchar *error; 1474 1475 pspec = g_param_spec_pool_lookup (pspec_pool, 1476 name, 1477 G_OBJECT_TYPE (object), 1478 TRUE); 1479 if (!pspec) 1480 { 1481 g_warning ("%s: object class `%s' has no property named `%s'", 1482 G_STRFUNC, 1483 G_OBJECT_TYPE_NAME (object), 1484 name); 1485 break; 1486 } 1487 if (!(pspec->flags & G_PARAM_READABLE)) 1488 { 1489 g_warning ("%s: property `%s' of object class `%s' is not readable", 1490 G_STRFUNC, 1491 pspec->name, 1492 G_OBJECT_TYPE_NAME (object)); 1493 break; 1494 } 1495 1496 g_value_init (&value, G_PARAM_SPEC_VALUE_TYPE (pspec)); 1497 1498 object_get_property (object, pspec, &value); 1499 1500 G_VALUE_LCOPY (&value, var_args, 0, &error); 1501 if (error) 1502 { 1503 g_warning ("%s: %s", G_STRFUNC, error); 1504 g_free (error); 1505 g_value_unset (&value); 1506 break; 1507 } 1508 1509 g_value_unset (&value); 1510 1511 name = va_arg (var_args, gchar*); 1512 } 1513 1514 g_object_unref (object); 1515 } 1516 1517 /** 1518 * g_object_set: 1519 * @object: a #GObject 1520 * @first_property_name: name of the first property to set 1521 * @...: value for the first property, followed optionally by more 1522 * name/value pairs, followed by %NULL 1523 * 1524 * Sets properties on an object. 1525 */ 1526 void 1527 g_object_set (gpointer _object, 1528 const gchar *first_property_name, 1529 ...) 1530 { 1531 GObject *object = _object; 1532 va_list var_args; 1533 1534 g_return_if_fail (G_IS_OBJECT (object)); 1535 1536 va_start (var_args, first_property_name); 1537 g_object_set_valist (object, first_property_name, var_args); 1538 va_end (var_args); 1539 } 1540 1541 /** 1542 * g_object_get: 1543 * @object: a #GObject 1544 * @first_property_name: name of the first property to get 1545 * @...: return location for the first property, followed optionally by more 1546 * name/return location pairs, followed by %NULL 1547 * 1548 * Gets properties of an object. 1549 * 1550 * In general, a copy is made of the property contents and the caller 1551 * is responsible for freeing the memory in the appropriate manner for 1552 * the type, for instance by calling g_free() or g_object_unref(). 1553 * 1554 * <example> 1555 * <title>Using g_object_get(<!-- -->)</title> 1556 * An example of using g_object_get() to get the contents 1557 * of three properties - one of type #G_TYPE_INT, 1558 * one of type #G_TYPE_STRING, and one of type #G_TYPE_OBJECT: 1559 * <programlisting> 1560 * gint intval; 1561 * gchar *strval; 1562 * GObject *objval; 1563 * 1564 * g_object_get (my_object, 1565 * "int-property", &intval, 1566 * "str-property", &strval, 1567 * "obj-property", &objval, 1568 * NULL); 1569 * 1570 * // Do something with intval, strval, objval 1571 * 1572 * g_free (strval); 1573 * g_object_unref (objval); 1574 * </programlisting> 1575 * </example> 1576 */ 1577 void 1578 g_object_get (gpointer _object, 1579 const gchar *first_property_name, 1580 ...) 1581 { 1582 GObject *object = _object; 1583 va_list var_args; 1584 1585 g_return_if_fail (G_IS_OBJECT (object)); 1586 1587 va_start (var_args, first_property_name); 1588 g_object_get_valist (object, first_property_name, var_args); 1589 va_end (var_args); 1590 } 1591 1592 /** 1593 * g_object_set_property: 1594 * @object: a #GObject 1595 * @property_name: the name of the property to set 1596 * @value: the value 1597 * 1598 * Sets a property on an object. 1599 */ 1600 void 1601 g_object_set_property (GObject *object, 1602 const gchar *property_name, 1603 const GValue *value) 1604 { 1605 GObjectNotifyQueue *nqueue; 1606 GParamSpec *pspec; 1607 1608 g_return_if_fail (G_IS_OBJECT (object)); 1609 g_return_if_fail (property_name != NULL); 1610 g_return_if_fail (G_IS_VALUE (value)); 1611 1612 g_object_ref (object); 1613 nqueue = g_object_notify_queue_freeze (object, &property_notify_context); 1614 1615 pspec = g_param_spec_pool_lookup (pspec_pool, 1616 property_name, 1617 G_OBJECT_TYPE (object), 1618 TRUE); 1619 if (!pspec) 1620 g_warning ("%s: object class `%s' has no property named `%s'", 1621 G_STRFUNC, 1622 G_OBJECT_TYPE_NAME (object), 1623 property_name); 1624 else if (!(pspec->flags & G_PARAM_WRITABLE)) 1625 g_warning ("%s: property `%s' of object class `%s' is not writable", 1626 G_STRFUNC, 1627 pspec->name, 1628 G_OBJECT_TYPE_NAME (object)); 1629 else if ((pspec->flags & G_PARAM_CONSTRUCT_ONLY) && !object_in_construction_list (object)) 1630 g_warning ("%s: construct property \"%s\" for object `%s' can't be set after construction", 1631 G_STRFUNC, pspec->name, G_OBJECT_TYPE_NAME (object)); 1632 else 1633 object_set_property (object, pspec, value, nqueue); 1634 1635 g_object_notify_queue_thaw (object, nqueue); 1636 g_object_unref (object); 1637 } 1638 1639 /** 1640 * g_object_get_property: 1641 * @object: a #GObject 1642 * @property_name: the name of the property to get 1643 * @value: return location for the property value 1644 * 1645 * Gets a property of an object. 1646 * 1647 * In general, a copy is made of the property contents and the caller is 1648 * responsible for freeing the memory by calling g_value_unset(). 1649 * 1650 * Note that g_object_get_property() is really intended for language 1651 * bindings, g_object_get() is much more convenient for C programming. 1652 */ 1653 void 1654 g_object_get_property (GObject *object, 1655 const gchar *property_name, 1656 GValue *value) 1657 { 1658 GParamSpec *pspec; 1659 1660 g_return_if_fail (G_IS_OBJECT (object)); 1661 g_return_if_fail (property_name != NULL); 1662 g_return_if_fail (G_IS_VALUE (value)); 1663 1664 g_object_ref (object); 1665 1666 pspec = g_param_spec_pool_lookup (pspec_pool, 1667 property_name, 1668 G_OBJECT_TYPE (object), 1669 TRUE); 1670 if (!pspec) 1671 g_warning ("%s: object class `%s' has no property named `%s'", 1672 G_STRFUNC, 1673 G_OBJECT_TYPE_NAME (object), 1674 property_name); 1675 else if (!(pspec->flags & G_PARAM_READABLE)) 1676 g_warning ("%s: property `%s' of object class `%s' is not readable", 1677 G_STRFUNC, 1678 pspec->name, 1679 G_OBJECT_TYPE_NAME (object)); 1680 else 1681 { 1682 GValue *prop_value, tmp_value = { 0, }; 1683 1684 /* auto-conversion of the callers value type 1685 */ 1686 if (G_VALUE_TYPE (value) == G_PARAM_SPEC_VALUE_TYPE (pspec)) 1687 { 1688 g_value_reset (value); 1689 prop_value = value; 1690 } 1691 else if (!g_value_type_transformable (G_PARAM_SPEC_VALUE_TYPE (pspec), G_VALUE_TYPE (value))) 1692 { 1693 g_warning ("%s: can't retrieve property `%s' of type `%s' as value of type `%s'", 1694 G_STRFUNC, pspec->name, 1695 g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspec)), 1696 G_VALUE_TYPE_NAME (value)); 1697 g_object_unref (object); 1698 return; 1699 } 1700 else 1701 { 1702 g_value_init (&tmp_value, G_PARAM_SPEC_VALUE_TYPE (pspec)); 1703 prop_value = &tmp_value; 1704 } 1705 object_get_property (object, pspec, prop_value); 1706 if (prop_value != value) 1707 { 1708 g_value_transform (prop_value, value); 1709 g_value_unset (&tmp_value); 1710 } 1711 } 1712 1713 g_object_unref (object); 1714 } 1715 1716 /** 1717 * g_object_connect: 1718 * @object: a #GObject 1719 * @signal_spec: the spec for the first signal 1720 * @...: #GCallback for the first signal, followed by data for the 1721 * first signal, followed optionally by more signal 1722 * spec/callback/data triples, followed by %NULL 1723 * 1724 * A convenience function to connect multiple signals at once. 1725 * 1726 * The signal specs expected by this function have the form 1727 * "modifier::signal_name", where modifier can be one of the following: 1728 * <variablelist> 1729 * <varlistentry> 1730 * <term>signal</term> 1731 * <listitem><para> 1732 * equivalent to <literal>g_signal_connect_data (..., NULL, 0)</literal> 1733 * </para></listitem> 1734 * </varlistentry> 1735 * <varlistentry> 1736 * <term>object_signal</term> 1737 * <term>object-signal</term> 1738 * <listitem><para> 1739 * equivalent to <literal>g_signal_connect_object (..., 0)</literal> 1740 * </para></listitem> 1741 * </varlistentry> 1742 * <varlistentry> 1743 * <term>swapped_signal</term> 1744 * <term>swapped-signal</term> 1745 * <listitem><para> 1746 * equivalent to <literal>g_signal_connect_data (..., NULL, G_CONNECT_SWAPPED)</literal> 1747 * </para></listitem> 1748 * </varlistentry> 1749 * <varlistentry> 1750 * <term>swapped_object_signal</term> 1751 * <term>swapped-object-signal</term> 1752 * <listitem><para> 1753 * equivalent to <literal>g_signal_connect_object (..., G_CONNECT_SWAPPED)</literal> 1754 * </para></listitem> 1755 * </varlistentry> 1756 * <varlistentry> 1757 * <term>signal_after</term> 1758 * <term>signal-after</term> 1759 * <listitem><para> 1760 * equivalent to <literal>g_signal_connect_data (..., NULL, G_CONNECT_AFTER)</literal> 1761 * </para></listitem> 1762 * </varlistentry> 1763 * <varlistentry> 1764 * <term>object_signal_after</term> 1765 * <term>object-signal-after</term> 1766 * <listitem><para> 1767 * equivalent to <literal>g_signal_connect_object (..., G_CONNECT_AFTER)</literal> 1768 * </para></listitem> 1769 * </varlistentry> 1770 * <varlistentry> 1771 * <term>swapped_signal_after</term> 1772 * <term>swapped-signal-after</term> 1773 * <listitem><para> 1774 * equivalent to <literal>g_signal_connect_data (..., NULL, G_CONNECT_SWAPPED | G_CONNECT_AFTER)</literal> 1775 * </para></listitem> 1776 * </varlistentry> 1777 * <varlistentry> 1778 * <term>swapped_object_signal_after</term> 1779 * <term>swapped-object-signal-after</term> 1780 * <listitem><para> 1781 * equivalent to <literal>g_signal_connect_object (..., G_CONNECT_SWAPPED | G_CONNECT_AFTER)</literal> 1782 * </para></listitem> 1783 * </varlistentry> 1784 * </variablelist> 1785 * 1786 * |[ 1787 * menu->toplevel = g_object_connect (g_object_new (GTK_TYPE_WINDOW, 1788 * "type", GTK_WINDOW_POPUP, 1789 * "child", menu, 1790 * NULL), 1791 * "signal::event", gtk_menu_window_event, menu, 1792 * "signal::size_request", gtk_menu_window_size_request, menu, 1793 * "signal::destroy", gtk_widget_destroyed, &menu->toplevel, 1794 * NULL); 1795 * ]| 1796 * 1797 * Returns: @object 1798 */ 1799 gpointer 1800 g_object_connect (gpointer _object, 1801 const gchar *signal_spec, 1802 ...) 1803 { 1804 GObject *object = _object; 1805 va_list var_args; 1806 1807 g_return_val_if_fail (G_IS_OBJECT (object), NULL); 1808 g_return_val_if_fail (object->ref_count > 0, object); 1809 1810 va_start (var_args, signal_spec); 1811 while (signal_spec) 1812 { 1813 GCallback callback = va_arg (var_args, GCallback); 1814 gpointer data = va_arg (var_args, gpointer); 1815 gulong sid; 1816 1817 if (strncmp (signal_spec, "signal::", 8) == 0) 1818 sid = g_signal_connect_data (object, signal_spec + 8, 1819 callback, data, NULL, 1820 0); 1821 else if (strncmp (signal_spec, "object_signal::", 15) == 0 || 1822 strncmp (signal_spec, "object-signal::", 15) == 0) 1823 sid = g_signal_connect_object (object, signal_spec + 15, 1824 callback, data, 1825 0); 1826 else if (strncmp (signal_spec, "swapped_signal::", 16) == 0 || 1827 strncmp (signal_spec, "swapped-signal::", 16) == 0) 1828 sid = g_signal_connect_data (object, signal_spec + 16, 1829 callback, data, NULL, 1830 G_CONNECT_SWAPPED); 1831 else if (strncmp (signal_spec, "swapped_object_signal::", 23) == 0 || 1832 strncmp (signal_spec, "swapped-object-signal::", 23) == 0) 1833 sid = g_signal_connect_object (object, signal_spec + 23, 1834 callback, data, 1835 G_CONNECT_SWAPPED); 1836 else if (strncmp (signal_spec, "signal_after::", 14) == 0 || 1837 strncmp (signal_spec, "signal-after::", 14) == 0) 1838 sid = g_signal_connect_data (object, signal_spec + 14, 1839 callback, data, NULL, 1840 G_CONNECT_AFTER); 1841 else if (strncmp (signal_spec, "object_signal_after::", 21) == 0 || 1842 strncmp (signal_spec, "object-signal-after::", 21) == 0) 1843 sid = g_signal_connect_object (object, signal_spec + 21, 1844 callback, data, 1845 G_CONNECT_AFTER); 1846 else if (strncmp (signal_spec, "swapped_signal_after::", 22) == 0 || 1847 strncmp (signal_spec, "swapped-signal-after::", 22) == 0) 1848 sid = g_signal_connect_data (object, signal_spec + 22, 1849 callback, data, NULL, 1850 G_CONNECT_SWAPPED | G_CONNECT_AFTER); 1851 else if (strncmp (signal_spec, "swapped_object_signal_after::", 29) == 0 || 1852 strncmp (signal_spec, "swapped-object-signal-after::", 29) == 0) 1853 sid = g_signal_connect_object (object, signal_spec + 29, 1854 callback, data, 1855 G_CONNECT_SWAPPED | G_CONNECT_AFTER); 1856 else 1857 { 1858 g_warning ("%s: invalid signal spec \"%s\"", G_STRFUNC, signal_spec); 1859 break; 1860 } 1861 signal_spec = va_arg (var_args, gchar*); 1862 } 1863 va_end (var_args); 1864 1865 return object; 1866 } 1867 1868 /** 1869 * g_object_disconnect: 1870 * @object: a #GObject 1871 * @signal_spec: the spec for the first signal 1872 * @...: #GCallback for the first signal, followed by data for the first signal, 1873 * followed optionally by more signal spec/callback/data triples, 1874 * followed by %NULL 1875 * 1876 * A convenience function to disconnect multiple signals at once. 1877 * 1878 * The signal specs expected by this function have the form 1879 * "any_signal", which means to disconnect any signal with matching 1880 * callback and data, or "any_signal::signal_name", which only 1881 * disconnects the signal named "signal_name". 1882 */ 1883 void 1884 g_object_disconnect (gpointer _object, 1885 const gchar *signal_spec, 1886 ...) 1887 { 1888 GObject *object = _object; 1889 va_list var_args; 1890 1891 g_return_if_fail (G_IS_OBJECT (object)); 1892 g_return_if_fail (object->ref_count > 0); 1893 1894 va_start (var_args, signal_spec); 1895 while (signal_spec) 1896 { 1897 GCallback callback = va_arg (var_args, GCallback); 1898 gpointer data = va_arg (var_args, gpointer); 1899 guint sid = 0, detail = 0, mask = 0; 1900 1901 if (strncmp (signal_spec, "any_signal::", 12) == 0 || 1902 strncmp (signal_spec, "any-signal::", 12) == 0) 1903 { 1904 signal_spec += 12; 1905 mask = G_SIGNAL_MATCH_ID | G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA; 1906 } 1907 else if (strcmp (signal_spec, "any_signal") == 0 || 1908 strcmp (signal_spec, "any-signal") == 0) 1909 { 1910 signal_spec += 10; 1911 mask = G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA; 1912 } 1913 else 1914 { 1915 g_warning ("%s: invalid signal spec \"%s\"", G_STRFUNC, signal_spec); 1916 break; 1917 } 1918 1919 if ((mask & G_SIGNAL_MATCH_ID) && 1920 !g_signal_parse_name (signal_spec, G_OBJECT_TYPE (object), &sid, &detail, FALSE)) 1921 g_warning ("%s: invalid signal name \"%s\"", G_STRFUNC, signal_spec); 1922 else if (!g_signal_handlers_disconnect_matched (object, mask | (detail ? G_SIGNAL_MATCH_DETAIL : 0), 1923 sid, detail, 1924 NULL, (gpointer)callback, data)) 1925 g_warning ("%s: signal handler %p(%p) is not connected", G_STRFUNC, callback, data); 1926 signal_spec = va_arg (var_args, gchar*); 1927 } 1928 va_end (var_args); 1929 } 1930 1931 typedef struct { 1932 GObject *object; 1933 guint n_weak_refs; 1934 struct { 1935 GWeakNotify notify; 1936 gpointer data; 1937 } weak_refs[1]; /* flexible array */ 1938 } WeakRefStack; 1939 1940 static void 1941 weak_refs_notify (gpointer data) 1942 { 1943 WeakRefStack *wstack = data; 1944 guint i; 1945 1946 for (i = 0; i < wstack->n_weak_refs; i++) 1947 wstack->weak_refs[i].notify (wstack->weak_refs[i].data, wstack->object); 1948 g_free (wstack); 1949 } 1950 1951 /** 1952 * g_object_weak_ref: 1953 * @object: #GObject to reference weakly 1954 * @notify: callback to invoke before the object is freed 1955 * @data: extra data to pass to notify 1956 * 1957 * Adds a weak reference callback to an object. Weak references are 1958 * used for notification when an object is finalized. They are called 1959 * "weak references" because they allow you to safely hold a pointer 1960 * to an object without calling g_object_ref() (g_object_ref() adds a 1961 * strong reference, that is, forces the object to stay alive). 1962 */ 1963 void 1964 g_object_weak_ref (GObject *object, 1965 GWeakNotify notify, 1966 gpointer data) 1967 { 1968 WeakRefStack *wstack; 1969 guint i; 1970 1971 g_return_if_fail (G_IS_OBJECT (object)); 1972 g_return_if_fail (notify != NULL); 1973 g_return_if_fail (object->ref_count >= 1); 1974 1975 wstack = g_datalist_id_remove_no_notify (&object->qdata, quark_weak_refs); 1976 if (wstack) 1977 { 1978 i = wstack->n_weak_refs++; 1979 wstack = g_realloc (wstack, sizeof (*wstack) + sizeof (wstack->weak_refs[0]) * i); 1980 } 1981 else 1982 { 1983 wstack = g_renew (WeakRefStack, NULL, 1); 1984 wstack->object = object; 1985 wstack->n_weak_refs = 1; 1986 i = 0; 1987 } 1988 wstack->weak_refs[i].notify = notify; 1989 wstack->weak_refs[i].data = data; 1990 g_datalist_id_set_data_full (&object->qdata, quark_weak_refs, wstack, weak_refs_notify); 1991 } 1992 1993 /** 1994 * g_object_weak_unref: 1995 * @object: #GObject to remove a weak reference from 1996 * @notify: callback to search for 1997 * @data: data to search for 1998 * 1999 * Removes a weak reference callback to an object. 2000 */ 2001 void 2002 g_object_weak_unref (GObject *object, 2003 GWeakNotify notify, 2004 gpointer data) 2005 { 2006 WeakRefStack *wstack; 2007 gboolean found_one = FALSE; 2008 2009 g_return_if_fail (G_IS_OBJECT (object)); 2010 g_return_if_fail (notify != NULL); 2011 2012 wstack = g_datalist_id_get_data (&object->qdata, quark_weak_refs); 2013 if (wstack) 2014 { 2015 guint i; 2016 2017 for (i = 0; i < wstack->n_weak_refs; i++) 2018 if (wstack->weak_refs[i].notify == notify && 2019 wstack->weak_refs[i].data == data) 2020 { 2021 found_one = TRUE; 2022 wstack->n_weak_refs -= 1; 2023 if (i != wstack->n_weak_refs) 2024 wstack->weak_refs[i] = wstack->weak_refs[wstack->n_weak_refs]; 2025 2026 break; 2027 } 2028 } 2029 if (!found_one) 2030 g_warning ("%s: couldn't find weak ref %p(%p)", G_STRFUNC, notify, data); 2031 } 2032 2033 /** 2034 * g_object_add_weak_pointer: 2035 * @object: The object that should be weak referenced. 2036 * @weak_pointer_location: The memory address of a pointer. 2037 * 2038 * Adds a weak reference from weak_pointer to @object to indicate that 2039 * the pointer located at @weak_pointer_location is only valid during 2040 * the lifetime of @object. When the @object is finalized, 2041 * @weak_pointer will be set to %NULL. 2042 */ 2043 void 2044 g_object_add_weak_pointer (GObject *object, 2045 gpointer *weak_pointer_location) 2046 { 2047 g_return_if_fail (G_IS_OBJECT (object)); 2048 g_return_if_fail (weak_pointer_location != NULL); 2049 2050 g_object_weak_ref (object, 2051 (GWeakNotify) g_nullify_pointer, 2052 weak_pointer_location); 2053 } 2054 2055 /** 2056 * g_object_remove_weak_pointer: 2057 * @object: The object that is weak referenced. 2058 * @weak_pointer_location: The memory address of a pointer. 2059 * 2060 * Removes a weak reference from @object that was previously added 2061 * using g_object_add_weak_pointer(). The @weak_pointer_location has 2062 * to match the one used with g_object_add_weak_pointer(). 2063 */ 2064 void 2065 g_object_remove_weak_pointer (GObject *object, 2066 gpointer *weak_pointer_location) 2067 { 2068 g_return_if_fail (G_IS_OBJECT (object)); 2069 g_return_if_fail (weak_pointer_location != NULL); 2070 2071 g_object_weak_unref (object, 2072 (GWeakNotify) g_nullify_pointer, 2073 weak_pointer_location); 2074 } 2075 2076 static guint 2077 object_floating_flag_handler (GObject *object, 2078 gint job) 2079 { 2080 switch (job) 2081 { 2082 gpointer oldvalue; 2083 case +1: /* force floating if possible */ 2084 do 2085 oldvalue = g_atomic_pointer_get (&object->qdata); 2086 while (!g_atomic_pointer_compare_and_exchange ((void**) &object->qdata, oldvalue, 2087 (gpointer) ((gsize) oldvalue | OBJECT_FLOATING_FLAG))); 2088 return (gsize) oldvalue & OBJECT_FLOATING_FLAG; 2089 case -1: /* sink if possible */ 2090 do 2091 oldvalue = g_atomic_pointer_get (&object->qdata); 2092 while (!g_atomic_pointer_compare_and_exchange ((void**) &object->qdata, oldvalue, 2093 (gpointer) ((gsize) oldvalue & ~(gsize) OBJECT_FLOATING_FLAG))); 2094 return (gsize) oldvalue & OBJECT_FLOATING_FLAG; 2095 default: /* check floating */ 2096 return 0 != ((gsize) g_atomic_pointer_get (&object->qdata) & OBJECT_FLOATING_FLAG); 2097 } 2098 } 2099 2100 /** 2101 * g_object_is_floating: 2102 * @object: a #GObject 2103 * 2104 * Checks wether @object has a <link linkend="floating-ref">floating</link> 2105 * reference. 2106 * 2107 * Since: 2.10 2108 * 2109 * Returns: %TRUE if @object has a floating reference 2110 */ 2111 gboolean 2112 g_object_is_floating (gpointer _object) 2113 { 2114 GObject *object = _object; 2115 g_return_val_if_fail (G_IS_OBJECT (object), FALSE); 2116 return floating_flag_handler (object, 0); 2117 } 2118 2119 /** 2120 * g_object_ref_sink: 2121 * @object: a #GObject 2122 * 2123 * Increase the reference count of @object, and possibly remove the 2124 * <link linkend="floating-ref">floating</link> reference, if @object 2125 * has a floating reference. 2126 * 2127 * In other words, if the object is floating, then this call "assumes 2128 * ownership" of the floating reference, converting it to a normal 2129 * reference by clearing the floating flag while leaving the reference 2130 * count unchanged. If the object is not floating, then this call 2131 * adds a new normal reference increasing the reference count by one. 2132 * 2133 * Since: 2.10 2134 * 2135 * Returns: @object 2136 */ 2137 gpointer 2138 g_object_ref_sink (gpointer _object) 2139 { 2140 GObject *object = _object; 2141 gboolean was_floating; 2142 g_return_val_if_fail (G_IS_OBJECT (object), object); 2143 g_return_val_if_fail (object->ref_count >= 1, object); 2144 g_object_ref (object); 2145 was_floating = floating_flag_handler (object, -1); 2146 if (was_floating) 2147 g_object_unref (object); 2148 return object; 2149 } 2150 2151 /** 2152 * g_object_force_floating: 2153 * @object: a #GObject 2154 * 2155 * This function is intended for #GObject implementations to re-enforce a 2156 * <link linkend="floating-ref">floating</link> object reference. 2157 * Doing this is seldomly required, all 2158 * #GInitiallyUnowned<!-- -->s are created with a floating reference which 2159 * usually just needs to be sunken by calling g_object_ref_sink(). 2160 * 2161 * Since: 2.10 2162 */ 2163 void 2164 g_object_force_floating (GObject *object) 2165 { 2166 gboolean was_floating; 2167 g_return_if_fail (G_IS_OBJECT (object)); 2168 g_return_if_fail (object->ref_count >= 1); 2169 2170 was_floating = floating_flag_handler (object, +1); 2171 } 2172 2173 typedef struct { 2174 GObject *object; 2175 guint n_toggle_refs; 2176 struct { 2177 GToggleNotify notify; 2178 gpointer data; 2179 } toggle_refs[1]; /* flexible array */ 2180 } ToggleRefStack; 2181 2182 static void 2183 toggle_refs_notify (GObject *object, 2184 gboolean is_last_ref) 2185 { 2186 ToggleRefStack *tstack = g_datalist_id_get_data (&object->qdata, quark_toggle_refs); 2187 2188 /* Reentrancy here is not as tricky as it seems, because a toggle reference 2189 * will only be notified when there is exactly one of them. 2190 */ 2191 g_assert (tstack->n_toggle_refs == 1); 2192 tstack->toggle_refs[0].notify (tstack->toggle_refs[0].data, tstack->object, is_last_ref); 2193 } 2194 2195 /** 2196 * g_object_add_toggle_ref: 2197 * @object: a #GObject 2198 * @notify: a function to call when this reference is the 2199 * last reference to the object, or is no longer 2200 * the last reference. 2201 * @data: data to pass to @notify 2202 * 2203 * Increases the reference count of the object by one and sets a 2204 * callback to be called when all other references to the object are 2205 * dropped, or when this is already the last reference to the object 2206 * and another reference is established. 2207 * 2208 * This functionality is intended for binding @object to a proxy 2209 * object managed by another memory manager. This is done with two 2210 * paired references: the strong reference added by 2211 * g_object_add_toggle_ref() and a reverse reference to the proxy 2212 * object which is either a strong reference or weak reference. 2213 * 2214 * The setup is that when there are no other references to @object, 2215 * only a weak reference is held in the reverse direction from @object 2216 * to the proxy object, but when there are other references held to 2217 * @object, a strong reference is held. The @notify callback is called 2218 * when the reference from @object to the proxy object should be 2219 * <firstterm>toggled</firstterm> from strong to weak (@is_last_ref 2220 * true) or weak to strong (@is_last_ref false). 2221 * 2222 * Since a (normal) reference must be held to the object before 2223 * calling g_object_toggle_ref(), the initial state of the reverse 2224 * link is always strong. 2225 * 2226 * Multiple toggle references may be added to the same gobject, 2227 * however if there are multiple toggle references to an object, none 2228 * of them will ever be notified until all but one are removed. For 2229 * this reason, you should only ever use a toggle reference if there 2230 * is important state in the proxy object. 2231 * 2232 * Since: 2.8 2233 */ 2234 void 2235 g_object_add_toggle_ref (GObject *object, 2236 GToggleNotify notify, 2237 gpointer data) 2238 { 2239 ToggleRefStack *tstack; 2240 guint i; 2241 2242 g_return_if_fail (G_IS_OBJECT (object)); 2243 g_return_if_fail (notify != NULL); 2244 g_return_if_fail (object->ref_count >= 1); 2245 2246 g_object_ref (object); 2247 2248 tstack = g_datalist_id_remove_no_notify (&object->qdata, quark_toggle_refs); 2249 if (tstack) 2250 { 2251 i = tstack->n_toggle_refs++; 2252 /* allocate i = tstate->n_toggle_refs - 1 positions beyond the 1 declared 2253 * in tstate->toggle_refs */ 2254 tstack = g_realloc (tstack, sizeof (*tstack) + sizeof (tstack->toggle_refs[0]) * i); 2255 } 2256 else 2257 { 2258 tstack = g_renew (ToggleRefStack, NULL, 1); 2259 tstack->object = object; 2260 tstack->n_toggle_refs = 1; 2261 i = 0; 2262 } 2263 2264 /* Set a flag for fast lookup after adding the first toggle reference */ 2265 if (tstack->n_toggle_refs == 1) 2266 g_datalist_set_flags (&object->qdata, OBJECT_HAS_TOGGLE_REF_FLAG); 2267 2268 tstack->toggle_refs[i].notify = notify; 2269 tstack->toggle_refs[i].data = data; 2270 g_datalist_id_set_data_full (&object->qdata, quark_toggle_refs, tstack, 2271 (GDestroyNotify)g_free); 2272 } 2273 2274 /** 2275 * g_object_remove_toggle_ref: 2276 * @object: a #GObject 2277 * @notify: a function to call when this reference is the 2278 * last reference to the object, or is no longer 2279 * the last reference. 2280 * @data: data to pass to @notify 2281 * 2282 * Removes a reference added with g_object_add_toggle_ref(). The 2283 * reference count of the object is decreased by one. 2284 * 2285 * Since: 2.8 2286 */ 2287 void 2288 g_object_remove_toggle_ref (GObject *object, 2289 GToggleNotify notify, 2290 gpointer data) 2291 { 2292 ToggleRefStack *tstack; 2293 gboolean found_one = FALSE; 2294 2295 g_return_if_fail (G_IS_OBJECT (object)); 2296 g_return_if_fail (notify != NULL); 2297 2298 tstack = g_datalist_id_get_data (&object->qdata, quark_toggle_refs); 2299 if (tstack) 2300 { 2301 guint i; 2302 2303 for (i = 0; i < tstack->n_toggle_refs; i++) 2304 if (tstack->toggle_refs[i].notify == notify && 2305 tstack->toggle_refs[i].data == data) 2306 { 2307 found_one = TRUE; 2308 tstack->n_toggle_refs -= 1; 2309 if (i != tstack->n_toggle_refs) 2310 tstack->toggle_refs[i] = tstack->toggle_refs[tstack->n_toggle_refs]; 2311 2312 if (tstack->n_toggle_refs == 0) 2313 g_datalist_unset_flags (&object->qdata, OBJECT_HAS_TOGGLE_REF_FLAG); 2314 2315 g_object_unref (object); 2316 2317 break; 2318 } 2319 } 2320 2321 if (!found_one) 2322 g_warning ("%s: couldn't find toggle ref %p(%p)", G_STRFUNC, notify, data); 2323 } 2324 2325 /** 2326 * g_object_ref: 2327 * @object: a #GObject 2328 * 2329 * Increases the reference count of @object. 2330 * 2331 * Returns: the same @object 2332 */ 2333 gpointer 2334 g_object_ref (gpointer _object) 2335 { 2336 GObject *object = _object; 2337 gint old_val; 2338 2339 g_return_val_if_fail (G_IS_OBJECT (object), NULL); 2340 g_return_val_if_fail (object->ref_count > 0, NULL); 2341 2342 #ifdef G_ENABLE_DEBUG 2343 if (g_trap_object_ref == object) 2344 G_BREAKPOINT (); 2345 #endif /* G_ENABLE_DEBUG */ 2346 2347 2348 old_val = g_atomic_int_exchange_and_add ((int *)&object->ref_count, 1); 2349 2350 if (old_val == 1 && OBJECT_HAS_TOGGLE_REF (object)) 2351 toggle_refs_notify (object, FALSE); 2352 2353 return object; 2354 } 2355 2356 /** 2357 * g_object_unref: 2358 * @object: a #GObject 2359 * 2360 * Decreases the reference count of @object. When its reference count 2361 * drops to 0, the object is finalized (i.e. its memory is freed). 2362 */ 2363 void 2364 g_object_unref (gpointer _object) 2365 { 2366 GObject *object = _object; 2367 gint old_ref; 2368 gboolean is_zero; 2369 2370 g_return_if_fail (G_IS_OBJECT (object)); 2371 g_return_if_fail (object->ref_count > 0); 2372 2373 #ifdef G_ENABLE_DEBUG 2374 if (g_trap_object_ref == object) 2375 G_BREAKPOINT (); 2376 #endif /* G_ENABLE_DEBUG */ 2377 2378 /* here we want to atomically do: if (ref_count>1) { ref_count--; return; } */ 2379 retry_atomic_decrement1: 2380 old_ref = g_atomic_int_get (&object->ref_count); 2381 if (old_ref > 1) 2382 { 2383 if (!g_atomic_int_compare_and_exchange ((int *)&object->ref_count, old_ref, old_ref - 1)) 2384 goto retry_atomic_decrement1; 2385 2386 /* if we went from 2->1 we need to notify toggle refs if any */ 2387 if (old_ref == 2 && OBJECT_HAS_TOGGLE_REF (object)) 2388 toggle_refs_notify (object, TRUE); 2389 } 2390 else 2391 { 2392 /* we are about tp remove the last reference */ 2393 G_OBJECT_GET_CLASS (object)->dispose (object); 2394 2395 /* may have been re-referenced meanwhile */ 2396 retry_atomic_decrement2: 2397 old_ref = g_atomic_int_get ((int *)&object->ref_count); 2398 if (old_ref > 1) 2399 { 2400 if (!g_atomic_int_compare_and_exchange ((int *)&object->ref_count, old_ref, old_ref - 1)) 2401 goto retry_atomic_decrement2; 2402 2403 /* if we went from 2->1 we need to notify toggle refs if any */ 2404 if (old_ref == 2 && OBJECT_HAS_TOGGLE_REF (object)) 2405 toggle_refs_notify (object, TRUE); 2406 2407 return; 2408 } 2409 2410 /* we are still in the process of taking away the last ref */ 2411 g_datalist_id_set_data (&object->qdata, quark_closure_array, NULL); 2412 g_signal_handlers_destroy (object); 2413 g_datalist_id_set_data (&object->qdata, quark_weak_refs, NULL); 2414 2415 /* decrement the last reference */ 2416 is_zero = g_atomic_int_dec_and_test ((int *)&object->ref_count); 2417 2418 /* may have been re-referenced meanwhile */ 2419 if (G_LIKELY (is_zero)) 2420 { 2421 G_OBJECT_GET_CLASS (object)->finalize (object); 2422 #ifdef G_ENABLE_DEBUG 2423 IF_DEBUG (OBJECTS) 2424 { 2425 /* catch objects not chaining finalize handlers */ 2426 G_LOCK (debug_objects); 2427 g_assert (g_hash_table_lookup (debug_objects_ht, object) == NULL); 2428 G_UNLOCK (debug_objects); 2429 } 2430 #endif /* G_ENABLE_DEBUG */ 2431 g_type_free_instance ((GTypeInstance*) object); 2432 } 2433 } 2434 } 2435 2436 /** 2437 * g_object_get_qdata: 2438 * @object: The GObject to get a stored user data pointer from 2439 * @quark: A #GQuark, naming the user data pointer 2440 * 2441 * This function gets back user data pointers stored via 2442 * g_object_set_qdata(). 2443 * 2444 * Returns: The user data pointer set, or %NULL 2445 */ 2446 gpointer 2447 g_object_get_qdata (GObject *object, 2448 GQuark quark) 2449 { 2450 g_return_val_if_fail (G_IS_OBJECT (object), NULL); 2451 2452 return quark ? g_datalist_id_get_data (&object->qdata, quark) : NULL; 2453 } 2454 2455 /** 2456 * g_object_set_qdata: 2457 * @object: The GObject to set store a user data pointer 2458 * @quark: A #GQuark, naming the user data pointer 2459 * @data: An opaque user data pointer 2460 * 2461 * This sets an opaque, named pointer on an object. 2462 * The name is specified through a #GQuark (retrived e.g. via 2463 * g_quark_from_static_string()), and the pointer 2464 * can be gotten back from the @object with g_object_get_qdata() 2465 * until the @object is finalized. 2466 * Setting a previously set user data pointer, overrides (frees) 2467 * the old pointer set, using #NULL as pointer essentially 2468 * removes the data stored. 2469 */ 2470 void 2471 g_object_set_qdata (GObject *object, 2472 GQuark quark, 2473 gpointer data) 2474 { 2475 g_return_if_fail (G_IS_OBJECT (object)); 2476 g_return_if_fail (quark > 0); 2477 2478 g_datalist_id_set_data (&object->qdata, quark, data); 2479 } 2480 2481 /** 2482 * g_object_set_qdata_full: 2483 * @object: The GObject to set store a user data pointer 2484 * @quark: A #GQuark, naming the user data pointer 2485 * @data: An opaque user data pointer 2486 * @destroy: Function to invoke with @data as argument, when @data 2487 * needs to be freed 2488 * 2489 * This function works like g_object_set_qdata(), but in addition, 2490 * a void (*destroy) (gpointer) function may be specified which is 2491 * called with @data as argument when the @object is finalized, or 2492 * the data is being overwritten by a call to g_object_set_qdata() 2493 * with the same @quark. 2494 */ 2495 void 2496 g_object_set_qdata_full (GObject *object, 2497 GQuark quark, 2498 gpointer data, 2499 GDestroyNotify destroy) 2500 { 2501 g_return_if_fail (G_IS_OBJECT (object)); 2502 g_return_if_fail (quark > 0); 2503 2504 g_datalist_id_set_data_full (&object->qdata, quark, data, 2505 data ? destroy : (GDestroyNotify) NULL); 2506 } 2507 2508 /** 2509 * g_object_steal_qdata: 2510 * @object: The GObject to get a stored user data pointer from 2511 * @quark: A #GQuark, naming the user data pointer 2512 * 2513 * This function gets back user data pointers stored via 2514 * g_object_set_qdata() and removes the @data from object 2515 * without invoking its destroy() function (if any was 2516 * set). 2517 * Usually, calling this function is only required to update 2518 * user data pointers with a destroy notifier, for example: 2519 * |[ 2520 * void 2521 * object_add_to_user_list (GObject *object, 2522 * const gchar *new_string) 2523 * { 2524 * // the quark, naming the object data 2525 * GQuark quark_string_list = g_quark_from_static_string ("my-string-list"); 2526 * // retrive the old string list 2527 * GList *list = g_object_steal_qdata (object, quark_string_list); 2528 * 2529 * // prepend new string 2530 * list = g_list_prepend (list, g_strdup (new_string)); 2531 * // this changed 'list', so we need to set it again 2532 * g_object_set_qdata_full (object, quark_string_list, list, free_string_list); 2533 * } 2534 * static void 2535 * free_string_list (gpointer data) 2536 * { 2537 * GList *node, *list = data; 2538 * 2539 * for (node = list; node; node = node->next) 2540 * g_free (node->data); 2541 * g_list_free (list); 2542 * } 2543 * ]| 2544 * Using g_object_get_qdata() in the above example, instead of 2545 * g_object_steal_qdata() would have left the destroy function set, 2546 * and thus the partial string list would have been freed upon 2547 * g_object_set_qdata_full(). 2548 * 2549 * Returns: The user data pointer set, or %NULL 2550 */ 2551 gpointer 2552 g_object_steal_qdata (GObject *object, 2553 GQuark quark) 2554 { 2555 g_return_val_if_fail (G_IS_OBJECT (object), NULL); 2556 g_return_val_if_fail (quark > 0, NULL); 2557 2558 return g_datalist_id_remove_no_notify (&object->qdata, quark); 2559 } 2560 2561 /** 2562 * g_object_get_data: 2563 * @object: #GObject containing the associations 2564 * @key: name of the key for that association 2565 * 2566 * Gets a named field from the objects table of associations (see g_object_set_data()). 2567 * 2568 * Returns: the data if found, or %NULL if no such data exists. 2569 */ 2570 gpointer 2571 g_object_get_data (GObject *object, 2572 const gchar *key) 2573 { 2574 GQuark quark; 2575 2576 g_return_val_if_fail (G_IS_OBJECT (object), NULL); 2577 g_return_val_if_fail (key != NULL, NULL); 2578 2579 quark = g_quark_try_string (key); 2580 2581 return quark ? g_datalist_id_get_data (&object->qdata, quark) : NULL; 2582 } 2583 2584 /** 2585 * g_object_set_data: 2586 * @object: #GObject containing the associations. 2587 * @key: name of the key 2588 * @data: data to associate with that key 2589 * 2590 * Each object carries around a table of associations from 2591 * strings to pointers. This function lets you set an association. 2592 * 2593 * If the object already had an association with that name, 2594 * the old association will be destroyed. 2595 */ 2596 void 2597 g_object_set_data (GObject *object, 2598 const gchar *key, 2599 gpointer data) 2600 { 2601 g_return_if_fail (G_IS_OBJECT (object)); 2602 g_return_if_fail (key != NULL); 2603 2604 g_datalist_id_set_data (&object->qdata, g_quark_from_string (key), data); 2605 } 2606 2607 /** 2608 * g_object_set_data_full: 2609 * @object: #GObject containing the associations 2610 * @key: name of the key 2611 * @data: data to associate with that key 2612 * @destroy: function to call when the association is destroyed 2613 * 2614 * Like g_object_set_data() except it adds notification 2615 * for when the association is destroyed, either by setting it 2616 * to a different value or when the object is destroyed. 2617 * 2618 * Note that the @destroy callback is not called if @data is %NULL. 2619 */ 2620 void 2621 g_object_set_data_full (GObject *object, 2622 const gchar *key, 2623 gpointer data, 2624 GDestroyNotify destroy) 2625 { 2626 g_return_if_fail (G_IS_OBJECT (object)); 2627 g_return_if_fail (key != NULL); 2628 2629 g_datalist_id_set_data_full (&object->qdata, g_quark_from_string (key), data, 2630 data ? destroy : (GDestroyNotify) NULL); 2631 } 2632 2633 /** 2634 * g_object_steal_data: 2635 * @object: #GObject containing the associations 2636 * @key: name of the key 2637 * 2638 * Remove a specified datum from the object's data associations, 2639 * without invoking the association's destroy handler. 2640 * 2641 * Returns: the data if found, or %NULL if no such data exists. 2642 */ 2643 gpointer 2644 g_object_steal_data (GObject *object, 2645 const gchar *key) 2646 { 2647 GQuark quark; 2648 2649 g_return_val_if_fail (G_IS_OBJECT (object), NULL); 2650 g_return_val_if_fail (key != NULL, NULL); 2651 2652 quark = g_quark_try_string (key); 2653 2654 return quark ? g_datalist_id_remove_no_notify (&object->qdata, quark) : NULL; 2655 } 2656 2657 static void 2658 g_value_object_init (GValue *value) 2659 { 2660 value->data[0].v_pointer = NULL; 2661 } 2662 2663 static void 2664 g_value_object_free_value (GValue *value) 2665 { 2666 if (value->data[0].v_pointer) 2667 g_object_unref (value->data[0].v_pointer); 2668 } 2669 2670 static void 2671 g_value_object_copy_value (const GValue *src_value, 2672 GValue *dest_value) 2673 { 2674 if (src_value->data[0].v_pointer) 2675 dest_value->data[0].v_pointer = g_object_ref (src_value->data[0].v_pointer); 2676 else 2677 dest_value->data[0].v_pointer = NULL; 2678 } 2679 2680 static void 2681 g_value_object_transform_value (const GValue *src_value, 2682 GValue *dest_value) 2683 { 2684 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))) 2685 dest_value->data[0].v_pointer = g_object_ref (src_value->data[0].v_pointer); 2686 else 2687 dest_value->data[0].v_pointer = NULL; 2688 } 2689 2690 static gpointer 2691 g_value_object_peek_pointer (const GValue *value) 2692 { 2693 return value->data[0].v_pointer; 2694 } 2695 2696 static gchar* 2697 g_value_object_collect_value (GValue *value, 2698 guint n_collect_values, 2699 GTypeCValue *collect_values, 2700 guint collect_flags) 2701 { 2702 if (collect_values[0].v_pointer) 2703 { 2704 GObject *object = collect_values[0].v_pointer; 2705 2706 if (object->g_type_instance.g_class == NULL) 2707 return g_strconcat ("invalid unclassed object pointer for value type `", 2708 G_VALUE_TYPE_NAME (value), 2709 "'", 2710 NULL); 2711 else if (!g_value_type_compatible (G_OBJECT_TYPE (object), G_VALUE_TYPE (value))) 2712 return g_strconcat ("invalid object type `", 2713 G_OBJECT_TYPE_NAME (object), 2714 "' for value type `", 2715 G_VALUE_TYPE_NAME (value), 2716 "'", 2717 NULL); 2718 /* never honour G_VALUE_NOCOPY_CONTENTS for ref-counted types */ 2719 value->data[0].v_pointer = g_object_ref (object); 2720 } 2721 else 2722 value->data[0].v_pointer = NULL; 2723 2724 return NULL; 2725 } 2726 2727 static gchar* 2728 g_value_object_lcopy_value (const GValue *value, 2729 guint n_collect_values, 2730 GTypeCValue *collect_values, 2731 guint collect_flags) 2732 { 2733 GObject **object_p = collect_values[0].v_pointer; 2734 2735 if (!object_p) 2736 return g_strdup_printf ("value location for `%s' passed as NULL", G_VALUE_TYPE_NAME (value)); 2737 2738 if (!value->data[0].v_pointer) 2739 *object_p = NULL; 2740 else if (collect_flags & G_VALUE_NOCOPY_CONTENTS) 2741 *object_p = value->data[0].v_pointer; 2742 else 2743 *object_p = g_object_ref (value->data[0].v_pointer); 2744 2745 return NULL; 2746 } 2747 2748 /** 2749 * g_value_set_object: 2750 * @value: a valid #GValue of %G_TYPE_OBJECT derived type 2751 * @v_object: object value to be set 2752 * 2753 * Set the contents of a %G_TYPE_OBJECT derived #GValue to @v_object. 2754 * 2755 * g_value_set_object() increases the reference count of @v_object 2756 * (the #GValue holds a reference to @v_object). If you do not wish 2757 * to increase the reference count of the object (i.e. you wish to 2758 * pass your current reference to the #GValue because you no longer 2759 * need it), use g_value_take_object() instead. 2760 * 2761 * It is important that your #GValue holds a reference to @v_object (either its 2762 * own, or one it has taken) to ensure that the object won't be destroyed while 2763 * the #GValue still exists). 2764 */ 2765 void 2766 g_value_set_object (GValue *value, 2767 gpointer v_object) 2768 { 2769 GObject *old; 2770 2771 g_return_if_fail (G_VALUE_HOLDS_OBJECT (value)); 2772 2773 old = value->data[0].v_pointer; 2774 2775 if (v_object) 2776 { 2777 g_return_if_fail (G_IS_OBJECT (v_object)); 2778 g_return_if_fail (g_value_type_compatible (G_OBJECT_TYPE (v_object), G_VALUE_TYPE (value))); 2779 2780 value->data[0].v_pointer = v_object; 2781 g_object_ref (value->data[0].v_pointer); 2782 } 2783 else 2784 value->data[0].v_pointer = NULL; 2785 2786 if (old) 2787 g_object_unref (old); 2788 } 2789 2790 /** 2791 * g_value_set_object_take_ownership: 2792 * @value: a valid #GValue of %G_TYPE_OBJECT derived type 2793 * @v_object: object value to be set 2794 * 2795 * This is an internal function introduced mainly for C marshallers. 2796 * 2797 * Deprecated: 2.4: Use g_value_take_object() instead. 2798 */ 2799 void 2800 g_value_set_object_take_ownership (GValue *value, 2801 gpointer v_object) 2802 { 2803 g_value_take_object (value, v_object); 2804 } 2805 2806 /** 2807 * g_value_take_object: 2808 * @value: a valid #GValue of %G_TYPE_OBJECT derived type 2809 * @v_object: object value to be set 2810 * 2811 * Sets the contents of a %G_TYPE_OBJECT derived #GValue to @v_object 2812 * and takes over the ownership of the callers reference to @v_object; 2813 * the caller doesn't have to unref it any more (i.e. the reference 2814 * count of the object is not increased). 2815 * 2816 * If you want the #GValue to hold its own reference to @v_object, use 2817 * g_value_set_object() instead. 2818 * 2819 * Since: 2.4 2820 */ 2821 void 2822 g_value_take_object (GValue *value, 2823 gpointer v_object) 2824 { 2825 g_return_if_fail (G_VALUE_HOLDS_OBJECT (value)); 2826 2827 if (value->data[0].v_pointer) 2828 { 2829 g_object_unref (value->data[0].v_pointer); 2830 value->data[0].v_pointer = NULL; 2831 } 2832 2833 if (v_object) 2834 { 2835 g_return_if_fail (G_IS_OBJECT (v_object)); 2836 g_return_if_fail (g_value_type_compatible (G_OBJECT_TYPE (v_object), G_VALUE_TYPE (value))); 2837 2838 value->data[0].v_pointer = v_object; /* we take over the reference count */ 2839 } 2840 } 2841 2842 /** 2843 * g_value_get_object: 2844 * @value: a valid #GValue of %G_TYPE_OBJECT derived type 2845 * 2846 * Get the contents of a %G_TYPE_OBJECT derived #GValue. 2847 * 2848 * Returns: object contents of @value 2849 */ 2850 gpointer 2851 g_value_get_object (const GValue *value) 2852 { 2853 g_return_val_if_fail (G_VALUE_HOLDS_OBJECT (value), NULL); 2854 2855 return value->data[0].v_pointer; 2856 } 2857 2858 /** 2859 * g_value_dup_object: 2860 * @value: a valid #GValue whose type is derived from %G_TYPE_OBJECT 2861 * 2862 * Get the contents of a %G_TYPE_OBJECT derived #GValue, increasing 2863 * its reference count. 2864 * 2865 * Returns: object content of @value, should be unreferenced when no 2866 * longer needed. 2867 */ 2868 gpointer 2869 g_value_dup_object (const GValue *value) 2870 { 2871 g_return_val_if_fail (G_VALUE_HOLDS_OBJECT (value), NULL); 2872 2873 return value->data[0].v_pointer ? g_object_ref (value->data[0].v_pointer) : NULL; 2874 } 2875 2876 /** 2877 * g_signal_connect_object: 2878 * @instance: the instance to connect to. 2879 * @detailed_signal: a string of the form "signal-name::detail". 2880 * @c_handler: the #GCallback to connect. 2881 * @gobject: the object to pass as data to @c_handler. 2882 * @connect_flags: a combination of #GConnnectFlags. 2883 * 2884 * This is similar to g_signal_connect_data(), but uses a closure which 2885 * ensures that the @gobject stays alive during the call to @c_handler 2886 * by temporarily adding a reference count to @gobject. 2887 * 2888 * Note that there is a bug in GObject that makes this function 2889 * much less useful than it might seem otherwise. Once @gobject is 2890 * disposed, the callback will no longer be called, but, the signal 2891 * handler is <emphasis>not</emphasis> currently disconnected. If the 2892 * @instance is itself being freed at the same time than this doesn't 2893 * matter, since the signal will automatically be removed, but 2894 * if @instance persists, then the signal handler will leak. You 2895 * should not remove the signal yourself because in a future versions of 2896 * GObject, the handler <emphasis>will</emphasis> automatically 2897 * be disconnected. 2898 * 2899 * It's possible to work around this problem in a way that will 2900 * continue to work with future versions of GObject by checking 2901 * that the signal handler is still connected before disconnected it: 2902 * <informalexample><programlisting> 2903 * if (g_signal_handler_is_connected (instance, id)) 2904 * g_signal_handler_disconnect (instance, id); 2905 * </programlisting></informalexample> 2906 * 2907 * Returns: the handler id. 2908 */ 2909 gulong 2910 g_signal_connect_object (gpointer instance, 2911 const gchar *detailed_signal, 2912 GCallback c_handler, 2913 gpointer gobject, 2914 GConnectFlags connect_flags) 2915 { 2916 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0); 2917 g_return_val_if_fail (detailed_signal != NULL, 0); 2918 g_return_val_if_fail (c_handler != NULL, 0); 2919 2920 if (gobject) 2921 { 2922 GClosure *closure; 2923 2924 g_return_val_if_fail (G_IS_OBJECT (gobject), 0); 2925 2926 closure = ((connect_flags & G_CONNECT_SWAPPED) ? g_cclosure_new_object_swap : g_cclosure_new_object) (c_handler, gobject); 2927 2928 return g_signal_connect_closure (instance, detailed_signal, closure, connect_flags & G_CONNECT_AFTER); 2929 } 2930 else 2931 return g_signal_connect_data (instance, detailed_signal, c_handler, NULL, NULL, connect_flags); 2932 } 2933 2934 typedef struct { 2935 GObject *object; 2936 guint n_closures; 2937 GClosure *closures[1]; /* flexible array */ 2938 } CArray; 2939 /* don't change this structure without supplying an accessor for 2940 * watched closures, e.g.: 2941 * GSList* g_object_list_watched_closures (GObject *object) 2942 * { 2943 * CArray *carray; 2944 * g_return_val_if_fail (G_IS_OBJECT (object), NULL); 2945 * carray = g_object_get_data (object, "GObject-closure-array"); 2946 * if (carray) 2947 * { 2948 * GSList *slist = NULL; 2949 * guint i; 2950 * for (i = 0; i < carray->n_closures; i++) 2951 * slist = g_slist_prepend (slist, carray->closures[i]); 2952 * return slist; 2953 * } 2954 * return NULL; 2955 * } 2956 */ 2957 2958 static void 2959 object_remove_closure (gpointer data, 2960 GClosure *closure) 2961 { 2962 GObject *object = data; 2963 CArray *carray = g_object_get_qdata (object, quark_closure_array); 2964 guint i; 2965 2966 for (i = 0; i < carray->n_closures; i++) 2967 if (carray->closures[i] == closure) 2968 { 2969 carray->n_closures--; 2970 if (i < carray->n_closures) 2971 carray->closures[i] = carray->closures[carray->n_closures]; 2972 return; 2973 } 2974 g_assert_not_reached (); 2975 } 2976 2977 static void 2978 destroy_closure_array (gpointer data) 2979 { 2980 CArray *carray = data; 2981 GObject *object = carray->object; 2982 guint i, n = carray->n_closures; 2983 2984 for (i = 0; i < n; i++) 2985 { 2986 GClosure *closure = carray->closures[i]; 2987 2988 /* removing object_remove_closure() upfront is probably faster than 2989 * letting it fiddle with quark_closure_array which is empty anyways 2990 */ 2991 g_closure_remove_invalidate_notifier (closure, object, object_remove_closure); 2992 g_closure_invalidate (closure); 2993 } 2994 g_free (carray); 2995 } 2996 2997 /** 2998 * g_object_watch_closure: 2999 * @object: GObject restricting lifetime of @closure 3000 * @closure: GClosure to watch 3001 * 3002 * This function essentially limits the life time of the @closure to 3003 * the life time of the object. That is, when the object is finalized, 3004 * the @closure is invalidated by calling g_closure_invalidate() on 3005 * it, in order to prevent invocations of the closure with a finalized 3006 * (nonexisting) object. Also, g_object_ref() and g_object_unref() are 3007 * added as marshal guards to the @closure, to ensure that an extra 3008 * reference count is held on @object during invocation of the 3009 * @closure. Usually, this function will be called on closures that 3010 * use this @object as closure data. 3011 */ 3012 void 3013 g_object_watch_closure (GObject *object, 3014 GClosure *closure) 3015 { 3016 CArray *carray; 3017 guint i; 3018 3019 g_return_if_fail (G_IS_OBJECT (object)); 3020 g_return_if_fail (closure != NULL); 3021 g_return_if_fail (closure->is_invalid == FALSE); 3022 g_return_if_fail (closure->in_marshal == FALSE); 3023 g_return_if_fail (object->ref_count > 0); /* this doesn't work on finalizing objects */ 3024 3025 g_closure_add_invalidate_notifier (closure, object, object_remove_closure); 3026 g_closure_add_marshal_guards (closure, 3027 object, (GClosureNotify) g_object_ref, 3028 object, (GClosureNotify) g_object_unref); 3029 carray = g_datalist_id_remove_no_notify (&object->qdata, quark_closure_array); 3030 if (!carray) 3031 { 3032 carray = g_renew (CArray, NULL, 1); 3033 carray->object = object; 3034 carray->n_closures = 1; 3035 i = 0; 3036 } 3037 else 3038 { 3039 i = carray->n_closures++; 3040 carray = g_realloc (carray, sizeof (*carray) + sizeof (carray->closures[0]) * i); 3041 } 3042 carray->closures[i] = closure; 3043 g_datalist_id_set_data_full (&object->qdata, quark_closure_array, carray, destroy_closure_array); 3044 } 3045 3046 /** 3047 * g_closure_new_object: 3048 * @sizeof_closure: the size of the structure to allocate, must be at least 3049 * <literal>sizeof (GClosure)</literal> 3050 * @object: a #GObject pointer to store in the @data field of the newly 3051 * allocated #GClosure 3052 * 3053 * A variant of g_closure_new_simple() which stores @object in the 3054 * @data field of the closure and calls g_object_watch_closure() on 3055 * @object and the created closure. This function is mainly useful 3056 * when implementing new types of closures. 3057 * 3058 * Returns: a newly allocated #GClosure 3059 */ 3060 GClosure* 3061 g_closure_new_object (guint sizeof_closure, 3062 GObject *object) 3063 { 3064 GClosure *closure; 3065 3066 g_return_val_if_fail (G_IS_OBJECT (object), NULL); 3067 g_return_val_if_fail (object->ref_count > 0, NULL); /* this doesn't work on finalizing objects */ 3068 3069 closure = g_closure_new_simple (sizeof_closure, object); 3070 g_object_watch_closure (object, closure); 3071 3072 return closure; 3073 } 3074 3075 /** 3076 * g_cclosure_new_object: 3077 * @callback_func: the function to invoke 3078 * @object: a #GObject pointer to pass to @callback_func 3079 * 3080 * A variant of g_cclosure_new() which uses @object as @user_data and 3081 * calls g_object_watch_closure() on @object and the created 3082 * closure. This function is useful when you have a callback closely 3083 * associated with a #GObject, and want the callback to no longer run 3084 * after the object is is freed. 3085 * 3086 * Returns: a new #GCClosure 3087 */ 3088 GClosure* 3089 g_cclosure_new_object (GCallback callback_func, 3090 GObject *object) 3091 { 3092 GClosure *closure; 3093 3094 g_return_val_if_fail (G_IS_OBJECT (object), NULL); 3095 g_return_val_if_fail (object->ref_count > 0, NULL); /* this doesn't work on finalizing objects */ 3096 g_return_val_if_fail (callback_func != NULL, NULL); 3097 3098 closure = g_cclosure_new (callback_func, object, NULL); 3099 g_object_watch_closure (object, closure); 3100 3101 return closure; 3102 } 3103 3104 /** 3105 * g_cclosure_new_object_swap: 3106 * @callback_func: the function to invoke 3107 * @object: a #GObject pointer to pass to @callback_func 3108 * 3109 * A variant of g_cclosure_new_swap() which uses @object as @user_data 3110 * and calls g_object_watch_closure() on @object and the created 3111 * closure. This function is useful when you have a callback closely 3112 * associated with a #GObject, and want the callback to no longer run 3113 * after the object is is freed. 3114 * 3115 * Returns: a new #GCClosure 3116 */ 3117 GClosure* 3118 g_cclosure_new_object_swap (GCallback callback_func, 3119 GObject *object) 3120 { 3121 GClosure *closure; 3122 3123 g_return_val_if_fail (G_IS_OBJECT (object), NULL); 3124 g_return_val_if_fail (object->ref_count > 0, NULL); /* this doesn't work on finalizing objects */ 3125 g_return_val_if_fail (callback_func != NULL, NULL); 3126 3127 closure = g_cclosure_new_swap (callback_func, object, NULL); 3128 g_object_watch_closure (object, closure); 3129 3130 return closure; 3131 } 3132 3133 gsize 3134 g_object_compat_control (gsize what, 3135 gpointer data) 3136 { 3137 switch (what) 3138 { 3139 gpointer *pp; 3140 case 1: /* floating base type */ 3141 return G_TYPE_INITIALLY_UNOWNED; 3142 case 2: /* FIXME: remove this once GLib/Gtk+ break ABI again */ 3143 floating_flag_handler = (guint(*)(GObject*,gint)) data; 3144 return 1; 3145 case 3: /* FIXME: remove this once GLib/Gtk+ break ABI again */ 3146 pp = data; 3147 *pp = floating_flag_handler; 3148 return 1; 3149 default: 3150 return 0; 3151 } 3152 } 3153 3154 G_DEFINE_TYPE (GInitiallyUnowned, g_initially_unowned, G_TYPE_OBJECT); 3155 3156 static void 3157 g_initially_unowned_init (GInitiallyUnowned *object) 3158 { 3159 g_object_force_floating (object); 3160 } 3161 3162 static void 3163 g_initially_unowned_class_init (GInitiallyUnownedClass *klass) 3164 { 3165 } 3166 3167 #define __G_OBJECT_C__ 3168 #include "gobjectaliasdef.c" 3169