There are two kinds of types, final type and derivable type. Final type doesn't have any child object. Derivable type has child objects.
The main difference between two objects above is their class. Final type objects doesn't have its own class area. The only member of the class is its parent class.
Derivable object has its own area in the class. The class is open to its descendants.
G_DECLARE_DERIVABLE_TYPE is used to declare derivable type.
It is written in a header file like this:
#define T_TYPE_NUMBER (t_number_get_type ())
G_DECLARE_DERIVABLE_TYPE (TNumber, t_number, T, NUMBER, GObject)Abstract type doesn't have any instance. This type of object is derivable and its children can use functions and signals of the abstract object.
The example of this section is TNumber, TInt and TDouble object. TInt and TDouble have already made in the previous section. They represent integer and floating point respectively. Numbers are more abstract than integer and floating point.
TNumber is an abstract object which represents numbers. TNumber is a parent object of TInt and TDouble. TNumber isn't instantiated because it's abstract type. When an instance is TNumber type, it is an instance of TInt or TDouble as well.
TInt and TDouble have five operations: addition, subtraction, multiplication, division and unary minus operation. Those operations can be defined on TNumber object.
In this section we will define TNumber object and five functions above.
In addition to_s function will be added.
It converts the value of TNumber into a string.
It is like sprintf function.
And we will rewrite TInt and TDouble to implement the functions.
tnumber.h is a header file for TNumber object.
@@@include tnumber/tnumber.h @@@
- 7:
G_DECLARE_DERIVABLE_TYPEmacro. This is similar toG_DECLARE_FINAL_TYPEmacro. The difference is the object is derivable or final.G_DECLARE_DERIVABLE_TYPEis expanded to:- Declaration of
t_number_get_type ()function. This function must be defined intnumber.cfile. The definition is usually done withG_DEFINE_TYPEor its family macros. - Definition of TNumber instance, whose member is its parent only.
- Declaration of TNumberClass. It should be defined later in the header file.
- Convenience macros
T_NUMBER(cast to instance),T_NUMBER_CLASS(cast to class),T_IS_NUMBER(instance check),T_IS_NUMBER_CLASS(class check) andT_NUMBER_GET_CLASSare defined. g_autoptr()support.
- Declaration of
- 9-19: Definition of the structure of TNumberClass.
- 11-16: These are pointers to functions.
They are called class methods or virtual functions.
They are expected to be overridden by a function in the descendant object.
The methods are five arithmetic operators and
to_sfunction.to_sfunction is similar to sprintf function. - 18: A pointer to the default signal handler of "div-by-zero" signal.
The offset of this pointer is given to
g_signal_newas an argument. - 21-40: Functions. They are public.
tnumber.c is below.
@@@include tnumber/tnumber.c @@@
- 5:
G_DEFINE_ABSTRACT_TYPEmacro. This macro is used to define an abstract type object. Abstract type isn't instantiated. This macro is expanded to:- Declaration of
t_number_init ()function. - Declaration of
t_number_class_init ()function. - Definition of
t_number_get_type ()function. - Definition of
t_number_parent_classstatic variable that points the parent class.
- Declaration of
- 3, 7-10, 25-35: Defines division-by-zero signal.
div_by_zero_default_cbis a default handler of "div-by-zero" signal. Default handler doesn't have user data parameter.g_signal_newis used instead ofg_signal_new_class_handler.g_signal_newspecifies a handler as the offset from the top of the class to the pointer to the handler. - 12-36:
t_number_class_init. - 16-21: These class methods are virtual functions. They are expected to be overridden in the descendant object of TNumber. NULL is assigned here so that nothing happens when the methods are called.
- 23: Assigns the address of the function
dev_by_zero_default_cbtoclass->div_by_zero. This is the default handler of "div-by-zero" signal. - 38-40:
t_number_initis a initialization function for an instance. But abstract object isn't instantiated. So, nothing is done in this function. But you can't leave out the definition of this function. - 42-92: Public functions. These functions just call the corresponding class methods if the pointer to the class method is not NULL.
tint.h is a header file of TInt object.
TInt is a child object of TNumber.
@@@include tnumber/tint.h @@@
- 10-16:Declares public functions.
Arithmetic functions and
to_sare declared in TNumber, so TInt doesn't declare those functions. Only instance creation functions are declared.
tint.c implements virtual functions (class methods).
And the pointers of the methods in TNumberClass are rewritten here.
@@@include tnumber/tint.c @@@
- 4-5, 14-32, 124-127: Definition of the property "value". This is the same as before.
- 7-10: Definition of the structure of TInt.
This must be defined before
G_DEFINE_TYPE. - 12:
G_DEFINE_TYPEmacro. This macro expands to:- Declaration of
t_int_init ()function. - Definition of
t_int_get_type ()function. - Definition of
t_int_parent_classstatic variable which points the parent class.
- Declaration of
- 34-36:
t_int_init. - 40-109: These functions are connected to the class method pointers in TIntClass.
They are the implementation of the virtual functions defined in
tnumber.c. - 40-49: Defines a macro used in
t_int_add,t_int_subandt_int_mul. This macro is similar tot_int_divfunction. refer to the explanation below fort_int_div. - 51-70:
t_int_add,t_int_subandt_int_mulfunctions. The macrot_int_binary_opis used. - 72-93:
t_int_div.selfis the object on which the function is called.otheris another TNumber object. It can be TInt or TDouble. IF it is TDouble, its value is casted to int before the division is performed. If the divisor (other) is zero, "div-by-zero" signal is emitted. The signal is defined in TNumber, so TInt doesn't know the signal id. The emission is done withg_signal_emit_by_nameinstead ofg_signal_emit. The return value oft_int_divis TNumber type object However, because TNumber is abstract, the actual type of the object is TInt. - 95-100: A function with unary minus operator.
- 102-109:
to_sfunction. This function converts int to string. For example, if the value of the object is 123, then the result is a string "123". The caller should free the string if it becomes useless. - 111- 128:
t_int_class_init. - 117-122: The class methods are overridden.
For example, if
t_number_addis called on a TInt object, then the function calls the class method*tnumber_class->add. The pointer pointst_int_addfunction. Therefore,t_int_addis called finally. - 130-144: Instance creation functions are the same as before.
TDouble object is defined with tdouble.h and tdouble.c.
The definition is very similar to TInt.
So, this subsection just shows the contents of the files.
tdouble.h
@@@include tnumber/tdouble.h @@@
tdouble.c
@@@include tnumber/tdouble.c @@@
main.c is a simple program to test the objects.
@@@include tnumber/main.c @@@
- 6-20: "notify" handler. This handler is upgraded to support both TInt and TDouble.
- 22-71: A function
main. - 30-31: Connects the notify signals on
i(TInt) andd(TDouble). - 33-34: Set "value" properties on
iandd. - 36: Add
dtoi. The answer is TInt object. - 47: Add
itod. The answer is TDouble object. The addition of two TNumber objects isn't commutative because the type of the result will be different if the two objects are exchanged. - 56-63: Tests division by zero signal.
meson.buld controls the compilation process.
@@@include tnumber/meson.build @@@
Compilation and execution is done by the usual way.
$ meson _build
$ ninja -C _build
$ _build/tnumber
Then the following is shown on the display.
@@@shell cd tnumber; _build/tnumber @@@
The two answers are different because of the different types.
This section has shown a simple example of derivable and abstract class.
You can define your derivable object like this.
If your object isn't abstract, use G_DEFINE_TYPE instead of G_DEFINE_ABSTRACT_TYPE.
And you need one more thing, how to manage private data in your derivable object.
There is a tutorial in GObject Reference Manual.
See the tutorial for learning derivable object.
It is also good to see source files in Gtk. The stable version is now Gtk4 but most of the distributions still use Gtk3. Both of them are useful to learn how to write a derivable object.
- GObjectClass has been initialized before the function
mainstarts. - First call for
g_object_new (T_TYPE_INT, ...)org_object_new (T_TYPE_DOUBLE, ...)initializes TNumberClass. Because TNumber is an abstract object, it cannot be instantiated. Instead, its child object, TInt or TDouble, can be instantiated. And when a child is instantiated for the first time, TNumberClass is initialized before the child is initialized. And the initialization process is as follows. - Memory is allocated for TNumberClass.
- The parent (GObjectClass) part of the class is copied from GObjectClass.
t_number_class_initis called to initialize TNumberClass. This includes initializing pointers to class method handlers and a default signal handler.
The diagram below shows the process.
- GNumberClass has been initialized before the initialization of GIntClass starts.
- First call for
g_object_new (T_TYPE_INT, ...)initializes TIntClass. And the initialization process is as follows. - Memory is allocated for TIntClass. TIntClass doesn't have its own area. Therefore its structure is the same as its parent class (TNumberClass).
- The parent (TNumberClass) part of the class (This is the same as whole TIntClass) is copied from TNumberClass.
t_int_class_initis called to initialize TIntClass. This includes overriding class methods, set_property and get_property.
The diagram below shows the process.

