[glib/wip/private-rework-3: 1/4] Add G_PRIVATE_OFFSET



commit 57804c4af089465d4faa0d740ba1708b9a62a0c6
Author: Emmanuele Bassi <ebassi gnome org>
Date:   Tue Jun 18 15:45:56 2013 +0100

    Add G_PRIVATE_OFFSET
    
    A macro that evaluates to the offset of a field inside an instance
    private data structure.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=700035

 gobject/gtype.h         |   38 +++++++++++++++++++++++++++-----------
 gobject/tests/private.c |   12 +++++++-----
 2 files changed, 34 insertions(+), 16 deletions(-)
---
diff --git a/gobject/gtype.h b/gobject/gtype.h
index 53e36ee..6813ba9 100644
--- a/gobject/gtype.h
+++ b/gobject/gtype.h
@@ -1355,7 +1355,7 @@ guint     g_type_get_type_registration_serial (void);
  * 
  * Since: 2.38
  */
-#define G_DEFINE_TYPE_WITH_PRIVATE(TN, t_n, T_P)            G_DEFINE_TYPE_EXTENDED (TN, t_n, T_P, 0, 
G_ADD_PRIVATE (TN, t_n))
+#define G_DEFINE_TYPE_WITH_PRIVATE(TN, t_n, T_P)            G_DEFINE_TYPE_EXTENDED (TN, t_n, T_P, 0, 
G_ADD_PRIVATE (TN))
 /**
  * G_DEFINE_ABSTRACT_TYPE:
  * @TN: The name of the new type, in Camel case.
@@ -1398,7 +1398,7 @@ guint     g_type_get_type_registration_serial (void);
  * 
  * Since: 2.4
  */
-#define G_DEFINE_ABSTRACT_TYPE_WITH_PRIVATE(TN, t_n, T_P)   G_DEFINE_TYPE_EXTENDED (TN, t_n, T_P, 
G_TYPE_FLAG_ABSTRACT, G_ADD_PRIVATE (TN, t_n))
+#define G_DEFINE_ABSTRACT_TYPE_WITH_PRIVATE(TN, t_n, T_P)   G_DEFINE_TYPE_EXTENDED (TN, t_n, T_P, 
G_TYPE_FLAG_ABSTRACT, G_ADD_PRIVATE (TN))
 /**
  * G_DEFINE_TYPE_EXTENDED:
  * @TN: The name of the new type, in Camel case.
@@ -1525,7 +1525,6 @@ guint     g_type_get_type_registration_serial (void);
 /**
  * G_ADD_PRIVATE:
  * @TypeName: the name of the type in CamelCase
- * @type_name: the name of the new type, in lowercase, with words separated by '_'
  *
  * A convenience macro to ease adding private data to instances of a new type
  * in the @_C_ section of G_DEFINE_TYPE_WITH_CODE() or
@@ -1543,7 +1542,7 @@ guint     g_type_get_type_registration_serial (void);
  *   } MyObjectPrivate;
  *
  *   G_DEFINE_TYPE_WITH_CODE (MyObject, my_object, G_TYPE_OBJECT,
- *                            G_ADD_PRIVATE (MyObject, my_object))
+ *                            G_ADD_PRIVATE (MyObject))
  * ]|
  *
  * Will add MyObjectPrivate as the private data to any instance of the MyObject
@@ -1578,22 +1577,39 @@ guint     g_type_get_type_registration_serial (void);
  *
  * Since: 2.38
  */
