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.DClosure;
26 
27 private import core.memory;
28 private import glib.Str;
29 private import glib.Variant;
30 private import gobject.Closure;
31 private import gobject.ObjectG;
32 private import gobject.ParamSpec;
33 private import gobject.c.functions;
34 public  import gobject.c.types;
35 public  import gtkc.gobjecttypes;
36 private import std.algorithm;
37 private import std.conv;
38 private import std.traits;
39 private import std.typecons;
40 
41 
42 /** */
43 struct DGClosure(T)
44 {
45 	GClosure closure;
46 	T callback;
47 }
48 
49 /**
50  * DClosure is a wrapper around the gobject library's GClosure with special handling for marshalling D delegates and function pointers as callbacks.
51  *
52  * Closures are central to the concept of asynchronous signal delivery which is widely used throughout GTK+ and GNOME applications.
53  * A closure is an abstraction, a generic representation of a callback.
54  */
55 class DClosure : Closure
56 {
57 	private void* callback;
58 
59 	/** Get the main Gtk struct */
60 	public GClosure* getDClosureStruct(bool transferOwnership = false)
61 	{
62 		if (transferOwnership)
63 			ownedRef = false;
64 		return gClosure;
65 	}
66 
67 	/**
68 	 * Sets our main struct and passes it to the parent class.
69 	 */
70 	public this (GClosure* gClosure, bool ownedRef = false)
71 	{
72 		super(gClosure, ownedRef);
73 	}
74 
75 	/**
76 	 * Create a new Closure that will call `callback` when it's invoked.
77 	 *
78 	 * Params:
79 	 *     callback = a delegate or function to call when the DClosure is invoked.
80 	 *     swap     = Should the first and last parameter passed to the callback be swapped.
81 	 *                This is usefull when using the closure for a Signal, where the instance is
82 	 *                 the first parameter, but when using delegates it usually isn't used.
83 	 */
84 	this(T)(T callback, bool swap = false)
85 	if ( isCallable!T )
86 	{
87 		GClosure* gClosure = g_closure_new_simple(DGClosure!(T).sizeof, null);
88 		g_closure_ref(gClosure);
89 		g_closure_sink(gClosure);
90 		g_closure_set_marshal(gClosure, &d_closure_marshal!T);
91 		if ( swap ) gClosure.derivativeFlag = true;
92 
93 		auto dClosure = cast(DGClosure!(T)*)gClosure;
94 		dClosure.callback = callback;
95 
96 		static if ( isDelegate!T )
97 			this.callback = callback.ptr;
98 		else static if ( isFunctionPointer!T )
99 			this.callback = callback;
100 		else
101 			this.callback = &callback;
102 
103 		super(gClosure, true);
104 	}
105 
106 	extern(C) static void d_closure_marshal(T)(GClosure* closure, GValue* return_value, uint n_param_values, /*const*/ GValue* param_values, void* invocation_hint, void* marshal_data)
107 	{
108 		DGClosure!(T)* cl = cast(DGClosure!(T)*)closure;
109 
110 		if ( Parameters!(T).length > n_param_values )
111 			assert(false, "DClosure doesn't have enough parameters.");
112 
113 		if ( closure.derivativeFlag )
114 		{
115 			GValue[] swapped = new GValue[n_param_values];
116 			swapped[0..n_param_values-1] = param_values[1..n_param_values];
117 			swapped[n_param_values-1] = param_values[0];
118 			param_values = swapped.ptr;
119 		}
120 
121 		mixin(getCallbackCall!T());
122 	}
123 
124 	private static string getCallbackCall(T)()
125 	{
126 		if (!__ctfe) assert(false);
127 
128 		string call;
129 
130 		alias Params = Parameters!T;
131 		foreach ( param; Params )
132 		{
133 			static if ( __traits(compiles, TemplateOf!param) && __traits(isSame, TemplateOf!param, glib.c.types.Scoped) )
134 				call ~= "import "~moduleName!(TemplateArgsOf!(param)[0])~";\n";
135 			else static if ( is(param == class) || is(param == interface) || is(param == struct) || is(param == enum) )
136 				call ~= "import "~moduleName!param~";\n";
137 			else static if ( isPointer!param && ( is(PointerTarget!param == struct) || is(PointerTarget!param == enum)) )
138 				//The moduleName template gives an forward reference error here.
139 			call ~= "import "~fullyQualifiedName!param.findSplitAfter(".c.types")[0]~";\n";
140 		}
141 		alias Ret = ReturnType!T;
142 		static if ( is(Ret == class) || is(Ret == interface) || is(Ret == struct) || is(Ret == enum) )
143 			call ~= "import "~moduleName!Ret~";\n";
144 		else static if ( isPointer!Ret && ( is(PointerTarget!Ret == struct) || is(PointerTarget!Ret == enum)) )
145 			call ~= "import "~fullyQualifiedName!Ret.findSplitAfter(".c.types")[0]~";\n";
146 
147 		static if ( !is(Ret == void) )
148 			call ~= "auto ret = ";
149 		call ~= "cl.callback(";
150 
151 		foreach ( i, param; Params )
152 		{
153 			if ( i > 0 )
154 				call ~= ", ";
155 			call ~= getValue!param(i);
156 		}
157 		call ~= ");\n";
158 
159 		static if ( is(Ret == bool) )
160 			call ~= "g_value_set_boolean(return_value, ret);";
161 		else static if ( is(Ret == byte) )
162 			call ~= "g_value_set_schar(return_value, ret);";
163 		else static if ( is(Ret == ubyte) || is(Ret == char) )
164 			call ~= "g_value_set_uchar(return_value, ret);";
165 		else static if ( is(Ret == int) )
166 			call ~= "g_value_set_int(return_value, ret);";
167 		else static if ( is(Ret == uint) )
168 			call ~= "g_value_set_uint(return_value, ret);";
169 		else static if ( is(Ret == long) )
170 			call ~= "g_value_set_int64(return_value, ret);";
171 		else static if ( is(Ret == ulong) )
172 			call ~= "g_value_set_uint64(return_value, ret);";
173 		else static if ( is(Ret == float) )
174 			call ~= "g_value_set_float(return_value, ret);";
175 		else static if ( is(Ret == double) )
176 			call ~= "g_value_set_double(return_value, ret);";
177 		else static if ( is(Ret == string) )
178 			call ~= "g_value_set_string(return_value, Str.toStringz(ret));";
179 		else static if ( is(Ret == string[]) )
180 			call ~= "g_value_set_pointer(return_value, Str.toStringzArray(ret));";
181 		else static if ( is(Ret == enum) )
182 			call ~= "g_type_is_a(return_value.gType, GType.ENUM) ? g_value_set_enum(return_value, ret) : g_value_set_flags(return_value, ret);";
183 		else static if ( isPointer!Ret )
184 			call ~= "g_type_is_a(return_value.gType, GType.POINTER) ? g_value_set_pointer(return_value, ret) : (g_type_is_a(return_value.gType, GType.BOXED) ? g_value_set_boxed(return_value, ret) : g_value_set_object(return_value, ret));";
185 		else static if ( is(Ret == interface) )
186 			call ~= "g_value_set_object(return_value, (cast(ObjectG)ret).getObjectGStruct());";
187 		else static if ( is(Ret == class) )
188 		{
189 			static if ( is(Ret == Variant) )
190 				call ~= "g_value_set_variant(return_value, ret.getVariantStruct());";
191 			else static if ( is(Ret == ParamSpec) )
192 				call ~= "g_value_set_param(return_value, ret.getParamSpecStruct());";
193 			else static if ( is(Ret : ObjectG) )
194 				call ~= "g_value_set_object(return_value, ret.getObjectGStruct());";
195 			else
196 				call ~= "g_type_is_a(return_value.gType, GType.POINTER) ? g_value_set_pointer(return_value, ret.get"~Ret.stringof~"Struct()) : (g_type_is_a(return_value.gType, GType.BOXED) ? g_value_set_boxed(return_value, ret.get"~Ret.stringof~"Struct()) : g_value_set_object(return_value, ret.get"~Ret.stringof~"Struct()));";
197 		}
198 
199 		return call;
200 	}
201 
202 	private static string getValue(Param)(int index)
203 	{
204 		if (!__ctfe) assert(false);
205 
206 		static if ( is(Param == bool) )
207 			return "g_value_get_boolean(&param_values["~to!string(index)~"]) != 0";
208 		else static if ( is(Param == byte) )
209 			return "g_value_get_schar(&param_values["~to!string(index)~"])";
210 		else static if ( is(Param == ubyte) || is(Param == char) )
211 			return "g_value_get_uchar(&param_values["~to!string(index)~"])";
212 		else static if ( is(Param == int) )
213 			return "g_value_get_int(&param_values["~to!string(index)~"])";
214 		else static if ( is(Param == uint) )
215 			return "g_value_get_uint(&param_values["~to!string(index)~"])";
216 		else static if ( is(Param == long) )
217 			return "g_value_get_int64(&param_values["~to!string(index)~"])";
218 		else static if ( is(Param == ulong) )
219 			return "g_value_get_uint64(&param_values["~to!string(index)~"])";
220 		else static if ( is(Param == float) )
221 			return "g_value_get_float(&param_values["~to!string(index)~"])";
222 		else static if ( is(Param == double) )
223 			return "g_value_get_double(&param_values["~to!string(index)~"])";
224 		else static if ( is(Param == string) )
225 			return "Str.toString(g_value_get_string(&param_values["~to!string(index)~"]))";
226 		else static if ( is(Param == string[]) )
227 			return "Str.toStringArray(cast(const(char*)*)g_value_get_pointer(&param_values["~to!string(index)~"]))";
228 		else static if ( is(Param == enum) )
229 			return "cast("~fullyQualifiedName!Param~")(g_type_is_a(param_values["~to!string(index)~"].gType, GType.ENUM) ? g_value_get_enum(&param_values["~to!string(index)~"]) : g_value_get_flags(&param_values["~to!string(index)~"]))";
230 		else static if ( isPointer!Param )
231 			return "cast("~fullyQualifiedName!Param~")(g_type_is_a(param_values["~to!string(index)~"].gType, GType.POINTER) ? g_value_get_pointer(&param_values["~to!string(index)~"]) : (g_type_is_a(param_values["~to!string(index)~"].gType, GType.BOXED) ? g_value_get_boxed(&param_values["~to!string(index)~"]) : g_value_get_object(&param_values["~to!string(index)~"])))";
232 		else static if ( __traits(compiles, TemplateOf!Param) && __traits(isSame, TemplateOf!Param, glib.c.types.Scoped) )
233 			return "getScopedGobject!("~fullyQualifiedName!(TemplateArgsOf!(Param)[0])~")(cast(typeof("~fullyQualifiedName!(TemplateArgsOf!(Param)[0])~".tupleof[0]))(g_type_is_a(param_values["~to!string(index)~"].gType, GType.POINTER) ? g_value_get_pointer(&param_values["~to!string(index)~"]) : (g_type_is_a(param_values["~to!string(index)~"].gType, GType.BOXED) ? g_value_get_boxed(&param_values["~to!string(index)~"]) : g_value_get_object(&param_values["~to!string(index)~"]))))";
234 		else static if ( is(Param == interface) )
235 			return "ObjectG.getDObject!("~fullyQualifiedName!Param~")(cast(GObject*)g_value_get_object(&param_values["~to!string(index)~"]))";
236 		else static if ( is(Param == class) )
237 		{
238 			static if ( is(Param == Variant) )
239 				return "new Variant(g_value_get_variant(&param_values["~to!string(index)~"]))";
240 			else static if ( is(Param== ParamSpec) )
241 				return "new ParamSpec(g_value_get_param(&param_values["~to!string(index)~"]))";
242 			else static if ( is(Param : ObjectG) )
243 				return "ObjectG.getDObject!("~fullyQualifiedName!Param~")(cast(typeof("~fullyQualifiedName!Param~".tupleof[0]))g_value_get_object(&param_values["~to!string(index)~"]))";
244 			else
245 				return "ObjectG.getDObject!("~fullyQualifiedName!Param~")(cast(typeof("~fullyQualifiedName!Param~".tupleof[0]))(g_type_is_a(param_values["~to!string(index)~"].gType, GType.POINTER) ? g_value_get_pointer(&param_values["~to!string(index)~"]) : (g_type_is_a(param_values["~to!string(index)~"].gType, GType.BOXED) ? g_value_get_boxed(&param_values["~to!string(index)~"]) : g_value_get_object(&param_values["~to!string(index)~"]))))";
246 		}
247 	}
248 }
249 
250 /**
251  */