class GObject::Object

Overview

The base object type.

All the fields in the GObject structure are private to the implementation and should never be accessed directly.

Since GLib 2.72, all #GObjects are guaranteed to be aligned to at least the alignment of the largest basic GLib type (typically this is #guint64 or #gdouble). If you need larger alignment for an element in a #GObject, you should allocate it on the heap (aligned), or arrange for your #GObject to be appropriately padded. This guarantee applies to the #GObject (or derived) struct, the #GObjectClass (or derived) struct, and any private data allocated by G_ADD_PRIVATE().

Direct Known Subclasses

Defined in:

auto/g_object-2.0/object.cr

Constructors

Class Method Summary

Instance Method Summary

Constructor Detail

def self.cast(obj : GObject::Object) : self #

Cast a GObject::Object to Object, throw TypeCastError if cast can't be made.


def self.cast?(obj : GObject::Object) : self? #

Cast a GObject::Object to Object, returns nil if cast can't be made.


def self.new #

Initialize a new Object.


def self.newv(object_type : UInt64, parameters : Enumerable(GObject::Parameter)) : self #

Creates a new instance of a #GObject subtype and sets its properties.

Construction parameters (see %G_PARAM_CONSTRUCT, %G_PARAM_CONSTRUCT_ONLY) which are not explicitly specified are set to their default values.


Class Method Detail

def self.compat_control(what : UInt64, data : Pointer(Void)?) : UInt64 #

def self.g_type : UInt64 #

Returns the type id (GType) registered in GLib type system.


def self.interface_find_property(g_iface : GObject::TypeInterface, property_name : String) : GObject::ParamSpec #

Find the #GParamSpec with the given name for an interface. Generally, the interface vtable passed in as @g_iface will be the default vtable from g_type_default_interface_ref(), or, if you know the interface has already been loaded, g_type_default_interface_peek().


def self.interface_list_properties(g_iface : GObject::TypeInterface) : Enumerable(GObject::ParamSpec) #

Lists the properties of an interface.Generally, the interface vtable passed in as @g_iface will be the default vtable from g_type_default_interface_ref(), or, if you know the interface has already been loaded, g_type_default_interface_peek().


Instance Method Detail

def bind_property(source_property : String, target : GObject::Object, target_property : String, flags : GObject::BindingFlags) : GObject::Binding #

Creates a binding between @source_property on @source and @target_property on @target.

Whenever the @source_property is changed the @target_property is updated using the same value. For instance:

|[ g_object_bind_property (action, "active", widget, "sensitive", 0); ]|

Will result in the "sensitive" property of the widget #GObject instance to be updated with the same value of the "active" property of the action #GObject instance.

If @flags contains %G_BINDING_BIDIRECTIONAL then the binding will be mutual: if @target_property on @target changes then the @source_property on @source will be updated as well.

The binding will automatically be removed when either the @source or the @target instances are finalized. To remove the binding without affecting the @source and the @target you can just call g_object_unref() on the returned #GBinding instance.

Removing the binding by calling g_object_unref() on it must only be done if the binding, @source and @target are only used from a single thread and it is clear that both @source and @target outlive the binding. Especially it is not safe to rely on this if the binding, @source or @target can be finalized from different threads. Keep another reference to the binding and use g_binding_unbind() instead to be on the safe side.

A #GObject can have multiple bindings.


def bind_property_full(source_property : String, target : GObject::Object, target_property : String, flags : GObject::BindingFlags, transform_to : GObject::Closure, transform_from : GObject::Closure) : GObject::Binding #

Complete version of g_object_bind_property().

Creates a binding between @source_property on @source and @target_property on @target, allowing you to set the transformation functions to be used by the binding.

If @flags contains %G_BINDING_BIDIRECTIONAL then the binding will be mutual: if @target_property on @target changes then the @source_property on @source will be updated as well. The @transform_from function is only used in case of bidirectional bindings, otherwise it will be ignored

The binding will automatically be removed when either the @source or the @target instances are finalized. This will release the reference that is being held on the #GBinding instance; if you want to hold on to the #GBinding instance, you will need to hold a reference to it.

To remove the binding, call g_binding_unbind().

A #GObject can have multiple bindings.

The same @user_data parameter will be used for both @transform_to and @transform_from transformation functions; the @notify function will be called once, when the binding is removed. If you need different data for each transformation function, please use g_object_bind_property_with_closures() instead.


def data(key : String) : Pointer(Void)? #

Gets a named field from the objects table of associations (see g_object_set_data()).


def finalize #

Called by the garbage collector. Decreases the reference count of object. (i.e. its memory is freed).


def freeze_notify : Nil #

Increases the freeze count on @object. If the freeze count is non-zero, the emission of "notify" signals on @object is stopped. The signals are queued until the freeze count is decreased to zero. Duplicate notifications are squashed so that at most one #GObject::notify signal is emitted for each property modified while the object is frozen.

This is necessary for accessors that modify multiple properties to prevent premature notification while the object is still being modified.


