Mercurial > projects > dstep
annotate 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 |
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 { | |
16
19885b43130e
Huge update, the bridge actually works now
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
110 static if (is(T == char) || is(T == wchar) || is(T == dchar)) |
19885b43130e
Huge update, the bridge actually works now
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
111 foreach (i, dchar e ; arr) |
19885b43130e
Huge update, the bridge actually works now
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
112 if (e == element) |
19885b43130e
Huge update, the bridge actually works now
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
113 return i; |
19885b43130e
Huge update, the bridge actually works now
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
114 |
19885b43130e
Huge update, the bridge actually works now
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
115 else |
19885b43130e
Huge update, the bridge actually works now
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
116 foreach (i, e ; arr) |
19885b43130e
Huge update, the bridge actually works now
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
117 if (e == element) |
19885b43130e
Huge update, the bridge actually works now
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
118 return i; |
1 | 119 |
120 return size_t.max; | |
16
19885b43130e
Huge update, the bridge actually works now
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
121 } |
19885b43130e
Huge update, the bridge actually works now
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
122 |
19885b43130e
Huge update, the bridge actually works now
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
123 /** |
19885b43130e
Huge update, the bridge actually works now
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
124 * 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
|
125 * |
19885b43130e
Huge update, the bridge actually works now
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
126 * Params: |
19885b43130e
Huge update, the bridge actually works now
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
127 * T = the type of the class/struct |
19885b43130e
Huge update, the bridge actually works now
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
128 * method = the name of the method |
19885b43130e
Huge update, the bridge actually works now
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
129 */ |
19885b43130e
Huge update, the bridge actually works now
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
130 template hasInstanceMethod (T, string method) |
19885b43130e
Huge update, the bridge actually works now
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
131 { |
19885b43130e
Huge update, the bridge actually works now
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
132 const hasInstanceMethod = is(typeof({ |
19885b43130e
Huge update, the bridge actually works now
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
133 T t; |
19885b43130e
Huge update, the bridge actually works now
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
134 mixin("auto f = &t." ~ method ~ ";"); |
19885b43130e
Huge update, the bridge actually works now
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
135 })); |
19885b43130e
Huge update, the bridge actually works now
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
136 } |
19885b43130e
Huge update, the bridge actually works now
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
137 |
19885b43130e
Huge update, the bridge actually works now
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
138 /** |
19885b43130e
Huge update, the bridge actually works now
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
139 * 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
|
140 * |
19885b43130e
Huge update, the bridge actually works now
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
141 * Params: |
19885b43130e
Huge update, the bridge actually works now
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
142 * T = the type of the class/struct |
19885b43130e
Huge update, the bridge actually works now
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
143 * method = the name of the method |
19885b43130e
Huge update, the bridge actually works now
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
144 */ |
19885b43130e
Huge update, the bridge actually works now
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
145 template hasClassMethod (T, string method) |
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 const hasClassMethod = is(typeof({ |
19885b43130e
Huge update, the bridge actually works now
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
148 mixin("auto f = &T." ~ method ~ ";"); |
19885b43130e
Huge update, the bridge actually works now
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
149 })); |
19885b43130e
Huge update, the bridge actually works now
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
150 } |
19885b43130e
Huge update, the bridge actually works now
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
151 |
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 * 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
|
154 * with the given name. |
19885b43130e
Huge update, the bridge actually works now
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
155 * |
19885b43130e
Huge update, the bridge actually works now
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
156 * Params: |
19885b43130e
Huge update, the bridge actually works now
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
157 * T = the type of the class/struct |
19885b43130e
Huge update, the bridge actually works now
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
158 * method = the name of the method |
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 template hasMethod (T, string method) |
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 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
|
163 } |
19885b43130e
Huge update, the bridge actually works now
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
164 |
19885b43130e
Huge update, the bridge actually works now
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
165 /** |
19885b43130e
Huge update, the bridge actually works now
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
166 * 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
|
167 * |
19885b43130e
Huge update, the bridge actually works now
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
168 * Params: |
19885b43130e
Huge update, the bridge actually works now
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
169 * T = the type of the class/struct |
19885b43130e
Huge update, the bridge actually works now
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
170 * field = the name of the field |
19885b43130e
Huge update, the bridge actually works now
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
171 */ |
19885b43130e
Huge update, the bridge actually works now
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
172 template hasField (T, string field) |
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 const hasField = is(typeof({ |
19885b43130e
Huge update, the bridge actually works now
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
175 T t; |
19885b43130e
Huge update, the bridge actually works now
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
176 mixin("auto f = t." ~ field ~ ";"); |
19885b43130e
Huge update, the bridge actually works now
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
177 })); |
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 |
19885b43130e
Huge update, the bridge actually works now
Jacob Carlborg <doob@me.com>
parents:
1
diff
changeset
|
180 /// 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
|
181 template hasBindedMethods (T) |
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 const bool hasBindedMethods = hasField!(T, Bridge.objcClassMethodDeclarationVar) || hasField!(T, Bridge.objcMethodDeclarationVar); |
1 | 184 } |