This time GObject refers to the type named GObject in the GObject library. Don't be confused.
The type GObject is supposed to be the base class of other user-defined classes.
- GObject has reference counting and deallocate the instance when reference count reaches zero.
- GObject allows users to add properties to their subclasses.
- GObject also has "signal" support for asynchronized event handling which is roughly analog to "event" in C#.
Here is a subclass of GObject. The reference counting feature is demonstrated.
/* A subclass of GObject. */
#include <stdio.h>
#include <glib-object.h>
typedef struct {
GObject something_as_boilerplate;
int an_instance_member;
} myinstance_t;
typedef struct {
GObjectClass something_as_boilerplate;
} myclass_t;
void my_instance_init_func(myinstance_t *instance, gpointer data) {
instance->an_instance_member = 42;
}
void my_class_init_func(myclass_t* klass, gpointer data) {
}
GType get_my_typeid() {
static my_type_id = 0;
if(my_type_id==0) {
GTypeInfo my_type_info = {
sizeof(myclass_t), //class_size;
NULL, //base_init;
NULL, //base_finalize;
/* classed types, instantiated types */
(GClassInitFunc)my_class_init_func, //class_init;
NULL, //class_finalize;
NULL, //class_data;
/* instantiated types */
sizeof(myinstance_t),//instance_size;
0, //n_preallocs;
(GInstanceInitFunc)my_instance_init_func, //instance_init;
/* value handling */
NULL, //value_table;
};
my_type_id = g_type_register_static(
G_TYPE_OBJECT,
"MyClass",
&my_type_info,
0
);
}
return my_type_id;
}
void weak_ref_notifying_func(gpointer data, GObject* where_it_was) {
printf("My object has died.\n");
}
int main() {
g_type_init();
printf("Type id: %d\n",get_my_typeid());
printf("Type name: %s\n",g_type_name(get_my_typeid()));
myinstance_t *instance = (myinstance_t*)g_object_new(get_my_typeid(),NULL);
printf("Member: %d\n",instance->an_instance_member);
printf("Is instance of GObject? %s\n",
G_TYPE_CHECK_INSTANCE_TYPE(instance, G_TYPE_OBJECT)?"yes":"no");
g_object_weak_ref(
G_TYPE_CHECK_INSTANCE_CAST(instance,G_TYPE_OBJECT,GObject),
(GWeakNotify)weak_ref_notifying_func, NULL);
g_object_ref(instance);
printf("Member: %d\n",instance->an_instance_member); // 2 refs, valid.
g_object_unref(instance);
printf("Member: %d\n",instance->an_instance_member); // 1 ref, still valid.
g_object_unref(instance);
printf("Member: %d\n",instance->an_instance_member); // No longer valid.
return 0;
}
Since it is a subclass of GObject, the class struct and the instance struct begin with GObjectClass and GObject, respectively. This type is not registered as a fundamental type, either.
You should create instances of GObject (as well as its subclasses) using g_object_new function. You may also pass additional "properties" to be set during initialization. This will be discussed later.
You may use g_object_ref and g_object_unref to increase or decrease the reference count, respectively.
A weak reference is a reference that refers to an object while not counted in its reference count. If there is a strong reference to an object, this object cannot be deallocated, which is not expected in some situation. A weak reference will ge notified when the (strong) reference count of an object reaches zero and is deallocated so that the weak reference can respond to such an event.
分享到:
相关推荐
利用本软件,c++开发者可以在不依赖c++编译器之外的任何工具的前提下,实现c++中类与类之间的解耦合(class A对象与class B对象之间的解耦合)。 本软件支持c++11并向后兼容(支持c++11及其后续版本)。 开发环境为...
GObject是GTK+库的核心组件,它为C语言提供了一种面向对象的编程模型。在GTK+和Gnome开发中,GObject系统是构建复杂、可扩展软件的基础。这个离线API手册包含了GObject框架的详细信息,对于理解并利用GObject进行...
gobject-query - display a tree of types IV. Tutorial How To define and implement a new GObject? Boilerplate header code Boilerplate code Object Construction Object Destruction Object methods ...
UltiSnips C 片段有助于编写 GObject 代码 安装 这些片段使用 UltiSnips ( ) 所以你应该先安装和配置它。 我使用 Vundle ( ) 来管理 vim 插件,但这不是强制性的。 如果你使用 Vundle,你可以添加到你的 ~/.vimrc ...
《GObject参考手册》是针对GLib库中的核心组件GObject进行详细解释的重要参考资料,尤其对深入理解并熟练使用GObject系统至关重要。GLib是GNOME桌面环境的基础库,广泛应用于各种C语言开发的跨平台项目。GObject是...
crystal-gobject:Crystal的gobject-introspection
Gobject 资料,全面介绍Gobject 资料,全面介绍Gobject 资料,全面介绍Gobject 资料,全面介绍Gobject 资料,全面介绍Gobject 资料,全面介绍Gobject 资料,全面介绍
Groonga GObject 描述 Groonga的GObject包装器。 去做... 安装 Debian GNU / Linux 。 运行以下命令: % sudo apt-get install -V -y gir1.2-groonga libgroonga-gobject0 的Ubuntu 。 运行以下命令: % ...
在IT领域,特别是软件开发中,GObject是一个关键的概念,它是GTK+库中的基础对象模型。GObject系统为C语言提供了面向对象编程的支持,而这个"gobject_setup_property_demo.zip"压缩包显然提供了一个关于如何使用...
本示例用于演示GObject创建新类,类的继承与重载。代码实现了shape和square类,继承关系为: square -> shape ->gobject 其中,square重载了shape类的info接口。
gtkforphp gobject扩展为GObject功能提供了语言绑定。 有关该库的文档和信息可以在找到 现在,这只不过是一个占位符。 您可以发送评论,补丁,问题 这仍然是实验性的扩展。 安装/配置 此扩展需要gtkforphp / glib...
离线安装包,亲测可用
码-对象反省码插件,用于基于GObject-Intropection构建库文档。要求Ruby / GObject-Introspection尝试一下 git clone ...
离线安装包,亲测可用
液化天然气LGI是基于gobject内省的动态Lua绑定到基于GObject的库。 它允许直接从Lua使用基于GObject的库。 已获得许可的许可,请参见LICENSE文件全文。 该项目的主页位于。 LGI经过测试,并与标准Lua 5.1,Lua 5.2,...
**Python3-pygobject3: GObject库的Python 3绑定** `python3-pygobject3` 是一个Python 3版本的绑定库,它允许Python程序员能够利用GObject库的功能,这是GNOME桌面环境的核心组件之一。GObject库是C语言编写的一个...
gir_ffi, 在运行时使用 FFI,为基于GObject的库自动生成绑定 GirFFI由 Matijs van Zuijlen描述使用GObject内省存储库的GNOME的ruby 绑定。状态 特性为基于任何gobject的库创建绑定。在运行时生成绑定。为选定方法...
### gobject对象系统详解 #### 一、简介 GObject是一种强大的对象系统,它为C语言提供了一套灵活且易于扩展的对象模型。虽然C语言本身不是面向对象的语言,但通过GObject,开发者能够构建出类似面向对象编程的功能...
离线安装包,亲测可用
avahi-gobject-0.6.25-11.el6.i686.rpm是centos工具包。