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