1 /*
2  * This file is part of gtkD.
3  *
4  * gtkD is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License
6  * as published by the Free Software Foundation; either version 3
7  * of the License, or (at your option) any later version, with
8  * some exceptions, please read the COPYING file.
9  *
10  * gtkD is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with gtkD; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA
18  */
19 
20 // generated automatically - do not change
21 // find conversion definition on APILookup.txt
22 // implement new conversion functionalities on the wrap.utils pakage
23 
24 
25 module gobject.Closure;
26 
27 private import glib.ConstructionException;
28 private import glib.Source;
29 private import gobject.ObjectG;
30 private import gobject.Value;
31 private import gtkc.gobject;
32 public  import gtkc.gobjecttypes;
33 
34 
35 /**
36  * A #GClosure represents a callback supplied by the programmer. It
37  * will generally comprise a function of some kind and a marshaller
38  * used to call it. It is the responsibility of the marshaller to
39  * convert the arguments for the invocation from #GValues into
40  * a suitable form, perform the callback on the converted arguments,
41  * and transform the return value back into a #GValue.
42  * 
43  * In the case of C programs, a closure usually just holds a pointer
44  * to a function and maybe a data argument, and the marshaller
45  * converts between #GValue and native C types. The GObject
46  * library provides the #GCClosure type for this purpose. Bindings for
47  * other languages need marshallers which convert between #GValue<!--
48  * -->s and suitable representations in the runtime of the language in
49  * order to use functions written in that languages as callbacks.
50  * 
51  * Within GObject, closures play an important role in the
52  * implementation of signals. When a signal is registered, the
53  * @c_marshaller argument to g_signal_new() specifies the default C
54  * marshaller for any closure which is connected to this
55  * signal. GObject provides a number of C marshallers for this
56  * purpose, see the g_cclosure_marshal_*() functions. Additional C
57  * marshallers can be generated with the [glib-genmarshal][glib-genmarshal]
58  * utility.  Closures can be explicitly connected to signals with
59  * g_signal_connect_closure(), but it usually more convenient to let
60  * GObject create a closure automatically by using one of the
61  * g_signal_connect_*() functions which take a callback function/user
62  * data pair.
63  * 
64  * Using closures has a number of important advantages over a simple
65  * callback function/data pointer combination:
66  * 
67  * - Closures allow the callee to get the types of the callback parameters,
68  * which means that language bindings don't have to write individual glue
69  * for each callback type.
70  * 
71  * - The reference counting of #GClosure makes it easy to handle reentrancy
72  * right; if a callback is removed while it is being invoked, the closure
73  * and its parameters won't be freed until the invocation finishes.
74  * 
75  * - g_closure_invalidate() and invalidation notifiers allow callbacks to be
76  * automatically removed when the objects they point to go away.
77  */
78 public class Closure
79 {
80 	/** the main Gtk struct */
81 	protected GClosure* gClosure;
82 	protected bool ownedRef;
83 
84 	/** Get the main Gtk struct */
85 	public GClosure* getClosureStruct()
86 	{
87 		return gClosure;
88 	}
89 
90 	/** the main Gtk struct as a void* */
91 	protected void* getStruct()
92 	{
93 		return cast(void*)gClosure;
94 	}
95 
96 	/**
97 	 * Sets our main struct and passes it to the parent class.
98 	 */
99 	public this (GClosure* gClosure, bool ownedRef = false)
100 	{
101 		this.gClosure = gClosure;
102 		this.ownedRef = ownedRef;
103 	}
104 
105 
106 	/** */
107 	public static GType getType()
108 	{
109 		return g_closure_get_type();
110 	}
111 
112 	/**
113 	 * A variant of g_closure_new_simple() which stores @object in the
114 	 * @data field of the closure and calls g_object_watch_closure() on
115 	 * @object and the created closure. This function is mainly useful
116 	 * when implementing new types of closures.
117 	 *
118 	 * Params:
119 	 *     sizeofClosure = the size of the structure to allocate, must be at least
120 	 *         `sizeof (GClosure)`
121 	 *     object = a #GObject pointer to store in the @data field of the newly
122 	 *         allocated #GClosure
123 	 *
124 	 * Return: a newly allocated #GClosure
125 	 *
126 	 * Throws: ConstructionException GTK+ fails to create the object.
127 	 */
128 	public this(uint sizeofClosure, ObjectG object)
129 	{
130 		auto p = g_closure_new_object(sizeofClosure, (object is null) ? null : object.getObjectGStruct());
131 		
132 		if(p is null)
133 		{
134 			throw new ConstructionException("null returned by new_object");
135 		}
136 		
137 		this(cast(GClosure*) p);
138 	}
139 
140 	/**
141 	 * Allocates a struct of the given size and initializes the initial
142 	 * part as a #GClosure. This function is mainly useful when
143 	 * implementing new types of closures.
144 	 *
145 	 * |[<!-- language="C" -->
146 	 * typedef struct _MyClosure MyClosure;
147 	 * struct _MyClosure
148 	 * {
149 	 * GClosure closure;
150 	 * // extra data goes here
151 	 * };
152 	 *
153 	 * static void
154 	 * my_closure_finalize (gpointer  notify_data,
155 	 * GClosure *closure)
156 	 * {
157 	 * MyClosure *my_closure = (MyClosure *)closure;
158 	 *
159 	 * // free extra data here
160 	 * }
161 	 *
162 	 * MyClosure *my_closure_new (gpointer data)
163 	 * {
164 	 * GClosure *closure;
165 	 * MyClosure *my_closure;
166 	 *
167 	 * closure = g_closure_new_simple (sizeof (MyClosure), data);
168 	 * my_closure = (MyClosure *) closure;
169 	 *
170 	 * // initialize extra data here
171 	 *
172 	 * g_closure_add_finalize_notifier (closure, notify_data,
173 	 * my_closure_finalize);
174 	 * return my_closure;
175 	 * }
176 	 * ]|
177 	 *
178 	 * Params:
179 	 *     sizeofClosure = the size of the structure to allocate, must be at least
180 	 *         `sizeof (GClosure)`
181 	 *     data = data to store in the @data field of the newly allocated #GClosure
182 	 *
183 	 * Return: a newly allocated #GClosure
184 	 *
185 	 * Throws: ConstructionException GTK+ fails to create the object.
186 	 */
187 	public this(uint sizeofClosure, void* data)
188 	{
189 		auto p = g_closure_new_simple(sizeofClosure, data);
190 		
191 		if(p is null)
192 		{
193 			throw new ConstructionException("null returned by new_simple");
194 		}
195 		
196 		this(cast(GClosure*) p);
197 	}
198 
199 	/**
200 	 * Registers a finalization notifier which will be called when the
201 	 * reference count of @closure goes down to 0. Multiple finalization
202 	 * notifiers on a single closure are invoked in unspecified order. If
203 	 * a single call to g_closure_unref() results in the closure being
204 	 * both invalidated and finalized, then the invalidate notifiers will
205 	 * be run before the finalize notifiers.
206 	 *
207 	 * Params:
208 	 *     notifyData = data to pass to @notify_func
209 	 *     notifyFunc = the callback function to register
210 	 */
211 	public void addFinalizeNotifier(void* notifyData, GClosureNotify notifyFunc)
212 	{
213 		g_closure_add_finalize_notifier(gClosure, notifyData, notifyFunc);
214 	}
215 
216 	/**
217 	 * Registers an invalidation notifier which will be called when the
218 	 * @closure is invalidated with g_closure_invalidate(). Invalidation
219 	 * notifiers are invoked before finalization notifiers, in an
220 	 * unspecified order.
221 	 *
222 	 * Params:
223 	 *     notifyData = data to pass to @notify_func
224 	 *     notifyFunc = the callback function to register
225 	 */
226 	public void addInvalidateNotifier(void* notifyData, GClosureNotify notifyFunc)
227 	{
228 		g_closure_add_invalidate_notifier(gClosure, notifyData, notifyFunc);
229 	}
230 
231 	/**
232 	 * Adds a pair of notifiers which get invoked before and after the
233 	 * closure callback, respectively. This is typically used to protect
234 	 * the extra arguments for the duration of the callback. See
235 	 * g_object_watch_closure() for an example of marshal guards.
236 	 *
237 	 * Params:
238 	 *     preMarshalData = data to pass to @pre_marshal_notify
239 	 *     preMarshalNotify = a function to call before the closure callback
240 	 *     postMarshalData = data to pass to @post_marshal_notify
241 	 *     postMarshalNotify = a function to call after the closure callback
242 	 */
243 	public void addMarshalGuards(void* preMarshalData, GClosureNotify preMarshalNotify, void* postMarshalData, GClosureNotify postMarshalNotify)
244 	{
245 		g_closure_add_marshal_guards(gClosure, preMarshalData, preMarshalNotify, postMarshalData, postMarshalNotify);
246 	}
247 
248 	/**
249 	 * Sets a flag on the closure to indicate that its calling
250 	 * environment has become invalid, and thus causes any future
251 	 * invocations of g_closure_invoke() on this @closure to be
252 	 * ignored. Also, invalidation notifiers installed on the closure will
253 	 * be called at this point. Note that unless you are holding a
254 	 * reference to the closure yourself, the invalidation notifiers may
255 	 * unref the closure and cause it to be destroyed, so if you need to
256 	 * access the closure after calling g_closure_invalidate(), make sure
257 	 * that you've previously called g_closure_ref().
258 	 *
259 	 * Note that g_closure_invalidate() will also be called when the
260 	 * reference count of a closure drops to zero (unless it has already
261 	 * been invalidated before).
262 	 */
263 	public void invalidate()
264 	{
265 		g_closure_invalidate(gClosure);
266 	}
267 
268 	/**
269 	 * Invokes the closure, i.e. executes the callback represented by the @closure.
270 	 *
271 	 * Params:
272 	 *     returnValue = a #GValue to store the return
273 	 *         value. May be %NULL if the callback of @closure
274 	 *         doesn't return a value.
275 	 *     nParamValues = the length of the @param_values array
276 	 *     paramValues = an array of
277 	 *         #GValues holding the arguments on which to
278 	 *         invoke the callback of @closure
279 	 *     invocationHint = a context-dependent invocation hint
280 	 */
281 	public void invoke(Value returnValue, Value[] paramValues, void* invocationHint)
282 	{
283 		GValue[] paramValuesArray = new GValue[paramValues.length];
284 		for ( int i = 0; i < paramValues.length; i++ )
285 		{
286 			paramValuesArray[i] = *(paramValues[i].getValueStruct());
287 		}
288 		
289 		g_closure_invoke(gClosure, (returnValue is null) ? null : returnValue.getValueStruct(), cast(uint)paramValues.length, paramValuesArray.ptr, invocationHint);
290 	}
291 
292 	/**
293 	 * Increments the reference count on a closure to force it staying
294 	 * alive while the caller holds a pointer to it.
295 	 *
296 	 * Return: The @closure passed in, for convenience
297 	 */
298 	public Closure doref()
299 	{
300 		auto p = g_closure_ref(gClosure);
301 		
302 		if(p is null)
303 		{
304 			return null;
305 		}
306 		
307 		return ObjectG.getDObject!(Closure)(cast(GClosure*) p);
308 	}
309 
310 	/**
311 	 * Removes a finalization notifier.
312 	 *
313 	 * Notice that notifiers are automatically removed after they are run.
314 	 *
315 	 * Params:
316 	 *     notifyData = data which was passed to g_closure_add_finalize_notifier()
317 	 *         when registering @notify_func
318 	 *     notifyFunc = the callback function to remove
319 	 */
320 	public void removeFinalizeNotifier(void* notifyData, GClosureNotify notifyFunc)
321 	{
322 		g_closure_remove_finalize_notifier(gClosure, notifyData, notifyFunc);
323 	}
324 
325 	/**
326 	 * Removes an invalidation notifier.
327 	 *
328 	 * Notice that notifiers are automatically removed after they are run.
329 	 *
330 	 * Params:
331 	 *     notifyData = data which was passed to g_closure_add_invalidate_notifier()
332 	 *         when registering @notify_func
333 	 *     notifyFunc = the callback function to remove
334 	 */
335 	public void removeInvalidateNotifier(void* notifyData, GClosureNotify notifyFunc)
336 	{
337 		g_closure_remove_invalidate_notifier(gClosure, notifyData, notifyFunc);
338 	}
339 
340 	/**
341 	 * Sets the marshaller of @closure. The `marshal_data`
342 	 * of @marshal provides a way for a meta marshaller to provide additional
343 	 * information to the marshaller. (See g_closure_set_meta_marshal().) For
344 	 * GObject's C predefined marshallers (the g_cclosure_marshal_*()
345 	 * functions), what it provides is a callback function to use instead of
346 	 * @closure->callback.
347 	 *
348 	 * Params:
349 	 *     marshal = a #GClosureMarshal function
350 	 */
351 	public void setMarshal(GClosureMarshal marshal)
352 	{
353 		g_closure_set_marshal(gClosure, marshal);
354 	}
355 
356 	/**
357 	 * Sets the meta marshaller of @closure.  A meta marshaller wraps
358 	 * @closure->marshal and modifies the way it is called in some
359 	 * fashion. The most common use of this facility is for C callbacks.
360 	 * The same marshallers (generated by [glib-genmarshal][glib-genmarshal]),
361 	 * are used everywhere, but the way that we get the callback function
362 	 * differs. In most cases we want to use @closure->callback, but in
363 	 * other cases we want to use some different technique to retrieve the
364 	 * callback function.
365 	 *
366 	 * For example, class closures for signals (see
367 	 * g_signal_type_cclosure_new()) retrieve the callback function from a
368 	 * fixed offset in the class structure.  The meta marshaller retrieves
369 	 * the right callback and passes it to the marshaller as the
370 	 * @marshal_data argument.
371 	 *
372 	 * Params:
373 	 *     marshalData = context-dependent data to pass to @meta_marshal
374 	 *     metaMarshal = a #GClosureMarshal function
375 	 */
376 	public void setMetaMarshal(void* marshalData, GClosureMarshal metaMarshal)
377 	{
378 		g_closure_set_meta_marshal(gClosure, marshalData, metaMarshal);
379 	}
380 
381 	/**
382 	 * Takes over the initial ownership of a closure.  Each closure is
383 	 * initially created in a "floating" state, which means that the initial
384 	 * reference count is not owned by any caller. g_closure_sink() checks
385 	 * to see if the object is still floating, and if so, unsets the
386 	 * floating state and decreases the reference count. If the closure
387 	 * is not floating, g_closure_sink() does nothing. The reason for the
388 	 * existence of the floating state is to prevent cumbersome code
389 	 * sequences like:
390 	 * |[<!-- language="C" -->
391 	 * closure = g_cclosure_new (cb_func, cb_data);
392 	 * g_source_set_closure (source, closure);
393 	 * g_closure_unref (closure); // GObject doesn't really need this
394 	 * ]|
395 	 * Because g_source_set_closure() (and similar functions) take ownership of the
396 	 * initial reference count, if it is unowned, we instead can write:
397 	 * |[<!-- language="C" -->
398 	 * g_source_set_closure (source, g_cclosure_new (cb_func, cb_data));
399 	 * ]|
400 	 *
401 	 * Generally, this function is used together with g_closure_ref(). Ane example
402 	 * of storing a closure for later notification looks like:
403 	 * |[<!-- language="C" -->
404 	 * static GClosure *notify_closure = NULL;
405 	 * void
406 	 * foo_notify_set_closure (GClosure *closure)
407 	 * {
408 	 * if (notify_closure)
409 	 * g_closure_unref (notify_closure);
410 	 * notify_closure = closure;
411 	 * if (notify_closure)
412 	 * {
413 	 * g_closure_ref (notify_closure);
414 	 * g_closure_sink (notify_closure);
415 	 * }
416 	 * }
417 	 * ]|
418 	 *
419 	 * Because g_closure_sink() may decrement the reference count of a closure
420 	 * (if it hasn't been called on @closure yet) just like g_closure_unref(),
421 	 * g_closure_ref() should be called prior to this function.
422 	 */
423 	public void sink()
424 	{
425 		g_closure_sink(gClosure);
426 	}
427 
428 	/**
429 	 * Decrements the reference count of a closure after it was previously
430 	 * incremented by the same caller. If no other callers are using the
431 	 * closure, then the closure will be destroyed and freed.
432 	 */
433 	public void unref()
434 	{
435 		g_closure_unref(gClosure);
436 	}
437 
438 	/**
439 	 * Set the callback for a source as a #GClosure.
440 	 *
441 	 * If the source is not one of the standard GLib types, the @closure_callback
442 	 * and @closure_marshal fields of the #GSourceFuncs structure must have been
443 	 * filled in with pointers to appropriate functions.
444 	 *
445 	 * Params:
446 	 *     source = the source
447 	 *     closure = a #GClosure
448 	 */
449 	public static void sourceSetClosure(Source source, Closure closure)
450 	{
451 		g_source_set_closure((source is null) ? null : source.getSourceStruct(), (closure is null) ? null : closure.getClosureStruct());
452 	}
453 
454 	/**
455 	 * Sets a dummy callback for @source. The callback will do nothing, and
456 	 * if the source expects a #gboolean return value, it will return %TRUE.
457 	 * (If the source expects any other type of return value, it will return
458 	 * a 0/%NULL value; whatever g_value_init() initializes a #GValue to for
459 	 * that type.)
460 	 *
461 	 * If the source is not one of the standard GLib types, the
462 	 * @closure_callback and @closure_marshal fields of the #GSourceFuncs
463 	 * structure must have been filled in with pointers to appropriate
464 	 * functions.
465 	 *
466 	 * Params:
467 	 *     source = the source
468 	 */
469 	public static void sourceSetDummyCallback(Source source)
470 	{
471 		g_source_set_dummy_callback((source is null) ? null : source.getSourceStruct());
472 	}
473 }