GObject 参考手册:概念:GLib 动态类型系统:不可实例化和不可类化的基础类型

A lot of types are not instantiable by the type system and do not have a class. Most of these types are fundamental trivial types such as gchar, registered in g_value_types_init (in gvaluetypes.c).
在类型系统中,许多类型是不可实例化而且没有父类的。大多数这些类型是最基础的基本类型,如 gchar,它由 g_value_types_init 注册(在gvaluetypes.c中)。

To register such a type in the type system, you just need to fill the GTypeInfo structure with zeros since these types are also most of the time fundamental:
如果想在类型系统中注册这样一个类型,你仅仅需要用 0 来填充 GTypeInfo 结构。

  GTypeInfo info = {
    0,                                /* class_size */
    NULL,                        /* base_init */
    NULL,                        /* base_destroy */
    NULL,                        /* class_init */
    NULL,                        /* class_destroy */
    NULL,                        /* class_data */
    0,                                /* instance_size */
    0,                                /* n_preallocs */
    NULL,                        /* instance_init */
    NULL,                        /* value_table */
  static const GTypeValueTable value_table = {
    value_init_long0,                /* value_init */
    NULL,                        /* value_free */
    value_copy_long0,                /* value_copy */
    NULL,                        /* value_peek_pointer */
    "i",                        /* collect_format */
    value_collect_int,        /* collect_value */
    "p",                        /* lcopy_format */
    value_lcopy_char,                /* lcopy_value */
  info.value_table = &value_table;
  type = g_type_register_fundamental (G_TYPE_CHAR, "gchar", &info, &finfo, 0);

Having non-instantiable types might seem a bit useless: what good is a type if you cannot instantiate an instance of that type ? Most of these types are used in conjunction with GValues: a GValue is initialized with an integer or a string and it is passed around by using the registered type’s value_table. GValues (and by extension these trivial fundamental types) are most useful when used in conjunction with object properties and signals.
使用不可实例的类型似乎是无用的:定义一个不能实例化的类型有什么好处呢?大多数这种类型与 GValue 用作一块:一个 GValue 由一个整型或一个字符串来初始化,再被传递了一个已注册类型的 value_table 。GValue(以基本类型延伸)最有用的时候是在与对象的属性和信号用在一块时。

Leave a Reply

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