class GObject::Closure

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:

Defined in:

lib/gi-crystal/src/auto/g_object-2.0/closure.cr

Constructors

Class Method Summary

Instance Method Summary

Constructor Detail

def self.new(pointer : Pointer(Void), transfer : GICrystal::Transfer) #

def self.new(data : LibGObject::Closure, transfer : GICrystal::Transfer) #

def self.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) #

def self.new_object(sizeof_closure : UInt32, object : GObject::Object) : self #

def self.new_simple(sizeof_closure : UInt32, data : Pointer(Void)?) : self #

Class Method Detail

def self.g_type : UInt64 #

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


Instance Method Detail

def ==(other : self) : Bool #
Description copied from class Reference

Returns true if this reference is the same as other. Invokes same?.


def data : Pointer(Void)? #

def data! #

def derivative_flag : UInt32 #

def derivative_flag=(value : UInt32) #

def floating : UInt32 #

def floating=(value : UInt32) #

def in_inotify : UInt32 #

def in_inotify=(value : UInt32) #

def in_marshal : UInt32 #

def in_marshal=(value : UInt32) #

def invalidate : Nil #

def invoke(param_values : Enumerable(_), invocation_hint : Pointer(Void)?) : GObject::Value #

def is_invalid : UInt32 #

def is_invalid=(value : UInt32) #

def marshal : GObject::Marshal #

def marshal=(value : GObject::Marshal) #

def meta_marshal_nouse : UInt32 #

def meta_marshal_nouse=(value : UInt32) #

def n_fnotifiers : UInt32 #

def n_fnotifiers=(value : UInt32) #

def n_guards : UInt32 #

def n_guards=(value : UInt32) #

def n_inotifiers : UInt32 #

def n_inotifiers=(value : UInt32) #

def notifiers : GObject::ClosureNotifyData? #

def notifiers! #

def ref : GObject::Closure #

def ref_count : UInt32 #

def ref_count=(value : UInt32) #

def sink : Nil #

def to_unsafe #

def unref : Nil #