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.1 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, see <http://www.gnu.org/licenses/>.
16  */
17 #ifndef __G_TYPE_MODULE_H__
18 #define __G_TYPE_MODULE_H__
19 
20 #if !defined (__GLIB_GOBJECT_H_INSIDE__) && !defined (GOBJECT_COMPILATION)
21 #error "Only <glib-object.h> can be included directly."
22 #endif
23 
24 #include <gobject/gobject.h>
25 #include <gobject/genums.h>
26 
27 G_BEGIN_DECLS
28 
29 typedef struct _GTypeModule      GTypeModule;
30 typedef struct _GTypeModuleClass GTypeModuleClass;
31 
32 #define G_TYPE_TYPE_MODULE              (g_type_module_get_type ())
33 #define G_TYPE_MODULE(module)           (G_TYPE_CHECK_INSTANCE_CAST ((module), G_TYPE_TYPE_MODULE, GTypeModule))
34 #define G_TYPE_MODULE_CLASS(class)      (G_TYPE_CHECK_CLASS_CAST ((class), G_TYPE_TYPE_MODULE, GTypeModuleClass))
35 #define G_IS_TYPE_MODULE(module)        (G_TYPE_CHECK_INSTANCE_TYPE ((module), G_TYPE_TYPE_MODULE))
36 #define G_IS_TYPE_MODULE_CLASS(class)   (G_TYPE_CHECK_CLASS_TYPE ((class), G_TYPE_TYPE_MODULE))
37 #define G_TYPE_MODULE_GET_CLASS(module) (G_TYPE_INSTANCE_GET_CLASS ((module), G_TYPE_TYPE_MODULE, GTypeModuleClass))
38 
39 G_DEFINE_AUTOPTR_CLEANUP_FUNC(GTypeModule, g_object_unref)
40 
41 /**
42  * GTypeModule:
43  * @name: the name of the module
44  *
45  * The members of the GTypeModule 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.
97  *
98  * Furthermore, it defines a `*_get_type()` and a static `*_register_type()`
99  * functions for use in your `module_init()`.
100  *
101  * See G_DEFINE_DYNAMIC_TYPE_EXTENDED() for an example.
102  *
103  * Since: 2.14
104  */
105 #define G_DEFINE_DYNAMIC_TYPE(TN, t_n, T_P)          G_DEFINE_DYNAMIC_TYPE_EXTENDED (TN, t_n, T_P, 0, {})
106 /**
107  * G_DEFINE_DYNAMIC_TYPE_EXTENDED:
108  * @TypeName: The name of the new type, in Camel case.
109  * @type_name: The name of the new type, in lowercase, with words
110  *  separated by '_'.
111  * @TYPE_PARENT: The #GType of the parent type.
112  * @flags: #GTypeFlags to pass to g_type_module_register_type()
113  * @CODE: Custom code that gets inserted in the *_get_type() function.
114  *
115  * A more general version of G_DEFINE_DYNAMIC_TYPE() which
116  * allows to specify #GTypeFlags and custom code.
117  *
118  * |[<!-- language="C" -->
119  * G_DEFINE_DYNAMIC_TYPE_EXTENDED (GtkGadget,
120  *                                 gtk_gadget,
121  *                                 GTK_TYPE_THING,
122  *                                 0,
123  *                                 G_IMPLEMENT_INTERFACE_DYNAMIC (TYPE_GIZMO,
124  *                                                                gtk_gadget_gizmo_init));
125  * ]|
126  *
127  * expands to
128  *
129  * |[<!-- language="C" -->
130  * static void     gtk_gadget_init              (GtkGadget      *self);
131  * static void     gtk_gadget_class_init        (GtkGadgetClass *klass);
132  * static void     gtk_gadget_class_finalize    (GtkGadgetClass *klass);
133  *
134  * static gpointer gtk_gadget_parent_class = NULL;
135  * static GType    gtk_gadget_type_id = 0;
136  *
137  * static void     gtk_gadget_class_intern_init (gpointer klass)
138  * {
139  *   gtk_gadget_parent_class = g_type_class_peek_parent (klass);
140  *   gtk_gadget_class_init ((GtkGadgetClass*) klass);
141  * }
142  *
143  * GType
144  * gtk_gadget_get_type (void)
145  * {
146  *   return gtk_gadget_type_id;
147  * }
148  *
149  * static void
150  * gtk_gadget_register_type (GTypeModule *type_module)
151  * {
152  *   const GTypeInfo g_define_type_info = {
153  *     sizeof (GtkGadgetClass),
154  *     (GBaseInitFunc) NULL,
155  *     (GBaseFinalizeFunc) NULL,
156  *     (GClassInitFunc) gtk_gadget_class_intern_init,
157  *     (GClassFinalizeFunc) gtk_gadget_class_finalize,
158  *     NULL,   // class_data
159  *     sizeof (GtkGadget),
160  *     0,      // n_preallocs
161  *     (GInstanceInitFunc) gtk_gadget_init,
162  *     NULL    // value_table
163  *   };
164  *   gtk_gadget_type_id = g_type_module_register_type (type_module,
165  *                                                     GTK_TYPE_THING,
166  *                                                     "GtkGadget",
167  *                                                     &g_define_type_info,
168  *                                                     (GTypeFlags) flags);
169  *   {
170  *     const GInterfaceInfo g_implement_interface_info = {
171  *       (GInterfaceInitFunc) gtk_gadget_gizmo_init
172  *     };
173  *     g_type_module_add_interface (type_module, g_define_type_id, TYPE_GIZMO, &g_implement_interface_info);
174  *   }
175  * }
176  * ]|
177  *
178  * Since: 2.14
179  */
180 #define G_DEFINE_DYNAMIC_TYPE_EXTENDED(TypeName, type_name, TYPE_PARENT, flags, CODE) \
181 static void     type_name##_init              (TypeName        *self); \
182 static void     type_name##_class_init        (TypeName##Class *klass); \
183 static void     type_name##_class_finalize    (TypeName##Class *klass); \
184 static gpointer type_name##_parent_class = NULL; \
185 static GType    type_name##_type_id = 0; \
186 static gint     TypeName##_private_offset; \
187 \
188 _G_DEFINE_TYPE_EXTENDED_CLASS_INIT(TypeName, type_name) \
189 \
190 G_GNUC_UNUSED \
191 static inline gpointer \
192 type_name##_get_instance_private (TypeName *self) \
193 { \
194   return (G_STRUCT_MEMBER_P (self, TypeName##_private_offset)); \
195 } \
196 \
197 GType \
198 type_name##_get_type (void) \
199 { \
200   return type_name##_type_id; \
201 } \
202 static void \
203 type_name##_register_type (GTypeModule *type_module) \
204 { \
205   GType g_define_type_id G_GNUC_UNUSED; \
206   const GTypeInfo g_define_type_info = { \
207     sizeof (TypeName##Class), \
208     (GBaseInitFunc) NULL, \
209     (GBaseFinalizeFunc) NULL, \
210     (GClassInitFunc)(void (*)(void)) type_name##_class_intern_init, \
211     (GClassFinalizeFunc)(void (*)(void)) type_name##_class_finalize, \
212     NULL,   /* class_data */ \
213     sizeof (TypeName), \
214     0,      /* n_preallocs */ \
215     (GInstanceInitFunc)(void (*)(void)) type_name##_init, \
216     NULL    /* value_table */ \
217   }; \
218   type_name##_type_id = g_type_module_register_type (type_module, \
219 						     TYPE_PARENT, \
220 						     #TypeName, \
221 						     &g_define_type_info, \
222 						     (GTypeFlags) flags); \
223   g_define_type_id = type_name##_type_id; \
224   { CODE ; } \
225 }
226 
227 /**
228  * G_IMPLEMENT_INTERFACE_DYNAMIC:
229  * @TYPE_IFACE: The #GType of the interface to add
230  * @iface_init: The interface init function
231  *
232  * A convenience macro to ease interface addition in the @_C_ section
233  * of G_DEFINE_DYNAMIC_TYPE_EXTENDED().
234  *
235  * See G_DEFINE_DYNAMIC_TYPE_EXTENDED() for an example.
236  *
237  * Note that this macro can only be used together with the
238  * G_DEFINE_DYNAMIC_TYPE_EXTENDED macros, since it depends on variable
239  * names from that macro.
240  *
241  * Since: 2.24
242  */
243 #define G_IMPLEMENT_INTERFACE_DYNAMIC(TYPE_IFACE, iface_init)       { \
244   const GInterfaceInfo g_implement_interface_info = { \
245     (GInterfaceInitFunc)(void (*)(void)) iface_init, NULL, NULL      \
246   }; \
247   g_type_module_add_interface (type_module, g_define_type_id, TYPE_IFACE, &g_implement_interface_info); \
248 }
249 
250 /**
251  * G_ADD_PRIVATE_DYNAMIC:
252  * @TypeName: the name of the type in CamelCase
253  *
254  * A convenience macro to ease adding private data to instances of a new dynamic
255  * type in the @_C_ section of G_DEFINE_DYNAMIC_TYPE_EXTENDED().
256  *
257  * See G_ADD_PRIVATE() for details, it is similar but for static types.
258  *
259  * Note that this macro can only be used together with the
260  * G_DEFINE_DYNAMIC_TYPE_EXTENDED macros, since it depends on variable
261  * names from that macro.
262  *
263  * Since: 2.38
264  */
265 #define G_ADD_PRIVATE_DYNAMIC(TypeName)         { \
266   TypeName##_private_offset = sizeof (TypeName##Private); \
267 }
268 
269 GLIB_AVAILABLE_IN_ALL
270 GType    g_type_module_get_type       (void) G_GNUC_CONST;
271 GLIB_AVAILABLE_IN_ALL
272 gboolean g_type_module_use            (GTypeModule          *module);
273 GLIB_AVAILABLE_IN_ALL
274 void     g_type_module_unuse          (GTypeModule          *module);
275 GLIB_AVAILABLE_IN_ALL
276 void     g_type_module_set_name       (GTypeModule          *module,
277                                        const gchar          *name);
278 GLIB_AVAILABLE_IN_ALL
279 GType    g_type_module_register_type  (GTypeModule          *module,
280                                        GType                 parent_type,
281                                        const gchar          *type_name,
282                                        const GTypeInfo      *type_info,
283                                        GTypeFlags            flags);
284 GLIB_AVAILABLE_IN_ALL
285 void     g_type_module_add_interface  (GTypeModule          *module,
286                                        GType                 instance_type,
287                                        GType                 interface_type,
288                                        const GInterfaceInfo *interface_info);
289 GLIB_AVAILABLE_IN_ALL
290 GType    g_type_module_register_enum  (GTypeModule          *module,
291                                        const gchar          *name,
292                                        const GEnumValue     *const_static_values);
293 GLIB_AVAILABLE_IN_ALL
294 GType    g_type_module_register_flags (GTypeModule          *module,
295                                        const gchar          *name,
296                                        const GFlagsValue    *const_static_values);
297 
298 G_END_DECLS
299 
300 #endif /* __G_TYPE_MODULE_H__ */
301