gobject.h 33 KB

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