gobject.h 34 KB

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