Mercurial > projects > dstep
annotate dstep/objc/bridge/Bridge.d @ 2:9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
author | Jacob Carlborg <doob@me.com> |
---|---|
date | Sun, 05 Jul 2009 17:16:19 +0200 |
parents | 033d260cfc9b |
children | 07194b026fa4 |
rev | line source |
---|---|
1 | 1 /** |
2 * Copyright: Copyright (c) 2009 Jacob Carlborg. | |
3 * Authors: Jacob Carlborg | |
4 * Version: Initial created: Feb 4, 2009 | |
5 * License: $(LINK2 http://www.boost.org/LICENSE_1_0.txt, Boost Software License 1.0) | |
6 */ | |
7 module dstep.objc.bridge.Bridge; | |
8 | |
9 import mambo.io; | |
10 | |
11 version (Tango) | |
12 { | |
13 import tango.core.Memory; | |
14 import tango.core.Traits : ParameterTupleOf, ReturnTypeOf; | |
15 | |
16 alias GC.addRoot addRoot; | |
17 } | |
18 | |
19 else | |
20 { | |
21 import std.gc : addRoot; | |
22 import std.traits : ParameterTypeTuple, ReturnType; | |
23 | |
24 alias ReturnType ReturnTypeOf; | |
25 alias ParameterTypeTuple ParameterTupleOf; | |
26 } | |
27 | |
28 import dstep.internal.String; | |
29 import dstep.internal.Tuple; | |
30 import dstep.internal.Version; | |
31 import dstep.objc.bridge.Capsule; | |
32 import dstep.objc.bridge.ClassInitializer; | |
33 import dstep.objc.bridge.TypeEncoding; | |
34 import dstep.objc.bridge.Wrapper; | |
35 import dstep.objc.message; | |
36 import dstep.objc.objc; | |
37 import dstep.objc.runtime; | |
38 | |
39 /** | |
40 * Builds a string representing a selector out of the given function | |
41 * | |
42 * It will build the string like this: | |
43 * | |
44 * --- | |
45 * foo (int x, int y); // foo:y: | |
46 * bar (); // bar | |
47 * fooBar (int x); // fooBar: | |
48 * --- | |
49 * | |
50 * Params: | |
51 * func = the function alias to build the selector of | |
52 * | |
53 * Returns: a string representing the selector | |
54 */ | |
55 template selector (alias func) | |
56 { | |
57 const selector = buildSelector!(func); | |
58 } | |
59 | |
60 template ObjcWrap () | |
61 { | |
62 mixin ObjcWrap!(this.stringof); | |
63 } | |
64 | |
65 template ObjcWrap (string name) | |
66 { | |
2
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
67 private |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
68 { |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
69 import dstep.objc.bridge.ClassInitializer : ObjcSubclassInitializer, subclassInit; |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
70 import dstep.objc.objc : Class, id, IMP, SEL; |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
71 |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
72 static Class objcClass_; |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
73 static Class objcSuperClass_; |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
74 } |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
75 |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
76 this () |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
77 { |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
78 objcObject = invokeObjcSelfClass!(id, "alloc"); |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
79 id ret = invokeObjcSelf!(id, "init"); |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
80 |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
81 if (ret) |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
82 objcObject = ret; |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
83 |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
84 dObject = this; |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
85 } |
1 | 86 |
87 this (id object) | |
88 { | |
89 super(object); | |
90 } | |
91 | |
92 static typeof(this) alloc () | |
93 { | |
94 return invokeObjcSelfClass!(typeof(this), "alloc"); | |
95 } | |
96 | |
97 typeof(this) init () | |
98 { | |
99 id ret = invokeObjcSelf!(id, "init"); | |
100 | |
101 if (!ret) | |
102 return null; | |
103 | |
104 if (ret is objcObject) | |
105 { | |
106 dObject = this; | |
107 return this; | |
108 } | |
109 | |
110 auto object = new typeof(this) (ret); | |
111 object.dObject = this; | |
112 | |
113 return object; | |
114 } | |
115 | |
116 mixin ObjcSubclassInitializer!("D_" ~ name, name); | |
117 } | |
118 | |
119 struct ObjcMethodDeclaration (alias imp, R, string name, ARGS...) | |
120 { | |
121 alias imp methodImp; | |
122 alias R returnType; | |
123 const string methodName = name; | |
124 alias ARGS argsType; | |
125 } | |
126 | |
127 template ObjcBindMethod (alias method) | |
128 { | |
129 import dstep.objc.bridge.TypeEncoding : buildSelector, encode; | |
130 | |
131 mixin ObjcBindMethod!(method, buildSelector!(method)); | |
132 } | |
133 | |
134 template ObjcBindMethod (alias method, string selector) | |
135 { | |
136 mixin ObjcBindMethod!(method, ReturnTypeOf!(method), selector, ParameterTupleOf!(method)); | |
137 } | |
138 | |
139 template ObjcBindMethod (alias method, R, string selector, ARGS...) | |
140 { | |
2
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
141 private |
1 | 142 { |
2
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
143 import dstep.objc.bridge.Capsule : decapsule, encapsule, isCapsule; |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
144 import dstep.objc.bridge.Type : needsEncapsulation, ObjcType; |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
145 import dstep.internal.Tuple; |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
146 |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
147 ObjcMethodDeclaration!(method, R, selector, ARGS) objcMethodDeclaration; |
1 | 148 |
2
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
149 R delegate (ARGS) resolveVirtualCall () |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
150 { |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
151 return null; |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
152 } |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
153 |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
154 alias ReplaceAllClasses!(id, ARGS) ObjcArgs; |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
155 |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
156 static ObjcType!(R) forwardVirtualCall (id self, SEL cmd, ObjcArgs objcArgs) |
1 | 157 in |
158 { | |
159 assert(isCapsule(self)); | |
160 } | |
161 body | |
162 { | |
163 R delegate (ARGS) dg; | |
164 dg.funcptr = &method; | |
165 dg.ptr = Bridge.getDObject(self); | |
166 | |
167 ARGS args; | |
168 | |
169 foreach (i, a ; objcArgs) | |
170 { | |
171 alias typeof(args[i]) ArgType; | |
172 | |
173 args[i] = decapsule!(ArgType)(a); | |
174 } | |
175 | |
2
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
176 static if (is(R == void)) |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
177 dg(args); |
1 | 178 |
2
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
179 else |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
180 return encapsule!(R)(dg(args)); |
1 | 181 } |
182 } | |
183 } | |
184 | |
185 template ObjcBindClassMethod (alias method, string selector) | |
186 { | |
187 mixin ObjcBindClassMethod!(method, ReturnTypeOf!(method), selector, ParameterTupleOf!(method)); | |
188 } | |
189 | |
190 template ObjcBindClassMethod (alias method, R, string selector, ARGS...) | |
191 { | |
2
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
192 private |
1 | 193 { |
2
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
194 import dstep.objc.bridge.Capsule : decapsule, encapsule, isCapsule, needsEncapsulation, ObjcType; |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
195 import dstep.internal.Tuple; |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
196 |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
197 ObjcMethodDeclaration!(method, R, selector, ARGS) objcClassMethodDeclaration; |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
198 |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
199 alias ReplaceAllClasses!(id, ARGS) ObjcArgs; |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
200 |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
201 static ObjcType!(R) forwardStaticCall (id self, SEL cmd, ObjcArgs objcArgs) |
1 | 202 in |
203 { | |
204 assert(isCapsule(self)); | |
205 } | |
206 body | |
207 { | |
208 R function (ARGS) dg = &method; | |
209 | |
210 ARGS args; | |
211 | |
212 foreach (i, a ; objcArgs) | |
213 { | |
214 alias typeof(args[i]) ArgType; | |
215 | |
216 args[i] = decapsule!(ArgType)(a); | |
217 } | |
218 | |
2
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
219 static if (needsEncapsulation!(R)) |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
220 return encapsule!(R)(dg(args)); |
1 | 221 |
2
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
222 else |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
223 return dg(args); |
1 | 224 } |
225 } | |
2
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
226 } |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
227 |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
228 template ObjcBindFunction (alias func) |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
229 { |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
230 mixin ObjcBindFunction!(func, ReturnTypeOf!(func), ParameterTupleOf!(func)); |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
231 } |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
232 |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
233 template ObjcBindFunction (alias func, R, ARGS...) |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
234 { |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
235 private |
1 | 236 { |
2
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
237 import dstep.objc.bridge.Capsule : decapsule, encapsule, needsEncapsulation, ObjcType; |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
238 import dstep.internal.Tuple; |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
239 |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
240 ObjcMethodDeclaration!(method, R, selector, ARGS) objcClassMethodDeclaration; |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
241 |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
242 alias ReplaceAllClasses!(id, ARGS) ObjcArgs; |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
243 |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
244 extern (C) ObjcType!(R) forwardFunctionCall (ObjcArgs objcArgs) |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
245 { |
1 | 246 ARGS args; |
247 | |
248 foreach (i, a ; objcArgs) | |
249 { | |
250 alias typeof(args[i]) ArgType; | |
2
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
251 |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
252 args[i] = decapsule!(ArgType)(a); |
1 | 253 } |
254 | |
2
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
255 static if (needsEncapsulation!(R)) |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
256 return encapsule!(R)(dg(args)); |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
257 |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
258 else |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
259 return dg(args); |
1 | 260 } |
261 } | |
262 } | |
263 | |
264 struct Bridge | |
265 { | |
266 static: | |
267 | |
268 const objcMethodDeclarationVar = "objcMethodDeclaration"; | |
269 const objcClassMethodDeclarationVar = "objcClassMethodDeclaration"; | |
270 const dObjectVar = "dObject"; | |
271 alias void* DObjectType; | |
272 private ClassInfo[string] registeredClasses; | |
273 | |
274 void registerClass (string className, ClassInfo classInfo) | |
275 { | |
276 if (className.length > 2 && className[0 .. 2] == "D_") | |
277 registeredClasses[className[0 .. 2]] = classInfo; | |
278 | |
279 else | |
280 registeredClasses[className] = classInfo; | |
281 } | |
282 | |
283 Object createRegisteredObject (T) (string className) | |
284 { | |
285 return null; | |
286 } | |
287 | |
288 DObjectType getDObject (id self) | |
289 { | |
290 DObjectType dObject; | |
291 | |
292 self.getInstanceVariable!(DObjectType, dObjectVar)(dObject); | |
293 | |
294 return dObject; | |
295 } | |
296 | |
297 id setDObject (Object dObject, id objcObject) | |
298 { | |
299 auto o = cast(DObjectType) dObject; | |
300 addRoot(o); | |
301 | |
302 objcObject.setInstanceVariable!(DObjectType, Bridge.dObjectVar)(o); | |
303 | |
304 return objcObject; | |
305 } | |
306 | |
307 R invokeObjcMethod (R, string selector, ARGS...) (id self, ARGS args) | |
308 { | |
309 static if (!is(R : void)) | |
310 R result; | |
311 | |
312 SEL sel = sel.registerName!(selector); | |
313 | |
314 alias ReplaceAllClasses!(id, ARGS) ObjcArgs; | |
315 ObjcArgs objcArgs; | |
316 | |
317 foreach (i, a ; args) | |
318 { | |
319 alias typeof(a) ArgType; | |
320 | |
321 objcArgs[i] = encapsule!(ArgType)(a); | |
322 } | |
323 | |
324 static if (is(R == struct)) | |
325 { | |
326 self.msgSend_stret(result, sel, objcArgs); | |
327 return result; | |
328 } | |
329 | |
330 else static if (is(R == float) || is(R == double) || is(R == real)) | |
331 { | |
332 version (X86) | |
333 { | |
334 static if (is(R == float)) | |
335 return self.msgSend_fpret!(R)(sel, objcArgs); | |
336 | |
337 else | |
338 return self.msgSend_fpret(sel, objcArgs); | |
339 } | |
340 | |
341 else version (X86_64) | |
342 { | |
343 static if (is(R == real)) | |
344 return self.msgSend_fpret(sel, objcArgs); | |
345 | |
346 else | |
347 return self.msgSend!(R)(sel, objcArgs); | |
348 } | |
349 | |
350 else | |
351 return self.msgSend!(R)(sel, objcArgs); | |
352 } | |
353 | |
354 else static if (is(R : ObjcWrapper)) | |
355 { | |
356 id r = self.msgSend(sel, objcArgs); | |
357 | |
358 if (!r) | |
359 return null; | |
360 | |
361 if (isCapsule(r)) | |
362 { | |
363 result = decapsule!(R)(r); | |
364 | |
365 if (result) | |
366 return result; | |
367 } | |
368 | |
369 return new R(r); | |
370 } | |
371 | |
372 else static if (is(R : Object)) | |
373 { | |
374 id r = self.msgSend(sel, objcArgs); | |
375 | |
376 if (!r) | |
377 return null; | |
378 | |
379 return decapsule!(R)(r); | |
380 } | |
381 | |
382 else | |
383 return self.msgSend!(R, ObjcArgs)(sel, objcArgs); | |
384 } | |
385 | |
386 R invokeObjcClassMethod (R = id, string selector, ARGS...) (Class cls, ARGS args) | |
387 { | |
388 return invokeObjcMethod!(R, selector, ARGS)(cast(id) cls, args); | |
389 } | |
390 | |
391 R invokeObjcSuperMethod (R = id, string selector, ARGS...) (objc_super* self, ARGS args) | |
392 { | |
393 R result; | |
394 SEL sel = sel.registerName!(selector); | |
395 | |
396 alias ReplaceAllClasses!(id, ARGS) ObjcArgs; | |
397 ObjcArgs objcArgs; | |
398 | |
399 foreach (i, a ; args) | |
400 { | |
401 alias typeof(a) ArgType; | |
402 | |
403 objcArgs[i] = encapsule!(ArgType)(a); | |
404 } | |
405 | |
406 static if (is(R == struct)) | |
407 { | |
408 self.msgSendSuper_stret(result, sel, objcArgs); | |
409 } | |
410 | |
411 else static if (is(R : ObjcWrapper)) | |
412 { | |
413 id r = self.msgSendSuper(sel, objcArgs); | |
414 | |
415 if (isCapsule(r)) | |
416 { | |
417 result = decapsule!(R)(r); | |
418 | |
419 if (result) | |
420 return result; | |
421 } | |
422 | |
423 return r ? new R(r) : null; | |
424 } | |
425 | |
426 else static if (is(R : Object)) | |
427 { | |
428 id r = self.msgSendSuper(sel, objcArgs); | |
429 return decapsule!(R)(r); | |
430 } | |
431 | |
432 else | |
433 return self.msgSend!(R)(sel, objcArgs); | |
434 } | |
2
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
435 |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
436 R invokeObjcFunction (R, alias func, ARGS...) (ARGS args) |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
437 { |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
438 static if (!is(R : void)) |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
439 R result; |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
440 |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
441 auto funcPtr = &func; // use a function pointer instead of the alias because the function can be private. |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
442 |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
443 alias ReplaceAllClasses!(id, ARGS) ObjcArgs; |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
444 ObjcArgs objcArgs; |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
445 |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
446 foreach (i, a ; args) |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
447 { |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
448 alias typeof(a) ArgType; |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
449 |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
450 objcArgs[i] = encapsule!(ArgType)(a); |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
451 } |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
452 |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
453 static if (is(R : ObjcWrapper)) |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
454 { |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
455 id r = funcPtr(objcArgs); |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
456 |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
457 if (!r) |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
458 return null; |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
459 |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
460 if (isCapsule(r)) |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
461 { |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
462 result = decapsule!(R)(r); |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
463 |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
464 if (result) |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
465 return result; |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
466 } |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
467 |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
468 return new R(r); |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
469 } |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
470 |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
471 else static if (is(R : Object)) |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
472 { |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
473 id r = funcPtr(objcArgs); |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
474 |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
475 if (!r) |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
476 return null; |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
477 |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
478 return decapsule!(R)(r); |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
479 } |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
480 |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
481 else |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
482 return funcPtr(objcArgs); |
9fd439a28ce3
Adapted the scripts for the new bridge + a lot more
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
483 } |
1 | 484 } |