函数宏
GObjectDEFINE_BOXED_TYPE
自:2.26
声明 [源代码]
#define G_DEFINE_BOXED_TYPE (
TypeName,
type_name,
copy_func,
free_func
)
描述 [源代码]
用于定义新自定义boxed类型的便捷宏。
使用此宏是定义新自定义boxed类型的推荐方式,比直接调用g_boxed_type_register_static()
更优。它定义了一个type_name_get_type()
函数,该函数将返回新定义的GType
,以实现延迟实例化。
您可以首先按照以下方式在头文件中放入声明
#define MY_TYPE_STRUCT my_struct_get_type ()
GType my_struct_get_type (void) G_GNUC_CONST;
MyStruct * my_struct_new (void);
void my_struct_free (MyStruct *self);
MyStruct * my_struct_copy (MyStruct *self);
然后使用此宏,并在源文件中按以下方式定义实现
MyStruct *
my_struct_new (void)
{
// ... your code to allocate a new MyStruct ...
}
void
my_struct_free (MyStruct *self)
{
// ... your code to free a MyStruct ...
}
MyStruct *
my_struct_copy (MyStruct *self)
{
// ... your code return a newly allocated copy of a MyStruct ...
}
G_DEFINE_BOXED_TYPE (MyStruct, my_struct, my_struct_copy, my_struct_free)
void
foo ()
{
MyStruct *ms;
ms = my_struct_new ();
// ... your code ...
my_struct_free (ms);
}
自:2.26
此函数对于语言绑定不可直接访问。