345
|
1 /*********************************************************
|
|
2 Copyright: QtD Team, 2010
|
|
3 Authors: Max Samukha
|
|
4 License: Boost Software License 1.0
|
|
5 *********************************************************/
|
|
6 module qtd.Attribute;
|
|
7
|
|
8 import
|
|
9 lds.meta.compiletime,
|
|
10 std.traits,
|
|
11 std.conv,
|
|
12 std.typetuple;
|
|
13
|
|
14 enum standardNamespace = "qtd";
|
|
15
|
|
16 /**
|
|
17 */
|
|
18 enum AttributeOptions
|
|
19 {
|
|
20 /**
|
|
21 */
|
|
22 none,
|
|
23
|
|
24 /**
|
|
25 Allows multiple attributes of the same category to be associated with the symbol.
|
|
26 */
|
346
|
27 allowMultiple = 0x0000_0001,
|
|
28
|
347
|
29 /* internal */ inner = 0x0000_0002,
|
|
30
|
|
31 /**
|
|
32 Attribute data are in key-value form.
|
|
33 */
|
|
34 named = 0x0000_0004
|
|
35 }
|
|
36
|
|
37 /**
|
|
38 When mixed in an aggregate, converts a compile-time tuple to
|
|
39 members of that aggregate.
|
|
40 */
|
|
41 mixin template tupleToMembers!(string nameSpace, size_t index, A...)
|
|
42 {
|
|
43 static if (index < A.length)
|
|
44 {
|
|
45 enum indexStr = to!string(index);
|
|
46
|
|
47 static if (is(__traits(compiles, { struct { typeof(A[index]) x; } }() })))
|
|
48 mixin("typeof(A[" ~ indexStr ~ "]) " ~ nameSpace ~ ~ " = A[" ~ indexStr ~"];\n" ~ next;
|
|
49 else
|
|
50 mixin("alias A[" ~ indexStr ~ "] " ~ nameSpace ~ indexStr ~ ";\n" ~ next;
|
|
51
|
|
52 mixin tupleToFields!(nameSpace, index + 1, A);
|
|
53 }
|
|
54 }
|
|
55
|
|
56 /**
|
|
57 When mixed in an aggregate, converts a compile-time tuple of name-value pairs to
|
|
58 members of that aggregate.
|
|
59 */
|
|
60 struct NamedValueTupleToFields(A...)
|
|
61 {
|
|
62
|
|
63 }
|
|
64
|
|
65 version (QtdUnittest)
|
|
66 {
|
|
67 unittest
|
|
68 {
|
|
69 static int foo()
|
|
70 {
|
|
71 return 42;
|
|
72 }
|
|
73
|
|
74 static struct S
|
|
75 {
|
|
76 mixin TupleToFields!("field", 0,
|
|
77 int,
|
|
78 "a",
|
|
79 22,
|
|
80 foo);
|
|
81 }
|
|
82
|
|
83 static assert(is(S.field0 == int));
|
|
84 S s;
|
|
85 assert(s.field1 == "a");
|
|
86 assert(s.field2 == "22");
|
|
87 assert(S.foo() == 42);
|
|
88 }
|
345
|
89 }
|
|
90
|
|
91 private template attributeId(alias symbol, uint index = 0)
|
|
92 {
|
|
93 enum attributeId = standardNamespace ~ "_attr_" ~ uniqueId!symbol ~ "_" ~ to!string(index);
|
|
94 }
|
|
95
|
|
96 /**
|
|
97 Attributes allow to associate arbitrary compile-time data
|
|
98 with a declaration and optionaly make that data available at run-time.
|
|
99
|
|
100 ----
|
|
101 class A
|
|
102 {
|
|
103 }
|
|
104 mixin Attribute!(C, "someAttribute", "B");
|
|
105 ----
|
|
106
|
|
107 The example above associates the string "B" with class A under the attribute category "someAttribute".
|
|
108 Multiple data can be associated with an attribute category for a single declaration:
|
|
109
|
|
110 ----
|
|
111 class A
|
|
112 {
|
|
113 }
|
|
114 mixin Attribute!(C, "someAttribute", "B");
|
|
115 mixin Attribute!(C, "someAttribute", AttributeOptions.allowMultiple, "C");
|
|
116 ----
|
|
117
|
|
118 Attributes of an attribute category can be accessed at compile-time:
|
|
119
|
|
120 ----
|
|
121 alias GetAttributes!(C, "someAttribute") attrs;
|
|
122 ----
|
|
123
|
|
124 GetAttribute returns a compile time tuple, which contains "someAttribute" category attributes in the form
|
|
125 of TypeTupleWrapper instances. The first element of the tuple wrapped in the TypeTupleWrapper
|
|
126 contains the attribute category name, other elements contain the attrubute data.
|
|
127
|
|
128 ----
|
|
129 alias attrs[0] attr0;
|
|
130 alias attrs[1] attr1;
|
|
131
|
|
132 static assert(attrs0.tuple[0] == "someAttribute" && attrs0.tuple[1] == "B");
|
|
133 static assert(attrs1.tuple[0] == "someAttribute" && attrs1.tuple[1] == "C");
|
|
134 ----
|
|
135
|
|
136 Attributes can be inserted inside the body of a declaration.
|
|
137 ----
|
|
138 class A
|
|
139 {
|
|
140 mixin Attribute!("someAttribute", "B");
|
|
141 }
|
|
142 ----
|
|
143
|
|
144 Attributes can be made available at run time by means of the declaration's meta-object
|
|
145 ----
|
|
146 // prints category names of all attributes associated with class A
|
|
147 auto a = new A;
|
|
148 foreach (attr; a.metaObject.attributes)
|
|
149 writeln(attr.name);
|
|
150 ----
|
|
151
|
|
152 Attributes can be specialized
|
|
153
|
|
154 ----
|
|
155 mixin template DbFieldAttribute(alias prop, string columnName)
|
|
156 {
|
|
157 mixin Attribute!(prop, "DbFieldAttribute", columnName);
|
|
158 }
|
|
159
|
|
160 class A
|
|
161 {
|
|
162 int value;
|
|
163 mixin DbFieldAttribute!(value, "Value");
|
|
164
|
|
165 int anotherValue;
|
|
166 mixin DbFieldAttribute!(anotherValue, "Value2");
|
|
167 }
|
|
168
|
|
169 assert(GetAttributes!(A.value, "DbFieldAttribute")[0].tuple[1] == "Value");
|
|
170 assert(GetAttributes!(A.anotherValue, "DbFieldAttribute")[0].tuple[1] == "Value2");
|
|
171 ----
|
|
172
|
|
173 */
|
|
174 mixin template Attribute(alias symbol, string attrClass, A...)
|
|
175 {
|
|
176 mixin Attribute!(symbol, attrClass, AttributeOptions.none, A);
|
|
177 }
|
|
178
|
|
179 /// ditto
|
|
180 mixin template Attribute(alias symbol, string attrClass, AttributeOptions opts, A...)
|
|
181 {
|
|
182 mixin AttributeImpl!(symbol, attrClass, opts, 0, A);
|
|
183 }
|
|
184
|
|
185
|
|
186 /// ditto
|
|
187 // TODO: probably make this an overload of Attribute
|
|
188 mixin template InnerAttribute(string attrClass, AttributeOptions opts, A...)
|
|
189 {
|
|
190 // BUG: needs to be generalized to accept any parent
|
|
191 mixin Attribute!(typeof(this), attrClass, opts | AttributeOptions.inner, A);
|
|
192 }
|
|
193
|
|
194 // ditto
|
|
195 mixin template InnerAttribute(string attrClass, A...)
|
|
196 {
|
|
197 // BUG: needs to be generalized to accept any parent
|
|
198 mixin InnerAttribute!(attrClass, AttributeOptions.none, A);
|
|
199 }
|
|
200
|
|
201 private mixin template AttributeImpl(alias symbol, string attrClass, AttributeOptions opts, size_t index, A...)
|
|
202 {
|
|
203 private enum attrId = attributeId!(symbol, index) ~ (opts & AttributeOptions.inner ? "_inner" : "");
|
|
204
|
|
205 static if (is(typeof(mixin(attrId))))
|
|
206 {
|
|
207 mixin ("alias " ~ attrId ~ " attr;");
|
|
208 static if (!(opts & AttributeOptions.allowMultiple))
|
|
209 {
|
|
210 static assert (attr[0] != attrClass, "Multiple " ~ attrClass ~ " attributes are not allowed for "
|
|
211 ~ __traits(parent, symbol).stringof ~ "." ~ stringOf!symbol);
|
|
212 }
|
|
213
|
|
214 mixin AttributeImpl!(symbol, attrClass, opts, index + 1, A);
|
|
215 }
|
|
216 else
|
|
217 mixin ("alias TypeTuple!(attrClass, A) " ~ attrId ~ ";");
|
|
218 }
|
|
219
|
347
|
220 /**
|
|
221 Base class for run time attributes
|
|
222 */
|
|
223 abstract class MetaAttribute
|
|
224 {
|
|
225 }
|
|
226
|
|
227 /**
|
|
228 Default implementation of run time attributes
|
|
229 */
|
|
230 final class MetaAttributeVariant : MetaAttribute
|
|
231 {
|
|
232 private:
|
|
233 Variant[] values_;
|
|
234
|
|
235 public:
|
|
236 Variant values()
|
|
237 {
|
|
238 return values_;
|
|
239 }
|
|
240
|
|
241 static MetaAttributeVariant create(string category, AttributeOptions opts, A...)()
|
|
242 {
|
|
243 }
|
|
244 }
|
|
245
|
|
246 class MetaAttributeTypedImpl(A...)
|
|
247 {
|
|
248 }
|
|
249
|
|
250
|
|
251 abstract class MetaAtributeTyped : MetaAttribute
|
|
252 {
|
|
253 void construct(A...)()
|
|
254 {
|
|
255 }
|
|
256 }
|
|
257
|
|
258
|
|
259
|
345
|
260 private string stringOfFunction(alias symbol)()
|
|
261 {
|
|
262 auto ptrType = typeof(&symbol).stringof;
|
|
263 auto paramList = ParameterTypeTuple!(symbol).stringof;
|
|
264
|
|
265 string result = ReturnType!(symbol).stringof ~ " " ~ __traits(identifier, symbol) ~ paramList;
|
|
266
|
|
267 if (ptrType[$ - 1] != ')')
|
|
268 {
|
|
269 for (size_t i = ptrType.length - 2;; --i)
|
|
270 {
|
|
271 if (ptrType[i] == ')')
|
|
272 {
|
|
273 result ~= ptrType[i + 1..$];
|
|
274 break;
|
|
275 }
|
|
276 }
|
|
277 }
|
|
278
|
|
279 return result;
|
|
280 }
|
|
281
|
|
282 /**
|
|
283 String of any symbol, including functions
|
|
284 */
|
|
285 template stringOf(alias symbol)
|
|
286 {
|
|
287 static if (isFunction!symbol)
|
|
288 enum stringOf = stringOfFunction!symbol;
|
|
289 else
|
|
290 enum stringOf = symbol.stringof;
|
|
291 }
|
|
292
|
|
293 /**
|
|
294 Returns the string uniquely identifying the
|
|
295 symbol in its container.
|
|
296 */
|
|
297 template uniqueName(alias symbol)
|
|
298 {
|
|
299 enum uniqueName = stringOf!symbol;
|
|
300 }
|
|
301
|
|
302 // TODO: works only for simple types. implement
|
|
303 /**
|
|
304 *
|
|
305 */
|
|
306 string uniqueIdImpl(string symbol)
|
|
307 {
|
|
308 char[] r = symbol.dup;
|
|
309 foreach (i, c; symbol)
|
|
310 {
|
|
311 if (c == '(' || c == ')' || c == ' ')
|
|
312 r[i] = '_';
|
|
313 }
|
|
314
|
|
315 return cast(immutable)r;
|
|
316 }
|
|
317
|
|
318 template uniqueId(alias symbol)
|
|
319 {
|
|
320 enum uniqueId = uniqueIdImpl(stringOf!symbol);
|
|
321 }
|
|
322
|
|
323 version (QtdUnittest)
|
|
324 {
|
|
325 unittest
|
|
326 {
|
|
327 static class C
|
|
328 {
|
|
329 void foo() const {};
|
|
330 bool bar(int) { return true; };
|
|
331 int x;
|
|
332 static assert (stringOf!foo == "void foo() const");
|
|
333 static assert (uniqueName!foo == "void foo() const");
|
|
334 static assert (stringOf!bar == "bool bar(int)");
|
|
335 static assert (uniqueName!bar == "bool bar(int)");
|
|
336 static assert (stringOf!x == "x");
|
|
337 static assert (uniqueName!x == "x");
|
|
338 }
|
|
339
|
|
340 static assert (stringOf!(C.foo) == "void foo() const");
|
|
341 static assert (uniqueName!(C.foo) == "void foo() const");
|
|
342 static assert (stringOf!(C.bar) == "bool bar(int)");
|
|
343 static assert (uniqueName!(C.bar) == "bool bar(int)");
|
|
344
|
|
345 static assert (stringOf!(C.x) == "x");
|
|
346 static assert (uniqueName!(C.x) == "x");
|
|
347 }
|
|
348 }
|
|
349
|
|
350 template truePred(A...)
|
|
351 {
|
|
352 enum truePred = true;
|
|
353 }
|
|
354
|
|
355 template attrCategoryPred(string name)
|
|
356 {
|
|
357 template attrCategoryPred(A...)
|
|
358 {
|
|
359 enum attrCategoryPred = A[0] == name;
|
|
360 }
|
|
361 }
|
|
362
|
|
363 /**
|
|
364 Returns a compile-time tuple of attributes that
|
|
365 match pred.
|
|
366 */
|
|
367 template GetAttributes(alias symbol, alias pred = truePred)
|
|
368 {
|
|
369 alias GetAttributesImpl!(symbol, 0, pred).result GetAttributes;
|
|
370 }
|
|
371
|
|
372 /**
|
|
373 Returns a compile-time tuple of attributes
|
|
374 matching the specified attribute category.
|
|
375 */
|
|
376 template GetAttributes(alias symbol, string attrCategory)
|
|
377 {
|
|
378 alias GetAttributes!(symbol, attrCategoryPred!attrCategory) GetAttributes;
|
|
379 }
|
|
380
|
|
381
|
|
382 template GetAttributesImpl(alias symbol, size_t index, alias pred)
|
|
383 {
|
|
384 //pragma(msg, mixin("symbol." ~ attributeId!(symbol, index) ~ "_inner").stringof);
|
|
385 static if (is(typeof(mixin("__traits(parent, symbol)." ~ attributeId!(symbol, index)))))
|
|
386 mixin ("alias lds.meta.compiletime.Alias!(__traits(parent, symbol))." ~ attributeId!(symbol, index) ~ " attr;");
|
|
387 else static if (is(typeof(mixin("symbol." ~ attributeId!(symbol, index) ~ "_inner"))))
|
|
388 mixin ("alias symbol." ~ attributeId!(symbol, index) ~ "_inner attr;");
|
|
389
|
|
390 static if (is(typeof(attr)))
|
|
391 {
|
|
392 alias GetAttributesImpl!(symbol, index + 1, pred).result next;
|
|
393
|
|
394 static if (pred!attr)
|
|
395 alias TypeTuple!(TypeTupleWrapper!attr, next) result;
|
|
396 else
|
|
397 alias next result;
|
|
398 }
|
|
399 else
|
|
400 alias TypeTuple!() result;
|
|
401 }
|
|
402
|
|
403 version (QtdUnittest)
|
|
404 {
|
|
405 mixin template MyAttribute(alias symbol, A...)
|
|
406 {
|
|
407 mixin Attribute!(symbol, "MyAttribute", AttributeOptions.allowMultiple, A);
|
|
408 }
|
|
409
|
|
410 mixin template ClassInfo(string name, alias value)
|
|
411 {
|
|
412 mixin InnerAttribute!("ClassInfo", AttributeOptions.allowMultiple, name, value);
|
|
413 }
|
|
414
|
|
415 unittest
|
|
416 {
|
|
417 static class C
|
|
418 {
|
|
419 // inner C attributes
|
|
420 mixin InnerAttribute!("Inner", 33); // generic
|
|
421 mixin ClassInfo!("version", 123);
|
|
422 mixin ClassInfo!("author", "James Bond");
|
|
423
|
|
424
|
|
425 void foo() {};
|
|
426 // foo attributes
|
|
427 mixin Attribute!(foo, "SomeAttribute", 42);
|
|
428 mixin MyAttribute!(foo, 1, 2);
|
|
429 mixin MyAttribute!(foo, 3, 4);
|
|
430
|
|
431 alias GetAttributes!(typeof(this), "Inner") innerAttrs;
|
|
432 static assert(innerAttrs[0].tuple[0] == "Inner");
|
|
433 }
|
|
434 // outer C attribute
|
|
435 mixin MyAttribute!(C, 24);
|
|
436
|
|
437 alias GetAttributes!(C, "Inner") innerAttrs;
|
|
438 static assert(innerAttrs[0].tuple[0] == "Inner" && innerAttrs[0].tuple[1] == 33);
|
|
439
|
|
440 alias GetAttributes!(C, "ClassInfo") ciAttrs;
|
|
441 static assert(ciAttrs[0].tuple[1] == "version" && ciAttrs[0].tuple[2] == 123);
|
|
442
|
|
443 alias GetAttributes!(C.foo, "SomeAttribute") someAttr;
|
|
444 static assert(someAttr.length == 1);
|
|
445 static assert(someAttr[0].tuple[0] == "SomeAttribute");
|
|
446
|
|
447 alias GetAttributes!(C.foo, "MyAttribute") myAttrs;
|
|
448
|
|
449 //COMPILER BUG: cannot 'alias myAttrs[0].tuple myAttrs_0';
|
|
450 static assert(myAttrs[0].tuple[0] == "MyAttribute");
|
|
451 static assert(myAttrs[0].tuple[1] == 1 && myAttrs[0].tuple[2] == 2);
|
|
452
|
|
453 static assert(myAttrs[1].tuple[0] == "MyAttribute");
|
|
454 static assert(myAttrs[1].tuple[1] == 3 && myAttrs[1].tuple[2] == 4);
|
|
455
|
|
456 /+ BUG: Fails: local declarations cannot be accessed as parent.localDecl
|
|
457 alias GetAttributes!(C, "MyAttribute") myAttrs2;
|
|
458 static assert(myAttrs2[0].tuple[0] == "MyAttribute");
|
|
459 static assert(myAttrs2[0].tuple[1] == 24);
|
|
460 +/
|
|
461 }
|
|
462 } |