gparam.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  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.1 of the License, or (at your option) any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General
  15. * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
  16. *
  17. * 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. G_PARAM_PRIVATE GLIB_DEPRECATED_ENUMERATOR_IN_2_26 = G_PARAM_STATIC_NAME,
  152. G_PARAM_STATIC_NICK = 1 << 6,
  153. G_PARAM_STATIC_BLURB = 1 << 7,
  154. /* User defined flags go here */
  155. G_PARAM_EXPLICIT_NOTIFY = 1 << 30,
  156. /* Avoid warning with -Wpedantic for gcc6 */
  157. G_PARAM_DEPRECATED = (gint)(1u << 31)
  158. } GParamFlags;
  159. /**
  160. * G_PARAM_STATIC_STRINGS:
  161. *
  162. * #GParamFlags value alias for %G_PARAM_STATIC_NAME | %G_PARAM_STATIC_NICK | %G_PARAM_STATIC_BLURB.
  163. *
  164. * Since 2.13.0
  165. */
  166. #define G_PARAM_STATIC_STRINGS (G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB)
  167. /* bits in the range 0xffffff00 are reserved for 3rd party usage */
  168. /**
  169. * G_PARAM_MASK:
  170. *
  171. * Mask containing the bits of #GParamSpec.flags which are reserved for GLib.
  172. */
  173. #define G_PARAM_MASK (0x000000ff)
  174. /**
  175. * G_PARAM_USER_SHIFT:
  176. *
  177. * Minimum shift count to be used for user defined flags, to be stored in
  178. * #GParamSpec.flags. The maximum allowed is 10.
  179. */
  180. #define G_PARAM_USER_SHIFT (8)
  181. /* --- typedefs & structures --- */
  182. typedef struct _GParamSpec GParamSpec;
  183. typedef struct _GParamSpecClass GParamSpecClass;
  184. typedef struct _GParameter GParameter GLIB_DEPRECATED_TYPE_IN_2_54;
  185. typedef struct _GParamSpecPool GParamSpecPool;
  186. /**
  187. * 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)
  188. * @g_type_instance: private #GTypeInstance portion
  189. * @name: name of this parameter: always an interned string
  190. * @flags: #GParamFlags flags for this parameter
  191. * @value_type: the #GValue type for this parameter
  192. * @owner_type: #GType type that uses (introduces) this parameter
  193. *
  194. * All other fields of the GParamSpec struct are private and
  195. * should not be used directly.
  196. */
  197. struct _GParamSpec
  198. {
  199. GTypeInstance g_type_instance;
  200. const gchar *name; /* interned string */
  201. GParamFlags flags;
  202. GType value_type;
  203. GType owner_type; /* class or interface using this property */
  204. /*< private >*/
  205. gchar *_nick;
  206. gchar *_blurb;
  207. GData *qdata;
  208. guint ref_count;
  209. guint param_id; /* sort-criteria */
  210. };
  211. /**
  212. * GParamSpecClass:
  213. * @g_type_class: the parent class
  214. * @value_type: the #GValue type for this parameter
  215. * @finalize: The instance finalization function (optional), should chain
  216. * up to the finalize method of the parent class.
  217. * @value_set_default: Resets a @value to the default value for this type
  218. * (recommended, the default is g_value_reset()), see
  219. * g_param_value_set_default().
  220. * @value_validate: Ensures that the contents of @value comply with the
  221. * specifications set out by this type (optional), see
  222. * g_param_value_validate().
  223. * @values_cmp: Compares @value1 with @value2 according to this type
  224. * (recommended, the default is memcmp()), see g_param_values_cmp().
  225. *
  226. * The class structure for the GParamSpec type.
  227. * Normally, GParamSpec classes are filled by
  228. * g_param_type_register_static().
  229. */
  230. struct _GParamSpecClass
  231. {
  232. GTypeClass g_type_class;
  233. GType value_type;
  234. void (*finalize) (GParamSpec *pspec);
  235. /* GParam methods */
  236. void (*value_set_default) (GParamSpec *pspec,
  237. GValue *value);
  238. gboolean (*value_validate) (GParamSpec *pspec,
  239. GValue *value);
  240. gint (*values_cmp) (GParamSpec *pspec,
  241. const GValue *value1,
  242. const GValue *value2);
  243. /*< private >*/
  244. gpointer dummy[4];
  245. };
  246. /**
  247. * GParameter:
  248. * @name: the parameter name
  249. * @value: the parameter value
  250. *
  251. * The GParameter struct is an auxiliary structure used
  252. * to hand parameter name/value pairs to g_object_newv().
  253. *
  254. * Deprecated: 2.54: This type is not introspectable.
  255. */
  256. struct _GParameter /* auxiliary structure for _setv() variants */
  257. {
  258. const gchar *name;
  259. GValue value;
  260. } GLIB_DEPRECATED_TYPE_IN_2_54;
  261. /* --- prototypes --- */
  262. GLIB_AVAILABLE_IN_ALL
  263. GParamSpec* g_param_spec_ref (GParamSpec *pspec);
  264. GLIB_AVAILABLE_IN_ALL
  265. void g_param_spec_unref (GParamSpec *pspec);
  266. GLIB_AVAILABLE_IN_ALL
  267. void g_param_spec_sink (GParamSpec *pspec);
  268. GLIB_AVAILABLE_IN_ALL
  269. GParamSpec* g_param_spec_ref_sink (GParamSpec *pspec);
  270. GLIB_AVAILABLE_IN_ALL
  271. gpointer g_param_spec_get_qdata (GParamSpec *pspec,
  272. GQuark quark);
  273. GLIB_AVAILABLE_IN_ALL
  274. void g_param_spec_set_qdata (GParamSpec *pspec,
  275. GQuark quark,
  276. gpointer data);
  277. GLIB_AVAILABLE_IN_ALL
  278. void g_param_spec_set_qdata_full (GParamSpec *pspec,
  279. GQuark quark,
  280. gpointer data,
  281. GDestroyNotify destroy);
  282. GLIB_AVAILABLE_IN_ALL
  283. gpointer g_param_spec_steal_qdata (GParamSpec *pspec,
  284. GQuark quark);
  285. GLIB_AVAILABLE_IN_ALL
  286. GParamSpec* g_param_spec_get_redirect_target (GParamSpec *pspec);
  287. GLIB_AVAILABLE_IN_ALL
  288. void g_param_value_set_default (GParamSpec *pspec,
  289. GValue *value);
  290. GLIB_AVAILABLE_IN_ALL
  291. gboolean g_param_value_defaults (GParamSpec *pspec,
  292. GValue *value);
  293. GLIB_AVAILABLE_IN_ALL
  294. gboolean g_param_value_validate (GParamSpec *pspec,
  295. GValue *value);
  296. GLIB_AVAILABLE_IN_ALL
  297. gboolean g_param_value_convert (GParamSpec *pspec,
  298. const GValue *src_value,
  299. GValue *dest_value,
  300. gboolean strict_validation);
  301. GLIB_AVAILABLE_IN_ALL
  302. gint g_param_values_cmp (GParamSpec *pspec,
  303. const GValue *value1,
  304. const GValue *value2);
  305. GLIB_AVAILABLE_IN_ALL
  306. const gchar * g_param_spec_get_name (GParamSpec *pspec);
  307. GLIB_AVAILABLE_IN_ALL
  308. const gchar * g_param_spec_get_nick (GParamSpec *pspec);
  309. GLIB_AVAILABLE_IN_ALL
  310. const gchar * g_param_spec_get_blurb (GParamSpec *pspec);
  311. GLIB_AVAILABLE_IN_ALL
  312. void g_value_set_param (GValue *value,
  313. GParamSpec *param);
  314. GLIB_AVAILABLE_IN_ALL
  315. GParamSpec* g_value_get_param (const GValue *value);
  316. GLIB_AVAILABLE_IN_ALL
  317. GParamSpec* g_value_dup_param (const GValue *value);
  318. GLIB_AVAILABLE_IN_ALL
  319. void g_value_take_param (GValue *value,
  320. GParamSpec *param);
  321. GLIB_DEPRECATED_FOR(g_value_take_param)
  322. void g_value_set_param_take_ownership (GValue *value,
  323. GParamSpec *param);
  324. GLIB_AVAILABLE_IN_2_36
  325. const GValue * g_param_spec_get_default_value (GParamSpec *pspec);
  326. GLIB_AVAILABLE_IN_2_46
  327. GQuark g_param_spec_get_name_quark (GParamSpec *pspec);
  328. /* --- convenience functions --- */
  329. typedef struct _GParamSpecTypeInfo GParamSpecTypeInfo;
  330. /**
  331. * GParamSpecTypeInfo:
  332. * @instance_size: Size of the instance (object) structure.
  333. * @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.
  334. * @instance_init: Location of the instance initialization function (optional).
  335. * @value_type: The #GType of values conforming to this #GParamSpec
  336. * @finalize: The instance finalization function (optional).
  337. * @value_set_default: Resets a @value to the default value for @pspec
  338. * (recommended, the default is g_value_reset()), see
  339. * g_param_value_set_default().
  340. * @value_validate: Ensures that the contents of @value comply with the
  341. * specifications set out by @pspec (optional), see
  342. * g_param_value_validate().
  343. * @values_cmp: Compares @value1 with @value2 according to @pspec
  344. * (recommended, the default is memcmp()), see g_param_values_cmp().
  345. *
  346. * This structure is used to provide the type system with the information
  347. * required to initialize and destruct (finalize) a parameter's class and
  348. * instances thereof.
  349. * The initialized structure is passed to the g_param_type_register_static()
  350. * The type system will perform a deep copy of this structure, so its memory
  351. * does not need to be persistent across invocation of
  352. * g_param_type_register_static().
  353. */
  354. struct _GParamSpecTypeInfo
  355. {
  356. /* type system portion */
  357. guint16 instance_size; /* obligatory */
  358. guint16 n_preallocs; /* optional */
  359. void (*instance_init) (GParamSpec *pspec); /* optional */
  360. /* class portion */
  361. GType value_type; /* obligatory */
  362. void (*finalize) (GParamSpec *pspec); /* optional */
  363. void (*value_set_default) (GParamSpec *pspec, /* recommended */
  364. GValue *value);
  365. gboolean (*value_validate) (GParamSpec *pspec, /* optional */
  366. GValue *value);
  367. gint (*values_cmp) (GParamSpec *pspec, /* recommended */
  368. const GValue *value1,
  369. const GValue *value2);
  370. };
  371. GLIB_AVAILABLE_IN_ALL
  372. GType g_param_type_register_static (const gchar *name,
  373. const GParamSpecTypeInfo *pspec_info);
  374. /* For registering builting types */
  375. GType _g_param_type_register_static_constant (const gchar *name,
  376. const GParamSpecTypeInfo *pspec_info,
  377. GType opt_type);
  378. /* --- protected --- */
  379. GLIB_AVAILABLE_IN_ALL
  380. gpointer g_param_spec_internal (GType param_type,
  381. const gchar *name,
  382. const gchar *nick,
  383. const gchar *blurb,
  384. GParamFlags flags);
  385. GLIB_AVAILABLE_IN_ALL
  386. GParamSpecPool* g_param_spec_pool_new (gboolean type_prefixing);
  387. GLIB_AVAILABLE_IN_ALL
  388. void g_param_spec_pool_insert (GParamSpecPool *pool,
  389. GParamSpec *pspec,
  390. GType owner_type);
  391. GLIB_AVAILABLE_IN_ALL
  392. void g_param_spec_pool_remove (GParamSpecPool *pool,
  393. GParamSpec *pspec);
  394. GLIB_AVAILABLE_IN_ALL
  395. GParamSpec* g_param_spec_pool_lookup (GParamSpecPool *pool,
  396. const gchar *param_name,
  397. GType owner_type,
  398. gboolean walk_ancestors);
  399. GLIB_AVAILABLE_IN_ALL
  400. GList* g_param_spec_pool_list_owned (GParamSpecPool *pool,
  401. GType owner_type);
  402. GLIB_AVAILABLE_IN_ALL
  403. GParamSpec** g_param_spec_pool_list (GParamSpecPool *pool,
  404. GType owner_type,
  405. guint *n_pspecs_p);
  406. /* contracts:
  407. *
  408. * gboolean value_validate (GParamSpec *pspec,
  409. * GValue *value):
  410. * modify value contents in the least destructive way, so
  411. * that it complies with pspec's requirements (i.e.
  412. * according to minimum/maximum ranges etc...). return
  413. * whether modification was necessary.
  414. *
  415. * gint values_cmp (GParamSpec *pspec,
  416. * const GValue *value1,
  417. * const GValue *value2):
  418. * return value1 - value2, i.e. (-1) if value1 < value2,
  419. * (+1) if value1 > value2, and (0) otherwise (equality)
  420. */
  421. G_END_DECLS
  422. #endif /* __G_PARAM_H__ */