def getv(names : Enumerable(String), values : Enumerable(_)) : Nil #

Gets @n_properties properties for an @object. Obtained properties will be set to @values. All properties must be valid. Warnings will be emitted and undefined behaviour may result if invalid properties are passed in.


def notify(property_name : String) : Nil #

Emits a "notify" signal for the property @property_name on @object.

When possible, eg. when signaling a property change from within the class that registered the property, you should use g_object_notify_by_pspec() instead.

Note that emission of the notify signal may be blocked with g_object_freeze_notify(). In this case, the signal emissions are queued and will be emitted (in reverse order) when g_object_thaw_notify() is called.


def notify_by_pspec(pspec : GObject::ParamSpec) : Nil #

Emits a "notify" signal for the property specified by @pspec on @object.

This function omits the property name lookup, hence it is faster than g_object_notify().

One way to avoid using g_object_notify() from within the class that registered the properties, and using g_object_notify_by_pspec() instead, is to store the GParamSpec used with g_object_class_install_property() inside a static array, e.g.:

|[ enum { PROP_0, PROP_FOO, PROP_LAST };

static GParamSpec *properties[PROP_LAST];

static void my_object_class_init (MyObjectClass *klass) { properties[PROP_FOO] = g_param_spec_int ("foo", "Foo", "The foo", 0, 100, 50, G_PARAM_READWRITE); g_object_class_install_property (gobject_class, PROP_FOO, properties[PROP_FOO]); } ]|

and then notify a change on the "foo" property with:

|[ g_object_notify_by_pspec (self, properties[PROP_FOO]); ]|


def notify_signal #

def property(property_name : String, value : _) : Nil #

Gets a property of an object.

The @value can be:

  • an empty #GValue initialized by %G_VALUE_INIT, which will be automatically initialized with the expected type of the property (since GLib 2.60)
  • a #GValue initialized with the expected type of the property
  • a #GValue initialized with a type to which the expected type of the property can be transformed

In general, a copy is made of the property contents and the caller is responsible for freeing the memory by calling g_value_unset().

Note that g_object_get_property() is really intended for language bindings, g_object_get() is much more convenient for C programming.


def qdata(quark : UInt32) : Pointer(Void)? #

This function gets back user data pointers stored via g_object_set_qdata().


def ref_count #

Returns GObject reference counter.


def run_dispose : Nil #

Releases all references to other objects. This can be used to break reference cycles.

This function should only be called from object system implementations.


def set_data(key : String, data : Pointer(Void)?) : Nil #

Each object carries around a table of associations from strings to pointers. This function lets you set an association.

If the object already had an association with that name, the old association will be destroyed.

Internally, the @key is converted to a #GQuark using g_quark_from_string(). This means a copy of @key is kept permanently (even after @object has been finalized) — so it is recommended to only use a small, bounded set of values for @key in your program, to avoid the #GQuark storage growing unbounded.


def set_property(property_name : String, value : _) : Nil #

Sets a property on an object.


def steal_data(key : String) : Pointer(Void)? #

Remove a specified datum from the object's data associations, without invoking the association's destroy handler.


def steal_qdata(quark : UInt32) : Pointer(Void)? #

This function gets back user data pointers stored via g_object_set_qdata() and removes the @data from object without invoking its destroy() function (if any was set). Usually, calling this function is only required to update user data pointers with a destroy notifier, for example: |[ void object_add_to_user_list (GObject *object, const gchar *new_string) { // the quark, naming the object data GQuark quark_string_list = g_quark_from_static_string ("my-string-list"); // retrieve the old string list GList *list = g_object_steal_qdata (object, quark_string_list);

// prepend new string list = g_list_prepend (list, g_strdup (new_string)); // this changed 'list', so we need to set it again g_object_set_qdata_full (object, quark_string_list, list, free_string_list); } static void free_string_list (gpointer data) { GList *node, *list = data;

for (node = list; node; node = node->next) g_free (node->data); g_list_free (list); } ]| Using g_object_get_qdata() in the above example, instead of g_object_steal_qdata() would have left the destroy function set, and thus the partial string list would have been freed upon g_object_set_qdata_full().


def thaw_notify : Nil #

Reverts the effect of a previous call to g_object_freeze_notify(). The freeze count is decreased on @object and when it reaches zero, queued "notify" signals are emitted.

Duplicate notifications for each property are squashed so that at most one #GObject::notify signal is emitted for each property, in the reverse order in which they have been queued.

It is an error to call this function when the freeze count is zero.


def to_unsafe : Pointer(Void) #

Returns a pointer to the C object.


def watch_closure(closure : GObject::Closure) : Nil #

This function essentially limits the life time of the @closure to the life time of the object. That is, when the object is finalized, the @closure is invalidated by calling g_closure_invalidate() on it, in order to prevent invocations of the closure with a finalized (nonexisting) object. Also, g_object_ref() and g_object_unref() are added as marshal guards to the @closure, to ensure that an extra reference count is held on @object during invocation of the @closure. Usually, this function will be called on closures that use this @object as closure data.