class GObject::Closure
- GObject::Closure
- Reference
- Object
Overview
A #GClosure represents a callback supplied by the programmer.
It will generally comprise a function of some kind and a marshaller used to call it. It is the responsibility of the marshaller to convert the arguments for the invocation from #GValues into a suitable form, perform the callback on the converted arguments, and transform the return value back into a #GValue.
In the case of C programs, a closure usually just holds a pointer to a function and maybe a data argument, and the marshaller converts between #GValue and native C types. The GObject library provides the #GCClosure type for this purpose. Bindings for other languages need marshallers which convert between #GValues and suitable representations in the runtime of the language in order to use functions written in that language as callbacks. Use g_closure_set_marshal() to set the marshaller on such a custom closure implementation.
Within GObject, closures play an important role in the implementation of signals. When a signal is registered, the @c_marshaller argument to g_signal_new() specifies the default C marshaller for any closure which is connected to this signal. GObject provides a number of C marshallers for this purpose, see the g_cclosure_marshal_() functions. Additional C marshallers can be generated with the [glib-genmarshal][glib-genmarshal] utility. Closures can be explicitly connected to signals with g_signal_connect_closure(), but it usually more convenient to let GObject create a closure automatically by using one of the g_signal_connect_() functions which take a callback function/user data pair.
Using closures has a number of important advantages over a simple callback function/data pointer combination:
-
Closures allow the callee to get the types of the callback parameters, which means that language bindings don't have to write individual glue for each callback type.
-
The reference counting of #GClosure makes it easy to handle reentrancy right; if a callback is removed while it is being invoked, the closure and its parameters won't be freed until the invocation finishes.
-
g_closure_invalidate() and invalidation notifiers allow callbacks to be automatically removed when the objects they point to go away.
Defined in:
lib/gi-crystal/src/auto/g_object-2.0/closure.crConstructors
- .new(pointer : Pointer(Void), transfer : GICrystal::Transfer)
- .new(ref_count : UInt32? = nil, meta_marshal_nouse : UInt32? = nil, n_guards : UInt32? = nil, n_fnotifiers : UInt32? = nil, n_inotifiers : UInt32? = nil, in_inotify : UInt32? = nil, floating : UInt32? = nil, derivative_flag : UInt32? = nil, in_marshal : UInt32? = nil, is_invalid : UInt32? = nil, marshal : GObject::Marshal? = nil, data : Pointer(Void)? = nil, notifiers : GObject::ClosureNotifyData? = nil)
- .new_object(sizeof_closure : UInt32, object : GObject::Object) : self
- .new_simple(sizeof_closure : UInt32, data : Pointer(Void)?) : self
Class Method Summary
-
.g_type : UInt64
Returns the type id (GType) registered in GLib type system.
Instance Method Summary
-
#==(other : self) : Bool
Returns
true
if this reference is the same as other. - #data : Pointer(Void)?
- #data!
- #data=(value : Pointer(Void)?)
- #derivative_flag : UInt32
- #derivative_flag=(value : UInt32)
- #floating : UInt32
- #floating=(value : UInt32)
- #in_inotify : UInt32
- #in_inotify=(value : UInt32)
- #in_marshal : UInt32
- #in_marshal=(value : UInt32)
- #invalidate : Nil
- #invoke(param_values : Enumerable(_), invocation_hint : Pointer(Void)?) : GObject::Value
- #is_invalid : UInt32
- #is_invalid=(value : UInt32)
- #marshal : GObject::Marshal
- #marshal=(value : GObject::Marshal)
- #meta_marshal_nouse : UInt32
- #meta_marshal_nouse=(value : UInt32)
- #n_fnotifiers : UInt32
- #n_fnotifiers=(value : UInt32)
- #n_guards : UInt32
- #n_guards=(value : UInt32)
- #n_inotifiers : UInt32
- #n_inotifiers=(value : UInt32)
- #notifiers : GObject::ClosureNotifyData?
- #notifiers!
- #notifiers=(value : GObject::ClosureNotifyData?)
- #ref : GObject::Closure
- #ref_count : UInt32
- #ref_count=(value : UInt32)
- #sink : Nil
- #to_unsafe
- #unref : Nil
Constructor Detail
Class Method Detail
Instance Method Detail
Returns true
if this reference is the same as other. Invokes same?
.