KGRKJGETMRETU895U-589TY5MIGM5JGB5SDFESFREWTGR54TY
Server : Apache/2.2.17 (Unix) mod_ssl/2.2.17 OpenSSL/0.9.8e-fips-rhel5 DAV/2 PHP/5.2.17
System : Linux localhost 2.6.18-419.el5 #1 SMP Fri Feb 24 22:47:42 UTC 2017 x86_64
User : nobody ( 99)
PHP Version : 5.2.17
Disable Function : NONE
Directory :  /usr/share/gtk-doc/html/gobject/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Current File : //usr/share/gtk-doc/html/gobject/ch02.html
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
<title>The Glib Dynamic Type System</title>
<meta name="generator" content="DocBook XSL Stylesheets V1.69.1">
<link rel="start" href="index.html" title="GObject Reference Manual">
<link rel="up" href="pt01.html" title="Part&#160;I.&#160;Concepts">
<link rel="prev" href="ch01s02.html" title="Exporting a C API">
<link rel="next" href="gtype-conventions.html" title="Conventions">
<meta name="generator" content="GTK-Doc V1.6 (XML mode)">
<link rel="stylesheet" href="style.css" type="text/css">
<link rel="preface" href="pr01.html" title="Introduction">
<link rel="part" href="pt01.html" title="Part&#160;I.&#160;Concepts">
<link rel="chapter" href="ch01.html" title="Background">
<link rel="chapter" href="ch02.html" title="The Glib Dynamic Type System">
<link rel="chapter" href="chapter-gobject.html" title="The GObject base class">
<link rel="chapter" href="chapter-signal.html" title="The GObject messaging system">
<link rel="reference" href="rn01.html" title="API Reference">
<link rel="reference" href="rn02.html" title="Tools Reference">
<link rel="part" href="pt02.html" title="Part&#160;IV.&#160;Tutorial">
<link rel="chapter" href="howto-gobject.html" title="How To define and implement a new GObject?">
<link rel="chapter" href="howto-interface.html" title="How To define and implement Interfaces?">
<link rel="chapter" href="howto-signals.html" title="Howto create and use signals">
<link rel="part" href="pt03.html" title="Part&#160;V.&#160;Related Tools">
<link rel="chapter" href="tools-gob.html" title="GObject builder">
<link rel="chapter" href="tools-ginspector.html" title="Graphical inspection of Gobjects">
<link rel="chapter" href="tools-refdb.html" title="Debugging reference count problems">
<link rel="chapter" href="tools-gtkdoc.html" title="Writing API docs">
<link rel="index" href="ix01.html" title="Index">
<link rel="index" href="ix02.html" title="Index of deprecated symbols">
<link rel="index" href="ix03.html" title="Index of new symbols in 2.2">
<link rel="index" href="ix04.html" title="Index of new symbols in 2.4">
<link rel="index" href="ix05.html" title="Index of new symbols in 2.6">
<link rel="index" href="ix06.html" title="Index of new symbols in 2.8">
<link rel="index" href="ix07.html" title="Index of new symbols in 2.10">
<link rel="index" href="ix08.html" title="Index of new symbols in 2.12">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table class="navigation" id="top" width="100%" summary="Navigation header" cellpadding="2" cellspacing="2"><tr valign="middle">
<td><a accesskey="p" href="ch01s02.html"><img src="left.png" width="24" height="24" border="0" alt="Prev"></a></td>
<td><a accesskey="u" href="pt01.html"><img src="up.png" width="24" height="24" border="0" alt="Up"></a></td>
<td><a accesskey="h" href="index.html"><img src="home.png" width="24" height="24" border="0" alt="Home"></a></td>
<th width="100%" align="center">GObject Reference Manual</th>
<td><a accesskey="n" href="gtype-conventions.html"><img src="right.png" width="24" height="24" border="0" alt="Next"></a></td>
</tr></table>
<div class="chapter" lang="en">
<div class="titlepage"><div><div><h2 class="title">
<a name="id2571326"></a>The Glib Dynamic Type System</h2></div></div></div>
<div class="toc"><dl>
<dt><span class="sect1"><a href="ch02.html#gtype-copy">Copy functions</a></span></dt>
<dt><span class="sect1"><a href="gtype-conventions.html">Conventions</a></span></dt>
<dt><span class="sect1"><a href="gtype-non-instantiable.html">Non-Instantiable non-classed fundamental types</a></span></dt>
<dt><span class="sect1"><a href="gtype-instantiable-classed.html">Instantiable classed types: objects</a></span></dt>
<dt><span class="sect1"><a href="gtype-non-instantiable-classed.html">Non-instantiable classed types: Interfaces.</a></span></dt>
<dd><dl>
<dt><span class="sect2"><a href="gtype-non-instantiable-classed.html#gtype-non-instantiable-classed-init">Interface Initialization</a></span></dt>
<dt><span class="sect2"><a href="gtype-non-instantiable-classed.html#gtype-non-instantiable-classed-dest">Interface Destruction</a></span></dt>
</dl></dd>
</dl></div>
<p>
	A type, as manipulated by the Glib type system, is much more generic than what
	is usually understood as an Object type. It is best explained by looking at the 
	structure and the functions used to register new types in the type system.
	</p>
