📄 objects.sgml
字号:
<!-- ##### SECTION Title ##### -->GObject<!-- ##### SECTION Short_Description ##### -->The base object type<!-- ##### SECTION Long_Description ##### --><para>GObject is the fundamental type providing the common attributes and methods for all object types in GTK+, Pango and other libraries based on GObject. The GObject class provides methods for object construction and destruction, property access methods, and signal support. Signals are described in detail in <xref linkend="gobject-Signals"/>.</para><para id="floating-ref">#GInitiallyUnowned is derived from #GObject. The only difference betweenthe two is that the initial reference of a #GInitiallyUnowned is flagged as a <firstterm>floating</firstterm> reference.This means that it is not specifically claimed to be "owned" byany code portion. The main motivation for providing floating references isC convenience. In particular, it allows code to be written as:<example><programlisting> container = create_container(); container_add_child (container, create_child());</programlisting></example>If <function>container_add_child()</function> will g_object_ref_sink() thepassed in child, no reference of the newly created child is leaked.Without floating references, <function>container_add_child()</function>can only g_object_ref() the new child, so to implement this code withoutreference leaks, it would have to be written as:<example><programlisting> Child *child; container = create_container(); child = create_child(); container_add_child (container, child); g_object_unref (child);</programlisting></example>The floating reference can be converted into an ordinary reference by calling g_object_ref_sink().For already sunken objects (objects that don't have a floating referenceanymore), g_object_ref_sink() is equivalent to g_object_ref() and returnsa new reference.Since floating references are useful almost exclusively for C convenience,language bindings that provide automated reference and memory ownershipmaintenance (such as smart pointers or garbage collection) therefore don'tneed to expose floating references in their API.</para><para>Some object implementations may need to save an objects floating stateacross certain code portions (an example is #GtkMenu), to achive this, thefollowing sequence can be used:</para><example><programlisting> /* save floating state */ gboolean was_floating = g_object_is_floating (object); g_object_ref_sink (object); /* protected code portion */ ...; /* restore floating state */ if (was_floating) g_object_force_floating (object); g_obejct_unref (object); /* release previously acquired reference */</programlisting></example><!-- ##### SECTION See_Also ##### --><para>#GParamSpecObject, g_param_spec_object()</para><!-- ##### SECTION Stability_Level ##### --><!-- ##### STRUCT GObject ##### --><para>All the fields in the <structname>GObject</structname> structure are private to the #GObject implementation and should never be accessed directly.</para><!-- ##### SIGNAL GObject::notify ##### --><para>The notify signal is emitted on an object when one of its properties has been changed. Note that getting this signal doesn't guarantee that thevalue of the property has actually changed, it may also be emitted whenthe setter for the property is called to reinstate the previous value.</para><para>This signal is typically used to obtain change notification for a single property, by specifying the property name as a detail in theg_signal_connect() call, like this:<informalexample><programlisting>g_signal_connect (text_view->buffer, "notify::paste-target-list", G_CALLBACK (gtk_text_view_target_list_notify), text_view)</programlisting></informalexample>It is important to note that you must use <link linkend="canonical-parameter-name">canonical</link> parameter names asdetail strings for the notify signal.</para>@pspec: the #GParamSpec of the property which changed@gobject: the object which received the signal.<!-- ##### STRUCT GObjectClass ##### --><para>The class structure for the <structname>GObject</structname> type.</para><example><title>Implementing singletons using a constructor</title><programlisting>static MySingleton *the_singleton = NULL;static GObject*my_singleton_constructor (GType type, guint n_construct_params, GObjectConstructParam *construct_params){ GObject *object; if (!the_singleton) { object = G_OBJECT_CLASS (parent_class)->constructor (type, n_construct_params, construct_params); the_singleton = MY_SINGLETON (object); } else object = g_object_ref (G_OBJECT (the_singleton)); return object;}</programlisting></example>@g_type_class: the parent class@constructor: the @constructor function is called by g_object_new () to complete the object initialization after all the construction properties are set. The first thing a @constructor implementation must do is chain up to the @constructor of the parent class. Overriding @constructor should be rarely needed, e.g. to handle construct properties, or to implement singletons.@set_property: the generic setter for all properties of this type. Should be overridden for every type with properties. Implementations of @set_property don't need to emit property change notification explicitly, this is handled by the type system.@get_property: the generic getter for all properties of this type. Should be overridden for every type with properties.@dispose: the @dispose function is supposed to drop all references to other objects, but keep the instance otherwise intact, so that client method invocations still work. It may be run multiple times (due to reference loops). Before returning, @dispose should chain up to the @dispose method of the parent class.@finalize: instance finalization function, should finish the finalization of the instance begun in @dispose and chain up to the @finalize method of the parent class.@dispatch_properties_changed: emits property change notification for a bunch of properties. Overriding @dispatch_properties_changed should be rarely needed.@notify: the class closure for the notify signal@constructed: the @constructed function is called by g_object_new() as the final step of the object creation process. At the point of the call, all construction properties have been set on the object. The purpose of this call is to allow for object initialisation steps that can only be performed after construction properties have been set. @constructed implementors should chain up to the @constructed call of their parent class to allow it to complete its initialisation.<!-- ##### STRUCT GObjectConstructParam ##### --><para>The <structname>GObjectConstructParam</structname> struct is an auxiliary structure used to hand #GParamSpec/#GValue pairs to the @constructor ofa #GObjectClass.</para>@pspec: the #GParamSpec of the construct parameter@value: the value to set the parameter to<!-- ##### USER_FUNCTION GObjectGetPropertyFunc ##### --><para>The type of the @get_property function of #GObjectClass. </para>@object: a #GObject@property_id: the numeric id under which the property was registered with g_object_class_install_property().@value: a #GValue to return the property value in@pspec: the #GParamSpec describing the property<!-- ##### USER_FUNCTION GObjectSetPropertyFunc ##### --><para>The type of the @set_property function of #GObjectClass. </para>@object: a #GObject@property_id: the numeric id under which the property was registered with g_object_class_install_property().@value: the new value for the property@pspec: the #GParamSpec describing the property<!-- ##### USER_FUNCTION GObjectFinalizeFunc ##### --><para>The type of the @finalize function of #GObjectClass.</para>@object: the #GObject being finalized<!-- ##### MACRO G_TYPE_IS_OBJECT ##### --><para>Returns a boolean value of %FALSE or %TRUE indicating whetherthe passed in type id is a %G_TYPE_OBJECT or derived from it.</para>@type: Type id to check for is a %G_TYPE_OBJECT relationship.@Returns: %FALSE or %TRUE, indicating whether @type is a %G_TYPE_OBJECT.<!-- ##### MACRO G_OBJECT ##### --><para>Casts a #GObject or derived pointer into a (GObject*) pointer.Depending on the current debugging level, this function may invokecertain runtime checks to identify invalid casts.</para>@object: Object which is subject to casting.<!-- ##### MACRO G_IS_OBJECT ##### --><para>Checks whether a valid #GTypeInstance pointer is of type %G_TYPE_OBJECT.</para>@object: Instance to check for being a %G_TYPE_OBJECT.<!-- ##### MACRO G_OBJECT_CLASS ##### --><para>Casts a derived #GObjectClass structure into a #GObjectClass structure.</para>@class: a valid #GObjectClass<!-- ##### MACRO G_IS_OBJECT_CLASS ##### --><para>Checks whether @class "is a" valid #GObjectClass structure of type%G_TYPE_OBJECT or derived.</para>@class: a #GObjectClass<!-- ##### MACRO G_OBJECT_GET_CLASS ##### --><para>Returns the class structure associated to a #GObject instance.</para>@object: a #GObject instance.<!-- ##### MACRO G_OBJECT_TYPE ##### --><para>Return the type id of an object.</para>@object: Object to return the type id for.@Returns: Type id of @object.<!-- ##### MACRO G_OBJECT_TYPE_NAME ##### --><para>Returns the name of an object's type.</para>@object: Object to return the type name for.@Returns: Type name of @object. The string is owned by the type system and should not be freed.<!-- ##### MACRO G_OBJECT_CLASS_TYPE ##### --><para>Return the type id of a class structure.</para>@class: a valid #GObjectClass@Returns: Type id of @class.<!-- ##### MACRO G_OBJECT_CLASS_NAME ##### --><para>Return the name of a class structure's type.</para>@class: a valid #GObjectClass@Returns: Type name of @class. The string is owned by the type system and should not be freed.<!-- ##### FUNCTION g_object_class_install_property ##### --><para>Installs a new property. This is usually done in the class initializer.</para>@oclass: a #GObjectClass@property_id: the id for the new property@pspec: the #GParamSpec for the new property<!-- ##### FUNCTION g_object_class_find_property ##### --><para>Looks up the #GParamSpec for a property of a class.</para>@oclass: a #GObjectClass@property_name: the name of the property to look up@Returns: the #GParamSpec for the property, or %NULL if the class doesn't havea property of that name<!-- ##### FUNCTION g_object_class_list_properties ##### --><para>Returns an array of #GParamSpec* for all properties of a class.</para>@oclass: a #GObjectClass@n_properties: return location for the length of the returned array@Returns: an array of #GParamSpec* which should be freed after use<!-- ##### FUNCTION g_object_class_override_property ##### --><para>Registers @property_id as referring to a property with thename @name in a parent class or in an interface implementedby @oclass. This allows this class to <firstterm>override</firstterm>a property implementation in a parent class or to providethe implementation of a property from an interface.</para><note><para>Internally, overriding is implemented by creating a property of type#GParamSpecOverride; generally operations that query the properties ofthe object class, such as g_object_class_find_property() org_object_class_list_properties() will return the overriddenproperty. However, in one case, the @construct_properties argument ofthe @constructor virtual function, the #GParamSpecOverride is passedinstead, so that the @param_id field of the #GParamSpec will becorrect. For virtually all uses, this makes no difference. If youneed to get the overridden property, you can callg_param_spec_get_redirect_target().</para></note>@oclass: a #GObjectClass@property_id: the new property ID@name: the name of a property registered in a parent class or in an interface of this class.@Since: 2.4<!-- ##### FUNCTION g_object_interface_install_property ##### --><para>Add a property to an interface; this is only useful for interfacesthat are added to GObject-derived types. Adding a property to aninterface forces all objects classes with that interface to have acompatible property. The compatible property could be a newlycreated #GParamSpec, but normallyg_object_class_override_property() will be used so that the objectclass only needs to provide an implementation and inherits theproperty description, default value, bounds, and so forth from theinterface property.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -