2 * This file is part of gtkD.
4 * gtkD is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License as published by
6 * the Free Software Foundation; either version 2.1 of the License, or
7 * (at your option) any later version.
9 * gtkD is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public License
15 * along with gtkD; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 // generated automatically - do not change
20 // find conversion definition on APILookup.txt
21 // implement new conversion functionalities on the wrap.utils pakage
24 * Conversion parameters:
25 * inFile = gobject-Closures.html
50 * - GClosure* -> Closure
51 * - GObject* -> ObjectG
52 * - GSource* -> Source
58 module gobject
.Closure
;
64 import tango
.io
.Stdout
; // use the tango loging?
68 private import gtkc
.gobjecttypes
;
70 private import gtkc
.gobject
;
73 private import gobject
.ObjectG
;
74 private import gobject
.Closure
;
75 private import gobject
.Value
;
76 private import glib
.Source
;
83 * A GClosure represents a callback supplied by the programmer. It will generally
84 * comprise a function of some kind and a marshaller used to call it. It is the
85 * reponsibility of the marshaller to convert the arguments for the invocation
86 * from GValues into a suitable form, perform the callback on the
87 * converted arguments, and transform the return value back into a GValue.
88 * In the case of C programs, a closure usually just holds a pointer to a function
89 * and maybe a data argument, and the marshaller converts between GValue
90 * and native C types. The GObject library provides the GCClosure type for this
91 * purpose. Bindings for other languages need marshallers which
92 * convert between GValues and suitable representations in the runtime
93 * of the language in order to use functions written in that languages as
95 * Within GObject, closures play an important role in the implementation of
96 * signals. When a signal is registered, the c_marshaller argument to
97 * g_signal_new() specifies the default C marshaller for any closure which is
98 * connected to this signal. GObject provides a number of C marshallers
99 * for this purpose, see the g_cclosure_marshal_*() functions. Additional
100 * C marshallers can be generated with the glib-genmarshal utility.
101 * Closures can be explicitly connected to signals with
102 * g_signal_connect_closure(), but it usually more convenient to let GObject
103 * create a closure automatically by using one of the g_signal_connect_*()
104 * functions which take a callback function/user data pair.
105 * Using closures has a number of important advantages over a simple
106 * callback function/data pointer combination:
107 * Closures allow the callee to get the types of the callback parameters,
108 * which means that language bindings don't have to write individual glue
109 * for each callback type.
110 * The reference counting of GClosure makes it easy to handle reentrancy
111 * right; if a callback is removed while it is being invoked, the closure
112 * and it's parameters won't be freed until the invocation finishes.
113 * g_closure_invalidate() and invalidation notifiers allow callbacks to be
114 * automatically removed when the objects they point to go away.
119 /** the main Gtk struct */
120 protected GClosure
* gClosure
;
123 public GClosure
* getClosureStruct()
129 /** the main Gtk struct as a void* */
130 protected void* getStruct()
132 return cast(void*)gClosure
;
136 * Sets our main struct and passes it to the parent class
138 public this (GClosure
* gClosure
)
142 if ( gClosure
is null )
147 Stdout("struct gClosure is null on constructor").newline
;
151 printf("struct gClosure is null on constructor");
158 assert(gClosure
!is null, "struct gClosure is null on constructor");
160 this.gClosure
= gClosure
;
181 * A variant of g_closure_new_simple() which stores object in the data
182 * field of the closure and calls g_object_watch_closure() on object and the
183 * created closure. This function is mainly useful when implementing new types
186 * the size of the structure to allocate, must be at least
189 * a GObject pointer to store in the data field of the newly
192 * a newly allocated GClosure
194 public this (uint sizeofClosure
, ObjectG object
)
196 // GClosure* g_closure_new_object (guint sizeof_closure, GObject *object);
197 this(cast(GClosure
*)g_closure_new_object(sizeofClosure
, (object
is null) ?
null : object
.getObjectGStruct()) );
201 * Increments the reference count on a closure to force it staying
202 * alive while the caller holds a pointer to it.
204 * GClosure to increment the reference count on
206 * The closure passed in, for convenience
208 public Closure
doref()
210 // GClosure* g_closure_ref (GClosure *closure);
211 return new Closure( g_closure_ref(gClosure
) );
215 * Takes over the initial ownership of a closure.
216 * Each closure is initially created in afloating state,
217 * which means that the initial reference count is not owned by any caller.
218 * g_closure_sink() checks to see if the object is still floating, and if so,
219 * unsets the floating state and decreases the reference count. If the closure
220 * is not floating, g_closure_sink() does nothing. The reason for the existance
221 * of the floating state is to prevent cumbersome code sequences like:
222 * closure = g_cclosure_new (cb_func, cb_data);
223 * g_source_set_closure (source, closure);
224 * g_closure_unref (closure); /+* XXX GObject doesn't really need this +/
225 * Because g_source_set_closure() (and similar functions) take ownership of the
226 * initial reference count, if it is unowned, we instead can write:
227 * g_source_set_closure (source, g_cclosure_new (cb_func, cb_data));
228 * Generally, this function is used together with g_closure_ref(). Ane example
229 * of storing a closure for later notification looks like:
230 * static GClosure *notify_closure = NULL;
232 * foo_notify_set_closure (GClosure *closure)
234 * if (notify_closure)
235 * g_closure_unref (notify_closure);
236 * notify_closure = closure;
237 * if (notify_closure)
239 * g_closure_ref (notify_closure);
240 * g_closure_sink (notify_closure);
243 * Because g_closure_sink() may decrement the reference count of a closure
244 * (if it hasn't been called on closure yet) just like g_closure_unref(),
245 * g_closure_ref() should be called prior to this function.
247 * GClosure to decrement the initial reference count on, if it's
252 // void g_closure_sink (GClosure *closure);
253 g_closure_sink(gClosure
);
257 * Decrements the reference count of a closure after it was previously
258 * incremented by the same caller. If no other callers are using the closure,
259 * then the closure will be destroyed and freed.
261 * GClosure to decrement the reference count on
265 // void g_closure_unref (GClosure *closure);
266 g_closure_unref(gClosure
);
270 * Invokes the closure, i.e. executes the callback represented by the closure.
274 * a GValue to store the return value. May be NULL if the
275 * callback of closure doesn't return a value.
277 * the length of the param_values array
279 * an array of GValues holding the arguments on
280 * which to invoke the callback of closure
282 * a context-dependent invocation hint
284 public void invoke(Value returnValue
, uint nParamValues
, Value paramValues
, void* invocationHint
)
286 // void g_closure_invoke (GClosure *closure, GValue *return_value, guint n_param_values, const GValue *param_values, gpointer invocation_hint);
287 g_closure_invoke(gClosure
, (returnValue
is null) ?
null : returnValue
.getValueStruct(), nParamValues
, (paramValues
is null) ?
null : paramValues
.getValueStruct(), invocationHint
);
291 * Sets a flag on the closure to indicate that it's calling environment has
292 * become invalid, and thus causes any future invocations of g_closure_invoke()
293 * on this closure to be ignored. Also, invalidation notifiers installed on
294 * the closure will be called at this point. Note that unless you are holding
295 * a reference to the closure yourself, the invalidation notifiers may unref
296 * the closure and cause it to be destroyed, so if you need to access the
297 * closure after calling g_closure_invalidate(), make sure that you've
298 * previously called g_closure_ref().
299 * Note that g_closure_invalidate() will also be called when the reference count
300 * of a closure drops to zero (unless it has already been invalidated before).
302 * GClosure to invalidate
304 public void invalidate()
306 // void g_closure_invalidate (GClosure *closure);
307 g_closure_invalidate(gClosure
);
311 * Registers a finalization notifier which will be called when the reference
312 * count of closure goes down to 0. Multiple finalization notifiers on a
313 * single closure are invoked in unspecified order. If a single call to
314 * g_closure_unref() results in the closure being both invalidated and
315 * finalized, then the invalidate notifiers will be run before the finalize
320 * data to pass to notify_func
322 * the callback function to register
324 public void addFinalizeNotifier(void* notifyData
, GClosureNotify notifyFunc
)
326 // void g_closure_add_finalize_notifier (GClosure *closure, gpointer notify_data, GClosureNotify notify_func);
327 g_closure_add_finalize_notifier(gClosure
, notifyData
, notifyFunc
);
331 * Registers an invalidation notifier which will be called when the closure
332 * is invalidated with g_closure_invalidate(). Invalidation notifiers are
333 * invoked before finalization notifiers, in an unspecified order.
337 * data to pass to notify_func
339 * the callback function to register
341 public void addInvalidateNotifier(void* notifyData
, GClosureNotify notifyFunc
)
343 // void g_closure_add_invalidate_notifier (GClosure *closure, gpointer notify_data, GClosureNotify notify_func);
344 g_closure_add_invalidate_notifier(gClosure
, notifyData
, notifyFunc
);
348 * Removes a finalization notifier. Notifiers are automatically removed after
353 * data which was passed to g_closure_add_finalize_notifier()
354 * when registering notify_func
356 * the callback function to remove
358 public void removeFinalizeNotifier(void* notifyData
, GClosureNotify notifyFunc
)
360 // void g_closure_remove_finalize_notifier (GClosure *closure, gpointer notify_data, GClosureNotify notify_func);
361 g_closure_remove_finalize_notifier(gClosure
, notifyData
, notifyFunc
);
365 * Removes a invalidation notifier. Notifiers are automatically removed after
370 * data which was passed to g_closure_add_invalidate_notifier()
371 * when registering notify_func
373 * the callback function to remove
375 public void removeInvalidateNotifier(void* notifyData
, GClosureNotify notifyFunc
)
377 // void g_closure_remove_invalidate_notifier (GClosure *closure, gpointer notify_data, GClosureNotify notify_func);
378 g_closure_remove_invalidate_notifier(gClosure
, notifyData
, notifyFunc
);
382 * Allocates a struct of the given size and initializes the initial part
383 * as a GClosure. This function is mainly useful when implementing new types
385 * typedef struct _MyClosure MyClosure;
389 * /+* extra data goes here +/
392 * my_closure_finalize (gpointer notify_data,
395 * MyClosure *my_closure = (MyClosure *)closure;
396 * /+* free extra data here +/
398 * MyClosure *my_closure_new (gpointer data)
401 * MyClosure *my_closure;
402 * closure = g_closure_new_simple (sizeof (MyClosure), data);
403 * my_closure = (MyClosure *) closure;
404 * / initialize extra data here +/
405 * g_closure_add_finalize_notifier (closure, notify_data,
406 * my_closure_finalize);
410 * the size of the structure to allocate, must be at least
413 * data to store in the data field of the newly allocated GClosure
415 * a newly allocated GClosure
417 public this (uint sizeofClosure
, void* data
)
419 // GClosure* g_closure_new_simple (guint sizeof_closure, gpointer data);
420 this(cast(GClosure
*)g_closure_new_simple(sizeofClosure
, data
) );
424 * Sets the marshaller of closure. The marshal_data provides a way for a
425 * meta marshaller to provide additional information to the marshaller.
426 * (See g_closure_set_meta_marshal().) For GObject's C predefined marshallers
427 * (the g_cclosure_marshal_*() functions), what it provides is a callback
428 * function to use instead of closure->callback.
432 * a GClosureMarshal function
434 public void setMarshal(GClosureMarshal marshal
)
436 // void g_closure_set_marshal (GClosure *closure, GClosureMarshal marshal);
437 g_closure_set_marshal(gClosure
, marshal
);
441 * Adds a pair of notifiers which get invoked before and after the closure
442 * callback, respectively. This is typically used to protect the extra arguments
443 * for the duration of the callback. See g_object_watch_closure() for an
444 * example of marshal guards.
448 * data to pass to pre_marshal_notify
449 * pre_marshal_notify:
450 * a function to call before the closure callback
452 * data to pass to post_marshal_notify
453 * post_marshal_notify:
454 * a function to call after the closure callback
456 public void addMarshalGuards(void* preMarshalData
, GClosureNotify preMarshalNotify
, void* postMarshalData
, GClosureNotify postMarshalNotify
)
458 // void g_closure_add_marshal_guards (GClosure *closure, gpointer pre_marshal_data, GClosureNotify pre_marshal_notify, gpointer post_marshal_data, GClosureNotify post_marshal_notify);
459 g_closure_add_marshal_guards(gClosure
, preMarshalData
, preMarshalNotify
, postMarshalData
, postMarshalNotify
);
463 * Sets the meta marshaller of closure.
464 * A meta marshaller wraps closure->marshal and modifies the way it is called
465 * in some fashion. The most common use of this facility is for C callbacks.
466 * The same marshallers (generated by
467 * glib-genmarshal) are used everywhere,
468 * but the way that we get the callback function differs. In most cases we want
469 * to use closure->callback, but in other cases we want to use use some
470 * different technique to retrieve the callbakc function.
471 * For example, class closures for signals (see g_signal_type_cclosure_new())
472 * retrieve the callback function from a fixed offset in the class structure.
473 * The meta marshaller retrieves the right callback and passes it to the
474 * marshaller as the marshal_data argument.
478 * context-dependent data to pass to meta_marshal
480 * a GClosureMarshal function
482 public void setMetaMarshal(void* marshalData
, GClosureMarshal metaMarshal
)
484 // void g_closure_set_meta_marshal (GClosure *closure, gpointer marshal_data, GClosureMarshal meta_marshal);
485 g_closure_set_meta_marshal(gClosure
, marshalData
, metaMarshal
);
489 * Set the callback for a source as a GClosure.
490 * If the source is not one of the standard GLib types, the closure_callback
491 * and closure_marshal fields of the GSourceFuncs structure must have been
492 * filled in with pointers to appropriate functions.
498 public static void gSourceSetClosure(Source source
, Closure closure
)
500 // void g_source_set_closure (GSource *source, GClosure *closure);
501 g_source_set_closure((source
is null) ?
null : source
.getSourceStruct(), (closure
is null) ?
null : closure
.getClosureStruct());