GObject 06: A subclass of GObject

本文介绍了GObject库的基础类GObject的特性,包括引用计数、属性添加和信号支持等内容,并通过一个子类实例展示了引用计数的具体应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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.
[list]
[*] 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#.
[/list]


Here is a subclass of GObject. The reference counting feature is demonstrated.
[code]/* 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;
}
[/code]

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.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值