Blame docs/reference/gobject/html/chapter-gtype.html

Packit ae235b
Packit ae235b
<html>
Packit ae235b
<head>
Packit ae235b
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
Packit ae235b
<title>The GLib Dynamic Type System: GObject Reference Manual</title>
Packit ae235b
<meta name="generator" content="DocBook XSL Stylesheets Vsnapshot">
Packit ae235b
<link rel="home" href="index.html" title="GObject Reference Manual">
Packit ae235b
<link rel="up" href="pt01.html" title="Part I. Concepts">
Packit ae235b
<link rel="prev" href="ch01s02.html" title="Exporting a C API">
Packit ae235b
<link rel="next" href="gtype-conventions.html" title="Conventions">
Packit ae235b
<meta name="generator" content="GTK-Doc V1.27 (XML mode)">
Packit ae235b
<link rel="stylesheet" href="style.css" type="text/css">
Packit ae235b
</head>
Packit ae235b
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
Packit ae235b
Packit ae235b
Packit ae235b
Home
Packit ae235b
Up
Packit ae235b
Prev
Packit ae235b
Next
Packit ae235b
Packit ae235b
Packit ae235b

Packit ae235b
The GLib Dynamic Type System
Packit ae235b
Packit ae235b
Copy functions
Packit ae235b
Conventions
Packit ae235b
Non-instantiable non-classed fundamental types
Packit ae235b
Instantiable classed types: objects
Packit ae235b
Initialization and Destruction
Packit ae235b
Non-instantiable classed types: interfaces
Packit ae235b
Packit ae235b
Interface Initialization
Packit ae235b
Interface Destruction
Packit ae235b
Packit ae235b
Packit ae235b

Packit ae235b
        A type, as manipulated by the GLib type system, is much more generic than what
Packit ae235b
        is usually understood as an Object type. It is best explained by looking at the 
Packit ae235b
        structure and the functions used to register new types in the type system.
Packit ae235b
        

Packit ae235b
Packit ae235b
  
Packit ae235b
    
Packit ae235b
      
Packit ae235b
        
1
Packit ae235b
2
Packit ae235b
3
Packit ae235b
4
Packit ae235b
5
Packit ae235b
6
Packit ae235b
7
Packit ae235b
8
Packit ae235b
9
Packit ae235b
10
Packit ae235b
11
Packit ae235b
12
Packit ae235b
13
Packit ae235b
14
Packit ae235b
15
Packit ae235b
16
Packit ae235b
17
Packit ae235b
18
Packit ae235b
19
Packit ae235b
20
Packit ae235b
21
Packit ae235b
22
Packit ae235b
23
Packit ae235b
24
Packit ae235b
25
Packit ae235b
26
Packit ae235b
27
Packit ae235b
28
Packit ae235b
29
Packit ae235b
30
Packit ae235b
31
Packit ae235b
        
typedef struct _GTypeInfo               GTypeInfo;
Packit ae235b
struct _GTypeInfo
Packit ae235b
{
Packit ae235b
  /* interface types, classed types, instantiated types */
Packit ae235b
  guint16                class_size;
Packit ae235b
  
Packit ae235b
  GBaseInitFunc          base_init;
Packit ae235b
  GBaseFinalizeFunc      base_finalize;
Packit ae235b
  
Packit ae235b
  /* classed types, instantiated types */
Packit ae235b
  GClassInitFunc         class_init;
Packit ae235b
  GClassFinalizeFunc     class_finalize;
Packit ae235b
  gconstpointer          class_data;
Packit ae235b
  
Packit ae235b
  /* instantiated types */
Packit ae235b
  guint16                instance_size;
Packit ae235b
  guint16                n_preallocs;
Packit ae235b
  GInstanceInitFunc      instance_init;
Packit ae235b
  
Packit ae235b
  /* value handling */
Packit ae235b
  const GTypeValueTable *value_table;
Packit ae235b
};
Packit ae235b
GType g_type_register_static (GType             parent_type,
Packit ae235b
                              const gchar      *type_name,
Packit ae235b
                              const GTypeInfo  *info,
Packit ae235b
                              GTypeFlags        flags);
Packit ae235b
GType g_type_register_fundamental (GType                       type_id,
Packit ae235b
                                   const gchar                *type_name,
Packit ae235b
                                   const GTypeInfo            *info,
Packit ae235b
                                   const GTypeFundamentalInfo *finfo,
Packit ae235b
                                   GTypeFlags                  flags);
Packit ae235b
      
Packit ae235b
    
Packit ae235b
  
Packit ae235b
Packit ae235b
Packit ae235b

Packit ae235b
      

Packit ae235b

