Mercurial > projects > dstep
annotate dstep/internal/Traits.d @ 17:c2fba45df857
Fixed: indexOf didn't compile
author | Jacob Carlborg <doob@me.com> |
---|---|
date | Sun, 10 Jan 2010 13:22:58 +0100 |
parents | 19885b43130e |
children |
rev | line source |
---|---|
1 | 1 /** |
2 * Copyright: Copyright (c) 2009 Jacob Carlborg. | |
3 * Authors: Jacob Carlborg | |
4 * Version: Initial created: Apr 28, 2009 | |
5 * License: $(LINK2 http://www.boost.org/LICENSE_1_0.txt, Boost Software License 1.0) | |
6 */ | |
7 module dstep.internal.Traits; | |
8 | |
16
19885b43130e
Huge update, the bridge actually works now
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
9 import dstep.objc.bridge.Bridge; |
19885b43130e
Huge update, the bridge actually works now
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
10 import dstep.internal.String; |
19885b43130e
Huge update, the bridge actually works now
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
11 |
1 | 12 /** |
13 * Returns the name of the given function | |
14 * | |
15 * Params: | |
16 * func = the function alias to get the name of | |
17 * | |
18 * Returns: the name of the function | |
19 */ | |
20 template functionNameOf (alias func) | |
21 { | |
22 version(LDC) | |
23 const functionNameOf = (&func).stringof[1 .. $]; | |
24 | |
25 else | |
26 const functionNameOf = (&func).stringof[2 .. $]; | |
27 } | |
28 | |
29 /** | |
30 * Returns the parameter names of the given function | |
31 * | |
32 * Params: | |
33 * func = the function alias to get the parameter names of | |
34 * | |
35 * Returns: an array of strings containing the parameter names | |
36 */ | |
37 template parameterNamesOf (alias func) | |
38 { | |
39 const parameterNamesOf = parameterNamesOfImpl!(func); | |
40 } | |
41 | |
16
19885b43130e
Huge update, the bridge actually works now
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
42 private string[] parameterNamesOfImpl (alias func) () |
1 | 43 { |
16
19885b43130e
Huge update, the bridge actually works now
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
44 string funcStr = typeof(&func).stringof; |
19885b43130e
Huge update, the bridge actually works now
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
45 |
1 | 46 auto start = funcStr.indexOf('('); |
47 auto end = funcStr.indexOf(')'); | |
48 | |
49 const firstPattern = ' '; | |
50 const secondPattern = ','; | |
51 | |
52 funcStr = funcStr[start + 1 .. end]; | |
53 | |
54 if (funcStr == "") | |
55 return null; | |
56 | |
57 funcStr ~= secondPattern; | |
58 | |
16
19885b43130e
Huge update, the bridge actually works now
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
59 string token; |
19885b43130e
Huge update, the bridge actually works now
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
60 string[] arr; |
1 | 61 |
62 foreach (c ; funcStr) | |
63 { | |
64 if (c != firstPattern && c != secondPattern) | |
65 token ~= c; | |
66 | |
67 else | |
68 { | |
69 if (token) | |
70 arr ~= token; | |
71 | |
72 token = null; | |
73 } | |
74 } | |
75 | |
16
19885b43130e
Huge update, the bridge actually works now
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
76 if (arr.length == 1) |
19885b43130e
Huge update, the bridge actually works now
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
77 return arr; |
19885b43130e
Huge update, the bridge actually works now
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
78 |
19885b43130e
Huge update, the bridge actually works now
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
79 string[] result; |
1 | 80 bool skip = false; |
81 | |
82 foreach (str ; arr) | |
83 { | |
84 skip = !skip; | |
85 | |
86 if (skip) | |
87 continue; | |
88 | |
89 result ~= str; | |
90 } | |
91 | |
92 return result; | |
93 } | |
94 | |
95 /** | |
96 * Compile-time function to get the index of the give element. | |
97 * | |
98 * Performs a linear scan, returning the index of the first occurrence | |
99 * of the specified element in the array, or U.max if the array does | |
100 * not contain the element. | |
101 * | |
102 * Params: | |
103 * arr = the array to get the index of the element from | |
104 * element = the element to find | |
105 * | |
106 * Returns: the index of the element or size_t.max if the element was not found. | |
107 */ | |
108 private size_t indexOf (T) (T[] arr, T element) | |
109 { | |
17 | 110 foreach (i, e ; arr) |
111 if (e == element) | |
112 return i; | |
1 | 113 |
114 return size_t.max; | |
16
19885b43130e
Huge update, the bridge actually works now
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
115 } |
19885b43130e
Huge update, the bridge actually works now
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
116 |
17 | 117 //FIXME fix this when http://d.puremagic.com/issues/show_bug.cgi?id=3512 is fixed |
118 version (none) | |
119 { | |
120 | |
121 /** | |
122 * Compile-time function to get the index of the give element. | |
123 * | |
124 * Performs a linear scan, returning the index of the first occurrence | |
125 * of the specified element in the array, or U.max if the array does | |
126 * not contain the element. | |
127 * | |
128 * Params: | |
129 * arr = the array to get the index of the element from | |
130 * element = the element to find | |
131 * | |
132 * Returns: the index of the element or size_t.max if the element was not found. | |
133 */ | |
134 private size_t indexOf (T) (T[] arr, dchar element) | |
135 { | |
136 static assert(is(T == char) || is(T == wchar) || is(T == dchar), `dstep.internal.Traits.indexOf: The given type "` ~ T.stringof ~ `" is not valid, it has to be char, wchar or dchar`); | |
137 | |
138 foreach (i, dchar e ; arr) | |
139 if (e == element) | |
140 return i; | |
141 | |
142 return size_t.max; | |
143 } | |
144 } | |
145 | |
16
19885b43130e
Huge update, the bridge actually works now
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
146 /** |
19885b43130e
Huge update, the bridge actually works now
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
147 * Evaluates to true if $(D_PARAM T) has a instance method with the given name |
19885b43130e
Huge update, the bridge actually works now
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
148 * |
19885b43130e
Huge update, the bridge actually works now
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
149 * Params: |
19885b43130e
Huge update, the bridge actually works now
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
150 * T = the type of the class/struct |
19885b43130e
Huge update, the bridge actually works now
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
151 * method = the name of the method |
19885b43130e
Huge update, the bridge actually works now
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
152 */ |
19885b43130e
Huge update, the bridge actually works now
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
153 template hasInstanceMethod (T, string method) |
19885b43130e
Huge update, the bridge actually works now
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
154 { |
19885b43130e
Huge update, the bridge actually works now
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
155 const hasInstanceMethod = is(typeof({ |
19885b43130e
Huge update, the bridge actually works now
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
156 T t; |
19885b43130e
Huge update, the bridge actually works now
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
157 mixin("auto f = &t." ~ method ~ ";"); |
19885b43130e
Huge update, the bridge actually works now
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
158 })); |
19885b43130e
Huge update, the bridge actually works now
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
159 } |
19885b43130e
Huge update, the bridge actually works now
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
160 |
19885b43130e
Huge update, the bridge actually works now
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
161 /** |
19885b43130e
Huge update, the bridge actually works now
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
162 * Evaluates to true if $(D_PARAM T) has a class method with the given name |
19885b43130e
Huge update, the bridge actually works now
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
163 * |
19885b43130e
Huge update, the bridge actually works now
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
164 * Params: |
19885b43130e
Huge update, the bridge actually works now
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
165 * T = the type of the class/struct |
19885b43130e
Huge update, the bridge actually works now
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
166 * method = the name of the method |
19885b43130e
Huge update, the bridge actually works now
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
167 */ |
19885b43130e
Huge update, the bridge actually works now
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
168 template hasClassMethod (T, string method) |
19885b43130e
Huge update, the bridge actually works now
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
169 { |
19885b43130e
Huge update, the bridge actually works now
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
170 const hasClassMethod = is(typeof({ |
19885b43130e
Huge update, the bridge actually works now
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
171 mixin("auto f = &T." ~ method ~ ";"); |
19885b43130e
Huge update, the bridge actually works now
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
172 })); |
19885b43130e
Huge update, the bridge actually works now
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
173 } |
19885b43130e
Huge update, the bridge actually works now
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
174 |
19885b43130e
Huge update, the bridge actually works now
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
175 /** |
19885b43130e
Huge update, the bridge actually works now
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
176 * Evaluates to true if $(D_PARAM T) has a either a class method or a instance method |
19885b43130e
Huge update, the bridge actually works now
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
177 * with the given name. |
19885b43130e
Huge update, the bridge actually works now
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
178 * |
19885b43130e
Huge update, the bridge actually works now
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
179 * Params: |
19885b43130e
Huge update, the bridge actually works now
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
180 * T = the type of the class/struct |
19885b43130e
Huge update, the bridge actually works now
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
181 * method = the name of the method |
19885b43130e
Huge update, the bridge actually works now
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
182 */ |
19885b43130e
Huge update, the bridge actually works now
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
183 template hasMethod (T, string method) |
19885b43130e
Huge update, the bridge actually works now
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
184 { |
19885b43130e
Huge update, the bridge actually works now
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
185 const hasMethod = hasClassMethod!(T, method) || hasInstanceMethod!(T, method); |
19885b43130e
Huge update, the bridge actually works now
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
186 } |
19885b43130e
Huge update, the bridge actually works now
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
187 |
19885b43130e
Huge update, the bridge actually works now
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
188 /** |
19885b43130e
Huge update, the bridge actually works now
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
189 * Evaluates to true if $(D_PARAM T) has a field with the given name |
19885b43130e
Huge update, the bridge actually works now
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
190 * |
19885b43130e
Huge update, the bridge actually works now
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
191 * Params: |
19885b43130e
Huge update, the bridge actually works now
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
192 * T = the type of the class/struct |
19885b43130e
Huge update, the bridge actually works now
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
193 * field = the name of the field |
19885b43130e
Huge update, the bridge actually works now
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
194 */ |
19885b43130e
Huge update, the bridge actually works now
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
195 template hasField (T, string field) |
19885b43130e
Huge update, the bridge actually works now
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
196 { |
19885b43130e
Huge update, the bridge actually works now
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
197 const hasField = is(typeof({ |
19885b43130e
Huge update, the bridge actually works now
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
198 T t; |
19885b43130e
Huge update, the bridge actually works now
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
199 mixin("auto f = t." ~ field ~ ";"); |
19885b43130e
Huge update, the bridge actually works now
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
200 })); |
19885b43130e
Huge update, the bridge actually works now
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
201 } |
19885b43130e
Huge update, the bridge actually works now
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
202 |
19885b43130e
Huge update, the bridge actually works now
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
203 /// Evaluates to true if $(D_PARAM T) has binded methods |
19885b43130e
Huge update, the bridge actually works now
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
204 template hasBindedMethods (T) |
19885b43130e
Huge update, the bridge actually works now
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
205 { |
19885b43130e
Huge update, the bridge actually works now
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
206 const bool hasBindedMethods = hasField!(T, Bridge.objcClassMethodDeclarationVar) || hasField!(T, Bridge.objcMethodDeclarationVar); |
1 | 207 } |