1 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
4 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
5 <title>The GLib Dynamic Type System</title>
6 <meta name="generator" content="DocBook XSL Stylesheets V1.75.2">
7 <link rel="home" href="index.html" title="GObject Reference Manual">
8 <link rel="up" href="pt01.html" title="Part I. Concepts">
9 <link rel="prev" href="ch01s02.html" title="Exporting a C API">
10 <link rel="next" href="gtype-conventions.html" title="Conventions">
11 <meta name="generator" content="GTK-Doc V1.13 (XML mode)">
12 <link rel="stylesheet" href="style.css" type="text/css">
13 <link rel="preface" href="pr01.html" title="Introduction">
14 <link rel="part" href="pt01.html" title="Part I. Concepts">
15 <link rel="chapter" href="chapter-intro.html" title="Background">
16 <link rel="chapter" href="chapter-gtype.html" title="The GLib Dynamic Type System">
17 <link rel="chapter" href="chapter-gobject.html" title="The GObject base class">
18 <link rel="chapter" href="chapter-signal.html" title="The GObject messaging system">
19 <link rel="reference" href="rn01.html" title="API Reference">
20 <link rel="reference" href="rn02.html" title="Tools Reference">
21 <link rel="part" href="pt02.html" title="Part IV. Tutorial">
22 <link rel="chapter" href="howto-gobject.html" title="How to define and implement a new GObject">
23 <link rel="chapter" href="howto-interface.html" title="How to define and implement interfaces">
24 <link rel="chapter" href="howto-signals.html" title="How to create and use signals">
25 <link rel="part" href="pt03.html" title="Part V. Related Tools">
26 <link rel="chapter" href="tools-vala.html" title="Vala">
27 <link rel="chapter" href="tools-gob.html" title="GObject builder">
28 <link rel="chapter" href="tools-ginspector.html" title="Graphical inspection of GObjects">
29 <link rel="chapter" href="tools-refdb.html" title="Debugging reference count problems">
30 <link rel="chapter" href="tools-gtkdoc.html" title="Writing API docs">
31 <link rel="index" href="api-index-full.html" title="Index">
32 <link rel="index" href="api-index-deprecated.html" title="Index of deprecated symbols">
33 <link rel="index" href="api-index-2-2.html" title="Index of new symbols in 2.2">
34 <link rel="index" href="api-index-2-4.html" title="Index of new symbols in 2.4">
35 <link rel="index" href="api-index-2-6.html" title="Index of new symbols in 2.6">
36 <link rel="index" href="api-index-2-8.html" title="Index of new symbols in 2.8">
37 <link rel="index" href="api-index-2-10.html" title="Index of new symbols in 2.10">
38 <link rel="index" href="api-index-2-12.html" title="Index of new symbols in 2.12">
39 <link rel="index" href="api-index-2-14.html" title="Index of new symbols in 2.14">
40 <link rel="index" href="api-index-2-18.html" title="Index of new symbols in 2.18">
41 <link rel="index" href="api-index-2-22.html" title="Index of new symbols in 2.22">
42 <link rel="index" href="api-index-2-24.html" title="Index of new symbols in 2.24">
44 <body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
45 <table class="navigation" id="top" width="100%" summary="Navigation header" cellpadding="2" cellspacing="2"><tr valign="middle">
46 <td><a accesskey="p" href="ch01s02.html"><img src="left.png" width="24" height="24" border="0" alt="Prev"></a></td>
47 <td><a accesskey="u" href="pt01.html"><img src="up.png" width="24" height="24" border="0" alt="Up"></a></td>
48 <td><a accesskey="h" href="index.html"><img src="home.png" width="24" height="24" border="0" alt="Home"></a></td>
49 <th width="100%" align="center">GObject Reference Manual</th>
50 <td><a accesskey="n" href="gtype-conventions.html"><img src="right.png" width="24" height="24" border="0" alt="Next"></a></td>
52 <div class="chapter" title="The GLib Dynamic Type System">
53 <div class="titlepage"><div><div><h2 class="title">
54 <a name="chapter-gtype"></a>The GLib Dynamic Type System</h2></div></div></div>
56 <dt><span class="sect1"><a href="chapter-gtype.html#gtype-copy">Copy functions</a></span></dt>
57 <dt><span class="sect1"><a href="gtype-conventions.html">Conventions</a></span></dt>
58 <dt><span class="sect1"><a href="gtype-non-instantiable.html">Non-instantiable non-classed fundamental types</a></span></dt>
59 <dt><span class="sect1"><a href="gtype-instantiable-classed.html">Instantiable classed types: objects</a></span></dt>
60 <dd><dl><dt><span class="sect2"><a href="gtype-instantiable-classed.html#gtype-instantiable-classed-init-done">Initialization and Destruction</a></span></dt></dl></dd>
61 <dt><span class="sect1"><a href="gtype-non-instantiable-classed.html">Non-instantiable classed types: interfaces</a></span></dt>
63 <dt><span class="sect2"><a href="gtype-non-instantiable-classed.html#gtype-non-instantiable-classed-init">Interface Initialization</a></span></dt>
64 <dt><span class="sect2"><a href="gtype-non-instantiable-classed.html#gtype-non-instantiable-classed-dest">Interface Destruction</a></span></dt>
68 A type, as manipulated by the GLib type system, is much more generic than what
69 is usually understood as an Object type. It is best explained by looking at the
70 structure and the functions used to register new types in the type system.
72 <pre class="programlisting">
73 typedef struct _GTypeInfo GTypeInfo;
76 /* interface types, classed types, instantiated types */
79 GBaseInitFunc base_init;
80 GBaseFinalizeFunc base_finalize;
82 /* classed types, instantiated types */
83 GClassInitFunc class_init;
84 GClassFinalizeFunc class_finalize;
85 gconstpointer class_data;
87 /* instantiated types */
88 guint16 instance_size;
90 GInstanceInitFunc instance_init;
93 const GTypeValueTable *value_table;
95 GType g_type_register_static (GType parent_type,
96 const gchar *type_name,
97 const GTypeInfo *info,
99 GType g_type_register_fundamental (GType type_id,
100 const gchar *type_name,
101 const GTypeInfo *info,
102 const GTypeFundamentalInfo *finfo,
108 <code class="function"><a class="link" href="gobject-Type-Information.html#g-type-register-static" title="g_type_register_static ()">g_type_register_static</a></code> and
109 <code class="function"><a class="link" href="gobject-Type-Information.html#g-type-register-fundamental" title="g_type_register_fundamental ()">g_type_register_fundamental</a></code>
110 are the C functions, defined in
111 <code class="filename">gtype.h</code> and implemented in <code class="filename">gtype.c</code>
112 which you should use to register a new <span class="type"><a class="link" href="gobject-Type-Information.html#GType" title="GType">GType</a></span> in the program's type system.
113 It is not likely you will ever need to use
114 <code class="function"><a class="link" href="gobject-Type-Information.html#g-type-register-fundamental" title="g_type_register_fundamental ()">g_type_register_fundamental</a></code> (you have to be Tim Janik
115 to do that) but in case you want to, the last chapter explains how to create
116 new fundamental types.
117 <sup>[<a name="id587107" href="#ftn.id587107" class="footnote">2</a>]</sup>
120 Fundamental types are top-level types which do not derive from any other type
121 while other non-fundamental types derive from other types.
122 Upon initialization by <code class="function"><a class="link" href="gobject-Type-Information.html#g-type-init" title="g_type_init ()">g_type_init</a></code>, the type system not
123 only initializes its internal data structures but it also registers a number of core
124 types: some of these are fundamental types. Others are types derived from these
128 Fundamental and non-fundamental types are defined by:
130 <div class="itemizedlist"><ul class="itemizedlist" type="disc">
131 <li class="listitem"><p>
132 class size: the class_size field in <span class="type"><a class="link" href="gobject-Type-Information.html#GTypeInfo" title="GTypeInfo">GTypeInfo</a></span>.
134 <li class="listitem"><p>
135 class initialization functions (C++ constructor): the base_init and
136 class_init fields in <span class="type"><a class="link" href="gobject-Type-Information.html#GTypeInfo" title="GTypeInfo">GTypeInfo</a></span>.
138 <li class="listitem"><p>
139 class destruction functions (C++ destructor): the base_finalize and
140 class_finalize fields in <span class="type"><a class="link" href="gobject-Type-Information.html#GTypeInfo" title="GTypeInfo">GTypeInfo</a></span>.
142 <li class="listitem"><p>
143 instance size (C++ parameter to new): the instance_size field in
144 <span class="type"><a class="link" href="gobject-Type-Information.html#GTypeInfo" title="GTypeInfo">GTypeInfo</a></span>.
146 <li class="listitem"><p>
147 instantiation policy (C++ type of new operator): the n_preallocs
148 field in <span class="type"><a class="link" href="gobject-Type-Information.html#GTypeInfo" title="GTypeInfo">GTypeInfo</a></span>.
150 <li class="listitem"><p>
151 copy functions (C++ copy operators): the value_table field in
152 <span class="type"><a class="link" href="gobject-Type-Information.html#GTypeInfo" title="GTypeInfo">GTypeInfo</a></span>.
154 <li class="listitem"><p>
155 type characteristic flags: <span class="type"><a class="link" href="gobject-Type-Information.html#GTypeFlags" title="enum GTypeFlags">GTypeFlags</a></span>.
159 Fundamental types are also defined by a set of <span class="type"><a class="link" href="gobject-Type-Information.html#GTypeFundamentalFlags" title="enum GTypeFundamentalFlags">GTypeFundamentalFlags</a></span>
160 which are stored in a <span class="type"><a class="link" href="gobject-Type-Information.html#GTypeFundamentalInfo" title="GTypeFundamentalInfo">GTypeFundamentalInfo</a></span>.
161 Non-fundamental types are furthermore defined by the type of their parent which is
162 passed as the parent_type parameter to <code class="function"><a class="link" href="gobject-Type-Information.html#g-type-register-static" title="g_type_register_static ()">g_type_register_static</a></code>
163 and <code class="function"><a class="link" href="gobject-Type-Information.html#g-type-register-dynamic" title="g_type_register_dynamic ()">g_type_register_dynamic</a></code>.
165 <div class="sect1" title="Copy functions">
166 <div class="titlepage"><div><div><h2 class="title" style="clear: both">
167 <a name="gtype-copy"></a>Copy functions</h2></div></div></div>
169 The major common point between <span class="emphasis"><em>all</em></span> GLib types (fundamental and
170 non-fundamental, classed and non-classed, instantiable and non-instantiable) is that
171 they can all be manipulated through a single API to copy/assign them.
174 The <span class="type"><a class="link" href="gobject-Generic-values.html#GValue" title="GValue">GValue</a></span> structure is used as an abstract container for all of these
175 types. Its simplistic API (defined in <code class="filename">gobject/gvalue.h</code>) can be
176 used to invoke the value_table functions registered
177 during type registration: for example <code class="function"><a class="link" href="gobject-Generic-values.html#g-value-copy" title="g_value_copy ()">g_value_copy</a></code> copies the
178 content of a <span class="type"><a class="link" href="gobject-Generic-values.html#GValue" title="GValue">GValue</a></span> to another <span class="type"><a class="link" href="gobject-Generic-values.html#GValue" title="GValue">GValue</a></span>. This is similar
179 to a C++ assignment which invokes the C++ copy operator to modify the default
180 bit-by-bit copy semantics of C++/C structures/classes.
183 The following code shows how you can copy around a 64 bit integer, as well as a <span class="type"><a class="link" href="gobject-The-Base-Object-Type.html#GObject">GObject</a></span>
184 instance pointer (sample code for this is located in the source tarball for this document in
185 <code class="filename">sample/gtype/test.c</code>):
187 <pre class="programlisting">
188 static void test_int (void)
190 GValue a_value = {0, };
191 GValue b_value = {0, };
196 g_value_init (&a_value, G_TYPE_UINT64);
197 g_value_set_uint64 (&a_value, a);
199 g_value_init (&b_value, G_TYPE_UINT64);
200 g_value_copy (&a_value, &b_value);
202 b = g_value_get_uint64 (&b_value);
205 g_print ("Yay !! 10 lines of code to copy around a uint64.\n");
207 g_print ("Are you sure this is not a Z80 ?\n");
211 static void test_object (void)
214 GValue obj_vala = {0, };
215 GValue obj_valb = {0, };
216 obj = g_object_new (MAMAN_TYPE_BAR, NULL);
218 g_value_init (&obj_vala, MAMAN_TYPE_BAR);
219 g_value_set_object (&obj_vala, obj);
221 g_value_init (&obj_valb, G_TYPE_OBJECT);
223 /* g_value_copy's semantics for G_TYPE_OBJECT types is to copy the reference.
224 This function thus calls g_object_ref.
225 It is interesting to note that the assignment works here because
226 MAMAN_TYPE_BAR is a G_TYPE_OBJECT.
228 g_value_copy (&obj_vala, &obj_valb);
230 g_object_unref (G_OBJECT (obj));
231 g_object_unref (G_OBJECT (obj));
235 The important point about the above code is that the exact semantics of the copy calls
236 is undefined since they depend on the implementation of the copy function. Certain
237 copy functions might decide to allocate a new chunk of memory and then to copy the
238 data from the source to the destination. Others might want to simply increment
239 the reference count of the instance and copy the reference to the new GValue.
242 The value_table used to specify these assignment functions is defined in
243 <code class="filename">gtype.h</code> and is thoroughly described in the
244 API documentation provided with GObject (for once ;-) which is why we will
245 not detail its exact semantics.
247 <pre class="programlisting">
248 typedef struct _GTypeValueTable GTypeValueTable;
249 struct _GTypeValueTable
251 void (*value_init) (GValue *value);
252 void (*value_free) (GValue *value);
253 void (*value_copy) (const GValue *src_value,
255 /* varargs functionality (optional) */
256 gpointer (*value_peek_pointer) (const GValue *value);
257 gchar *collect_format;
258 gchar* (*collect_value) (GValue *value,
259 guint n_collect_values,
260 GTypeCValue *collect_values,
261 guint collect_flags);
263 gchar* (*lcopy_value) (const GValue *value,
264 guint n_collect_values,
265 GTypeCValue *collect_values,
266 guint collect_flags);
270 Interestingly, it is also very unlikely
271 you will ever need to specify a value_table during type registration
272 because these value_tables are inherited from the parent types for
273 non-fundamental types which means that unless you want to write a
274 fundamental type (not a great idea!), you will not need to provide
275 a new value_table since you will inherit the value_table structure
276 from your parent type.
279 <div class="footnotes">
280 <br><hr width="100" align="left">
281 <div class="footnote"><p><sup>[<a name="ftn.id587107" href="#id587107" class="para">2</a>] </sup>
282 Please note that there exists another registration function: the
283 <code class="function"><a class="link" href="gobject-Type-Information.html#g-type-register-dynamic" title="g_type_register_dynamic ()">g_type_register_dynamic</a></code>. We will not discuss this
284 function here since its use is very similar to the <code class="function">_static</code>
291 Generated by GTK-Doc V1.13</div>