gparam.h 17 KB

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