• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #if !defined (__GLIB_GOBJECT_H_INSIDE__) && !defined (GOBJECT_COMPILATION)
20 #error "Only <glib-object.h> can be included directly."
21 #endif
22 
23 #ifndef __G_TYPE_MODULE_H__
24 #define __G_TYPE_MODULE_H__
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 (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_add_interface_static (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 void     type_name##_class_intern_init (gpointer klass) \
184 { \
185   type_name##_parent_class = g_type_class_peek_parent (klass); \
186   type_name##_class_init ((TypeName##Class*) klass); \
187 } \
188 GType \
189 type_name##_get_type (void) \
190 { \
191   return type_name##_type_id; \
192 } \
193 static void \
194 type_name##_register_type (GTypeModule *type_module) \
195 { \
196   GType g_define_type_id; \
197   const GTypeInfo g_define_type_info = { \
198     sizeof (TypeName##Class), \
199     (GBaseInitFunc) NULL, \
200     (GBaseFinalizeFunc) NULL, \
201     (GClassInitFunc) type_name##_class_intern_init, \
202     (GClassFinalizeFunc) type_name##_class_finalize, \
203     NULL,   /* class_data */ \
204     sizeof (TypeName), \
205     0,      /* n_preallocs */ \
206     (GInstanceInitFunc) type_name##_init, \
207     NULL    /* value_table */ \
208   }; \
209   type_name##_type_id = g_type_module_register_type (type_module, \
210 						     TYPE_PARENT, \
211 						     #TypeName, \
212 						     &g_define_type_info, \
213 						     (GTypeFlags) flags); \
214   g_define_type_id = type_name##_type_id; \
215   { CODE ; } \
216 }
217 
218 
219 GType    g_type_module_get_type       (void) G_GNUC_CONST;
220 gboolean g_type_module_use            (GTypeModule          *module);
221 void     g_type_module_unuse          (GTypeModule          *module);
222 void     g_type_module_set_name       (GTypeModule          *module,
223                                        const gchar          *name);
224 GType    g_type_module_register_type  (GTypeModule          *module,
225                                        GType                 parent_type,
226                                        const gchar          *type_name,
227                                        const GTypeInfo      *type_info,
228                                        GTypeFlags            flags);
229 void     g_type_module_add_interface  (GTypeModule          *module,
230                                        GType                 instance_type,
231                                        GType                 interface_type,
232                                        const GInterfaceInfo *interface_info);
233 GType    g_type_module_register_enum  (GTypeModule          *module,
234                                        const gchar          *name,
235                                        const GEnumValue     *const_static_values);
236 GType    g_type_module_register_flags (GTypeModule          *module,
237                                        const gchar          *name,
238                                        const GFlagsValue    *const_static_values);
239 
240 G_END_DECLS
241 
242 #endif /* __G_TYPE_MODULE_H__ */
243