[glib] Add G_DEFINE_INTERFACE



commit 91d96350a7eed2e2acfc0c254b6dfc4e6fe81a8b
Author: Dan Winship <danw gnome org>
Date:   Tue Dec 1 10:33:12 2009 +0100

    Add G_DEFINE_INTERFACE
    
    This is a macro similar to G_DEFINE_TYPE but it lets you define
    interfaces rather than classes.
    
    For discussion, see bug #320482

 docs/reference/gobject/gobject-sections.txt |    2 +
 gobject/gtype.h                             |   70 ++++++++++++++++++++++++++-
 gobject/tests/threadtests.c                 |   30 -----------
 3 files changed, 70 insertions(+), 32 deletions(-)
---
diff --git a/docs/reference/gobject/gobject-sections.txt b/docs/reference/gobject/gobject-sections.txt
index a6efb30..e418f25 100644
--- a/docs/reference/gobject/gobject-sections.txt
+++ b/docs/reference/gobject/gobject-sections.txt
@@ -101,6 +101,8 @@ G_DEFINE_TYPE
 G_DEFINE_TYPE_WITH_CODE
 G_DEFINE_ABSTRACT_TYPE
 G_DEFINE_ABSTRACT_TYPE_WITH_CODE
+G_DEFINE_INTERFACE
+G_DEFINE_INTERFACE_WITH_CODE
 G_IMPLEMENT_INTERFACE
 G_DEFINE_TYPE_EXTENDED
 
