Recall there are 3 types in GObject type system: Fundamental, static and dynamic. A fundamental type is a top-most type which has no parent type. Most of them are pre-defined. Static types never load/unload its class type (say, their class struct) at runtime, since they are static. On the contrary, dynamic types can be dynamically loaded/unloaded at runtime. They are normally used within a module.
We can call g_type_register_dynamic() to register a dynamic type. When used in a module of GObject library (may be a GTypeModule type), We can also call g_type_module_register_type() to create your dynamic types. g_type_register_dynamic() is invoked for you in that function. Let’s go through the code:
The implementation structure may be a little different with the stuff when creating a static type. An additional parameter GTypeModule is passed in. It represents the module your dynamic type belongs to. So, when the module is unloaded, all dynamic types in it are unaccessible.
Also note the bar_type_class_finalize() function. We use it to override the finalize() virtual function in GObjectClass. Now you can do un-initialiation in this function. It is like the destructor in a C++ class.
Let’s move on to the module type. This type inherits GTypeModule:
C
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// fakemodule.h
#ifndef FAKE_MODULE_H_
#define FAKE_MODULE_H_
#include <glib-object.h>
/* module object struct */
typedefstruct_FakeModule{
GTypeModule parent;
}FakeModule;
/* module class struct */
typedefstruct_FakeModuleClass{
GTypeModuleClass parent;
}FakeModuleClass;
/* type method */
GType fake_module_get_type();
#endif /* FAKE_MODULE_H_ */
C
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// fakemodule.c
#include "fakemodule.h"
/*
* If you implement a real shared library module, you
* can init module variables, assign virtual function here.
*/
gboolean fake_module_load(GTypeModule*module){
g_print("Invoking fake_module_load()\n");
/* successfully loaded */
returnTRUE;
}
/*
* If you implement a real shared library module, you
* can uninit module variables, and make all cleanups here.