<pre class="programlisting">
typedef struct _GTypeInfo               GTypeInfo;
struct _GTypeInfo
{
  /* interface types, classed types, instantiated types */
  guint16                class_size;
  
  GBaseInitFunc          base_init;
  GBaseFinalizeFunc      base_finalize;
  
  /* classed types, instantiated types */
  GClassInitFunc         class_init;
  GClassFinalizeFunc     class_finalize;
  gconstpointer          class_data;
  
  /* instantiated types */
  guint16                instance_size;
  guint16                n_preallocs;
  GInstanceInitFunc      instance_init;
  
  /* value handling */
  const GTypeValueTable *value_table;
};
GType g_type_register_static (GType             parent_type,
                              const gchar      *type_name,
                              const GTypeInfo  *info,
                              GTypeFlags        flags);
GType g_type_register_fundamental (GType                       type_id,
                                   const gchar                *type_name,
                                   const GTypeInfo            *info,
                                   const GTypeFundamentalInfo *finfo,
                                   GTypeFlags                  flags);
	</pre>
<p>
      </p>
<p>
	<code class="function"><a href="gobject-Type-Information.html#g-type-register-static">g_type_register_static</a></code> and 
	<code class="function"><a href="gobject-Type-Information.html#g-type-register-fundamental">g_type_register_fundamental</a></code>
	are the C functions, defined in
	<code class="filename">gtype.h</code> and implemented in <code class="filename">gtype.c</code>
	which you should use to register a new <span class="type"><a href="gobject-Type-Information.html#GType">GType</a></span> in the program's type system.
	It is not likely you will ever need to use 
	<code class="function"><a href="gobject-Type-Information.html#g-type-register-fundamental">g_type_register_fundamental</a></code> (you have to be Tim Janik 
	to do that) but in case you want to, the last chapter explains how to create
	new fundamental types.
	<sup>[<a name="id2615157" href="#ftn.id2615157">2</a>]</sup>
      </p>
<p>
	Fundamental types are top-level types which do not derive from any other type 
	while other non-fundamental types derive from other types.
	Upon initialization by <code class="function"><a href="gobject-Type-Information.html#g-type-init">g_type_init</a></code>, the type system not 
	only initializes its internal data structures but it also registers a number of core
	types: some of these are fundamental types. Others are types derived from these 
        fundamental types.
      </p>
<p>
	Fundamental and non-Fundamental types are defined by:
	</p>
<div class="itemizedlist"><ul type="disc">
<li><p>
            class size: the class_size field in <span class="type"><a href="gobject-Type-Information.html#GTypeInfo">GTypeInfo</a></span>.
          </p></li>
<li><p>
            class initialization functions (C++ constructor): the base_init and 
            class_init fields in <span class="type"><a href="gobject-Type-Information.html#GTypeInfo">GTypeInfo</a></span>.
          </p></li>
<li><p>
            class destruction functions (C++ destructor): the base_finalize and 
            class_finalize fields in <span class="type"><a href="gobject-Type-Information.html#GTypeInfo">GTypeInfo</a></span>.
          </p></li>
<li><p>
            instance size (C++ parameter to new): the instance_size field in 
            <span class="type"><a href="gobject-Type-Information.html#GTypeInfo">GTypeInfo</a></span>.
          </p></li>
<li><p>
            instanciation policy (C++ type of new operator): the n_preallocs
            field in <span class="type"><a href="gobject-Type-Information.html#GTypeInfo">GTypeInfo</a></span>.
          </p></li>
<li><p>
            copy functions (C++ copy operators): the value_table field in 
            <span class="type"><a href="gobject-Type-Information.html#GTypeInfo">GTypeInfo</a></span>.
          </p></li>
<li><p>
            type characteristic flags: <span class="type"><a href="gobject-Type-Information.html#GTypeFlags">GTypeFlags</a></span>.
          </p></li>
</ul></div>
<p>
        Fundamental types are also defined by a set of <span class="type"><a href="gobject-Type-Information.html#GTypeFundamentalFlags">GTypeFundamentalFlags</a></span> 
        which are stored in a <span class="type"><a href="gobject-Type-Information.html#GTypeFundamentalInfo">GTypeFundamentalInfo</a></span>.
        Non-Fundamental types are furthermore defined by the type of their parent which is
        passed as the parent_type parameter to <code class="function"><a href="gobject-Type-Information.html#g-type-register-static">g_type_register_static</a></code>
        and <code class="function"><a href="gobject-Type-Information.html#g-type-register-dynamic">g_type_register_dynamic</a></code>.
      </p>
<div class="sect1" lang="en">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="gtype-copy"></a>Copy functions</h2></div></div></div>
<p>
	  The major common point between <span class="emphasis"><em>all</em></span> glib types (fundamental and 
	  non-fundamental, classed and non-classed, instantiable and non-instantiable) is that
	  they can all be manipulated through a single API to copy/assign them.
	</p>
