GObject 初始化可失败类的派生类模板

此模板派生于 GObject 类,并实现了 GInitableIface 接口,其函数调用序列如下

** (process:1755): DEBUG: hev_iobj_new
** (process:1755): DEBUG: hev_iobj_class_init // 只有首次实例化被调用
** (process:1755): DEBUG: hev_iobj_initable_iface_init // 只有首次实例化被调用
** (process:1755): DEBUG: hev_iobj_constructor
** (process:1755): DEBUG: hev_iobj_init
** (process:1755): DEBUG: hev_iobj_constructed
** (process:1755): DEBUG: hev_iobj_initable_init
** (process:1755): DEBUG: hev_iobj_dispose
** (process:1755): DEBUG: hev_iobj_finalize

无论其初始化是否成功,其函数调用都是如上,当初始化失败(hev_iobj_initable_init 函数返回 FALSE),hev_iobj_new 函数返回 NULL,并设置参数 error。调用者通过其 error 参数判断错误类型。

源代码下载

编译

gcc -o hev-test hev-test.c hev-iobj.c `pkg-config --cflags --libs gio-2.0`

Over!

Leave a Reply

Your email address will not be published. Required fields are marked *