Mercurial > projects > dstep
comparison dstep/internal/Traits.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 | 033d260cfc9b |
children | c2fba45df857 |
comparison
equal
deleted
inserted
replaced
15:7ff919f595d5 | 16:19885b43130e |
---|---|
3 * Authors: Jacob Carlborg | 3 * Authors: Jacob Carlborg |
4 * Version: Initial created: Apr 28, 2009 | 4 * Version: Initial created: Apr 28, 2009 |
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.internal.Traits; | 7 module dstep.internal.Traits; |
8 | |
9 import dstep.objc.bridge.Bridge; | |
10 import dstep.internal.String; | |
8 | 11 |
9 /** | 12 /** |
10 * Returns the name of the given function | 13 * Returns the name of the given function |
11 * | 14 * |
12 * Params: | 15 * Params: |
34 template parameterNamesOf (alias func) | 37 template parameterNamesOf (alias func) |
35 { | 38 { |
36 const parameterNamesOf = parameterNamesOfImpl!(func); | 39 const parameterNamesOf = parameterNamesOfImpl!(func); |
37 } | 40 } |
38 | 41 |
39 private char[][] parameterNamesOfImpl (alias func) () | 42 private string[] parameterNamesOfImpl (alias func) () |
40 { | 43 { |
41 char[] funcStr = typeof(&func).stringof; | 44 string funcStr = typeof(&func).stringof; |
42 | 45 |
43 auto start = funcStr.indexOf('('); | 46 auto start = funcStr.indexOf('('); |
44 auto end = funcStr.indexOf(')'); | 47 auto end = funcStr.indexOf(')'); |
45 | 48 |
46 const firstPattern = ' '; | 49 const firstPattern = ' '; |
47 const secondPattern = ','; | 50 const secondPattern = ','; |
51 if (funcStr == "") | 54 if (funcStr == "") |
52 return null; | 55 return null; |
53 | 56 |
54 funcStr ~= secondPattern; | 57 funcStr ~= secondPattern; |
55 | 58 |
56 char[] token; | 59 string token; |
57 char[][] arr; | 60 string[] arr; |
58 | 61 |
59 foreach (c ; funcStr) | 62 foreach (c ; funcStr) |
60 { | 63 { |
61 if (c != firstPattern && c != secondPattern) | 64 if (c != firstPattern && c != secondPattern) |
62 token ~= c; | 65 token ~= c; |
68 | 71 |
69 token = null; | 72 token = null; |
70 } | 73 } |
71 } | 74 } |
72 | 75 |
73 char[][] result; | 76 if (arr.length == 1) |
77 return arr; | |
78 | |
79 string[] result; | |
74 bool skip = false; | 80 bool skip = false; |
75 | 81 |
76 foreach (str ; arr) | 82 foreach (str ; arr) |
77 { | 83 { |
78 skip = !skip; | 84 skip = !skip; |
99 * | 105 * |
100 * Returns: the index of the element or size_t.max if the element was not found. | 106 * Returns: the index of the element or size_t.max if the element was not found. |
101 */ | 107 */ |
102 private size_t indexOf (T) (T[] arr, T element) | 108 private size_t indexOf (T) (T[] arr, T element) |
103 { | 109 { |
104 foreach (i, e ; arr) | 110 static if (is(T == char) || is(T == wchar) || is(T == dchar)) |
105 if (e == element) | 111 foreach (i, dchar e ; arr) |
106 return i; | 112 if (e == element) |
113 return i; | |
114 | |
115 else | |
116 foreach (i, e ; arr) | |
117 if (e == element) | |
118 return i; | |
107 | 119 |
108 return size_t.max; | 120 return size_t.max; |
109 } | 121 } |
122 | |
123 /** | |
124 * Evaluates to true if $(D_PARAM T) has a instance method with the given name | |
125 * | |
126 * Params: | |
127 * T = the type of the class/struct | |
128 * method = the name of the method | |
129 */ | |
130 template hasInstanceMethod (T, string method) | |
131 { | |
132 const hasInstanceMethod = is(typeof({ | |
133 T t; | |
134 mixin("auto f = &t." ~ method ~ ";"); | |
135 })); | |
136 } | |
137 | |
138 /** | |
139 * Evaluates to true if $(D_PARAM T) has a class method with the given name | |
140 * | |
141 * Params: | |
142 * T = the type of the class/struct | |
143 * method = the name of the method | |
144 */ | |
145 template hasClassMethod (T, string method) | |
146 { | |
147 const hasClassMethod = is(typeof({ | |
148 mixin("auto f = &T." ~ method ~ ";"); | |
149 })); | |
150 } | |
151 | |
152 /** | |
153 * Evaluates to true if $(D_PARAM T) has a either a class method or a instance method | |
154 * with the given name. | |
155 * | |
156 * Params: | |
157 * T = the type of the class/struct | |
158 * method = the name of the method | |
159 */ | |
160 template hasMethod (T, string method) | |
161 { | |
162 const hasMethod = hasClassMethod!(T, method) || hasInstanceMethod!(T, method); | |
163 } | |
164 | |
165 /** | |
166 * Evaluates to true if $(D_PARAM T) has a field with the given name | |
167 * | |
168 * Params: | |
169 * T = the type of the class/struct | |
170 * field = the name of the field | |
171 */ | |
172 template hasField (T, string field) | |
173 { | |
174 const hasField = is(typeof({ | |
175 T t; | |
176 mixin("auto f = t." ~ field ~ ";"); | |
177 })); | |
178 } | |
179 | |
180 /// Evaluates to true if $(D_PARAM T) has binded methods | |
181 template hasBindedMethods (T) | |
182 { | |
183 const bool hasBindedMethods = hasField!(T, Bridge.objcClassMethodDeclarationVar) || hasField!(T, Bridge.objcMethodDeclarationVar); | |
184 } |