📄 gtype.h
字号:
* TypeAClass parent_class; * gfloat static_float; * GString *dynamic_gstring; * } TypeBClass; * static void * type_b_base_class_init (TypeBClass *class) * { * class->dynamic_gstring = g_string_new ("some other string"); * } * static void * type_b_base_class_finalize (TypeBClass *class) * { * g_string_free (class->dynamic_gstring); * } * static void * type_b_class_init (TypeBClass *class) * { * class->static_float = 3.14159265358979323846; * } * ]| * Initialization of TypeBClass will first cause initialization of * TypeAClass (derived classes reference their parent classes, see * g_type_class_ref() on this). * Initialization of TypeAClass roughly involves zero-initializing its fields, * then calling its GBaseInitFunc() type_a_base_class_init() to allocate * its dynamic members (dynamic_string), and finally calling its GClassInitFunc() * type_a_class_init() to initialize its static members (static_integer). * The first step in the initialization process of TypeBClass is then * a plain memory copy of the contents of TypeAClass into TypeBClass and * zero-initialization of the remaining fields in TypeBClass. * The dynamic members of TypeAClass within TypeBClass now need * reinitialization which is performed by calling type_a_base_class_init() * with an argument of TypeBClass. * After that, the GBaseInitFunc() of TypeBClass, type_b_base_class_init() * is called to allocate the dynamic members of TypeBClass (dynamic_gstring), * and finally the GClassInitFunc() of TypeBClass, type_b_class_init(), * is called to complete the initialization process with the static members * (static_float). * Corresponding finalization counter parts to the GBaseInitFunc() functions * have to be provided to release allocated resources at class finalization * time. */typedef void (*GClassInitFunc) (gpointer g_class, gpointer class_data);/** * GClassFinalizeFunc: * @g_class: The #GTypeClass structure to finalize. * @class_data: The @class_data member supplied via the #GTypeInfo structure. * * A callback function used by the type system to finalize a class. * This function is rarely needed, as dynamically allocated class resources * should be handled by GBaseInitFunc() and GBaseFinalizeFunc(). * Also, specification of a GClassFinalizeFunc() in the #GTypeInfo * structure of a static type is invalid, because classes of static types * will never be finalized (they are artificially kept alive when their * reference count drops to zero). */typedef void (*GClassFinalizeFunc) (gpointer g_class, gpointer class_data);/** * GInstanceInitFunc: * @instance: The instance to initialize. * @g_class: The class of the type the instance is created for. * * A callback function used by the type system to initialize a new * instance of a type. This function initializes all instance members and * allocates any resources required by it. * Initialization of a derived instance involves calling all its parent * types instance initializers, so the class member of the instance * is altered during its initialization to always point to the class that * belongs to the type the current initializer was introduced for. */typedef void (*GInstanceInitFunc) (GTypeInstance *instance, gpointer g_class);/** * GInterfaceInitFunc: * @g_iface: The interface structure to initialize. * @iface_data: The @interface_data supplied via the #GInterfaceInfo structure. * * A callback function used by the type system to initialize a new * interface. This function should initialize all internal data and * allocate any resources required by the interface. */typedef void (*GInterfaceInitFunc) (gpointer g_iface, gpointer iface_data);/** * GInterfaceFinalizeFunc: * @g_iface: The interface structure to finalize. * @iface_data: The @interface_data supplied via the #GInterfaceInfo structure. * * A callback function used by the type system to finalize an interface. * This function should destroy any internal data and release any resources * allocated by the corresponding GInterfaceInitFunc() function. */typedef void (*GInterfaceFinalizeFunc) (gpointer g_iface, gpointer iface_data);/** * GTypeClassCacheFunc: * @cache_data: data that was given to the g_type_add_class_cache_func() call * @g_class: The #GTypeClass structure which is unreferenced * * A callback function which is called when the reference count of a class * drops to zero. It may use g_type_class_ref() to prevent the class from * being freed. You should not call g_type_class_unref() from a * #GTypeClassCacheFunc function to prevent infinite recursion, use * g_type_class_unref_uncached() instead. * * The functions have to check the class id passed in to figure * whether they actually want to cache the class of this type, since all * classes are routed through the same #GTypeClassCacheFunc chain. * * Returns: %TRUE to stop further #GTypeClassCacheFunc<!-- -->s from being * called, %FALSE to continue. */typedef gboolean (*GTypeClassCacheFunc) (gpointer cache_data, GTypeClass *g_class);/** * GTypeInterfaceCheckFunc: * @check_data: data passed to g_type_add_interface_check(). * @g_iface: the interface that has been initialized * * A callback called after an interface vtable is initialized. * See g_type_add_interface_check(). * * Since: 2.4 */typedef void (*GTypeInterfaceCheckFunc) (gpointer check_data, gpointer g_iface);/** * GTypeFundamentalFlags: * @G_TYPE_FLAG_CLASSED: Indicates a classed type. * @G_TYPE_FLAG_INSTANTIATABLE: Indicates an instantiable type (implies classed). * @G_TYPE_FLAG_DERIVABLE: Indicates a flat derivable type. * @G_TYPE_FLAG_DEEP_DERIVABLE: Indicates a deep derivable type (implies derivable). * * Bit masks used to check or determine specific characteristics of a * fundamental type. */typedef enum /*< skip >*/{ G_TYPE_FLAG_CLASSED = (1 << 0), G_TYPE_FLAG_INSTANTIATABLE = (1 << 1), G_TYPE_FLAG_DERIVABLE = (1 << 2), G_TYPE_FLAG_DEEP_DERIVABLE = (1 << 3)} GTypeFundamentalFlags;/** * GTypeFlags: * @G_TYPE_FLAG_ABSTRACT: Indicates an abstract type. No instances can be * created for an abstract type. * @G_TYPE_FLAG_VALUE_ABSTRACT: Indicates an abstract value type, i.e. a type * that introduces a value table, but can't be used for * g_value_init(). * * Bit masks used to check or determine characteristics of a type. */typedef enum /*< skip >*/{ G_TYPE_FLAG_ABSTRACT = (1 << 4), G_TYPE_FLAG_VALUE_ABSTRACT = (1 << 5)} GTypeFlags;/** * GTypeInfo: * @class_size: Size of the class structure (required for interface, classed and instantiatable types). * @base_init: Location of the base initialization function (optional). * @base_finalize: Location of the base finalization function (optional). * @class_init: Location of the class initialization function for * classed and instantiatable types. Location of the default vtable * inititalization function for interface types. (optional) This function * is used both to fill in virtual functions in the class or default vtable, * and to do type-specific setup such as registering signals and object * properties. * @class_finalize: Location of the class finalization function for * classed and instantiatable types. Location fo the default vtable * finalization function for interface types. (optional) * @class_data: User-supplied data passed to the class init/finalize functions. * @instance_size: Size of the instance (object) structure (required for instantiatable types only). * @n_preallocs: Prior to GLib 2.10, it specified the number of pre-allocated (cached) instances to reserve memory for (0 indicates no caching). Since GLib 2.10, it is ignored, since instances are allocated with the <link linkend="glib-Memory-Slices">slice allocator</link> now. * @instance_init: Location of the instance initialization function (optional, for instantiatable types only). * @value_table: A #GTypeValueTable function table for generic handling of GValues of this type (usually only * useful for fundamental types). * * This structure is used to provide the type system with the information * required to initialize and destruct (finalize) a type's class and * its instances. * The initialized structure is passed to the g_type_register_static() function * (or is copied into the provided #GTypeInfo structure in the * g_type_plugin_complete_type_info()). The type system will perform a deep * copy of this structure, so its memory does not need to be persistent * across invocation of g_type_register_static(). */struct _GTypeInfo{ /* interface types, classed types, instantiated types */ guint16 class_size; GBaseInitFunc base_init; GBaseFinalizeFunc base_finalize; /* interface types, 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;};/** * GTypeFundamentalInfo: * @type_flags: #GTypeFundamentalFlags describing the characteristics of the fundamental type * * A structure that provides information to the type system which is * used specifically for managing fundamental types. */struct _GTypeFundamentalInfo{ GTypeFundamentalFlags type_flags;};/** * GInterfaceInfo: * @interface_init: location of the interface initialization function * @interface_finalize: location of the interface finalization function * @interface_data: user-supplied data passed to the interface init/finalize functions * * A structure that provides information to the type system which is * used specifically for managing interface types. */struct _GInterfaceInfo{ GInterfaceInitFunc interface_init; GInterfaceFinalizeFunc interface_finalize; gpointer interface_data;};/** * GTypeValueTable: * @value_init: Default initialize @values contents by poking values * directly into the value->data array. The data array of * the #GValue passed into this function was zero-filled * with <function>memset()</function>, so no care has to * be taken to free any * old contents. E.g. for the implementation of a string * value that may never be %NULL, the implementation might * look like: * |[ * value->data[0].v_pointer = g_strdup (""); * ]| * @value_free: Free any old contents that might be left in the * data array of the passed in @value. No resources may * remain allocated through the #GValue contents after * this function returns. E.g. for our above string type: * |[ * // only free strings without a specific flag for static storage * if (!(value->data[1].v_uint & G_VALUE_NOCOPY_CONTENTS)) * g_free (value->data[0].v_pointer); * ]| * @value_copy: @dest_value is a #GValue with zero-filled data section * and @src_value is a properly setup #GValue of same or * derived type. * The purpose of this function is to copy the contents of * @src_value into @dest_value in a way, that even after * @src_value has been freed, the contents of @dest_value * remain valid. String type example: * |[ * dest_value->data[0].v_pointer = g_strdup (src_value->data[0].v_pointer); * ]| * @value_peek_pointer: If the value contents fit into a pointer, such as objects * or strings, return this pointer, so the caller can peek at * the current contents. To extend on our above string example: * |[ * return value->data[0].v_pointer; * ]| * @collect_format: A string format describing how to collect the contents of * this value bit-by-bit. Each character in the format represents * an argument to be collected, and the characters themselves indicate * the type of the argument. Currently supported arguments are: * <variablelist> * <varlistentry><term /><listitem><para> * 'i' - Integers. passed as collect_values[].v_int. * </para></listitem></varlistentry> * <varlistentry><term /><listitem><para> * 'l' - Longs. passed as collect_values[].v_long. * </para></listitem></varlistentry> * <varlistentry><term /><listitem><para> * 'd' - Doubles. passed as collect_values[].v_double. * </para></listitem></varlistentry> * <varlistentry><term /><listitem><para> * 'p' - Pointers. passed as collect_values[].v_pointer. * </para></listitem></varlistentry> * </variablelist> * It should be noted that for variable argument list construction, * ANSI C promotes every type smaller than an integer to an int, and * floats to doubles. So for collection of short int or char, 'i' * needs to be used, and for collection of floats 'd'. * @collect_value: The collect_value() function is responsible for converting the * values collected from a variable argument list into contents * suitable for storage in a GValue. This function should setup * @value similar to value_init(); e.g. for a string value that * does not allow %NULL pointers, it needs to either spew an error, * or do an implicit conversion by storing an empty string. * The @value passed in to this function has a zero-filled data * array, so just like for value_init() it is guaranteed to not * contain any old contents that might need freeing. * @n_collect_values is exactly the string length of @collect_format, * and @collect_values is an array of unions #GTypeCValue with * length @n_collect_values, containing the collected values * according to @collect_format. * @collect_flags is an argument provided as a hint by the caller. * It may contain the flag %G_VALUE_NOCOPY_CONTENTS indicating, * that the collected value contents may be considered "static" * for the duration of the @value lifetime. * Thus an extra copy of the contents stored in @collect_values is * not required for assignment to @value. * For our above string example, we continue with: * |[ * if (!collect_values[0].v_pointer) * value->data[0].v_pointer = g_strdup (""); * else if (collect_flags & G_VALUE_NOCOPY_CONTENTS) * { * value->data[0].v_pointer = collect_values[0].v_pointer; * // keep a flag for the value_free() implementation to not free this string * value->data[1].v_uint = G_VALUE_NOCOPY_CONTENTS; * } * else * value->data[0].v_pointer = g_strdup (collect_values[0].v_pointer); * return NULL; * ]| * It should be noted, that it is generally a bad idea to follow the * #G_VALUE_NOCOPY_CONTENTS hint for reference counted types. Due to * reentrancy requirements and reference count assertions performed * by the #GSignal code, reference counts should always be incremented * for reference counted contents stored in the value->data array. * To deviate from our string example for a moment, and taking a look * at an exemplary implementation for collect_value() of #GObject: * |[ * if (collect_values[0].v_pointer) * { * GObject *object = G_OBJECT (collect_values[0].v_pointer); * // never honour G_VALUE_NOCOPY_CONTENTS for ref-counted types * value->data[0].v_pointer = g_object_ref (object); * return NULL; * } * else * return g_strdup_printf ("Object passed as invalid NULL pointer"); * } * ]| * The reference count for valid objects is always incremented, * regardless of @collect_flags. For invalid objects, the example * returns a newly allocated string without altering @value. * Upon success, collect_value() needs to return %NULL. If, however, * an error condition occurred, collect_value() may spew an * error by returning a newly allocated non-%NULL string, giving * a suitable description of the error condition. * The calling code makes no assumptions about the @value * contents being valid upon error returns, @value * is simply thrown away without further freeing. As such, it is * a good idea to not allocate #GValue contents, prior to returning * an error, however, collect_values() is not obliged to return * a correctly setup @value for error returns, simply because * any non-%NULL return is considered a fatal condition so further * program behaviour is undefined. * @lcopy_format: Format description of the arguments to collect for @lcopy_value, * analogous to @collect_format. Usually, @lcopy_format string consists * only of 'p's to provide lcopy_value() with pointers to storage locations. * @lcopy_value: This function is responsible for storing the @value contents into * arguments passed through a variable argument list which got * collected into @collect_values according to @lcopy_format. * @n_collect_values equals the string length of @lcopy_format, * and @collect_flags may contain %G_VALUE_NOCOPY_CONTENTS. * In contrast to collect_value(), lcopy_value() is obliged to * always properly support %G_VALUE_NOCOPY_CONTENTS. * Similar to collect_value() the function may prematurely abort * by returning a newly allocated string describing an error condition. * To complete the string example: * |[ * gchar **string_p = collect_values[0].v_pointer; * if (!string_p)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -