Mercurial > projects > dstep
annotate dstep/objc/bridge/TypeEncoding.d @ 27:57371c29ef73 default tip
ObjcWrap is now automatically mixed in. Added support for building as a dylib with DMD.
author | Jacob Carlborg <doob@me.com> |
---|---|
date | Fri, 09 Apr 2010 23:00:22 +0200 |
parents | 19885b43130e |
children |
rev | line source |
---|---|
1 | 1 /** |
2 * Copyright: Copyright (c) 2009 Jacob Carlborg. | |
3 * Authors: Jacob Carlborg | |
4 * Version: Initial created: Feb 8, 2009 | |
5 * License: $(LINK2 http://www.boost.org/LICENSE_1_0.txt, Boost Software License 1.0) | |
6 */ | |
7 module dstep.objc.bridge.TypeEncoding; | |
8 | |
9 version (Tango) | |
10 import tango.core.Traits; | |
11 | |
12 else | |
13 import std.traits; | |
14 | |
15 import dstep.internal.String; | |
16 import dstep.internal.Traits; | |
16
19885b43130e
Huge update, the bridge actually works now
Jacob Carlborg <doob@me.com>
parents:
11
diff
changeset
|
17 import dstep.objc.bridge.Type; |
1 | 18 import dstep.objc.objc; |
19 import dstep.objc.runtime; | |
20 | |
21 /** | |
22 * Creates the encoded (mangled) form of a type for the | |
23 * Objective-C runtime. | |
24 */ | |
25 template encode (TL...) | |
26 { | |
27 const string encode = encodeImpl!(TL)(); | |
28 } | |
29 | |
30 private string encodeImpl (TL...) () | |
31 { | |
32 char[] s; | |
33 | |
34 foreach (T ; TL) | |
35 { | |
16
19885b43130e
Huge update, the bridge actually works now
Jacob Carlborg <doob@me.com>
parents:
11
diff
changeset
|
36 static if (is (T == id) || needsEncapsulation!(T)) |
1 | 37 s ~= _C_ID; |
38 | |
39 else static if (is (T == Class)) | |
40 s ~= _C_CLASS; | |
41 | |
42 else static if (is (T == SEL)) | |
43 s ~= _C_SEL; | |
44 | |
45 else static if (is (T == byte)) | |
46 s ~= _C_CHR; | |
47 | |
48 else static if (is (T == ubyte)) | |
49 s ~= _C_UCHR; | |
50 | |
51 else static if (is (T == short)) | |
52 s ~= _C_SHT; | |
53 | |
54 else static if (is (T == ushort)) | |
55 s ~= _C_USHT; | |
56 | |
57 else static if (is (T == int)) | |
58 s ~= _C_INT; | |
59 | |
60 else static if (is (T == uint)) | |
61 s ~= _C_UINT; | |
62 | |
63 else static if (is (T == long)) | |
64 s ~= _C_LNG_LNG; | |
65 | |
66 else static if (is (T == ulong)) | |
67 s ~= _C_ULNG_LNG; | |
68 | |
69 else static if (is (T == float)) | |
70 s ~= _C_FLT; | |
71 | |
72 else static if (is (T == double)) | |
73 s ~= _C_DBL; | |
74 | |
75 else static if (is (T == bool)) | |
76 s ~= _C_BOOL; | |
77 | |
78 else static if (is (T == void)) | |
79 s ~= _C_VOID; | |
80 | |
81 else static if (is (T U : T*)) | |
82 s ~= _C_VOID ~ encode!(U); | |
83 | |
84 else static if (is (T : char*)) | |
85 s ~= _C_CHARPTR; | |
86 | |
87 else static if (is (T U : T)) | |
88 s ~= _C_ARY_B ~ encode!(U) ~ _C_ARY_E; | |
89 | |
90 else static if (is (T == union)) | |
91 s ~= _C_UNION_B ~ encode!(typeof(T.tupleof)) ~ _C_UNION_E; | |
92 | |
93 else static if (is (T == struct)) | |
94 s ~= _C_STRUCT_B ~ encode!(typeof(T.tupleof)) ~ _C_STRUCT_E; | |
95 | |
96 else | |
97 s ~= _C_UNDEF; | |
98 } | |
99 | |
100 return s; | |
101 } | |
102 | |
103 /** | |
104 * Create the encoded (mangled) form of a type for the | |
105 * Objective-C runtime out of the given callable type. | |
106 * This is a shortcut for encode | |
107 */ | |
108 template encodeCallable (alias Callable) | |
109 { | |
110 static assert(isCallableType!(typeof(Callable)), "dstep.objc.bridge.TypeCoding.encodeCallable: The given type is not a callable type"); | |
111 | |
112 const encodeCallable = encode!(ReturnTypeOf!(typeof(Callable)), id, SEL, ParameterTupleOf!(typeof(Callable))); | |
113 } | |
114 | |
16
19885b43130e
Huge update, the bridge actually works now
Jacob Carlborg <doob@me.com>
parents:
11
diff
changeset
|
115 template checkSelector (string selector, ARGS...) |
19885b43130e
Huge update, the bridge actually works now
Jacob Carlborg <doob@me.com>
parents:
11
diff
changeset
|
116 { |
19885b43130e
Huge update, the bridge actually works now
Jacob Carlborg <doob@me.com>
parents:
11
diff
changeset
|
117 const checkSelector = checkSelectorImpl!(selector, ARGS); |
19885b43130e
Huge update, the bridge actually works now
Jacob Carlborg <doob@me.com>
parents:
11
diff
changeset
|
118 } |
19885b43130e
Huge update, the bridge actually works now
Jacob Carlborg <doob@me.com>
parents:
11
diff
changeset
|
119 |
19885b43130e
Huge update, the bridge actually works now
Jacob Carlborg <doob@me.com>
parents:
11
diff
changeset
|
120 private bool checkSelectorImpl (string selector, ARGS...) () |
19885b43130e
Huge update, the bridge actually works now
Jacob Carlborg <doob@me.com>
parents:
11
diff
changeset
|
121 { |
19885b43130e
Huge update, the bridge actually works now
Jacob Carlborg <doob@me.com>
parents:
11
diff
changeset
|
122 size_t i = 0; |
19885b43130e
Huge update, the bridge actually works now
Jacob Carlborg <doob@me.com>
parents:
11
diff
changeset
|
123 |
19885b43130e
Huge update, the bridge actually works now
Jacob Carlborg <doob@me.com>
parents:
11
diff
changeset
|
124 foreach (c ; selector) |
19885b43130e
Huge update, the bridge actually works now
Jacob Carlborg <doob@me.com>
parents:
11
diff
changeset
|
125 if (c == ':') |
19885b43130e
Huge update, the bridge actually works now
Jacob Carlborg <doob@me.com>
parents:
11
diff
changeset
|
126 i++; |
19885b43130e
Huge update, the bridge actually works now
Jacob Carlborg <doob@me.com>
parents:
11
diff
changeset
|
127 |
19885b43130e
Huge update, the bridge actually works now
Jacob Carlborg <doob@me.com>
parents:
11
diff
changeset
|
128 return i == ARGS.length; |
19885b43130e
Huge update, the bridge actually works now
Jacob Carlborg <doob@me.com>
parents:
11
diff
changeset
|
129 } |
19885b43130e
Huge update, the bridge actually works now
Jacob Carlborg <doob@me.com>
parents:
11
diff
changeset
|
130 |
1 | 131 /** |
132 * Builds a string representing a selector out of the given function | |
133 * | |
134 * It will build the string like this: | |
135 * | |
136 * --- | |
137 * foo (int x, int y); // foo:y: | |
138 * bar (); // bar | |
139 * fooBar (int x); // fooBar: | |
140 * --- | |
141 * | |
142 * Params: | |
143 * func = the function alias to build the selector of | |
144 * | |
145 * Returns: a string representing the selector | |
146 */ | |
147 template buildSelector (alias method) | |
148 { | |
149 const buildSelector = buildSelectorImpl!(method); | |
150 } | |
151 | |
152 private string buildSelectorImpl (alias method) () | |
153 { | |
154 string result = functionNameOf!(method); | |
155 string[] parameterNames = parameterNamesOf!(method); | |
156 | |
157 if (parameterNames.length == 0) | |
158 return result; | |
159 | |
160 else if (parameterNames.length == 1) | |
161 return result ~ ':'; | |
162 | |
163 else if (parameterNames.length >= 2) | |
164 parameterNames = parameterNames[1 .. $]; | |
165 | |
166 result ~= ':'; | |
167 | |
168 foreach (str ; parameterNames) | |
169 result ~= str ~ ':'; | |
170 | |
171 return result; | |
172 } | |
173 | |
16
19885b43130e
Huge update, the bridge actually works now
Jacob Carlborg <doob@me.com>
parents:
11
diff
changeset
|
174 template decimalDigit (int n) // [3] |
19885b43130e
Huge update, the bridge actually works now
Jacob Carlborg <doob@me.com>
parents:
11
diff
changeset
|
175 { |
19885b43130e
Huge update, the bridge actually works now
Jacob Carlborg <doob@me.com>
parents:
11
diff
changeset
|
176 const string decimalDigit = "0123456789"[n .. n + 1]; |
19885b43130e
Huge update, the bridge actually works now
Jacob Carlborg <doob@me.com>
parents:
11
diff
changeset
|
177 } |
19885b43130e
Huge update, the bridge actually works now
Jacob Carlborg <doob@me.com>
parents:
11
diff
changeset
|
178 |
19885b43130e
Huge update, the bridge actually works now
Jacob Carlborg <doob@me.com>
parents:
11
diff
changeset
|
179 template itoa (long n) |
19885b43130e
Huge update, the bridge actually works now
Jacob Carlborg <doob@me.com>
parents:
11
diff
changeset
|
180 { |
19885b43130e
Huge update, the bridge actually works now
Jacob Carlborg <doob@me.com>
parents:
11
diff
changeset
|
181 static if (n < 0) |
19885b43130e
Huge update, the bridge actually works now
Jacob Carlborg <doob@me.com>
parents:
11
diff
changeset
|
182 const string itoa = "-" ~ itoa!(-n); |
19885b43130e
Huge update, the bridge actually works now
Jacob Carlborg <doob@me.com>
parents:
11
diff
changeset
|
183 |
19885b43130e
Huge update, the bridge actually works now
Jacob Carlborg <doob@me.com>
parents:
11
diff
changeset
|
184 else static if (n < 10) |
19885b43130e
Huge update, the bridge actually works now
Jacob Carlborg <doob@me.com>
parents:
11
diff
changeset
|
185 const string itoa = decimalDigit!(n); |
19885b43130e
Huge update, the bridge actually works now
Jacob Carlborg <doob@me.com>
parents:
11
diff
changeset
|
186 |
19885b43130e
Huge update, the bridge actually works now
Jacob Carlborg <doob@me.com>
parents:
11
diff
changeset
|
187 else |
19885b43130e
Huge update, the bridge actually works now
Jacob Carlborg <doob@me.com>
parents:
11
diff
changeset
|
188 const string itoa = itoa!(n / 10L) ~ decimalDigit!(n % 10L); |
19885b43130e
Huge update, the bridge actually works now
Jacob Carlborg <doob@me.com>
parents:
11
diff
changeset
|
189 } |
19885b43130e
Huge update, the bridge actually works now
Jacob Carlborg <doob@me.com>
parents:
11
diff
changeset
|
190 |
19885b43130e
Huge update, the bridge actually works now
Jacob Carlborg <doob@me.com>
parents:
11
diff
changeset
|
191 |
19885b43130e
Huge update, the bridge actually works now
Jacob Carlborg <doob@me.com>
parents:
11
diff
changeset
|
192 /// Converts the given string representing an OSType into an integer. |
11
07194b026fa4
Added bindings to a couple of frameworks, new license + some other things
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
193 template getOSType (string osType) |
07194b026fa4
Added bindings to a couple of frameworks, new license + some other things
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
194 { |
16
19885b43130e
Huge update, the bridge actually works now
Jacob Carlborg <doob@me.com>
parents:
11
diff
changeset
|
195 static assert(osType.length == 4, `dstep.objc.bridge.TypeEncoding.getOSType: The length of the given string, "` ~ osType ~ `", is not four`); |
11
07194b026fa4
Added bindings to a couple of frameworks, new license + some other things
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
196 const getOSType = (cast(int)osType[0]) << 24 | (cast(int)osType[1]) << 16 | (cast(int)osType[2]) << 8 | (cast(int)osType[3]); |
07194b026fa4
Added bindings to a couple of frameworks, new license + some other things
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
197 } |