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