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
This commit is contained in:
Dan Winship 2009-12-01 10:33:12 +01:00 committed by Alexander Larsson
parent 74a970f754
commit 91d96350a7
3 changed files with 70 additions and 32 deletions

View File

@ -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

View File

@ -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.
*
@ -1344,6 +1344,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
@ -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);

View File

@ -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 */