add an instance member value_type so the default value of the pspec class
[platform/upstream/glib.git] / gobject / gparam.h
1 /* GObject - GLib Type, Object, Parameter and Signal Library
2  * Copyright (C) 1997, 1998, 1999, 2000 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 #ifndef __G_PARAM_H__
22 #define __G_PARAM_H__
23
24
25 #include        <gobject/gvalue.h>
26
27
28 #ifdef __cplusplus
29 extern "C" {
30 #endif /* __cplusplus */
31
32
33 /* --- type macros --- */
34 #define G_TYPE_IS_PARAM(type)           (G_TYPE_FUNDAMENTAL (type) == G_TYPE_PARAM)
35 #define G_PARAM_SPEC_TYPE(pspec)        (G_TYPE_FROM_INSTANCE (pspec))
36 #define G_PARAM_SPEC_TYPE_NAME(pspec)   (g_type_name (G_PARAM_SPEC_TYPE (pspec)))
37 #define G_PARAM_SPEC(pspec)             (G_TYPE_CHECK_INSTANCE_CAST ((pspec), G_TYPE_PARAM, GParamSpec))
38 #define G_IS_PARAM_SPEC(pspec)          (G_TYPE_CHECK_INSTANCE_TYPE ((pspec), G_TYPE_PARAM))
39 #define G_PARAM_SPEC_GET_CLASS(pspec)   (G_TYPE_INSTANCE_GET_CLASS ((pspec), G_TYPE_PARAM, GParamSpecClass))
40 #define G_PARAM_SPEC_VALUE_TYPE(pspec)  (G_PARAM_SPEC (pspec)->value_type)
41 #define G_IS_VALUE_PARAM(value)         (G_TYPE_CHECK_VALUE_TYPE ((value), G_TYPE_PARAM))
42        
43
44 /* --- flags --- */
45 typedef enum
46 {
47   G_PARAM_READABLE            = 1 << 0,
48   G_PARAM_WRITABLE            = 1 << 1,
49   G_PARAM_CONSTRUCT           = 1 << 2,
50   G_PARAM_CONSTRUCT_ONLY      = 1 << 3,
51   G_PARAM_PRIVATE             = 1 << 4,
52 #define G_PARAM_MASK            (0x000000ff)
53   /* bits in the range 0xffffff00 are reserved for 3rd party usage */
54 #define G_PARAM_USER_MASK       (0xffffff00)
55 } GParamFlags;
56
57
58 /* --- typedefs & structures --- */
59 typedef struct _GParamSpec      GParamSpec;
60 typedef struct _GParamSpecClass GParamSpecClass;
61 typedef struct _GParamSpecPool  GParamSpecPool;
62 struct _GParamSpec
63 {
64   GTypeInstance  g_type_instance;
65
66   gchar         *name;
67   gchar         *nick;
68   gchar         *blurb;
69   GParamFlags    flags;
70   GType          value_type;
71
72   /*< private >*/
73   GType          owner_type;
74   GData         *qdata;
75   guint          ref_count;
76 };
77 struct _GParamSpecClass
78 {
79   GTypeClass      g_type_class;
80
81   GType           value_type;
82
83   void          (*finalize)             (GParamSpec   *pspec);
84
85   /* GParam methods */
86   void          (*value_set_default)    (GParamSpec   *pspec,
87                                          GValue       *value);
88   gboolean      (*value_validate)       (GParamSpec   *pspec,
89                                          GValue       *value);
90   gint          (*values_cmp)           (GParamSpec   *pspec,
91                                          const GValue *value1,
92                                          const GValue *value2);
93 };
94
95
96 /* --- prototypes --- */
97 GParamSpec*     g_param_spec_ref                (GParamSpec    *pspec);
98 void            g_param_spec_unref              (GParamSpec    *pspec);
99 void            g_param_spec_sink               (GParamSpec    *pspec);
100 gpointer        g_param_spec_get_qdata          (GParamSpec    *pspec,
101                                                  GQuark         quark);
102 void            g_param_spec_set_qdata          (GParamSpec    *pspec,
103                                                  GQuark         quark,
104                                                  gpointer       data);
105 void            g_param_spec_set_qdata_full     (GParamSpec    *pspec,
106                                                  GQuark         quark,
107                                                  gpointer       data,
108                                                  GDestroyNotify destroy);
109 gpointer        g_param_spec_steal_qdata        (GParamSpec    *pspec,
110                                                  GQuark         quark);
111 void            g_param_value_set_default       (GParamSpec    *pspec,
112                                                  GValue        *value);
113 gboolean        g_param_value_defaults          (GParamSpec    *pspec,
114                                                  GValue        *value);
115 gboolean        g_param_value_validate          (GParamSpec    *pspec,
116                                                  GValue        *value);
117 gint            g_param_values_cmp              (GParamSpec    *pspec,
118                                                  const GValue  *value1,
119                                                  const GValue  *value2);
120 void            g_value_set_param               (GValue        *value,
121                                                  GParamSpec    *param);
122 GParamSpec*     g_value_get_param               (const GValue  *value);
123 GParamSpec*     g_value_dup_param               (const GValue  *value);
124
125
126 /* --- convenience functions --- */
127 typedef struct _GParamSpecTypeInfo GParamSpecTypeInfo;
128 struct _GParamSpecTypeInfo
129 {
130   /* type system portion */
131   guint16         instance_size;                               /* obligatory */
132   guint16         n_preallocs;                                 /* optional */
133   void          (*instance_init)        (GParamSpec   *pspec); /* optional */
134
135   /* class portion */
136   GType           value_type;                                  /* obligatory */
137   void          (*finalize)             (GParamSpec   *pspec); /* optional */
138   void          (*value_set_default)    (GParamSpec   *pspec,  /* recommended */
139                                          GValue       *value);
140   gboolean      (*value_validate)       (GParamSpec   *pspec,  /* optional */
141                                          GValue       *value);
142   gint          (*values_cmp)           (GParamSpec   *pspec,  /* recommended */
143                                          const GValue *value1,
144                                          const GValue *value2);
145 };
146 GType   g_param_type_register_static    (const gchar              *name,
147                                          const GParamSpecTypeInfo *pspec_info);
148
149
150 /* --- protected --- */
151 gpointer        g_param_spec_internal           (GType          param_type,
152                                                  const gchar   *name,
153                                                  const gchar   *nick,
154                                                  const gchar   *blurb,
155                                                  GParamFlags    flags);
156 GParamSpecPool* g_param_spec_pool_new           (gboolean       type_prefixing);
157 void            g_param_spec_pool_insert        (GParamSpecPool *pool,
158                                                  GParamSpec     *pspec,
159                                                  GType           owner_type);
160 void            g_param_spec_pool_remove        (GParamSpecPool *pool,
161                                                  GParamSpec     *pspec);
162 GParamSpec*     g_param_spec_pool_lookup        (GParamSpecPool *pool,
163                                                  const gchar    *param_name,
164                                                  GType           owner_type,
165                                                  gboolean        walk_ancestors,
166                                                  const gchar   **trailer_p);
167
168
169 /* contracts:
170  *
171  * gboolean value_validate (GParamSpec *pspec,
172  *                          GValue     *value):
173  *      modify value contents in the least destructive way, so
174  *      that it complies with pspec's requirements (i.e.
175  *      according to minimum/maximum ranges etc...). return
176  *      whether modification was necessary.
177  *
178  * gint values_cmp (GParamSpec   *pspec,
179  *                  const GValue *value1,
180  *                  const GValue *value2):
181  *      return value1 - value2, i.e. <0 if value1 < value2,
182  *      >0 if value1 > value2, and 0 otherwise (they are equal)
183  */
184
185 #ifdef __cplusplus
186 }
187 #endif /* __cplusplus */
188
189 #endif /* __G_PARAM_H__ */