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