From dc1999316da0409c5bc15e37a0a568a8309bb5a3 Mon Sep 17 00:00:00 2001 From: Christian Persch Date: Sat, 24 May 2008 16:08:04 +0200 Subject: [PATCH] Add G_DEFINE_{BOXED,POINTER}_TYPE[_WITH_CODE] Add convenience type definition macros for boxed and pointer types similar to G_DEFINE_TYPE for object types. Bug #449565. --- docs/reference/gobject/gobject-sections.txt | 4 + gobject/gtype.h | 110 ++++++++++++++++++++++++++++ 2 files changed, 114 insertions(+) diff --git a/docs/reference/gobject/gobject-sections.txt b/docs/reference/gobject/gobject-sections.txt index d1f4a05..1135bd1 100644 --- a/docs/reference/gobject/gobject-sections.txt +++ b/docs/reference/gobject/gobject-sections.txt @@ -107,6 +107,10 @@ G_DEFINE_INTERFACE G_DEFINE_INTERFACE_WITH_CODE G_IMPLEMENT_INTERFACE G_DEFINE_TYPE_EXTENDED +G_DEFINE_BOXED_TYPE +G_DEFINE_BOXED_TYPE_WITH_CODE +G_DEFINE_POINTER_TYPE +G_DEFINE_POINTER_TYPE_WITH_CODE G_TYPE_FUNDAMENTAL_SHIFT diff --git a/gobject/gtype.h b/gobject/gtype.h index 253d68e..24970bc 100644 --- a/gobject/gtype.h +++ b/gobject/gtype.h @@ -1505,6 +1505,116 @@ type_name##_get_type (void) \ return g_define_type_id__volatile; \ } /* closes type_name##_get_type() */ +/** + * G_DEFINE_BOXED_TYPE: + * @TypeName: The name of the new type, in Camel case. + * @type_name: The name of the new type, in lowercase, with words + * separated by '_'. + * @copy_func: the #GBoxedCopyFunc for the new type + * @free_func: the #GBoxedFreeFunc for the new type + * + * A convenience macro for boxed type implementations, which defines a + * type_name_get_type() function registering the boxed type. + * + * Since: 2.26 + */ +#define G_DEFINE_BOXED_TYPE(TypeName, type_name, copy_func, free_func) G_DEFINE_BOXED_TYPE_WITH_CODE (TypeName, type_name, copy_func, free_func, {}) +/** + * G_DEFINE_BOXED_TYPE_WITH_CODE: + * @TypeName: The name of the new type, in Camel case. + * @type_name: The name of the new type, in lowercase, with words + * separated by '_'. + * @copy_func: the #GBoxedCopyFunc for the new type + * @free_func: the #GBoxedFreeFunc for the new type + * @_C_: Custom code that gets inserted in the *_get_type() function. + * + * A convenience macro for boxed type implementations. + * Similar to G_DEFINE_BOXED_TYPE(), but allows to insert custom code into the + * type_name_get_type() function, e.g. to register value transformations with + * g_value_register_transform_func(). + * + * Since: 2.26 + */ +#define G_DEFINE_BOXED_TYPE_WITH_CODE(TypeName, type_name, copy_func, free_func, _C_) _G_DEFINE_BOXED_TYPE_BEGIN (TypeName, type_name, copy_func, free_func) {_C_;} _G_DEFINE_TYPE_EXTENDED_END() + +#if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 7) +#define _G_DEFINE_BOXED_TYPE_BEGIN(TypeName, type_name, copy_func, free_func) \ +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_register_boxed) \ + (const gchar *, \ + union \ + { \ + TypeName * (*do_copy_type) (TypeName *); \ + TypeName * (*do_const_copy_type) (const TypeName *); \ + GBoxedCopyFunc do_copy_boxed; \ + } __attribute__((__transparent_union__)), \ + union \ + { \ + void (* do_free_type) (TypeName *); \ + GBoxedFreeFunc do_free_boxed; \ + } __attribute__((__transparent_union__)) \ + ) = g_boxed_type_register_static; \ + GType g_define_type_id = \ + _g_register_boxed (g_intern_static_string (#TypeName), copy_func, free_func); \ + { /* custom code follows */ +#else +#define _G_DEFINE_BOXED_TYPE_BEGIN(TypeName, type_name, copy_func, free_func) \ +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_boxed_type_register_static (g_intern_static_string (#TypeName), \ + (GBoxedCopyFunc) copy_func, \ + (GBoxedFreeFunc) free_func); \ + { /* custom code follows */ +#endif /* __GNUC__ */ + +/** + * G_DEFINE_POINTER_TYPE: + * @TypeName: The name of the new type, in Camel case. + * @type_name: The name of the new type, in lowercase, with words + * separated by '_'. + * + * A convenience macro for pointer type implementations, which defines a + * type_name_get_type() function registering the pointer type. + * + * Since: 2.26 + */ +#define G_DEFINE_POINTER_TYPE(TypeName, type_name) G_DEFINE_POINTER_TYPE_WITH_CODE (TypeName, type_name, {}) +/** + * G_DEFINE_POINTER_TYPE_WITH_CODE: + * @TypeName: The name of the new type, in Camel case. + * @type_name: The name of the new type, in lowercase, with words + * separated by '_'. + * @_C_: Custom code that gets inserted in the *_get_type() function. + * + * A convenience macro for pointer type implementations. + * Similar to G_DEFINE_POINTER_TYPE(), but allows to insert custom code into the + * type_name_get_type() function. + * + * Since: 2.26 + */ +#define G_DEFINE_POINTER_TYPE_WITH_CODE(TypeName, type_name, _C_) _G_DEFINE_POINTER_TYPE_BEGIN (TypeName, type_name) {_C_;} _G_DEFINE_TYPE_EXTENDED_END() + +#define _G_DEFINE_POINTER_TYPE_BEGIN(TypeName, type_name) \ +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_pointer_type_register_static (g_intern_static_string (#TypeName)); \ + { /* custom code follows */ + /* --- protected (for fundamental type implementations) --- */ GTypePlugin* g_type_get_plugin (GType type); GTypePlugin* g_type_interface_get_plugin (GType instance_type, -- 2.7.4