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  * Conversion parameters:
26  * inFile  = gobject-Closures.html
27  * outPack = gobject
28  * outFile = Closure
29  * strct   = GClosure
30  * realStrct=
31  * ctorStrct=
32  * clss    = Closure
33  * interf  = 
34  * class Code: No
35  * interface Code: No
36  * template for:
37  * extend  = 
38  * implements:
39  * prefixes:
40  * 	- g_closure_
41  * omit structs:
42  * omit prefixes:
43  * 	- g_cclosure_
44  * omit code:
45  * omit signals:
46  * imports:
47  * 	- gobject.ObjectG
48  * 	- gobject.Value
49  * 	- glib.Source
50  * structWrap:
51  * 	- GClosure* -> Closure
52  * 	- GObject* -> ObjectG
53  * 	- GSource* -> Source
54  * 	- GValue* -> Value
55  * module aliases:
56  * local aliases:
57  * overrides:
58  */
59 
60 module gobject.Closure;
61 
62 public  import gtkc.gobjecttypes;
63 
64 private import gtkc.gobject;
65 private import glib.ConstructionException;
66 private import gobject.ObjectG;
67 
68 
69 private import gobject.ObjectG;
70 private import gobject.Value;
71 private import glib.Source;
72 
73 
74 
75 
76 /**
77  * A GClosure represents a callback supplied by the programmer. It
78  * will generally comprise a function of some kind and a marshaller
79  * used to call it. It is the reponsibility of the marshaller to
80  * convert the arguments for the invocation from GValues into
81  * a suitable form, perform the callback on the converted arguments,
82  * and transform the return value back into a GValue.
83  *
84  * In the case of C programs, a closure usually just holds a pointer
85  * to a function and maybe a data argument, and the marshaller
86  * converts between GValue and native C types. The GObject
87  * library provides the GCClosure type for this purpose. Bindings for
88  * other languages need marshallers which convert between GValues and suitable representations in the runtime of the language in
89  * order to use functions written in that languages as callbacks.
90  *
91  * Within GObject, closures play an important role in the
92  * implementation of signals. When a signal is registered, the
93  * c_marshaller argument to g_signal_new() specifies the default C
94  * marshaller for any closure which is connected to this
95  * signal. GObject provides a number of C marshallers for this
96  * purpose, see the g_cclosure_marshal_*() functions. Additional C
97  * marshallers can be generated with the glib-genmarshal utility. Closures
98  * can be explicitly connected to signals with
99  * g_signal_connect_closure(), but it usually more convenient to let
100  * GObject create a closure automatically by using one of the
101  * g_signal_connect_*() functions which take a callback function/user
102  * data pair.
103  *
104  * Using closures has a number of important advantages over a simple
105  * callback function/data pointer combination:
106  *
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  *
111  * The reference counting of GClosure makes it easy to handle reentrancy
112  * right; if a callback is removed while it is being invoked, the closure
113  * and its parameters won't be freed until the invocation finishes.
114  *
115  * g_closure_invalidate() and invalidation notifiers allow callbacks to be
116  * automatically removed when the objects they point to go away.
117  */
118 public class Closure
119 {
120 	
121 	/** the main Gtk struct */
122 	protected GClosure* gClosure;
123 	
124 	
125 	public GClosure* getClosureStruct()
126 	{
127 		return gClosure;
128 	}
129 	
130 	
131 	/** the main Gtk struct as a void* */
132 	protected void* getStruct()
133 	{
134 		return cast(void*)gClosure;
135 	}
136 	
137 	/**
138 	 * Sets our main struct and passes it to the parent class
139 	 */
140 	public this (GClosure* gClosure)
141 	{
142 		this.gClosure = gClosure;
143 	}
144 	
145 	/**
146 	 */
147 	
148 	/**
149 	 * A variant of g_closure_new_simple() which stores object in the
150 	 * data field of the closure and calls g_object_watch_closure() on
151 	 * object and the created closure. This function is mainly useful
152 	 * when implementing new types of closures.
153 	 * Params:
154 	 * sizeofClosure = the size of the structure to allocate, must be at least
155 	 * sizeof (GClosure)
156 	 * object = a GObject pointer to store in the data field of the newly
157 	 * allocated GClosure
158 	 * Throws: ConstructionException GTK+ fails to create the object.
159 	 */
160 	public this (uint sizeofClosure, ObjectG object)
161 	{
162 		// GClosure * g_closure_new_object (guint sizeof_closure,  GObject *object);
163 		auto p = g_closure_new_object(sizeofClosure, (object is null) ? null : object.getObjectGStruct());
164 		if(p is null)
165 		{
166 			throw new ConstructionException("null returned by g_closure_new_object(sizeofClosure, (object is null) ? null : object.getObjectGStruct())");
167 		}
168 		this(cast(GClosure*) p);
169 	}
170 	
171 	/**
172 	 * Increments the reference count on a closure to force it staying
173 	 * alive while the caller holds a pointer to it.
174 	 * Returns: The closure passed in, for convenience. [transfer none]
175 	 */
176 	public Closure doref()
177 	{
178 		// GClosure * g_closure_ref (GClosure *closure);
179 		auto p = g_closure_ref(gClosure);
180 		
181 		if(p is null)
182 		{
183 			return null;
184 		}
185 		
186 		return ObjectG.getDObject!(Closure)(cast(GClosure*) p);
187 	}
188 	
189 	/**
190 	 * Takes over the initial ownership of a closure. Each closure is
191 	 * initially created in a floating state, which
192 	 * means that the initial reference count is not owned by any caller.
193 	 * g_closure_sink() checks to see if the object is still floating, and
194 	 * if so, unsets the floating state and decreases the reference
195 	 * count. If the closure is not floating, g_closure_sink() does
196 	 * nothing. The reason for the existence of the floating state is to
197 	 */
198 	public void sink()
199 	{
200 		// void g_closure_sink (GClosure *closure);
201 		g_closure_sink(gClosure);
202 	}
203 	
204 	/**
205 	 * Decrements the reference count of a closure after it was previously
206 	 * incremented by the same caller. If no other callers are using the
207 	 * closure, then the closure will be destroyed and freed.
208 	 */
209 	public void unref()
210 	{
211 		// void g_closure_unref (GClosure *closure);
212 		g_closure_unref(gClosure);
213 	}
214 	
215 	/**
216 	 * Invokes the closure, i.e. executes the callback represented by the closure.
217 	 * Params:
218 	 * returnValue = a GValue to store the return
219 	 * value. May be NULL if the callback of closure
220 	 * doesn't return a value. [allow-none]
221 	 * nParamValues = the length of the param_values array
222 	 * paramValues = an array of
223 	 * GValues holding the arguments on which to
224 	 * invoke the callback of closure. [array length=n_param_values]
225 	 * invocationHint = a context-dependent invocation hint. [allow-none]
226 	 */
227 	public void invoke(Value returnValue, uint nParamValues, Value paramValues, void* invocationHint)
228 	{
229 		// void g_closure_invoke (GClosure *closure,  GValue *return_value,  guint n_param_values,  const GValue *param_values,  gpointer invocation_hint);
230 		g_closure_invoke(gClosure, (returnValue is null) ? null : returnValue.getValueStruct(), nParamValues, (paramValues is null) ? null : paramValues.getValueStruct(), invocationHint);
231 	}
232 	
233 	/**
234 	 * Sets a flag on the closure to indicate that its calling
235 	 * environment has become invalid, and thus causes any future
236 	 * invocations of g_closure_invoke() on this closure to be
237 	 * ignored. Also, invalidation notifiers installed on the closure will
238 	 * be called at this point. Note that unless you are holding a
239 	 * reference to the closure yourself, the invalidation notifiers may
240 	 * unref the closure and cause it to be destroyed, so if you need to
241 	 * access the closure after calling g_closure_invalidate(), make sure
242 	 * that you've previously called g_closure_ref().
243 	 * Note that g_closure_invalidate() will also be called when the
244 	 * reference count of a closure drops to zero (unless it has already
245 	 * been invalidated before).
246 	 */
247 	public void invalidate()
248 	{
249 		// void g_closure_invalidate (GClosure *closure);
250 		g_closure_invalidate(gClosure);
251 	}
252 	
253 	/**
254 	 * Registers a finalization notifier which will be called when the
255 	 * reference count of closure goes down to 0. Multiple finalization
256 	 * notifiers on a single closure are invoked in unspecified order. If
257 	 * a single call to g_closure_unref() results in the closure being
258 	 * both invalidated and finalized, then the invalidate notifiers will
259 	 * be run before the finalize notifiers.
260 	 * Params:
261 	 * notifyData = data to pass to notify_func
262 	 * notifyFunc = the callback function to register
263 	 */
264 	public void addFinalizeNotifier(void* notifyData, GClosureNotify notifyFunc)
265 	{
266 		// void g_closure_add_finalize_notifier (GClosure *closure,  gpointer notify_data,  GClosureNotify notify_func);
267 		g_closure_add_finalize_notifier(gClosure, notifyData, notifyFunc);
268 	}
269 	
270 	/**
271 	 * Registers an invalidation notifier which will be called when the
272 	 * closure is invalidated with g_closure_invalidate(). Invalidation
273 	 * notifiers are invoked before finalization notifiers, in an
274 	 * unspecified order.
275 	 * Params:
276 	 * notifyData = data to pass to notify_func
277 	 * notifyFunc = the callback function to register
278 	 */
279 	public void addInvalidateNotifier(void* notifyData, GClosureNotify notifyFunc)
280 	{
281 		// void g_closure_add_invalidate_notifier (GClosure *closure,  gpointer notify_data,  GClosureNotify notify_func);
282 		g_closure_add_invalidate_notifier(gClosure, notifyData, notifyFunc);
283 	}
284 	
285 	/**
286 	 * Removes a finalization notifier.
287 	 * Notice that notifiers are automatically removed after they are run.
288 	 * Params:
289 	 * notifyData = data which was passed to g_closure_add_finalize_notifier()
290 	 * when registering notify_func
291 	 * notifyFunc = the callback function to remove
292 	 */
293 	public void removeFinalizeNotifier(void* notifyData, GClosureNotify notifyFunc)
294 	{
295 		// void g_closure_remove_finalize_notifier (GClosure *closure,  gpointer notify_data,  GClosureNotify notify_func);
296 		g_closure_remove_finalize_notifier(gClosure, notifyData, notifyFunc);
297 	}
298 	
299 	/**
300 	 * Removes an invalidation notifier.
301 	 * Notice that notifiers are automatically removed after they are run.
302 	 * Params:
303 	 * notifyData = data which was passed to g_closure_add_invalidate_notifier()
304 	 * when registering notify_func
305 	 * notifyFunc = the callback function to remove
306 	 */
307 	public void removeInvalidateNotifier(void* notifyData, GClosureNotify notifyFunc)
308 	{
309 		// void g_closure_remove_invalidate_notifier  (GClosure *closure,  gpointer notify_data,  GClosureNotify notify_func);
310 		g_closure_remove_invalidate_notifier(gClosure, notifyData, notifyFunc);
311 	}
312 	
313 	/**
314 	 * Allocates a struct of the given size and initializes the initial
315 	 * part as a GClosure. This function is mainly useful when
316 	 * implementing new types of closures.
317 	 * $(DDOC_COMMENT example)
318 	 * Params:
319 	 * sizeofClosure = the size of the structure to allocate, must be at least
320 	 * sizeof (GClosure)
321 	 * data = data to store in the data field of the newly allocated GClosure
322 	 * Throws: ConstructionException GTK+ fails to create the object.
323 	 */
324 	public this (uint sizeofClosure, void* data)
325 	{
326 		// GClosure * g_closure_new_simple (guint sizeof_closure,  gpointer data);
327 		auto p = g_closure_new_simple(sizeofClosure, data);
328 		if(p is null)
329 		{
330 			throw new ConstructionException("null returned by g_closure_new_simple(sizeofClosure, data)");
331 		}
332 		this(cast(GClosure*) p);
333 	}
334 	
335 	/**
336 	 * Sets the marshaller of closure. The marshal_data
337 	 * of marshal provides a way for a meta marshaller to provide additional
338 	 * information to the marshaller. (See g_closure_set_meta_marshal().) For
339 	 * GObject's C predefined marshallers (the g_cclosure_marshal_*()
340 	 * functions), what it provides is a callback function to use instead of
341 	 * closure->callback.
342 	 * Params:
343 	 * marshal = a GClosureMarshal function
344 	 */
345 	public void setMarshal(GClosureMarshal marshal)
346 	{
347 		// void g_closure_set_marshal (GClosure *closure,  GClosureMarshal marshal);
348 		g_closure_set_marshal(gClosure, marshal);
349 	}
350 	
351 	/**
352 	 * Adds a pair of notifiers which get invoked before and after the
353 	 * closure callback, respectively. This is typically used to protect
354 	 * the extra arguments for the duration of the callback. See
355 	 * g_object_watch_closure() for an example of marshal guards.
356 	 * Params:
357 	 * preMarshalData = data to pass to pre_marshal_notify
358 	 * preMarshalNotify = a function to call before the closure callback
359 	 * postMarshalData = data to pass to post_marshal_notify
360 	 * postMarshalNotify = a function to call after the closure callback
361 	 */
362 	public void addMarshalGuards(void* preMarshalData, GClosureNotify preMarshalNotify, void* postMarshalData, GClosureNotify postMarshalNotify)
363 	{
364 		// void g_closure_add_marshal_guards (GClosure *closure,  gpointer pre_marshal_data,  GClosureNotify pre_marshal_notify,  gpointer post_marshal_data,  GClosureNotify post_marshal_notify);
365 		g_closure_add_marshal_guards(gClosure, preMarshalData, preMarshalNotify, postMarshalData, postMarshalNotify);
366 	}
367 	
368 	/**
369 	 * Sets the meta marshaller of closure. A meta marshaller wraps
370 	 * closure->marshal and modifies the way it is called in some
371 	 * fashion. The most common use of this facility is for C callbacks.
372 	 * The same marshallers (generated by glib-genmarshal) are used
373 	 * everywhere, but the way that we get the callback function
374 	 * differs. In most cases we want to use closure->callback, but in
375 	 * other cases we want to use some different technique to retrieve the
376 	 * callback function.
377 	 * For example, class closures for signals (see
378 	 * g_signal_type_cclosure_new()) retrieve the callback function from a
379 	 * fixed offset in the class structure. The meta marshaller retrieves
380 	 * the right callback and passes it to the marshaller as the
381 	 * marshal_data argument.
382 	 * Params:
383 	 * marshalData = context-dependent data to pass to meta_marshal
384 	 * metaMarshal = a GClosureMarshal function
385 	 */
386 	public void setMetaMarshal(void* marshalData, GClosureMarshal metaMarshal)
387 	{
388 		// void g_closure_set_meta_marshal (GClosure *closure,  gpointer marshal_data,  GClosureMarshal meta_marshal);
389 		g_closure_set_meta_marshal(gClosure, marshalData, metaMarshal);
390 	}
391 	
392 	/**
393 	 * Set the callback for a source as a GClosure.
394 	 * If the source is not one of the standard GLib types, the closure_callback
395 	 * and closure_marshal fields of the GSourceFuncs structure must have been
396 	 * filled in with pointers to appropriate functions.
397 	 * Params:
398 	 * source = the source
399 	 * closure = a GClosure
400 	 */
401 	public static void gSourceSetClosure(Source source, Closure closure)
402 	{
403 		// void g_source_set_closure (GSource *source,  GClosure *closure);
404 		g_source_set_closure((source is null) ? null : source.getSourceStruct(), (closure is null) ? null : closure.getClosureStruct());
405 	}
406 	
407 	/**
408 	 * Sets a dummy callback for source. The callback will do nothing, and
409 	 * if the source expects a gboolean return value, it will return TRUE.
410 	 * (If the source expects any other type of return value, it will return
411 	 * a 0/NULL value; whatever g_value_init() initializes a GValue to for
412 	 * that type.)
413 	 * If the source is not one of the standard GLib types, the
414 	 * closure_callback and closure_marshal fields of the GSourceFuncs
415 	 * structure must have been filled in with pointers to appropriate
416 	 * functions.
417 	 * Params:
418 	 * source = the source
419 	 */
420 	public static void gSourceSetDummyCallback(Source source)
421 	{
422 		// void g_source_set_dummy_callback (GSource *source);
423 		g_source_set_dummy_callback((source is null) ? null : source.getSourceStruct());
424 	}
425 }