Add macros and symbols for private data on dynamic types
authorEmmanuele Bassi <ebassi@gnome.org>
Fri, 26 Jul 2013 15:45:48 +0000 (16:45 +0100)
committerMatthias Clasen <mclasen@redhat.com>
Sat, 27 Jul 2013 00:56:54 +0000 (20:56 -0400)
We need a TypeName_private_offset variable defined by the macros used to
register dynamic types. We also need to call the adjust_private_offset()
function inside class_init(). G_ADD_PRIVATE_DYNAMIC only sets the size
of the private data structure, and relies on the behaviour of the
g_type_class_adjuset_private_offset() function to register the private
data structure at class init time if passed a value greater than zero.

This allows using G_PRIVATE_OFFSET with dynamic types.

gobject/gtypemodule.h

index cc42bdd..22cbdaf 100644 (file)
@@ -180,11 +180,16 @@ static void     type_name##_class_init        (TypeName##Class *klass); \
 static void     type_name##_class_finalize    (TypeName##Class *klass); \
 static gpointer type_name##_parent_class = NULL; \
 static GType    type_name##_type_id = 0; \
-static void     type_name##_class_intern_init (gpointer klass) \
+static gint     TypeName##_private_offset; \
+\
+_G_DEFINE_TYPE_EXTENDED_CLASS_INIT(TypeName, type_name) \
+\
+static inline gpointer \
+type_name##_get_instance_private (TypeName *self) \
 { \
-  type_name##_parent_class = g_type_class_peek_parent (klass); \
-  type_name##_class_init ((TypeName##Class*) klass); \
+  return (G_STRUCT_MEMBER_P (self, TypeName##_private_offset)); \
 } \
+\
 GType \
 type_name##_get_type (void) \
 { \
@@ -237,6 +242,10 @@ type_name##_register_type (GTypeModule *type_module) \
   g_type_module_add_interface (type_module, g_define_type_id, TYPE_IFACE, &g_implement_interface_info); \
 }
 
+#define G_ADD_PRIVATE_DYNAMIC(TypeName)         { \
+  TypeName##_private_offset = sizeof (TypeNamePrivate); \
+}
+
 GLIB_AVAILABLE_IN_ALL
 GType    g_type_module_get_type       (void) G_GNUC_CONST;
 GLIB_AVAILABLE_IN_ALL