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