GObject 参考手册:概念:GLib 动态类型系统

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.
由 Glib 类型系统操作的一个类型,比传统上所讲的 Object 类型更具一般化。下面查看类型系统中有关类结构和注册新类型的函数,是会对此最好的解释。

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);

g_type_register_static and g_type_register_fundamental are the C functions, defined in gtype.h and implemented in gtype.c which you should use to register a new GType in the program’s type system. It is not likely you will ever need to use g_type_register_fundamental (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.
g_type_register_static 和 g_type_register_fundamental 这两个C函数定义在 gtype.h 中,并在 gtype.c 中具体实现。你可以用来在程序的类型系统中注册一个新的 GType。一般情况下你永远也不需要使用 g_type_register_fundamental(除非你是Tim Janik才会这样做),但是这次你要做,在最后一章会向你解释如何创建一个基本类型。

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 g_type_init, 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.
基本类型是不需要从任何其他类型取得的最顶级的类型,相对的,其他非基本类型是继承于其他类型的。在由 g_type_init 初始化时,类型系统不仅仅初始化它的内部数据结构,同时也注册一些核心类型:其中一些是基本类型,其他则是从基本类型继承的。

Fundamental and non-fundamental types are defined by:

  • class size: the class_size field in GTypeInfo.
  • 类的大小:GTypeInfo 的 class_size。
  • class initialization functions (C++ constructor): the base_init and class_init fields in GTypeInfo.
  • 类的初始化函数(C++ 构造函数):GTypeInfo 的 base_init 和 class_init。
  • class destruction functions (C++ destructor): the base_finalize and class_finalize fields in GTypeInfo.
  • 类的销毁函数(C++析构函数):GTypeInfo 的 base_finalize 和 class_finalize。
  • instance size (C++ parameter to new): the instance_size field in GTypeInfo.
  • 实例的大小(C++参数new):GTypeInfo中 的 instance_size。
  • instantiation policy (C++ type of new operator): the n_preallocs field in GTypeInfo.
  • 实例化策略(C++ 类的new operator):GTypeInfo 的 n_preallocs。
  • copy functions (C++ copy operators): the value_table field in GTypeInfo.
  • 复制函数(C++的复制操作):GTypeInfo 的 value_table。
  • type characteristic flags: GTypeFlags.
  • 类的字符标志:GTypeFlags。

Fundamental types are also defined by a set of GTypeFundamentalFlags which are stored in a GTypeFundamentalInfo. Non-fundamental types are furthermore defined by the type of their parent which is passed as the parent_type parameter to g_type_register_static and g_type_register_dynamic.
基本类型同样可以由 GTypeFundamentalFlags 来定义,并保存在 GTypeFundamentallInfo 中。非基本类型一般传递一个 parent_type 至 g_type_register_static 和 g_type_register_dynamic 中,然后交给父类来定义。

Leave a Reply

Your email address will not be published. Required fields are marked *