Fix G_ADD_PRIVATE_DYNAMIC()
[platform/upstream/glib.git] / gobject / gtypemodule.h
1 /* GObject - GLib Type, Object, Parameter and Signal Library
2  * Copyright (C) 2000 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 Public
15  * 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 #ifndef __G_TYPE_MODULE_H__
20 #define __G_TYPE_MODULE_H__
21
22 #if !defined (__GLIB_GOBJECT_H_INSIDE__) && !defined (GOBJECT_COMPILATION)
23 #error "Only <glib-object.h> can be included directly."
24 #endif
25
26 #include <gobject/gobject.h>
27 #include <gobject/genums.h>
28
29 G_BEGIN_DECLS
30
31 typedef struct _GTypeModule      GTypeModule;
32 typedef struct _GTypeModuleClass GTypeModuleClass;
33
34 #define G_TYPE_TYPE_MODULE              (g_type_module_get_type ())
35 #define G_TYPE_MODULE(module)           (G_TYPE_CHECK_INSTANCE_CAST ((module), G_TYPE_TYPE_MODULE, GTypeModule))
36 #define G_TYPE_MODULE_CLASS(class)      (G_TYPE_CHECK_CLASS_CAST ((class), G_TYPE_TYPE_MODULE, GTypeModuleClass))
37 #define G_IS_TYPE_MODULE(module)        (G_TYPE_CHECK_INSTANCE_TYPE ((module), G_TYPE_TYPE_MODULE))
38 #define G_IS_TYPE_MODULE_CLASS(class)   (G_TYPE_CHECK_CLASS_TYPE ((class), G_TYPE_TYPE_MODULE))
39 #define G_TYPE_MODULE_GET_CLASS(module) (G_TYPE_INSTANCE_GET_CLASS ((module), G_TYPE_TYPE_MODULE, GTypeModuleClass))
40
41 /**
42  * GTypeModule:
43  * @name: the name of the module
44  * 
45  * The members of the <structname>GTypeModule</structname> structure should not 
46  * be accessed directly, except for the @name field.
47  */
48 struct _GTypeModule 
49 {
50   GObject parent_instance;
51
52   guint use_count;
53   GSList *type_infos;
54   GSList *interface_infos;
55
56   /*< public >*/
57   gchar *name;
58 };
59
60 /**
61  * GTypeModuleClass:
62  * @parent_class: the parent class
63  * @load: loads the module and registers one or more types using
64  *  g_type_module_register_type().
65  * @unload: unloads the module
66  * 
67  * In order to implement dynamic loading of types based on #GTypeModule, 
68  * the @load and @unload functions in #GTypeModuleClass must be implemented.
69  */
70 struct _GTypeModuleClass
71 {
72   GObjectClass parent_class;
73
74   /*< public >*/
75   gboolean (* load)   (GTypeModule *module);
76   void     (* unload) (GTypeModule *module);
77
78   /*< private >*/
79   /* Padding for future expansion */
80   void (*reserved1) (void);
81   void (*reserved2) (void);
82   void (*reserved3) (void);
83   void (*reserved4) (void);
84 };
85
86 /**
87  * G_DEFINE_DYNAMIC_TYPE:
88  * @TN: The name of the new type, in Camel case.
89  * @t_n: The name of the new type, in lowercase, with words
90  *  separated by '_'.
91  * @T_P: The #GType of the parent type.
92  * 
93  * A convenience macro for dynamic type implementations, which declares a
94  * class initialization function, an instance initialization function (see 
95  * #GTypeInfo for information about these) and a static variable named 
96  * @t_n<!-- -->_parent_class pointing to the parent class. Furthermore, 
97  * it defines a <function>*_get_type()</function> and a static 
98  * <function>*_register_type()</function> function for use in your
99  * <function>module_init()</function>.
100  * See G_DEFINE_DYNAMIC_TYPE_EXTENDED() for an example.
101  * 
102  * Since: 2.14
103  */
104 #define G_DEFINE_DYNAMIC_TYPE(TN, t_n, T_P)          G_DEFINE_DYNAMIC_TYPE_EXTENDED (TN, t_n, T_P, 0, {})
105 /**
106  * G_DEFINE_DYNAMIC_TYPE_EXTENDED:
107  * @TypeName: The name of the new type, in Camel case.
108  * @type_name: The name of the new type, in lowercase, with words
109  *  separated by '_'.
110  * @TYPE_PARENT: The #GType of the parent type.
111  * @flags: #GTypeFlags to pass to g_type_module_register_type()
112  * @CODE: Custom code that gets inserted in the *_get_type() function.
113  * 
114  * A more general version of G_DEFINE_DYNAMIC_TYPE() which
115  * allows to specify #GTypeFlags and custom code.
116  * 
117  * |[
118  * G_DEFINE_DYNAMIC_TYPE_EXTENDED (GtkGadget,
119  *                                 gtk_gadget,
120  *                                 GTK_TYPE_THING,
121  *                                 0,
122  *                                 G_IMPLEMENT_INTERFACE_DYNAMIC (TYPE_GIZMO,
123  *                                                                gtk_gadget_gizmo_init));
124  * ]|
125  * expands to
126  * |[
127  * static void     gtk_gadget_init              (GtkGadget      *self);
128  * static void     gtk_gadget_class_init        (GtkGadgetClass *klass);
129  * static void     gtk_gadget_class_finalize    (GtkGadgetClass *klass);
130  * 
131  * static gpointer gtk_gadget_parent_class = NULL;
132  * static GType    gtk_gadget_type_id = 0;
133  * 
134  * static void     gtk_gadget_class_intern_init (gpointer klass)
135  * {
136  *   gtk_gadget_parent_class = g_type_class_peek_parent (klass); 
137  *   gtk_gadget_class_init ((GtkGadgetClass*) klass); 
138  * }
139  * 
140  * GType
141  * gtk_gadget_get_type (void)
142  * {
143  *   return gtk_gadget_type_id;
144  * }
145  * 
146  * static void
147  * gtk_gadget_register_type (GTypeModule *type_module)
148  * {
149  *   const GTypeInfo g_define_type_info = {
150  *     sizeof (GtkGadgetClass),
151  *     (GBaseInitFunc) NULL,
152  *     (GBaseFinalizeFunc) NULL,
153  *     (GClassInitFunc) gtk_gadget_class_intern_init,
154  *     (GClassFinalizeFunc) gtk_gadget_class_finalize,
155  *     NULL,   // class_data
156  *     sizeof (GtkGadget),
157  *     0,      // n_preallocs
158  *     (GInstanceInitFunc) gtk_gadget_init, 
159  *     NULL    // value_table
160  *   };
161  *   gtk_gadget_type_id = g_type_module_register_type (type_module,
162  *                                                     GTK_TYPE_THING,
163  *                                                     "GtkGadget",
164  *                                                     &g_define_type_info,
165  *                                                     (GTypeFlags) flags);
166  *   {
167  *     const GInterfaceInfo g_implement_interface_info = {
168  *       (GInterfaceInitFunc) gtk_gadget_gizmo_init
169  *     };
170  *     g_type_module_add_interface (type_module, g_define_type_id, TYPE_GIZMO, &g_implement_interface_info);
171  *   }
172  * }
173  * ]|
174  * 
175  * Since: 2.14
176  */
177 #define G_DEFINE_DYNAMIC_TYPE_EXTENDED(TypeName, type_name, TYPE_PARENT, flags, CODE) \
178 static void     type_name##_init              (TypeName        *self); \
179 static void     type_name##_class_init        (TypeName##Class *klass); \
180 static void     type_name##_class_finalize    (TypeName##Class *klass); \
181 static gpointer type_name##_parent_class = NULL; \
182 static GType    type_name##_type_id = 0; \
183 static gint     TypeName##_private_offset; \
184 \
185 _G_DEFINE_TYPE_EXTENDED_CLASS_INIT(TypeName, type_name) \
186 \
187 static inline gpointer \
188 type_name##_get_instance_private (TypeName *self) \
189 { \
190   return (G_STRUCT_MEMBER_P (self, TypeName##_private_offset)); \
191 } \
192 \
193 GType \
194 type_name##_get_type (void) \
195 { \
196   return type_name##_type_id; \
197 } \
198 static void \
199 type_name##_register_type (GTypeModule *type_module) \
200 { \
201   GType g_define_type_id G_GNUC_UNUSED; \
202   const GTypeInfo g_define_type_info = { \
203     sizeof (TypeName##Class), \
204     (GBaseInitFunc) NULL, \
205     (GBaseFinalizeFunc) NULL, \
206     (GClassInitFunc) type_name##_class_intern_init, \
207     (GClassFinalizeFunc) type_name##_class_finalize, \
208     NULL,   /* class_data */ \
209     sizeof (TypeName), \
210     0,      /* n_preallocs */ \
211     (GInstanceInitFunc) type_name##_init, \
212     NULL    /* value_table */ \
213   }; \
214   type_name##_type_id = g_type_module_register_type (type_module, \
215                                                      TYPE_PARENT, \
216                                                      #TypeName, \
217                                                      &g_define_type_info, \
218                                                      (GTypeFlags) flags); \
219   g_define_type_id = type_name##_type_id; \
220   { CODE ; } \
221 }
222
223 /**
224  * G_IMPLEMENT_INTERFACE_DYNAMIC:
225  * @TYPE_IFACE: The #GType of the interface to add
226  * @iface_init: The interface init function
227  *
228  * A convenience macro to ease interface addition in the @_C_ section
229  * of G_DEFINE_DYNAMIC_TYPE_EXTENDED(). See G_DEFINE_DYNAMIC_TYPE_EXTENDED()
230  * for an example.
231  *
232  * Note that this macro can only be used together with the
233  * G_DEFINE_DYNAMIC_TYPE_EXTENDED macros, since it depends on variable
234  * names from that macro.
235  *
236  * Since: 2.24
237  */
238 #define G_IMPLEMENT_INTERFACE_DYNAMIC(TYPE_IFACE, iface_init)       { \
239   const GInterfaceInfo g_implement_interface_info = { \
240     (GInterfaceInitFunc) iface_init, NULL, NULL      \
241   }; \
242   g_type_module_add_interface (type_module, g_define_type_id, TYPE_IFACE, &g_implement_interface_info); \
243 }
244
245 #define G_ADD_PRIVATE_DYNAMIC(TypeName)         { \
246   TypeName##_private_offset = sizeof (TypeName##Private); \
247 }
248
249 GLIB_AVAILABLE_IN_ALL
250 GType    g_type_module_get_type       (void) G_GNUC_CONST;
251 GLIB_AVAILABLE_IN_ALL
252 gboolean g_type_module_use            (GTypeModule          *module);
253 GLIB_AVAILABLE_IN_ALL
254 void     g_type_module_unuse          (GTypeModule          *module);
255 GLIB_AVAILABLE_IN_ALL
256 void     g_type_module_set_name       (GTypeModule          *module,
257                                        const gchar          *name);
258 GLIB_AVAILABLE_IN_ALL
259 GType    g_type_module_register_type  (GTypeModule          *module,
260                                        GType                 parent_type,
261                                        const gchar          *type_name,
262                                        const GTypeInfo      *type_info,
263                                        GTypeFlags            flags);
264 GLIB_AVAILABLE_IN_ALL
265 void     g_type_module_add_interface  (GTypeModule          *module,
266                                        GType                 instance_type,
267                                        GType                 interface_type,
268                                        const GInterfaceInfo *interface_info);
269 GLIB_AVAILABLE_IN_ALL
270 GType    g_type_module_register_enum  (GTypeModule          *module,
271                                        const gchar          *name,
272                                        const GEnumValue     *const_static_values);
273 GLIB_AVAILABLE_IN_ALL
274 GType    g_type_module_register_flags (GTypeModule          *module,
275                                        const gchar          *name,
276                                        const GFlagsValue    *const_static_values);
277
278 G_END_DECLS
279
280 #endif /* __G_TYPE_MODULE_H__ */