GObject Reference Manual |
---|
The signal system which was built in GType is pretty complex and flexible: it is possible for its users to connect at runtime any number of callbacks (implemented in any language for which a binding exists) [17] to any signal and to stop the emission of any signal at any state of the signal emission process. This flexibility makes it possible to use GSignal for much more than just emit signals which can be received by numerous clients.
The most basic use of signals is to implement simple event notification: for example, if we have a MamanFile object, and if this object has a write method, we might wish to be notified whenever someone has changed something via our MamanFile instance. The code below shows how the user can connect a callback to the "changed" signal.
file = g_object_new (MAMAN_FILE_TYPE, NULL); g_signal_connect (file, "changed", G_CALLBACK (changed_event), NULL); maman_file_write (file, buffer, strlen (buffer));
The MamanFile signal is registered in the class_init function:
file_signals[CHANGED] = g_signal_newv ("changed", G_TYPE_FROM_CLASS (gobject_class), G_SIGNAL_RUN_LAST | G_SIGNAL_NO_RECURSE | G_SIGNAL_NO_HOOKS, NULL /* closure */, NULL /* accumulator */, NULL /* accumulator data */, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE /* return_type */, 0 /* n_params */, NULL /* param_types */);
and the signal is emitted in maman_file_write
:
void maman_file_write (MamanFile *self, const guchar *buffer, gssize size) { /* First write data. */ /* Then, notify user of data written. */ g_signal_emit (self, file_signals[CHANGED], 0 /* details */); }
As shown above, you can safely set the details parameter to zero if you do not know what it can be used for. For a discussion of what you could used it for, see the section called “The detail argument”
The signature of the signal handler in the above example is defined as
g_cclosure_marshal_VOID__VOID
. Its name follows
a simple convention which encodes the function parameter and return value
types in the function name. Specifically, the value in front of the
double underscore is the type of the return value, while the value(s)
after the double underscore denote the parameter types.
The header gobject/gmarshal.h
defines a set of
commonly needed closures that one can use. If you want to have complex
marshallers for your signals you should probably use glib-genmarshal
to autogenerate them from a file containing their return and
parameter types.