gparam.h 15 KB

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