gparam.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. /* GObject - GLib Type, Object, Parameter and Signal Library
  2. * Copyright (C) 1997-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, see <http://www.gnu.org/licenses/>.
  16. *
  17. * gparam.h: GParamSpec base class implementation
  18. */
  19. #ifndef __G_PARAM_H__
  20. #define __G_PARAM_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/gvalue.h>
  25. G_BEGIN_DECLS
  26. /* --- standard type macros --- */
  27. /**
  28. * G_TYPE_IS_PARAM:
  29. * @type: a #GType ID
  30. *
  31. * Checks whether @type "is a" %G_TYPE_PARAM.
  32. */
  33. #define G_TYPE_IS_PARAM(type) (G_TYPE_FUNDAMENTAL (type) == G_TYPE_PARAM)
  34. /**
  35. * G_PARAM_SPEC:
  36. * @pspec: a valid #GParamSpec
  37. *
  38. * Casts a derived #GParamSpec object (e.g. of type #GParamSpecInt) into
  39. * a #GParamSpec object.
  40. */
  41. #define G_PARAM_SPEC(pspec) (G_TYPE_CHECK_INSTANCE_CAST ((pspec), G_TYPE_PARAM, GParamSpec))
  42. /**
  43. * G_IS_PARAM_SPEC:
  44. * @pspec: a #GParamSpec
  45. *
  46. * Checks whether @pspec "is a" valid #GParamSpec structure of type %G_TYPE_PARAM
  47. * or derived.
  48. */
  49. #if GLIB_VERSION_MAX_ALLOWED >= GLIB_VERSION_2_42
  50. #define G_IS_PARAM_SPEC(pspec) (G_TYPE_CHECK_INSTANCE_FUNDAMENTAL_TYPE ((pspec), G_TYPE_PARAM))
  51. #else
  52. #define G_IS_PARAM_SPEC(pspec) (G_TYPE_CHECK_INSTANCE_TYPE ((pspec), G_TYPE_PARAM))
  53. #endif
  54. /**
  55. * G_PARAM_SPEC_CLASS:
  56. * @pclass: a valid #GParamSpecClass
  57. *
  58. * Casts a derived #GParamSpecClass structure into a #GParamSpecClass structure.
  59. */
  60. #define G_PARAM_SPEC_CLASS(pclass) (G_TYPE_CHECK_CLASS_CAST ((pclass), G_TYPE_PARAM, GParamSpecClass))
  61. /**
  62. * G_IS_PARAM_SPEC_CLASS:
  63. * @pclass: a #GParamSpecClass
  64. *
  65. * Checks whether @pclass "is a" valid #GParamSpecClass structure of type
  66. * %G_TYPE_PARAM or derived.
  67. */
  68. #define G_IS_PARAM_SPEC_CLASS(pclass) (G_TYPE_CHECK_CLASS_TYPE ((pclass), G_TYPE_PARAM))
  69. /**
  70. * G_PARAM_SPEC_GET_CLASS:
  71. * @pspec: a valid #GParamSpec
  72. *
  73. * Retrieves the #GParamSpecClass of a #GParamSpec.
  74. */
  75. #define G_PARAM_SPEC_GET_CLASS(pspec) (G_TYPE_INSTANCE_GET_CLASS ((pspec), G_TYPE_PARAM, GParamSpecClass))
  76. /* --- convenience macros --- */
  77. /**
  78. * G_PARAM_SPEC_TYPE:
  79. * @pspec: a valid #GParamSpec
  80. *
  81. * Retrieves the #GType of this @pspec.
  82. */
  83. #define G_PARAM_SPEC_TYPE(pspec) (G_TYPE_FROM_INSTANCE (pspec))
  84. /**
  85. * G_PARAM_SPEC_TYPE_NAME:
  86. * @pspec: a valid #GParamSpec
  87. *
  88. * Retrieves the #GType name of this @pspec.
  89. */
  90. #define G_PARAM_SPEC_TYPE_NAME(pspec) (g_type_name (G_PARAM_SPEC_TYPE (pspec)))
  91. /**
  92. * G_PARAM_SPEC_VALUE_TYPE:
  93. * @pspec: a valid #GParamSpec
  94. *
  95. * Retrieves the #GType to initialize a #GValue for this parameter.
  96. */
  97. #define G_PARAM_SPEC_VALUE_TYPE(pspec) (G_PARAM_SPEC (pspec)->value_type)
  98. /**
  99. * G_VALUE_HOLDS_PARAM:
  100. * @value: a valid #GValue structure
  101. *
  102. * Checks whether the given #GValue can hold values derived from type %G_TYPE_PARAM.
  103. *
  104. * Returns: %TRUE on success.
  105. */
  106. #define G_VALUE_HOLDS_PARAM(value) (G_TYPE_CHECK_VALUE_TYPE ((value), G_TYPE_PARAM))
  107. /* --- flags --- */
  108. /**
  109. * GParamFlags:
  110. * @G_PARAM_READABLE: the parameter is readable
  111. * @G_PARAM_WRITABLE: the parameter is writable
  112. * @G_PARAM_READWRITE: alias for %G_PARAM_READABLE | %G_PARAM_WRITABLE
  113. * @G_PARAM_CONSTRUCT: the parameter will be set upon object construction
  114. * @G_PARAM_CONSTRUCT_ONLY: the parameter can only be set upon object construction
  115. * @G_PARAM_LAX_VALIDATION: upon parameter conversion (see g_param_value_convert())
  116. * strict validation is not required
  117. * @G_PARAM_STATIC_NAME: the string used as name when constructing the
  118. * parameter is guaranteed to remain valid and
  119. * unmodified for the lifetime of the parameter.
  120. * Since 2.8
  121. * @G_PARAM_STATIC_NICK: the string used as nick when constructing the
  122. * parameter is guaranteed to remain valid and
  123. * unmmodified for the lifetime of the parameter.
  124. * Since 2.8
  125. * @G_PARAM_STATIC_BLURB: the string used as blurb when constructing the
  126. * parameter is guaranteed to remain valid and
  127. * unmodified for the lifetime of the parameter.
  128. * Since 2.8
  129. * @G_PARAM_EXPLICIT_NOTIFY: calls to g_object_set_property() for this
  130. * property will not automatically result in a "notify" signal being
  131. * emitted: the implementation must call g_object_notify() themselves
  132. * in case the property actually changes. Since: 2.42.
  133. * @G_PARAM_PRIVATE: internal
  134. * @G_PARAM_DEPRECATED: the parameter is deprecated and will be removed
  135. * in a future version. A warning will be generated if it is used
  136. * while running with G_ENABLE_DIAGNOSTIC=1.
  137. * Since 2.26
  138. *
  139. * Through the #GParamFlags flag values, certain aspects of parameters
  140. * can be configured. See also #G_PARAM_STATIC_STRINGS.
  141. */
  142. typedef enum
  143. {
  144. G_PARAM_READABLE = 1 << 0,
  145. G_PARAM_WRITABLE = 1 << 1,
  146. G_PARAM_READWRITE = (G_PARAM_READABLE | G_PARAM_WRITABLE),
  147. G_PARAM_CONSTRUCT = 1 << 2,
  148. G_PARAM_CONSTRUCT_ONLY = 1 << 3,
  149. G_PARAM_LAX_VALIDATION = 1 << 4,
  150. G_PARAM_STATIC_NAME = 1 << 5,
  151. #ifndef G_DISABLE_DEPRECATED
  152. G_PARAM_PRIVATE = G_PARAM_STATIC_NAME,
  153. #endif
  154. G_PARAM_STATIC_NICK = 1 << 6,
  155. G_PARAM_STATIC_BLURB = 1 << 7,
  156. /* User defined flags go here */
  157. G_PARAM_EXPLICIT_NOTIFY = 1 << 30,
  158. G_PARAM_DEPRECATED = 1 << 31
  159. } GParamFlags;
  160. /**
  161. * G_PARAM_STATIC_STRINGS:
  162. *
  163. * #GParamFlags value alias for %G_PARAM_STATIC_NAME | %G_PARAM_STATIC_NICK | %G_PARAM_STATIC_BLURB.
  164. *
  165. * Since 2.13.0
  166. */
  167. #define G_PARAM_STATIC_STRINGS (G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB)
  168. /* bits in the range 0xffffff00 are reserved for 3rd party usage */
  169. /**
  170. * G_PARAM_MASK:
  171. *
  172. * Mask containing the bits of #GParamSpec.flags which are reserved for GLib.
  173. */
  174. #define G_PARAM_MASK (0x000000ff)
  175. /**
  176. * G_PARAM_USER_SHIFT:
  177. *
  178. * Minimum shift count to be used for user defined flags, to be stored in
  179. * #GParamSpec.flags. The maximum allowed is 10.
  180. */
  181. #define G_PARAM_USER_SHIFT (8)
  182. /* --- typedefs & structures --- */
  183. typedef struct _GParamSpec GParamSpec;
  184. typedef struct _GParamSpecClass GParamSpecClass;
  185. typedef struct _GParameter GParameter;
  186. typedef struct _GParamSpecPool GParamSpecPool;
  187. /**
  188. * GParamSpec: (ref-func g_param_spec_ref_sink) (unref-func g_param_spec_uref) (set-value-func g_value_set_param) (get-value-func g_value_get_param)
  189. * @g_type_instance: private #GTypeInstance portion
  190. * @name: name of this parameter: always an interned string
  191. * @flags: #GParamFlags flags for this parameter
  192. * @value_type: the #GValue type for this parameter
  193. * @owner_type: #GType type that uses (introduces) this parameter
  194. *
  195. * All other fields of the GParamSpec struct are private and
  196. * should not be used directly.
  197. */
  198. struct _GParamSpec
  199. {
  200. GTypeInstance g_type_instance;
  201. const gchar *name; /* interned string */
  202. GParamFlags flags;
  203. GType value_type;
  204. GType owner_type; /* class or interface using this property */
  205. /*< private >*/
  206. gchar *_nick;
  207. gchar *_blurb;
  208. GData *qdata;
  209. guint ref_count;
  210. guint param_id; /* sort-criteria */
  211. };
  212. /**
  213. * GParamSpecClass:
  214. * @g_type_class: the parent class
  215. * @value_type: the #GValue type for this parameter
  216. * @finalize: The instance finalization function (optional), should chain
  217. * up to the finalize method of the parent class.
  218. * @value_set_default: Resets a @value to the default value for this type
  219. * (recommended, the default is g_value_reset()), see
  220. * g_param_value_set_default().
  221. * @value_validate: Ensures that the contents of @value comply with the
  222. * specifications set out by this type (optional), see
  223. * g_param_value_validate().
  224. * @values_cmp: Compares @value1 with @value2 according to this type
  225. * (recommended, the default is memcmp()), see g_param_values_cmp().
  226. *
  227. * The class structure for the GParamSpec type.
  228. * Normally, GParamSpec classes are filled by
  229. * g_param_type_register_static().
  230. */
  231. struct _GParamSpecClass
  232. {
  233. GTypeClass g_type_class;
  234. GType value_type;
  235. void (*finalize) (GParamSpec *pspec);
  236. /* GParam methods */
  237. void (*value_set_default) (GParamSpec *pspec,
  238. GValue *value);
  239. gboolean (*value_validate) (GParamSpec *pspec,
  240. GValue *value);
  241. gint (*values_cmp) (GParamSpec *pspec,
  242. const GValue *value1,
  243. const GValue *value2);
  244. /*< private >*/
  245. gpointer dummy[4];
  246. };
  247. /**
  248. * GParameter:
  249. * @name: the parameter name
  250. * @value: the parameter value
  251. *
  252. * The GParameter struct is an auxiliary structure used
  253. * to hand parameter name/value pairs to g_object_newv().
  254. */
  255. struct _GParameter /* auxiliary structure for _setv() variants */
  256. {
  257. const gchar *name;
  258. GValue value;
  259. };
  260. /* --- prototypes --- */
  261. GLIB_AVAILABLE_IN_ALL
  262. GParamSpec* g_param_spec_ref (GParamSpec *pspec);
  263. GLIB_AVAILABLE_IN_ALL
  264. void g_param_spec_unref (GParamSpec *pspec);
  265. GLIB_AVAILABLE_IN_ALL
  266. void g_param_spec_sink (GParamSpec *pspec);
  267. GLIB_AVAILABLE_IN_ALL
  268. GParamSpec* g_param_spec_ref_sink (GParamSpec *pspec);
  269. GLIB_AVAILABLE_IN_ALL
  270. gpointer g_param_spec_get_qdata (GParamSpec *pspec,
  271. GQuark quark);
  272. GLIB_AVAILABLE_IN_ALL
  273. void g_param_spec_set_qdata (GParamSpec *pspec,
  274. GQuark quark,
  275. gpointer data);
  276. GLIB_AVAILABLE_IN_ALL
  277. void g_param_spec_set_qdata_full (GParamSpec *pspec,
  278. GQuark quark,
  279. gpointer data,
  280. GDestroyNotify destroy);
  281. GLIB_AVAILABLE_IN_ALL
  282. gpointer g_param_spec_steal_qdata (GParamSpec *pspec,
  283. GQuark quark);
  284. GLIB_AVAILABLE_IN_ALL
  285. GParamSpec* g_param_spec_get_redirect_target (GParamSpec *pspec);
  286. GLIB_AVAILABLE_IN_ALL
  287. void g_param_value_set_default (GParamSpec *pspec,
  288. GValue *value);
  289. GLIB_AVAILABLE_IN_ALL
  290. gboolean g_param_value_defaults (GParamSpec *pspec,
  291. GValue *value);
  292. GLIB_AVAILABLE_IN_ALL
  293. gboolean g_param_value_validate (GParamSpec *pspec,
  294. GValue *value);
  295. GLIB_AVAILABLE_IN_ALL
  296. gboolean g_param_value_convert (GParamSpec *pspec,
  297. const GValue *src_value,
  298. GValue *dest_value,
  299. gboolean strict_validation);
  300. GLIB_AVAILABLE_IN_ALL
  301. gint g_param_values_cmp (GParamSpec *pspec,
  302. const GValue *value1,
  303. const GValue *value2);
  304. GLIB_AVAILABLE_IN_ALL
  305. const gchar * g_param_spec_get_name (GParamSpec *pspec);
  306. GLIB_AVAILABLE_IN_ALL
  307. const gchar * g_param_spec_get_nick (GParamSpec *pspec);
  308. GLIB_AVAILABLE_IN_ALL
  309. const gchar * g_param_spec_get_blurb (GParamSpec *pspec);
  310. GLIB_AVAILABLE_IN_ALL
  311. void g_value_set_param (GValue *value,
  312. GParamSpec *param);
  313. GLIB_AVAILABLE_IN_ALL
  314. GParamSpec* g_value_get_param (const GValue *value);
  315. GLIB_AVAILABLE_IN_ALL
  316. GParamSpec* g_value_dup_param (const GValue *value);
  317. GLIB_AVAILABLE_IN_ALL
  318. void g_value_take_param (GValue *value,
  319. GParamSpec *param);
  320. GLIB_DEPRECATED_FOR(g_value_take_param)
  321. void g_value_set_param_take_ownership (GValue *value,
  322. GParamSpec *param);
  323. GLIB_AVAILABLE_IN_2_36
  324. const GValue * g_param_spec_get_default_value (GParamSpec *pspec);
  325. GLIB_AVAILABLE_IN_2_46
  326. GQuark g_param_spec_get_name_quark (GParamSpec *pspec);
  327. /* --- convenience functions --- */
  328. typedef struct _GParamSpecTypeInfo GParamSpecTypeInfo;
  329. /**
  330. * GParamSpecTypeInfo:
  331. * @instance_size: Size of the instance (object) structure.
  332. * @n_preallocs: Prior to GLib 2.10, it specified the number of pre-allocated (cached) instances to reserve memory for (0 indicates no caching). Since GLib 2.10, it is ignored, since instances are allocated with the [slice allocator][glib-Memory-Slices] now.
  333. * @instance_init: Location of the instance initialization function (optional).
  334. * @value_type: The #GType of values conforming to this #GParamSpec
  335. * @finalize: The instance finalization function (optional).
  336. * @value_set_default: Resets a @value to the default value for @pspec
  337. * (recommended, the default is g_value_reset()), see
  338. * g_param_value_set_default().
  339. * @value_validate: Ensures that the contents of @value comply with the
  340. * specifications set out by @pspec (optional), see
  341. * g_param_value_validate().
  342. * @values_cmp: Compares @value1 with @value2 according to @pspec
  343. * (recommended, the default is memcmp()), see g_param_values_cmp().
  344. *
  345. * This structure is used to provide the type system with the information
  346. * required to initialize and destruct (finalize) a parameter's class and
  347. * instances thereof.
  348. * The initialized structure is passed to the g_param_type_register_static()
  349. * The type system will perform a deep copy of this structure, so its memory
  350. * does not need to be persistent across invocation of
  351. * g_param_type_register_static().
  352. */
  353. struct _GParamSpecTypeInfo
  354. {
  355. /* type system portion */
  356. guint16 instance_size; /* obligatory */
  357. guint16 n_preallocs; /* optional */
  358. void (*instance_init) (GParamSpec *pspec); /* optional */
  359. /* class portion */
  360. GType value_type; /* obligatory */
  361. void (*finalize) (GParamSpec *pspec); /* optional */
  362. void (*value_set_default) (GParamSpec *pspec, /* recommended */
  363. GValue *value);
  364. gboolean (*value_validate) (GParamSpec *pspec, /* optional */
  365. GValue *value);
  366. gint (*values_cmp) (GParamSpec *pspec, /* recommended */
  367. const GValue *value1,
  368. const GValue *value2);
  369. };
  370. GLIB_AVAILABLE_IN_ALL
  371. GType g_param_type_register_static (const gchar *name,
  372. const GParamSpecTypeInfo *pspec_info);
  373. /* For registering builting types */
  374. GType _g_param_type_register_static_constant (const gchar *name,
  375. const GParamSpecTypeInfo *pspec_info,
  376. GType opt_type);
  377. /* --- protected --- */
  378. GLIB_AVAILABLE_IN_ALL
  379. gpointer g_param_spec_internal (GType param_type,
  380. const gchar *name,
  381. const gchar *nick,
  382. const gchar *blurb,
  383. GParamFlags flags);
  384. GLIB_AVAILABLE_IN_ALL
  385. GParamSpecPool* g_param_spec_pool_new (gboolean type_prefixing);
  386. GLIB_AVAILABLE_IN_ALL
  387. void g_param_spec_pool_insert (GParamSpecPool *pool,
  388. GParamSpec *pspec,
  389. GType owner_type);
  390. GLIB_AVAILABLE_IN_ALL
  391. void g_param_spec_pool_remove (GParamSpecPool *pool,
  392. GParamSpec *pspec);
  393. GLIB_AVAILABLE_IN_ALL
  394. GParamSpec* g_param_spec_pool_lookup (GParamSpecPool *pool,
  395. const gchar *param_name,
  396. GType owner_type,
  397. gboolean walk_ancestors);
  398. GLIB_AVAILABLE_IN_ALL
  399. GList* g_param_spec_pool_list_owned (GParamSpecPool *pool,
  400. GType owner_type);
  401. GLIB_AVAILABLE_IN_ALL
  402. GParamSpec** g_param_spec_pool_list (GParamSpecPool *pool,
  403. GType owner_type,
  404. guint *n_pspecs_p);
  405. /* contracts:
  406. *
  407. * gboolean value_validate (GParamSpec *pspec,
  408. * GValue *value):
  409. * modify value contents in the least destructive way, so
  410. * that it complies with pspec's requirements (i.e.
  411. * according to minimum/maximum ranges etc...). return
  412. * whether modification was necessary.
  413. *
  414. * gint values_cmp (GParamSpec *pspec,
  415. * const GValue *value1,
  416. * const GValue *value2):
  417. * return value1 - value2, i.e. (-1) if value1 < value2,
  418. * (+1) if value1 > value2, and (0) otherwise (equality)
  419. */
  420. G_END_DECLS
  421. #endif /* __G_PARAM_H__ */