三 可实例化的类型:对象(Instantiable classed types: objects)

来源:互联网 发布:凯立德v4.0端口修改器 编辑:程序博客网 时间:2024/06/14 20:24
可实例化的类型:对象(Instantiable classed types: objects)

对于那些注册为类并且声明为实例的类型是最象对象的类了。(用中文太难表达了)。

Instantiable classed types: objects

Types which are registered with a class and are declared instantiable are what most closely resembles an object. GObjects are the most well known type of instantiable classed types. Wo will discuss GObject  later, here we wil discuss some other instantiable classed types, which used as the base of an inheritance hierarchy have been externally developped and they are all built on the fundamental features described below.
For example, the code below shows how you could register such a fundamental object type in the type system:
typedef struct ...{
  GObject parent;
  
/**//* instance members */
  
int field_a;
}
 MamanBar;

typedef 
struct ...{
  GObjectClass class_parent;
  
/**//* class members */
  
void (*do_action_public_virtual) (MamanBar *self, guint8 i);

  
void (*do_action_public_pure_virtual) (MamanBar *self, guint8 i);
}
 MamanBarClass;

#define MAMAN_BAR_TYPE (maman_bar_get_type ())

GType 
maman_bar_get_type (
void)
...
{
  
static GType type = 0;
  
if (type == 0) ...{
    
static const GTypeInfo info = ...{
      
sizeof (MamanBarClass),
      NULL,           
/**//* base_init */
      NULL,           
/**//* base_finalize */
      (GClassInitFunc) foo_class_init,
      NULL,           
/**//* class_finalize */
      NULL,           
/**//* class_data */
      
sizeof (MamanBar),
      
0,              /**//* n_preallocs */
      (GInstanceInitFunc) NULL 
/**//* instance_init */
    }
;
    type 
= g_type_register_static (G_TYPE_OBJECT,
                                   
"BarType",
                                   
&info, 0);
  }

  
return type;
}


Upon the first call to maman_bar_get_type, the type named BarType will be registered in the type system as inheriting from the type G_TYPE_OBJECT.

Every object must define two structures: its class structure and its instance structure. All class structures must contain as first member a GTypeClass structure. All instance structures must contain as first member a GTypeInstance structure. The declaration of these C types, coming from gtype.h is shown below:
struct _GTypeClass
{
  GType g_type;
}
;
struct _GTypeInstance
{
  GTypeClass 
*g_class;
}
;
These constraints allow the type system to make sure that every object instance (identified by a pointer to the object's instance structure) contains in its first bytes a pointer to the object's class structure.

This relationship is best explained by an example: let's take object B which inherits from object A:

/* A definitions */
typedef 
struct {
  GTypeInstance parent;
  
int field_a;
  
int field_b;
}
 A;
typedef 
struct {
  GTypeClass parent_class;
  
void (*method_a) (void);
  
void (*method_b) (void);
}
 AClass;

/* B definitions. */
typedef 
struct {
  A parent;
  
int field_c;
  
int field_d;
}
 B;
typedef 
struct {
  AClass parent_class;
  
void (*method_c) (void);
  
void (*method_d) (void);
}
 BClass;
The C standard mandates that the first field of a C structure is stored starting in the first byte of the buffer used to hold the structure's fields in memory. This means that the first field of an instance of an object B is A's first field which in turn is GTypeInstance's first field which in turn is g_class, a pointer to B's class structure.

 



(to be continue....)