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