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 
83 	/** Get the main Gtk struct */
84 	public GClosure* getClosureStruct()
85 	{
86 		return gClosure;
87 	}
88 
89 	/** the main Gtk struct as a void* */
90 	protected void* getStruct()
91 	{
92 		return cast(void*)gClosure;
93 	}
94 
95 	/**
96 	 * Sets our main struct and passes it to the parent class.
97 	 */
98 	public this (GClosure* gClosure)
99 	{
100 		this.gClosure = gClosure;
101 	}
102 
103 	/**
104 	 */
105 
106 	public static GType getType()
107 	{
108 		return g_closure_get_type();
109 	}
110 
111 	/**
112 	 * A variant of g_closure_new_simple() which stores @object in the
113 	 * @data field of the closure and calls g_object_watch_closure() on
114 	 * @object and the created closure. This function is mainly useful
115 	 * when implementing new types of closures.
116 	 *
117 	 * Params:
118 	 *     sizeofClosure = the size of the structure to allocate, must be at least
119 	 *         `sizeof (GClosure)`
120 	 *     object = a #GObject pointer to store in the @data field of the newly
121 	 *         allocated #GClosure
122 	 *
123 	 * Return: a newly allocated #GClosure
124 	 *
125 	 * Throws: ConstructionException GTK+ fails to create the object.
126 	 */
127 	public this(uint sizeofClosure, ObjectG object)
128 	{
129 		auto p = g_closure_new_object(sizeofClosure, (object is null) ? null : object.getObjectGStruct());
130 		
131 		if(p is null)
132 		{
133 			throw new ConstructionException("null returned by new_object");
134 		}
135 		
136 		this(cast(GClosure*) p);
137 	}
138 
139 	/**
140 	 * Allocates a struct of the given size and initializes the initial
141 	 * part as a #GClosure. This function is mainly useful when
142 	 * implementing new types of closures.
143 	 *
144 	 * |[<!-- language="C" -->
145 	 * typedef struct _MyClosure MyClosure;
146 	 * struct _MyClosure
147 	 * {
148 	 * GClosure closure;
149 	 * // extra data goes here
150 	 * };
151 	 *
152 	 * static void
153 	 * my_closure_finalize (gpointer  notify_data,
154 	 * GClosure *closure)
155 	 * {
156 	 * MyClosure *my_closure = (MyClosure *)closure;
157 	 *
158 	 * // free extra data here
159 	 * }
160 	 *
161 	 * MyClosure *my_closure_new (gpointer data)
162 	 * {
163 	 * GClosure *closure;
164 	 * MyClosure *my_closure;
165 	 *
166 	 * closure = g_closure_new_simple (sizeof (MyClosure), data);
167 	 * my_closure = (MyClosure *) closure;
168 	 *
169 	 * // initialize extra data here
170 	 *
171 	 * g_closure_add_finalize_notifier (closure, notify_data,
172 	 * my_closure_finalize);
173 	 * return my_closure;
174 	 * }
175 	 * ]|
176 	 *
177 	 * Params:
178 	 *     sizeofClosure = the size of the structure to allocate, must be at least
179 	 *         `sizeof (GClosure)`
180 	 *     data = data to store in the @data field of the newly allocated #GClosure
181 	 *
182 	 * Return: a newly allocated #GClosure
183 	 *
184 	 * Throws: ConstructionException GTK+ fails to create the object.
185 	 */
186 	public this(uint sizeofClosure, void* data)
187 	{
188 		auto p = g_closure_new_simple(sizeofClosure, data);
189 		
190 		if(p is null)
191 		{
192 			throw new ConstructionException("null returned by new_simple");
193 		}
194 		
195 		this(cast(GClosure*) p);
196 	}
197 
198 	/**
199 	 * Registers a finalization notifier which will be called when the
200 	 * reference count of @closure goes down to 0. Multiple finalization
201 	 * notifiers on a single closure are invoked in unspecified order. If
202 	 * a single call to g_closure_unref() results in the closure being
203 	 * both invalidated and finalized, then the invalidate notifiers will
204 	 * be run before the finalize notifiers.
205 	 *
206 	 * Params:
207 	 *     notifyData = data to pass to @notify_func
208 	 *     notifyFunc = the callback function to register
209 	 */
210 	public void addFinalizeNotifier(void* notifyData, GClosureNotify notifyFunc)
211 	{
212 		g_closure_add_finalize_notifier(gClosure, notifyData, notifyFunc);
213 	}
214 
215 	/**
216 	 * Registers an invalidation notifier which will be called when the
217 	 * @closure is invalidated with g_closure_invalidate(). Invalidation
218 	 * notifiers are invoked before finalization notifiers, in an
219 	 * unspecified order.
220 	 *
221 	 * Params:
222 	 *     notifyData = data to pass to @notify_func
223 	 *     notifyFunc = the callback function to register
224 	 */
225 	public void addInvalidateNotifier(void* notifyData, GClosureNotify notifyFunc)
226 	{
227 		g_closure_add_invalidate_notifier(gClosure, notifyData, notifyFunc);
228 	}
229 
230 	/**
231 	 * Adds a pair of notifiers which get invoked before and after the
232 	 * closure callback, respectively. This is typically used to protect
233 	 * the extra arguments for the duration of the callback. See
234 	 * g_object_watch_closure() for an example of marshal guards.
235 	 *
236 	 * Params:
237 	 *     preMarshalData = data to pass to @pre_marshal_notify
238 	 *     preMarshalNotify = a function to call before the closure callback
239 	 *     postMarshalData = data to pass to @post_marshal_notify
240 	 *     postMarshalNotify = a function to call after the closure callback
241 	 */
242 	public void addMarshalGuards(void* preMarshalData, GClosureNotify preMarshalNotify, void* postMarshalData, GClosureNotify postMarshalNotify)
243 	{
244 		g_closure_add_marshal_guards(gClosure, preMarshalData, preMarshalNotify, postMarshalData, postMarshalNotify);
245 	}
246 
247 	/**
248 	 * Sets a flag on the closure to indicate that its calling
249 	 * environment has become invalid, and thus causes any future
250 	 * invocations of g_closure_invoke() on this @closure to be
251 	 * ignored. Also, invalidation notifiers installed on the closure will
252 	 * be called at this point. Note that unless you are holding a
253 	 * reference to the closure yourself, the invalidation notifiers may
254 	 * unref the closure and cause it to be destroyed, so if you need to
255 	 * access the closure after calling g_closure_invalidate(), make sure
256 	 * that you've previously called g_closure_ref().
257 	 *
258 	 * Note that g_closure_invalidate() will also be called when the
259 	 * reference count of a closure drops to zero (unless it has already
260 	 * been invalidated before).
261 	 */
262 	public void invalidate()
263 	{
264 		g_closure_invalidate(gClosure);
265 	}
266 
267 	/**
268 	 * Invokes the closure, i.e. executes the callback represented by the @closure.
269 	 *
270 	 * Params:
271 	 *     returnValue = a #GValue to store the return
272 	 *         value. May be %NULL if the callback of @closure
273 	 *         doesn't return a value.
274 	 *     nParamValues = the length of the @param_values array
275 	 *     paramValues = an array of
276 	 *         #GValues holding the arguments on which to
277 	 *         invoke the callback of @closure
278 	 *     invocationHint = a context-dependent invocation hint
279 	 */
280 	public void invoke(Value returnValue, Value[] paramValues, void* invocationHint)
281 	{
282 		GValue[] paramValuesArray = new GValue[paramValues.length];
283 		for ( int i = 0; i < paramValues.length; i++ )
284 		{
285 			paramValuesArray[i] = *(paramValues[i].getValueStruct());
286 		}
287 		
288 		g_closure_invoke(gClosure, (returnValue is null) ? null : returnValue.getValueStruct(), cast(uint)paramValues.length, paramValuesArray.ptr, invocationHint);
289 	}
290 
291 	/**
292 	 * Increments the reference count on a closure to force it staying
293 	 * alive while the caller holds a pointer to it.
294 	 *
295 	 * Return: The @closure passed in, for convenience
296 	 */
297 	public Closure doref()
298 	{
299 		auto p = g_closure_ref(gClosure);
300 		
301 		if(p is null)
302 		{
303 			return null;
304 		}
305 		
306 		return ObjectG.getDObject!(Closure)(cast(GClosure*) p);
307 	}
308 
309 	/**
310 	 * Removes a finalization notifier.
311 	 *
312 	 * Notice that notifiers are automatically removed after they are run.
313 	 *
314 	 * Params:
315 	 *     notifyData = data which was passed to g_closure_add_finalize_notifier()
316 	 *         when registering @notify_func
317 	 *     notifyFunc = the callback function to remove
318 	 */
319 	public void removeFinalizeNotifier(void* notifyData, GClosureNotify notifyFunc)
320 	{
321 		g_closure_remove_finalize_notifier(gClosure, notifyData, notifyFunc);
322 	}
323 
324 	/**
325 	 * Removes an invalidation notifier.
326 	 *
327 	 * Notice that notifiers are automatically removed after they are run.
328 	 *
329 	 * Params:
330 	 *     notifyData = data which was passed to g_closure_add_invalidate_notifier()
331 	 *         when registering @notify_func
332 	 *     notifyFunc = the callback function to remove
333 	 */
334 	public void removeInvalidateNotifier(void* notifyData, GClosureNotify notifyFunc)
335 	{
336 		g_closure_remove_invalidate_notifier(gClosure, notifyData, notifyFunc);
337 	}
338 
339 	/**
340 	 * Sets the marshaller of @closure. The `marshal_data`
341 	 * of @marshal provides a way for a meta marshaller to provide additional
342 	 * information to the marshaller. (See g_closure_set_meta_marshal().) For
343 	 * GObject's C predefined marshallers (the g_cclosure_marshal_*()
344 	 * functions), what it provides is a callback function to use instead of
345 	 * @closure->callback.
346 	 *
347 	 * Params:
348 	 *     marshal = a #GClosureMarshal function
349 	 */
350 	public void setMarshal(GClosureMarshal marshal)
351 	{
352 		g_closure_set_marshal(gClosure, marshal);
353 	}
354 
355 	/**
356 	 * Sets the meta marshaller of @closure.  A meta marshaller wraps
357 	 * @closure->marshal and modifies the way it is called in some
358 	 * fashion. The most common use of this facility is for C callbacks.
359 	 * The same marshallers (generated by [glib-genmarshal][glib-genmarshal]),
360 	 * are used everywhere, but the way that we get the callback function
361 	 * differs. In most cases we want to use @closure->callback, but in
362 	 * other cases we want to use some different technique to retrieve the
363 	 * callback function.
364 	 *
365 	 * For example, class closures for signals (see
366 	 * g_signal_type_cclosure_new()) retrieve the callback function from a
367 	 * fixed offset in the class structure.  The meta marshaller retrieves
368 	 * the right callback and passes it to the marshaller as the
369 	 * @marshal_data argument.
370 	 *
371 	 * Params:
372 	 *     marshalData = context-dependent data to pass to @meta_marshal
373 	 *     metaMarshal = a #GClosureMarshal function
374 	 */
375 	public void setMetaMarshal(void* marshalData, GClosureMarshal metaMarshal)
376 	{
377 		g_closure_set_meta_marshal(gClosure, marshalData, metaMarshal);
378 	}
379 
380 	/**
381 	 * Takes over the initial ownership of a closure.  Each closure is
382 	 * initially created in a "floating" state, which means that the initial
383 	 * reference count is not owned by any caller. g_closure_sink() checks
384 	 * to see if the object is still floating, and if so, unsets the
385 	 * floating state and decreases the reference count. If the closure
386 	 * is not floating, g_closure_sink() does nothing. The reason for the
387 	 * existence of the floating state is to prevent cumbersome code
388 	 * sequences like:
389 	 * |[<!-- language="C" -->
390 	 * closure = g_cclosure_new (cb_func, cb_data);
391 	 * g_source_set_closure (source, closure);
392 	 * g_closure_unref (closure); // GObject doesn't really need this
393 	 * ]|
394 	 * Because g_source_set_closure() (and similar functions) take ownership of the
395 	 * initial reference count, if it is unowned, we instead can write:
396 	 * |[<!-- language="C" -->
397 	 * g_source_set_closure (source, g_cclosure_new (cb_func, cb_data));
398 	 * ]|
399 	 *
400 	 * Generally, this function is used together with g_closure_ref(). Ane example
401 	 * of storing a closure for later notification looks like:
402 	 * |[<!-- language="C" -->
403 	 * static GClosure *notify_closure = NULL;
404 	 * void
405 	 * foo_notify_set_closure (GClosure *closure)
406 	 * {
407 	 * if (notify_closure)
408 	 * g_closure_unref (notify_closure);
409 	 * notify_closure = closure;
410 	 * if (notify_closure)
411 	 * {
412 	 * g_closure_ref (notify_closure);
413 	 * g_closure_sink (notify_closure);
414 	 * }
415 	 * }
416 	 * ]|
417 	 *
418 	 * Because g_closure_sink() may decrement the reference count of a closure
419 	 * (if it hasn't been called on @closure yet) just like g_closure_unref(),
420 	 * g_closure_ref() should be called prior to this function.
421 	 */
422 	public void sink()
423 	{
424 		g_closure_sink(gClosure);
425 	}
426 
427 	/**
428 	 * Decrements the reference count of a closure after it was previously
429 	 * incremented by the same caller. If no other callers are using the
430 	 * closure, then the closure will be destroyed and freed.
431 	 */
432 	public void unref()
433 	{
434 		g_closure_unref(gClosure);
435 	}
436 
437 	/**
438 	 * Set the callback for a source as a #GClosure.
439 	 *
440 	 * If the source is not one of the standard GLib types, the @closure_callback
441 	 * and @closure_marshal fields of the #GSourceFuncs structure must have been
442 	 * filled in with pointers to appropriate functions.
443 	 *
444 	 * Params:
445 	 *     source = the source
446 	 *     closure = a #GClosure
447 	 */
448 	public static void sourceSetClosure(Source source, Closure closure)
449 	{
450 		g_source_set_closure((source is null) ? null : source.getSourceStruct(), (closure is null) ? null : closure.getClosureStruct());
451 	}
452 
453 	/**
454 	 * Sets a dummy callback for @source. The callback will do nothing, and
455 	 * if the source expects a #gboolean return value, it will return %TRUE.
456 	 * (If the source expects any other type of return value, it will return
457 	 * a 0/%NULL value; whatever g_value_init() initializes a #GValue to for
458 	 * that type.)
459 	 *
460 	 * If the source is not one of the standard GLib types, the
461 	 * @closure_callback and @closure_marshal fields of the #GSourceFuncs
462 	 * structure must have been filled in with pointers to appropriate
463 	 * functions.
464 	 *
465 	 * Params:
466 	 *     source = the source
467 	 */
468 	public static void sourceSetDummyCallback(Source source)
469 	{
470 		g_source_set_dummy_callback((source is null) ? null : source.getSourceStruct());
471 	}
472 }