-#define G_ADD_PRIVATE(TypeName, type_name)         { \
-  type_name##_private_offset = \
+#define G_ADD_PRIVATE(TypeName) { \
+  TypeName##_private_offset = \
     g_type_add_instance_private (g_define_type_id, sizeof (TypeName##Private)); \
 }
 
+/**
+ * G_PRIVATE_OFFSET:
+ * @TypeName: the name of the type in CamelCase
+ * @field: the name of the field in the private data structure
+ *
+ * Evaluates to the offset of the @field inside the instance private data
+ * structure for @TypeName.
+ *
+ * Note that this macro can only be used together with the G_DEFINE_TYPE_*
+ * and G_ADD_PRIVATE() macros, since it depends on variable names from
+ * those macros.
+ *
+ * Since: 2.38
+ */
+#define G_PRIVATE_OFFSET(TypeName, field) \
+  (TypeName##_private_offset + (G_STRUCT_OFFSET (TypeName##Private, (field))))
+
 #define _G_DEFINE_TYPE_EXTENDED_BEGIN(TypeName, type_name, TYPE_PARENT, flags) \
 \
 static void     type_name##_init              (TypeName        *self); \
 static void     type_name##_class_init        (TypeName##Class *klass); \
 static gpointer type_name##_parent_class = NULL; \
-static gint     type_name##_private_offset; \
+static gint     TypeName##_private_offset; \
 static void     type_name##_class_intern_init (gpointer klass) \
 { \
   type_name##_parent_class = g_type_class_peek_parent (klass); \
-  if (type_name##_private_offset != 0) \
-    g_type_class_add_instance_private (klass, &type_name##_private_offset); \
+  if (TypeName##_private_offset != 0) \
+    g_type_class_add_instance_private (klass, &TypeName##_private_offset); \
   type_name##_class_init ((TypeName##Class*) klass); \
 } \
 \
@@ -1623,8 +1639,8 @@ type_name##_get_type (void) \
 static inline gpointer \
 type_name##_get_private (TypeName *self) \
 { \
-  if (G_LIKELY (type_name##_private_offset != 0)) \
-    return (G_STRUCT_MEMBER_P (self, type_name##_private_offset)); \
+  if (G_LIKELY (TypeName##_private_offset != 0)) \
+    return (G_STRUCT_MEMBER_P (self, TypeName##_private_offset)); \
 \
   return g_type_instance_get_private ((GTypeInstance *) self, type_name##_get_type ()); \
 }
diff --git a/gobject/tests/private.c b/gobject/tests/private.c
index ce1cdc5..db1779a 100644
--- a/gobject/tests/private.c
+++ b/gobject/tests/private.c
@@ -16,7 +16,7 @@ typedef struct {
 GType test_object_get_type (void);
 
 G_DEFINE_TYPE_WITH_CODE (TestObject, test_object, G_TYPE_OBJECT,
-                         G_ADD_PRIVATE (TestObject, test_object))
+                         G_ADD_PRIVATE (TestObject))
 
 static void
 test_object_class_init (TestObjectClass *klass)
@@ -31,7 +31,7 @@ test_object_init (TestObject *self)
   if (g_test_verbose ())
     g_print ("Offset of TestObjectPrivate for type '%s': %d\n",
              G_OBJECT_TYPE_NAME (self),
-             test_object_private_offset);
+             TestObject_private_offset);
 
   priv->dummy_0 = 42;
   priv->dummy_1 = 3.14159f;
@@ -68,7 +68,7 @@ typedef struct {
 GType test_derived_get_type (void);
 
 G_DEFINE_TYPE_WITH_CODE (TestDerived, test_derived, test_object_get_type (),
-                         G_ADD_PRIVATE (TestDerived, test_derived))
+                         G_ADD_PRIVATE (TestDerived))
 
 static void
 test_derived_finalize (GObject *obj)
@@ -94,7 +94,7 @@ test_derived_init (TestDerived *self)
   if (g_test_verbose ())
     g_print ("Offset of TestDerivedPrivate for type '%s': %d\n",
              G_OBJECT_TYPE_NAME (self),
-             test_derived_private_offset);
+             TestDerived_private_offset);
 
   priv->dummy_2 = g_strdup ("Hello");
 }
@@ -123,11 +123,13 @@ GType test_mixed_get_type (void);
 
 G_DEFINE_TYPE (TestMixed, test_mixed, test_object_get_type ())
 
+G_GNUC_BEGIN_IGNORE_DEPRECATIONS
 static void
 test_mixed_class_init (TestMixedClass *klass)
 {
   g_type_class_add_private (klass, sizeof (TestMixedPrivate));
 }
+G_GNUC_END_IGNORE_DEPRECATIONS
 
 static void
 test_mixed_init (TestMixed *self)
@@ -160,7 +162,7 @@ typedef struct {
 GType test_mixed_derived_get_type (void);
 
 G_DEFINE_TYPE_WITH_CODE (TestMixedDerived, test_mixed_derived, test_mixed_get_type (),
-                         G_ADD_PRIVATE (TestMixedDerived, test_mixed_derived))
+                         G_ADD_PRIVATE (TestMixedDerived))
 
 static void
 test_mixed_derived_class_init (TestMixedDerivedClass *klass)


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]