Mercurial > projects > dstep
comparison dstep/objc/bridge/Type.d @ 16:19885b43130e
Huge update, the bridge actually works now
author | Jacob Carlborg <doob@me.com> |
---|---|
date | Sun, 03 Jan 2010 22:06:11 +0100 |
parents | 9fd439a28ce3 |
children |
comparison
equal
deleted
inserted
replaced
15:7ff919f595d5 | 16:19885b43130e |
---|---|
5 * License: $(LINK2 http://www.boost.org/LICENSE_1_0.txt, Boost Software License 1.0) | 5 * License: $(LINK2 http://www.boost.org/LICENSE_1_0.txt, Boost Software License 1.0) |
6 */ | 6 */ |
7 module dstep.objc.bridge.Type; | 7 module dstep.objc.bridge.Type; |
8 | 8 |
9 version (Tango) | 9 version (Tango) |
10 import tango.core.Tuple; | 10 import tango.core.Tuple : Tuple; |
11 | 11 |
12 else | 12 else |
13 import std.typetuple; | 13 import std.typetuple : Tuple; |
14 | 14 |
15 import dstep.objc.objc; | 15 import dstep.objc.objc; |
16 | 16 |
17 /** | |
18 * Evaluates to the Objective-C equivalent type for $(D_PARAM T). | |
19 * | |
20 * Examples: | |
21 * --- | |
22 * static assert(is(ObjcType!(Object) == id)); | |
23 * static assert(is(ObjcType!(int) == int)); | |
24 * --- | |
25 */ | |
17 template ObjcType (T) | 26 template ObjcType (T) |
18 { | 27 { |
19 static if (needsEncapsulation!(T)) | 28 static if (needsEncapsulation!(T)) |
20 alias id ObjcType; | 29 alias id ObjcType; |
21 | 30 |
22 else | 31 else |
23 alias T ObjcType; | 32 alias T ObjcType; |
24 } | 33 } |
25 | 34 |
35 /** | |
36 * Evaluates to $(D_KEYWORD true) if $(D_PARAM T) needs to be encapsulated. | |
37 * | |
38 * Examples: | |
39 * --- | |
40 * static assert(needsEncapsulation!(Object)); | |
41 * static assert(!needsEncapsulation!(int)); | |
42 * --- | |
43 */ | |
26 template needsEncapsulation (T) | 44 template needsEncapsulation (T) |
27 { | 45 { |
28 static if (is(T == class)) | 46 static if (is(T == class) || is (T : Object) || is(T == interface)) |
29 const needsEncapsulation = true; | 47 const needsEncapsulation = true; |
30 | 48 |
31 else | 49 else |
32 const needsEncapsulation = false; | 50 const needsEncapsulation = false; |
33 } | 51 } |
34 | 52 |
35 template ObjcTypeTuple (TList...) | 53 /** |
54 * Evaluates to a tuple of Objective-C equivalent types. | |
55 * | |
56 * Examples: | |
57 * --- | |
58 * static assert(is(typeof(ObjcTypes!(id, Object, id)) == typeof(Tuple!(id, id, id)))); | |
59 * static assert(is(typeof(ObjcTypes!(id, id, id)) == typeof(Tuple!(id, id, id)))); | |
60 * --- | |
61 */ | |
62 template ObjcTypes (Types...) | |
36 { | 63 { |
37 alias replaceClasses!(TList).types ObjcTypeTuple; | 64 alias EncapsulateTypes!(id, Types) ObjcTypes; |
38 } | 65 } |
39 | 66 |
40 private struct Wrap (TList...) | 67 /** |
68 * Evaluates to $(D_KEYWORD true) if $(D_PARAM T) has a constructor that takes one | |
69 * argument of the type $(D_PSYMBOL id). | |
70 * | |
71 * Examples: | |
72 * --- | |
73 * class NSString : NSObject | |
74 * { | |
75 * this (id object) | |
76 * { | |
77 * super(object); | |
78 * } | |
79 * } | |
80 * | |
81 * static assert(hasIdConstructor!(NSString)); | |
82 * static assert(!hasIdConstructor!(Object)); | |
83 * --- | |
84 */ | |
85 template hasIdConstructor (T) | |
41 { | 86 { |
42 TList types; | 87 const hasIdConstructor = is(typeof( |
88 { | |
89 objc_object o; | |
90 new T(&o); | |
91 })); | |
43 } | 92 } |
44 | 93 |
45 private Wrap!(TList) replaceClasses (TList...) () | 94 private template EncapsulateTypes (U, TList... ) |
46 { | 95 { |
47 alias Tuple!(TList) Types; | 96 static if( TList.length == 0 ) |
97 alias TList EncapsulateTypes; | |
48 | 98 |
49 foreach (ref type ; Types) | 99 else static if(needsEncapsulation!(TList[0])) |
50 static if (is(type == class)) | 100 alias Tuple!( U, EncapsulateTypes!(U, TList[1 .. $] ) ) EncapsulateTypes; |
51 type = id; | |
52 | 101 |
53 return Wrap!(Types); | 102 else |
103 alias Tuple!( TList[0], EncapsulateTypes!( U, TList[1 .. $] ) ) EncapsulateTypes; | |
54 } | 104 } |