<p>
	  The <span class="type"><a href="gobject-Generic-values.html#GValue">GValue</a></span> structure is used as an abstract container for all of these 
	  types. Its simplistic API (defined in <code class="filename">gobject/gvalue.h</code>) can be 
	  used to invoke the value_table functions registered
	  during type registration: for example <code class="function"><a href="gobject-Generic-values.html#g-value-copy">g_value_copy</a></code> copies the 
	  content of a <span class="type"><a href="gobject-Generic-values.html#GValue">GValue</a></span> to another <span class="type"><a href="gobject-Generic-values.html#GValue">GValue</a></span>. This is similar
	  to a C++ assignment which invokes the C++ copy operator to modify the default
	  bit-by-bit copy semantics of C++/C structures/classes.
	</p>
<p>
	  The following code shows how you can copy around a 64 bit integer, as well as a <span class="type"><a href="gobject-The-Base-Object-Type.html#GObject">GObject</a></span>
	  instance pointer (sample code for this is located in the source tarball for this document in 
          <code class="filename">sample/gtype/test.c</code>):
</p>
<pre class="programlisting">
static void test_int (void)
{
  GValue a_value = {0, }; 
  GValue b_value = {0, };
  guint64 a, b;

  a = 0xdeadbeaf;

  g_value_init (&amp;a_value, G_TYPE_UINT64);
  g_value_set_uint64 (&amp;a_value, a);

  g_value_init (&amp;b_value, G_TYPE_UINT64);
  g_value_copy (&amp;a_value, &amp;b_value);

  b = g_value_get_uint64 (&amp;b_value);

  if (a == b) {
    g_print ("Yay !! 10 lines of code to copy around a uint64.\n");
  } else {
    g_print ("Are you sure this is not a Z80 ?\n");
  }
}

static void test_object (void)
{
  GObject *obj;
  GValue obj_vala = {0, };
  GValue obj_valb = {0, };
  obj = g_object_new (MAMAN_BAR_TYPE, NULL);

  g_value_init (&amp;obj_vala, MAMAN_BAR_TYPE);
  g_value_set_object (&amp;obj_vala, obj);

  g_value_init (&amp;obj_valb, G_TYPE_OBJECT);

  /* g_value_copy's semantics for G_TYPE_OBJECT types is to copy the reference.
     This function thus calls g_object_ref.
     It is interesting to note that the assignment works here because
     MAMAN_BAR_TYPE is a G_TYPE_OBJECT.
   */
  g_value_copy (&amp;obj_vala, &amp;obj_valb);

  g_object_unref (G_OBJECT (obj));
  g_object_unref (G_OBJECT (obj));
}
</pre>
<p>
	  The important point about the above code is that the exact semantic of the copy calls
	  is undefined since they depend on the implementation of the copy function. Certain 
	  copy functions might decide to allocate a new chunk of memory and then to copy the 
	  data from the source to the destination. Others might want to simply increment
	  the reference count of the instance and copy the reference to the new GValue.
	</p>
<p>
	  The value_table used to specify these assignment functions is defined in
	  <code class="filename">gtype.h</code> and is thoroughly described in the
	  API documentation provided with GObject (for once ;-) which is why we will
	  not detail its exact semantics. 
	  </p>
<pre class="programlisting">
typedef struct _GTypeValueTable         GTypeValueTable;
struct _GTypeValueTable
{
  void     (*value_init)         (GValue       *value);
  void     (*value_free)         (GValue       *value);
  void     (*value_copy)         (const GValue *src_value,
				  GValue       *dest_value);
  /* varargs functionality (optional) */
  gpointer (*value_peek_pointer) (const GValue *value);
  gchar	    *collect_format;
  gchar*   (*collect_value)      (GValue       *value,
				  guint         n_collect_values,
				  GTypeCValue  *collect_values,
				  guint		collect_flags);
  gchar	    *lcopy_format;
  gchar*   (*lcopy_value)        (const GValue *value,
				  guint         n_collect_values,
				  GTypeCValue  *collect_values,
				  guint		collect_flags);
};
	  </pre>
<p>
	  Interestingly, it is also very unlikely
	  you will ever need to specify a value_table during type registration
	  because these value_tables are inherited from the parent types for
	  non-fundamental types which means that unless you want to write a 
	  fundamental type (not a great idea !), you will not need to provide
	  a new value_table since you will inherit the value_table structure
	  from your parent type.
	</p>
</div>
<div class="footnotes">
<br><hr width="100" align="left">
<div class="footnote"><p><sup>[<a name="ftn.id2615157" href="#id2615157">2</a>] </sup>
	    Please, note that there exist another registration function: the 
	    <code class="function"><a href="gobject-Type-Information.html#g-type-register-dynamic">g_type_register_dynamic</a></code>. We will not discuss this
	    function here since its use is very similar to the <code class="function">_static</code> 
	    version.
	  </p></div>
</div>
</div>
</body>
</html>

Anon7 - 2021