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.