diff options
author | Dan Winship <danw@gnome.org> | 2009-12-01 10:33:12 +0100 |
---|---|---|
committer | Alexander Larsson <alexl@redhat.com> | 2009-12-01 10:44:42 +0100 |
commit | 91d96350a7eed2e2acfc0c254b6dfc4e6fe81a8b (patch) | |
tree | 9a6460872f52820beaa4cbaa9bc7878ea7f5c2a8 /gobject/gtype.h | |
parent | 74a970f754bbffcc9f0a3fa6dd9977a8640544c8 (diff) | |
download | glib-91d96350a7eed2e2acfc0c254b6dfc4e6fe81a8b.tar.gz |
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
Diffstat (limited to 'gobject/gtype.h')
-rw-r--r-- | gobject/gtype.h | 70 |
1 files changed, 68 insertions, 2 deletions
diff --git a/gobject/gtype.h b/gobject/gtype.h index 860b51821..c5f78983f 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); |