You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

568 lines
22 KiB

  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. #if !defined (__GLIB_GOBJECT_H_INSIDE__) && !defined (GOBJECT_COMPILATION)
  20. #error "Only <glib-object.h> can be included directly."
  21. #endif
  22. #ifndef __G_OBJECT_H__
  23. #define __G_OBJECT_H__
  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. G_BEGIN_DECLS
  30. /* --- type macros --- */
  31. /**
  32. * G_TYPE_IS_OBJECT:
  33. * @type: Type id to check
  34. *
  35. * Check if the passed in type id is a %G_TYPE_OBJECT or derived from it.
  36. *
  37. * Returns: %FALSE or %TRUE, indicating whether @type is a %G_TYPE_OBJECT.
  38. */
  39. #define G_TYPE_IS_OBJECT(type) (G_TYPE_FUNDAMENTAL (type) == G_TYPE_OBJECT)
  40. /**
  41. * G_OBJECT:
  42. * @object: Object which is subject to casting.
  43. *
  44. * Casts a #GObject or derived pointer into a (GObject*) pointer.
  45. * Depending on the current debugging level, this function may invoke
  46. * certain runtime checks to identify invalid casts.
  47. */
  48. #define G_OBJECT(object) (G_TYPE_CHECK_INSTANCE_CAST ((object), G_TYPE_OBJECT, GObject))
  49. /**
  50. * G_OBJECT_CLASS:
  51. * @class: a valid #GObjectClass
  52. *
  53. * Casts a derived #GObjectClass structure into a #GObjectClass structure.
  54. */
  55. #define G_OBJECT_CLASS(class) (G_TYPE_CHECK_CLASS_CAST ((class), G_TYPE_OBJECT, GObjectClass))
  56. /**
  57. * G_IS_OBJECT:
  58. * @object: Instance to check for being a %G_TYPE_OBJECT.
  59. *
  60. * Checks whether a valid #GTypeInstance pointer is of type %G_TYPE_OBJECT.
  61. */
  62. #define G_IS_OBJECT(object) (G_TYPE_CHECK_INSTANCE_TYPE ((object), G_TYPE_OBJECT))
  63. /**
  64. * G_IS_OBJECT_CLASS:
  65. * @class: a #GObjectClass
  66. *
  67. * Checks whether @class "is a" valid #GObjectClass structure of type
  68. * %G_TYPE_OBJECT or derived.
  69. */
  70. #define G_IS_OBJECT_CLASS(class) (G_TYPE_CHECK_CLASS_TYPE ((class), G_TYPE_OBJECT))
  71. /**
  72. * G_OBJECT_GET_CLASS:
  73. * @object: a #GObject instance.
  74. *
  75. * Get the class structure associated to a #GObject instance.
  76. *
  77. * Returns: pointer to object class structure.
  78. */
  79. #define G_OBJECT_GET_CLASS(object) (G_TYPE_INSTANCE_GET_CLASS ((object), G_TYPE_OBJECT, GObjectClass))
  80. /**
  81. * G_OBJECT_TYPE:
  82. * @object: Object to return the type id for.
  83. *
  84. * Get the type id of an object.
  85. *
  86. * Returns: Type id of @object.
  87. */
  88. #define G_OBJECT_TYPE(object) (G_TYPE_FROM_INSTANCE (object))
  89. /**
  90. * G_OBJECT_TYPE_NAME:
  91. * @object: Object to return the type name for.
  92. *
  93. * Get the name of an object's type.
  94. *
  95. * Returns: Type name of @object. The string is owned by the type system and
  96. * should not be freed.
  97. */
  98. #define G_OBJECT_TYPE_NAME(object) (g_type_name (G_OBJECT_TYPE (object)))
  99. /**
  100. * G_OBJECT_CLASS_TYPE:
  101. * @class: a valid #GObjectClass
  102. *
  103. * Get the type id of a class structure.
  104. *
  105. * Returns: Type id of @class.
  106. */
  107. #define G_OBJECT_CLASS_TYPE(class) (G_TYPE_FROM_CLASS (class))
  108. /**
  109. * G_OBJECT_CLASS_NAME:
  110. * @class: a valid #GObjectClass
  111. *
  112. * Return the name of a class structure's type.
  113. *
  114. * Returns: Type name of @class. The string is owned by the type system and
  115. * should not be freed.
  116. */
  117. #define G_OBJECT_CLASS_NAME(class) (g_type_name (G_OBJECT_CLASS_TYPE (class)))
  118. /**
  119. * G_VALUE_HOLDS_OBJECT:
  120. * @value: a valid #GValue structure
  121. *
  122. * Checks whether the given #GValue can hold values derived from type %G_TYPE_OBJECT.
  123. *
  124. * Returns: %TRUE on success.
  125. */
  126. #define G_VALUE_HOLDS_OBJECT(value) (G_TYPE_CHECK_VALUE_TYPE ((value), G_TYPE_OBJECT))
  127. /* --- type macros --- */
  128. /**
  129. * G_TYPE_INITIALLY_UNOWNED:
  130. *
  131. * The type for #GInitiallyUnowned.
  132. */
  133. #define G_TYPE_INITIALLY_UNOWNED (g_initially_unowned_get_type())
  134. /**
  135. * G_INITIALLY_UNOWNED:
  136. * @object: Object which is subject to casting.
  137. *
  138. * Casts a #GInitiallyUnowned or derived pointer into a (GInitiallyUnowned*)
  139. * pointer. Depending on the current debugging level, this function may invoke
  140. * certain runtime checks to identify invalid casts.
  141. */
  142. #define G_INITIALLY_UNOWNED(object) (G_TYPE_CHECK_INSTANCE_CAST ((object), G_TYPE_INITIALLY_UNOWNED, GInitiallyUnowned))
  143. /**
  144. * G_INITIALLY_UNOWNED_CLASS:
  145. * @class: a valid #GInitiallyUnownedClass
  146. *
  147. * Casts a derived #GInitiallyUnownedClass structure into a
  148. * #GInitiallyUnownedClass structure.
  149. */
  150. #define G_INITIALLY_UNOWNED_CLASS(class) (G_TYPE_CHECK_CLASS_CAST ((class), G_TYPE_INITIALLY_UNOWNED, GInitiallyUnownedClass))
  151. /**
  152. * G_IS_INITIALLY_UNOWNED:
  153. * @object: Instance to check for being a %G_TYPE_INITIALLY_UNOWNED.
  154. *
  155. * Checks whether a valid #GTypeInstance pointer is of type %G_TYPE_INITIALLY_UNOWNED.
  156. */
  157. #define G_IS_INITIALLY_UNOWNED(object) (G_TYPE_CHECK_INSTANCE_TYPE ((object), G_TYPE_INITIALLY_UNOWNED))
  158. /**
  159. * G_IS_INITIALLY_UNOWNED_CLASS:
  160. * @class: a #GInitiallyUnownedClass
  161. *
  162. * Checks whether @class "is a" valid #GInitiallyUnownedClass structure of type
  163. * %G_TYPE_INITIALLY_UNOWNED or derived.
  164. */
  165. #define G_IS_INITIALLY_UNOWNED_CLASS(class) (G_TYPE_CHECK_CLASS_TYPE ((class), G_TYPE_INITIALLY_UNOWNED))
  166. /**
  167. * G_INITIALLY_UNOWNED_GET_CLASS:
  168. * @object: a #GInitiallyUnowned instance.
  169. *
  170. * Get the class structure associated to a #GInitiallyUnowned instance.
  171. *
  172. * Returns: pointer to object class structure.
  173. */
  174. #define G_INITIALLY_UNOWNED_GET_CLASS(object) (G_TYPE_INSTANCE_GET_CLASS ((object), G_TYPE_INITIALLY_UNOWNED, GInitiallyUnownedClass))
  175. /* GInitiallyUnowned ia a GObject with initially floating reference count */
  176. /* --- typedefs & structures --- */
  177. typedef struct _GObject GObject;
  178. typedef struct _GObjectClass GObjectClass;
  179. typedef struct _GObject GInitiallyUnowned;
  180. typedef struct _GObjectClass GInitiallyUnownedClass;
  181. typedef struct _GObjectConstructParam GObjectConstructParam;
  182. /**
  183. * GObjectGetPropertyFunc:
  184. * @object: a #GObject
  185. * @property_id: the numeric id under which the property was registered with
  186. * g_object_class_install_property().
  187. * @value: a #GValue to return the property value in
  188. * @pspec: the #GParamSpec describing the property
  189. *
  190. * The type of the @get_property function of #GObjectClass.
  191. */
  192. typedef void (*GObjectGetPropertyFunc) (GObject *object,
  193. guint property_id,
  194. GValue *value,
  195. GParamSpec *pspec);
  196. /**
  197. * GObjectSetPropertyFunc:
  198. * @object: a #GObject
  199. * @property_id: the numeric id under which the property was registered with
  200. * g_object_class_install_property().
  201. * @value: the new value for the property
  202. * @pspec: the #GParamSpec describing the property
  203. *
  204. * The type of the @set_property function of #GObjectClass.
  205. */
  206. typedef void (*GObjectSetPropertyFunc) (GObject *object,
  207. guint property_id,
  208. const GValue *value,
  209. GParamSpec *pspec);
  210. /**
  211. * GObjectFinalizeFunc:
  212. * @object: the #GObject being finalized
  213. *
  214. * The type of the @finalize function of #GObjectClass.
  215. */
  216. typedef void (*GObjectFinalizeFunc) (GObject *object);
  217. /**
  218. * GWeakNotify:
  219. * @data: data that was provided when the weak reference was established
  220. * @where_the_object_was: the object being finalized
  221. *
  222. * A #GWeakNotify function can be added to an object as a callback that gets
  223. * triggered when the object is finalized. Since the object is already being
  224. * finalized when the #GWeakNotify is called, there's not much you could do
  225. * with the object, apart from e.g. using its adress as hash-index or the like.
  226. */
  227. typedef void (*GWeakNotify) (gpointer data,
  228. GObject *where_the_object_was);
  229. /**
  230. * GObject:
  231. *
  232. * All the fields in the <structname>GObject</structname> structure are private
  233. * to the #GObject implementation and should never be accessed directly.
  234. */
  235. struct _GObject
  236. {
  237. GTypeInstance g_type_instance;
  238. /*< private >*/
  239. volatile guint ref_count;
  240. GData *qdata;
  241. };
  242. /**
  243. * GObjectClass:
  244. * @g_type_class: the parent class
  245. * @constructor: the @constructor function is called by g_object_new () to
  246. * complete the object initialization after all the construction properties are
  247. * set. The first thing a @constructor implementation must do is chain up to the
  248. * @constructor of the parent class. Overriding @constructor should be rarely
  249. * needed, e.g. to handle construct properties, or to implement singletons.
  250. * @set_property: the generic setter for all properties of this type. Should be
  251. * overridden for every type with properties. Implementations of @set_property
  252. * don't need to emit property change notification explicitly, this is handled
  253. * by the type system.
  254. * @get_property: the generic getter for all properties of this type. Should be
  255. * overridden for every type with properties.
  256. * @dispose: the @dispose function is supposed to drop all references to other
  257. * objects, but keep the instance otherwise intact, so that client method
  258. * invocations still work. It may be run multiple times (due to reference
  259. * loops). Before returning, @dispose should chain up to the @dispose method
  260. * of the parent class.
  261. * @finalize: instance finalization function, should finish the finalization of
  262. * the instance begun in @dispose and chain up to the @finalize method of the
  263. * parent class.
  264. * @dispatch_properties_changed: emits property change notification for a bunch
  265. * of properties. Overriding @dispatch_properties_changed should be rarely
  266. * needed.
  267. * @notify: the class closure for the notify signal
  268. * @constructed: the @constructed function is called by g_object_new() as the
  269. * final step of the object creation process. At the point of the call, all
  270. * construction properties have been set on the object. The purpose of this
  271. * call is to allow for object initialisation steps that can only be performed
  272. * after construction properties have been set. @constructed implementors
  273. * should chain up to the @constructed call of their parent class to allow it
  274. * to complete its initialisation.
  275. *
  276. * The class structure for the <structname>GObject</structname> type.
  277. *
  278. * <example>
  279. * <title>Implementing singletons using a constructor</title>
  280. * <programlisting>
  281. * static MySingleton *the_singleton = NULL;
  282. *
  283. * static GObject*
  284. * my_singleton_constructor (GType type,
  285. * guint n_construct_params,
  286. * GObjectConstructParam *construct_params)
  287. * {
  288. * GObject *object;
  289. *
  290. * if (!the_singleton)
  291. * {
  292. * object = G_OBJECT_CLASS (parent_class)->constructor (type,
  293. * n_construct_params,
  294. * construct_params);
  295. * the_singleton = MY_SINGLETON (object);
  296. * }
  297. * else
  298. * object = g_object_ref (G_OBJECT (the_singleton));
  299. *
  300. * return object;
  301. * }
  302. * </programlisting></example>
  303. */
  304. struct _GObjectClass
  305. {
  306. GTypeClass g_type_class;
  307. /*< private >*/
  308. GSList *construct_properties;
  309. /*< public >*/
  310. /* seldomly overidden */
  311. GObject* (*constructor) (GType type,
  312. guint n_construct_properties,
  313. GObjectConstructParam *construct_properties);
  314. /* overridable methods */
  315. void (*set_property) (GObject *object,
  316. guint property_id,
  317. const GValue *value,
  318. GParamSpec *pspec);
  319. void (*get_property) (GObject *object,
  320. guint property_id,
  321. GValue *value,
  322. GParamSpec *pspec);
  323. void (*dispose) (GObject *object);
  324. void (*finalize) (GObject *object);
  325. /* seldomly overidden */
  326. void (*dispatch_properties_changed) (GObject *object,
  327. guint n_pspecs,
  328. GParamSpec **pspecs);
  329. /* signals */
  330. void (*notify) (GObject *object,
  331. GParamSpec *pspec);
  332. /* called when done constructing */
  333. void (*constructed) (GObject *object);
  334. /*< private >*/
  335. gsize flags;
  336. /* padding */
  337. gpointer pdummy[6];
  338. };
  339. /**
  340. * GObjectConstructParam:
  341. * @pspec: the #GParamSpec of the construct parameter
  342. * @value: the value to set the parameter to
  343. *
  344. * The <structname>GObjectConstructParam</structname> struct is an auxiliary
  345. * structure used to hand #GParamSpec/#GValue pairs to the @constructor of
  346. * a #GObjectClass.
  347. */
  348. struct _GObjectConstructParam
  349. {
  350. GParamSpec *pspec;
  351. GValue *value;
  352. };
  353. /**
  354. * GInitiallyUnowned:
  355. *
  356. * All the fields in the <structname>GInitiallyUnowned</structname> structure
  357. * are private to the #GInitiallyUnowned implementation and should never be
  358. * accessed directly.
  359. */
  360. /**
  361. * GInitiallyUnownedClass:
  362. *
  363. * The class structure for the <structname>GInitiallyUnowned</structname> type.
  364. */
  365. /* --- prototypes --- */
  366. GType g_initially_unowned_get_type (void);
  367. void g_object_class_install_property (GObjectClass *oclass,
  368. guint property_id,
  369. GParamSpec *pspec);
  370. GParamSpec* g_object_class_find_property (GObjectClass *oclass,
  371. const gchar *property_name);
  372. GParamSpec**g_object_class_list_properties (GObjectClass *oclass,
  373. guint *n_properties);
  374. void g_object_class_override_property (GObjectClass *oclass,
  375. guint property_id,
  376. const gchar *name);
  377. void g_object_class_install_properties (GObjectClass *oclass,
  378. guint n_pspecs,
  379. GParamSpec **pspecs);
  380. void g_object_interface_install_property (gpointer g_iface,
  381. GParamSpec *pspec);
  382. GParamSpec* g_object_interface_find_property (gpointer g_iface,
  383. const gchar *property_name);
  384. GParamSpec**g_object_interface_list_properties (gpointer g_iface,
  385. guint *n_properties_p);
  386. GType g_object_get_type (void) G_GNUC_CONST;
  387. gpointer g_object_new (GType object_type,
  388. const gchar *first_property_name,
  389. ...);
  390. gpointer g_object_newv (GType object_type,
  391. guint n_parameters,
  392. GParameter *parameters);
  393. GObject* g_object_new_valist (GType object_type,
  394. const gchar *first_property_name,
  395. va_list var_args);
  396. void g_object_set (gpointer object,
  397. const gchar *first_property_name,
  398. ...) G_GNUC_NULL_TERMINATED;
  399. void g_object_get (gpointer object,
  400. const gchar *first_property_name,
  401. ...) G_GNUC_NULL_TERMINATED;
  402. gpointer g_object_connect (gpointer object,
  403. const gchar *signal_spec,
  404. ...) G_GNUC_NULL_TERMINATED;
  405. void g_object_disconnect (gpointer object,
  406. const gchar *signal_spec,
  407. ...) G_GNUC_NULL_TERMINATED;
  408. void g_object_set_valist (GObject *object,
  409. const gchar *first_property_name,
  410. va_list var_args);
  411. void g_object_get_valist (GObject *object,
  412. const gchar *first_property_name,
  413. va_list var_args);
  414. void g_object_set_property (GObject *object,
  415. const gchar *property_name,
  416. const GValue *value);
  417. void g_object_get_property (GObject *object,
  418. const gchar *property_name,
  419. GValue *value);
  420. void g_object_freeze_notify (GObject *object);
  421. void g_object_notify (GObject *object,
  422. const gchar *property_name);
  423. void g_object_notify_by_pspec (GObject *object,
  424. GParamSpec *pspec);
  425. void g_object_thaw_notify (GObject *object);
  426. gboolean g_object_is_floating (gpointer object);
  427. gpointer g_object_ref_sink (gpointer object);
  428. gpointer g_object_ref (gpointer object);
  429. void g_object_unref (gpointer object);
  430. void g_object_weak_ref (GObject *object,
  431. GWeakNotify notify,
  432. gpointer data);
  433. void g_object_weak_unref (GObject *object,
  434. GWeakNotify notify,
  435. gpointer data);
  436. void g_object_add_weak_pointer (GObject *object,
  437. gpointer *weak_pointer_location);
  438. void g_object_remove_weak_pointer (GObject *object,
  439. gpointer *weak_pointer_location);
  440. /**
  441. * GToggleNotify:
  442. * @data: Callback data passed to g_object_add_toggle_ref()
  443. * @object: The object on which g_object_add_toggle_ref() was called.
  444. * @is_last_ref: %TRUE if the toggle reference is now the
  445. * last reference to the object. %FALSE if the toggle
  446. * reference was the last reference and there are now other
  447. * references.
  448. *
  449. * A callback function used for notification when the state
  450. * of a toggle reference changes. See g_object_add_toggle_ref().
  451. */
  452. typedef void (*GToggleNotify) (gpointer data,
  453. GObject *object,
  454. gboolean is_last_ref);
  455. void g_object_add_toggle_ref (GObject *object,
  456. GToggleNotify notify,
  457. gpointer data);
  458. void g_object_remove_toggle_ref (GObject *object,
  459. GToggleNotify notify,
  460. gpointer data);
  461. gpointer g_object_get_qdata (GObject *object,
  462. GQuark quark);
  463. void g_object_set_qdata (GObject *object,
  464. GQuark quark,
  465. gpointer data);
  466. void g_object_set_qdata_full (GObject *object,
  467. GQuark quark,
  468. gpointer data,
  469. GDestroyNotify destroy);
  470. gpointer g_object_steal_qdata (GObject *object,
  471. GQuark quark);
  472. gpointer g_object_get_data (GObject *object,
  473. const gchar *key);
  474. void g_object_set_data (GObject *object,
  475. const gchar *key,
  476. gpointer data);
  477. void g_object_set_data_full (GObject *object,
  478. const gchar *key,
  479. gpointer data,
  480. GDestroyNotify destroy);
  481. gpointer g_object_steal_data (GObject *object,
  482. const gchar *key);
  483. void g_object_watch_closure (GObject *object,
  484. GClosure *closure);
  485. GClosure* g_cclosure_new_object (GCallback callback_func,
  486. GObject *object);
  487. GClosure* g_cclosure_new_object_swap (GCallback callback_func,
  488. GObject *object);
  489. GClosure* g_closure_new_object (guint sizeof_closure,
  490. GObject *object);
  491. void g_value_set_object (GValue *value,
  492. gpointer v_object);
  493. gpointer g_value_get_object (const GValue *value);
  494. gpointer g_value_dup_object (const GValue *value);
  495. gulong g_signal_connect_object (gpointer instance,
  496. const gchar *detailed_signal,
  497. GCallback c_handler,
  498. gpointer gobject,
  499. GConnectFlags connect_flags);
  500. /*< protected >*/
  501. void g_object_force_floating (GObject *object);
  502. void g_object_run_dispose (GObject *object);
  503. void g_value_take_object (GValue *value,
  504. gpointer v_object);
  505. #ifndef G_DISABLE_DEPRECATED
  506. void g_value_set_object_take_ownership (GValue *value,
  507. gpointer v_object);
  508. #endif
  509. #if !defined(G_DISABLE_DEPRECATED) || defined(GTK_COMPILATION)
  510. gsize g_object_compat_control (gsize what,
  511. gpointer data);
  512. #endif
  513. /* --- implementation macros --- */
  514. #define G_OBJECT_WARN_INVALID_PSPEC(object, pname, property_id, pspec) \
  515. G_STMT_START { \
  516. GObject *_object = (GObject*) (object); \
  517. GParamSpec *_pspec = (GParamSpec*) (pspec); \
  518. guint _property_id = (property_id); \
  519. g_warning ("%s: invalid %s id %u for \"%s\" of type `%s' in `%s'", \
  520. G_STRLOC, \
  521. (pname), \
  522. _property_id, \
  523. _pspec->name, \
  524. g_type_name (G_PARAM_SPEC_TYPE (_pspec)), \
  525. G_OBJECT_TYPE_NAME (_object)); \
  526. } G_STMT_END
  527. /**
  528. * G_OBJECT_WARN_INVALID_PROPERTY_ID:
  529. * @object: the #GObject on which set_property() or get_property() was called
  530. * @property_id: the numeric id of the property
  531. * @pspec: the #GParamSpec of the property
  532. *
  533. * This macro should be used to emit a standard warning about unexpected
  534. * properties in set_property() and get_property() implementations.
  535. */
  536. #define G_OBJECT_WARN_INVALID_PROPERTY_ID(object, property_id, pspec) \
  537. G_OBJECT_WARN_INVALID_PSPEC ((object), "property", (property_id), (pspec))
  538. G_END_DECLS
  539. #endif /* __G_OBJECT_H__ */