diff --git a/gobject/gtype.h b/gobject/gtype.h
index 860b518..c5f7898 100644
--- a/gobject/gtype.h
+++ b/gobject/gtype.h
@@ -1243,7 +1243,7 @@ gpointer g_type_instance_get_private    (GTypeInstance              *instance,
  * @_C_: Custom code that gets inserted in the *_get_type() function.
  * 
  * A convenience macro for type implementations.  
- * Similar to G_DEFINE_TYPE(), but allows to insert custom code into the 
+ * Similar to G_DEFINE_TYPE(), but allows you to insert custom code into the 
  * *_get_type() function, e.g. interface implementations via G_IMPLEMENT_INTERFACE().
  * See G_DEFINE_TYPE_EXTENDED() for an example.
  * 
@@ -1273,7 +1273,7 @@ gpointer g_type_instance_get_private    (GTypeInstance              *instance,
  * @_C_: Custom code that gets inserted in the @type_name_get_type() function.
  * 
  * A convenience macro for type implementations.
- * Similar to G_DEFINE_TYPE_WITH_CODE(), but defines an abstract type and allows to 
+ * Similar to G_DEFINE_TYPE_WITH_CODE(), but defines an abstract type and allows you to 
  * insert custom code into the *_get_type() function, e.g. interface implementations 
  * via G_IMPLEMENT_INTERFACE(). See G_DEFINE_TYPE_EXTENDED() for an example.
  * 
@@ -1345,6 +1345,44 @@ gpointer g_type_instance_get_private    (GTypeInstance              *instance,
 #define G_DEFINE_TYPE_EXTENDED(TN, t_n, T_P, _f_, _C_)	    _G_DEFINE_TYPE_EXTENDED_BEGIN (TN, t_n, T_P, _f_) {_C_;} _G_DEFINE_TYPE_EXTENDED_END()
 
 /**
+ * G_DEFINE_INTERFACE:
+ * @TN: The name of the new type, in Camel case.
+ * @t_n: The name of the new type, in lowercase, with words separated by '_'.
+ * @T_P: The #GType of the prerequisite type for the interface, or 0
+ * (%G_TYPE_INVALID) for no prerequisite type.
+ *
+ * A convenience macro for #GTypeInterface definitions, which declares
+ * a default vtable initialization function and defines a *_get_type()
+ * function.
+ *
+ * The macro expects the interface initialization function to have the
+ * name <literal>t_n ## _default_init</literal>, and the interface
+ * structure to have the name <literal>TN ## Interface</literal>.
+ *
+ * @Since: 2.20
+ */
+#define G_DEFINE_INTERFACE(TN, t_n, T_P)		    G_DEFINE_INTERFACE_WITH_CODE(TN, t_n, T_P, ;)
+
+/**
+ * G_DEFINE_INTERFACE_WITH_CODE:
+ * @TN: The name of the new type, in Camel case.
+ * @t_n: The name of the new type, in lowercase, with words separated by '_'.
+ * @T_P: The #GType of the prerequisite type for the interface, or 0
+ * (%G_TYPE_INVALID) for no prerequisite type.
+ * @_C_: Custom code that gets inserted in the *_get_type() function.
+ *
+ * A convenience macro for #GTypeInterface definitions. Similar to
+ * G_DEFINE_INTERFACE(), but allows you to insert custom code into the
+ * *_get_type() function, e.g. additional interface implementations
+ * via G_IMPLEMENT_INTERFACE(), or additional prerequisite types. See
+ * G_DEFINE_TYPE_EXTENDED() for a similar example using
+ * G_DEFINE_TYPE_WITH_CODE().
+ *
+ * @Since: 2.20
+ */
+#define G_DEFINE_INTERFACE_WITH_CODE(TN, t_n, T_P, _C_)     _G_DEFINE_INTERFACE_EXTENDED_BEGIN(TN, t_n, T_P) {_C_;} _G_DEFINE_INTERFACE_EXTENDED_END()
+
+/**
  * G_IMPLEMENT_INTERFACE:
  * @TYPE_IFACE: The #GType of the interface to add
  * @iface_init: The interface init function
@@ -1399,6 +1437,34 @@ type_name##_get_type (void) \
   return g_define_type_id__volatile;	\
 } /* closes type_name##_get_type() */
 
+#define _G_DEFINE_INTERFACE_EXTENDED_BEGIN(TypeName, type_name, TYPE_PREREQ) \
+\
+static void     type_name##_default_init        (TypeName##Interface *klass); \
+\
+GType \
+type_name##_get_type (void) \
+{ \
+  static volatile gsize g_define_type_id__volatile = 0; \
+  if (g_once_init_enter (&g_define_type_id__volatile))  \
+    { \
+      GType g_define_type_id = \
+        g_type_register_static_simple (G_TYPE_INTERFACE, \
+                                       g_intern_static_string (#TypeName), \
+                                       sizeof (TypeName##Interface), \
+                                       (GClassInitFunc)type_name##_default_init, \
+                                       0, \
+                                       (GInstanceInitFunc)NULL, \
+                                       (GTypeFlags) 0); \
+      if (TYPE_PREREQ) \
+        g_type_interface_add_prerequisite (g_define_type_id, TYPE_PREREQ); \
+      { /* custom code follows */
+#define _G_DEFINE_INTERFACE_EXTENDED_END()	\
+        /* following custom code */		\
+      }						\
+      g_once_init_leave (&g_define_type_id__volatile, g_define_type_id); \
+    }						\
+  return g_define_type_id__volatile;			\
+} /* closes type_name##_get_type() */
 
 /* --- protected (for fundamental type implementations) --- */
 GTypePlugin*	 g_type_get_plugin		(GType		     type);
diff --git a/gobject/tests/threadtests.c b/gobject/tests/threadtests.c
index eabbe05..757df70 100644
--- a/gobject/tests/threadtests.c
+++ b/gobject/tests/threadtests.c
@@ -22,36 +22,6 @@
 #include <glib.h>
 #include <glib-object.h>
 
-#define G_DEFINE_INTERFACE(TN, t_n, T_P)                   G_DEFINE_INTERFACE_WITH_CODE(TN, t_n, T_P, ;)
-#define G_DEFINE_INTERFACE_WITH_CODE(TN, t_n, T_P, _C_)     _G_DEFINE_INTERFACE_EXTENDED_BEGIN(TN, t_n, T_P) {_C_;} _G_DEFINE_INTERFACE_EXTENDED_END()
-/* _default_init, ##Interface, if(TYPE_PREREQ); */
-#define _G_DEFINE_INTERFACE_EXTENDED_BEGIN(TypeName, type_name, TYPE_PREREQ) \
-static void type_name##_default_init  (TypeName##Interface *klass); \
-GType \
-type_name##_get_type (void) \
-{ \
-  static volatile gsize g_define_type_id__volatile = 0; \
-  if (g_once_init_enter (&g_define_type_id__volatile))  \
-    { \
-      GType g_define_type_id = \
-        g_type_register_static_simple (G_TYPE_INTERFACE, \
-                                       g_intern_static_string (#TypeName), \
-                                       sizeof (TypeName##Interface), \
-                                       (GClassInitFunc) type_name##_default_init, \
-                                       0, \
-                                       (GInstanceInitFunc) NULL, \
-                                       (GTypeFlags) 0); \
-      if (TYPE_PREREQ) \
-        g_type_interface_add_prerequisite (g_define_type_id, TYPE_PREREQ); \
- { /* custom code follows */
-#define _G_DEFINE_INTERFACE_EXTENDED_END()        \
-        /* following custom code */             \
- }                                              \
-      g_once_init_leave (&g_define_type_id__volatile, g_define_type_id); \
- }                                               \
-  return g_define_type_id__volatile;                 \
-} /* closes type_name##_get_type() */
-
 static volatile int mtsafe_call_counter = 0; /* multi thread safe call counter */
 static int          unsafe_call_counter = 0; /* single-threaded call counter */
 



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