Packit ae235b
        g_type_register_static,
Packit ae235b
        g_type_register_dynamic and 
Packit ae235b
        g_type_register_fundamental
Packit ae235b
        are the C functions, defined in
Packit ae235b
        gtype.h and implemented in gtype.c
Packit ae235b
        which you should use to register a new GType in the program's type system.
Packit ae235b
        It is not likely you will ever need to use 
Packit ae235b
        g_type_register_fundamental
Packit ae235b
        but in case you want to, the last chapter explains how to create
Packit ae235b
        new fundamental types.
Packit ae235b
      

Packit ae235b

Packit ae235b
        Fundamental types are top-level types which do not derive from any other type 
Packit ae235b
        while other non-fundamental types derive from other types.
Packit ae235b
        Upon initialization, the type system not only initializes its
Packit ae235b
        internal data structures but it also registers a number of core
Packit ae235b
        types: some of these are fundamental types. Others are types derived from these 
Packit ae235b
        fundamental types.
Packit ae235b
      

Packit ae235b

Packit ae235b
        Fundamental and non-fundamental types are defined by:
Packit ae235b
        

Packit ae235b
    Packit ae235b
  • Packit ae235b
                class size: the class_size field in GTypeInfo.
    Packit ae235b
              

    Packit ae235b
  • Packit ae235b
                class initialization functions (C++ constructor): the base_init and 
    Packit ae235b
                class_init fields in GTypeInfo.
    Packit ae235b
              

    Packit ae235b
  • Packit ae235b
                class destruction functions (C++ destructor): the base_finalize and 
    Packit ae235b
                class_finalize fields in GTypeInfo.
    Packit ae235b
              

    Packit ae235b
  • Packit ae235b
                instance size (C++ parameter to new): the instance_size field in 
    Packit ae235b
                GTypeInfo.
    Packit ae235b
              

    Packit ae235b
  • Packit ae235b
                instantiation policy (C++ type of new operator): the n_preallocs
    Packit ae235b
                field in GTypeInfo.
    Packit ae235b
              

    Packit ae235b
  • Packit ae235b
                copy functions (C++ copy operators): the value_table field in 
    Packit ae235b
                GTypeInfo.
    Packit ae235b
              

    Packit ae235b
  • Packit ae235b
                type characteristic flags: GTypeFlags.
    Packit ae235b
              

    Packit ae235b
    Packit ae235b

    Packit ae235b
            Fundamental types are also defined by a set of GTypeFundamentalFlags 
    Packit ae235b
            which are stored in a GTypeFundamentalInfo.
    Packit ae235b
            Non-fundamental types are furthermore defined by the type of their parent which is
    Packit ae235b
            passed as the parent_type parameter to g_type_register_static
    Packit ae235b
            and g_type_register_dynamic.
    Packit ae235b
          

    Packit ae235b
    Packit ae235b

    Packit ae235b
    Copy functions
    Packit ae235b

    Packit ae235b
              The major common point between all GLib types (fundamental and 
    Packit ae235b
              non-fundamental, classed and non-classed, instantiable and non-instantiable) is that
    Packit ae235b
              they can all be manipulated through a single API to copy/assign them.
    Packit ae235b
            

    Packit ae235b

    Packit ae235b
              The GValue structure is used as an abstract container for all of these 
    Packit ae235b
              types. Its simplistic API (defined in gobject/gvalue.h) can be 
    Packit ae235b
              used to invoke the value_table functions registered
    Packit ae235b
              during type registration: for example g_value_copy copies the 
    Packit ae235b
              content of a GValue to another GValue. This is similar
    Packit ae235b
              to a C++ assignment which invokes the C++ copy operator to modify the default
    Packit ae235b
              bit-by-bit copy semantics of C++/C structures/classes.
    Packit ae235b
            

    Packit ae235b

    Packit ae235b
              The following code shows how you can copy around a 64 bit integer, as well as a GObject
    Packit ae235b
              instance pointer:
    Packit ae235b

    Packit ae235b
    Packit ae235b
      
    Packit ae235b
        
    Packit ae235b
          
    Packit ae235b
            
    1
    Packit ae235b
    2
    Packit ae235b
    3
    Packit ae235b
    4
    Packit ae235b
    5
    Packit ae235b
    6
    Packit ae235b
    7
    Packit ae235b
    8
    Packit ae235b
    9
    Packit ae235b
    10
    Packit ae235b
    11
    Packit ae235b
    12
    Packit ae235b
    13
    Packit ae235b
    14
    Packit ae235b
    15
    Packit ae235b
    16
    Packit ae235b
    17
    Packit ae235b
    18
    Packit ae235b
    19
    Packit ae235b
    20
    Packit ae235b
    21
    Packit ae235b
    22
    Packit ae235b
    23
    Packit ae235b
    24
    Packit ae235b
    25
    Packit ae235b
    26
    Packit ae235b
    27
    Packit ae235b
    28
    Packit ae235b
    29
    Packit ae235b
    30
    Packit ae235b
    31
    Packit ae235b
    32
    Packit ae235b
    33
    Packit ae235b
    34
    Packit ae235b
    35
    Packit ae235b
    36
    Packit ae235b
    37
    Packit ae235b
    38
    Packit ae235b
    39
    Packit ae235b
    40
    Packit ae235b
    41
    Packit ae235b
    42
    Packit ae235b
    43
    Packit ae235b
    44
    Packit ae235b
    45
    Packit ae235b
            
    static void test_int (void)
    Packit ae235b
    {
    Packit ae235b
      GValue a_value = G_VALUE_INIT;
    Packit ae235b
      GValue b_value = G_VALUE_INIT;
    Packit ae235b
      guint64 a, b;
    Packit ae235b
    Packit ae235b
      a = 0xdeadbeef;
    Packit ae235b
    Packit ae235b
      g_value_init (&a_value, G_TYPE_UINT64);
    Packit ae235b
      g_value_set_uint64 (&a_value, a);
    Packit ae235b
    Packit ae235b
      g_value_init (&b_value, G_TYPE_UINT64);
    Packit ae235b
      g_value_copy (&a_value, &b_value);
    Packit ae235b
    Packit ae235b
      b = g_value_get_uint64 (&b_value);
    Packit ae235b
    Packit ae235b
      if (a == b) {
    Packit ae235b
        g_print ("Yay !! 10 lines of code to copy around a uint64.\n");
    Packit ae235b
      } else {
    Packit ae235b
        g_print ("Are you sure this is not a Z80 ?\n");
    Packit ae235b
      }
    Packit ae235b
    }
    Packit ae235b
    Packit ae235b
    static void test_object (void)
    Packit ae235b
    {
    Packit ae235b
      GObject *obj;
    Packit ae235b
      GValue obj_vala = G_VALUE_INIT;
    Packit ae235b
      GValue obj_valb = G_VALUE_INIT;
    Packit ae235b
      obj = g_object_new (VIEWER_TYPE_FILE, NULL);
    Packit ae235b
    Packit ae235b
      g_value_init (&obj_vala, VIEWER_TYPE_FILE);
    Packit ae235b
      g_value_set_object (&obj_vala, obj);
    Packit ae235b
    Packit ae235b
      g_value_init (&obj_valb, G_TYPE_OBJECT);
    Packit ae235b
    Packit ae235b
      /* g_value_copy's semantics for G_TYPE_OBJECT types is to copy the reference.
    Packit ae235b
       * This function thus calls g_object_ref.
    Packit ae235b
       * It is interesting to note that the assignment works here because
    Packit ae235b
       * VIEWER_TYPE_FILE is a G_TYPE_OBJECT.
    Packit ae235b
       */
    Packit ae235b
      g_value_copy (&obj_vala, &obj_valb);
    Packit ae235b
    Packit ae235b
      g_object_unref (G_OBJECT (obj));
    Packit ae235b
      g_object_unref (G_OBJECT (obj));
    Packit ae235b
    }
    Packit ae235b
          
    Packit ae235b
        
    Packit ae235b
      
    Packit ae235b
    Packit ae235b
    Packit ae235b

    Packit ae235b
              The important point about the above code is that the exact semantics of the copy calls
    Packit ae235b
              is undefined since they depend on the implementation of the copy function. Certain 
    Packit ae235b
              copy functions might decide to allocate a new chunk of memory and then to copy the 
    Packit ae235b
              data from the source to the destination. Others might want to simply increment
    Packit ae235b
              the reference count of the instance and copy the reference to the new GValue.
    Packit ae235b
            

    Packit ae235b

    Packit ae235b
              The value table used to specify these assignment functions is
    Packit ae235b
              documented in
    Packit ae235b
              GTypeValueTable.
    Packit ae235b
            

    Packit ae235b

    Packit ae235b
              Interestingly, it is also very unlikely
    Packit ae235b
              you will ever need to specify a value_table during type registration
    Packit ae235b
              because these value_tables are inherited from the parent types for
    Packit ae235b
              non-fundamental types.
    Packit ae235b
            

    Packit ae235b
    Packit ae235b
    Packit ae235b
    Packit ae235b

    Generated by GTK-Doc V1.27
    Packit ae235b
    </body>
    Packit ae235b
    </html>