comparison d2/qtd/Attribute.d @ 348:29ea6511681f

more attribute prototyping
author maxter <spambox@d-coding.com>
date Sat, 15 May 2010 00:29:47 +0300
parents 970332a88b72
children 31520b2c0b3c
comparison
equal deleted inserted replaced
347:970332a88b72 348:29ea6511681f
7 7
8 import 8 import
9 lds.meta.compiletime, 9 lds.meta.compiletime,
10 std.traits, 10 std.traits,
11 std.conv, 11 std.conv,
12 std.variant,
12 std.typetuple; 13 std.typetuple;
13 14
14 enum standardNamespace = "qtd"; 15 enum standardNamespace = "qtd";
15 16
16 /** 17 /**
27 allowMultiple = 0x0000_0001, 28 allowMultiple = 0x0000_0001,
28 29
29 /* internal */ inner = 0x0000_0002, 30 /* internal */ inner = 0x0000_0002,
30 31
31 /** 32 /**
32 Attribute data are in key-value form. 33 Specifies that the attribute data are in name-value form.
33 */ 34 */
34 named = 0x0000_0004 35 nameValue = 0x0000_0004
35 } 36 }
36 37
37 /** 38 /**
38 When mixed in an aggregate, converts a compile-time tuple to 39 When mixed in an aggregate, converts a compile-time tuple to
39 members of that aggregate. 40 members of that aggregate.
55 56
56 /** 57 /**
57 When mixed in an aggregate, converts a compile-time tuple of name-value pairs to 58 When mixed in an aggregate, converts a compile-time tuple of name-value pairs to
58 members of that aggregate. 59 members of that aggregate.
59 */ 60 */
60 struct NamedValueTupleToFields(A...) 61 struct NameValueTupleToFields(A...)
61 { 62 {
62 63
63 } 64 }
64 65
65 version (QtdUnittest) 66 version (QtdUnittest)
216 else 217 else
217 mixin ("alias TypeTuple!(attrClass, A) " ~ attrId ~ ";"); 218 mixin ("alias TypeTuple!(attrClass, A) " ~ attrId ~ ";");
218 } 219 }
219 220
220 /** 221 /**
221 Base class for run time attributes 222 Base class for run time attribute implementations
222 */ 223 */
223 abstract class MetaAttribute 224 abstract class MetaAttribute
224 { 225 {
225 } 226 }
226 227
227 /** 228 /**
228 Default implementation of run time attributes 229 A run-time attribute implementation that stores the attribute data in an
229 */ 230 array of variants.
230 final class MetaAttributeVariant : MetaAttribute 231 */
231 { 232 final class MetaVariantAttribute : MetaAttribute
232 private: 233 {
233 Variant[] values_; 234 Variant[] values;
234 235
235 public: 236 private this()
236 Variant values() 237 {
237 { 238 }
238 return values_; 239
239 } 240 static MetaVariantAttribute create(string category, AttributeOptions opts, A...)()
240 241 {
241 static MetaAttributeVariant create(string category, AttributeOptions opts, A...)() 242 auto ret = new MetaVariantAttribute;
242 { 243 super.construct!(category, opts, A)();
243 } 244 foreach(i, _; A)
244 } 245 {
245 246 static if (__traits(compiles, { ret.values ~= Variant(A[i]); }() ))
246 class MetaAttributeTypedImpl(A...) 247 ret.values ~= Variant(A[i]);
247 { 248 }
248 } 249 return ret;
249 250 }
250 251 }
251 abstract class MetaAtributeTyped : MetaAttribute 252
252 { 253 /**
253 void construct(A...)() 254 A run-time attribute implementation that stores the attribute data in an
254 { 255 assiciative array of variants.
255 } 256 */
256 } 257 final class MetaVariantDictAttribute : MetaAttribute
257 258 {
258 259 Variant[string] values;
260
261 private this()
262 {
263 }
264
265 static MetaVariantAttribute create(string category, AttributeOptions opts, A...)()
266 {
267 auto ret = new MetaVariantAttribute;
268 super.construct!(category, opts, A)();
269 foreach(i, _; A)
270 {
271 static if (i % 2 == 0 && __traits(compiles, { ret.values[A[i]] = Variant(A[i + 1]); }() ))
272 ret.values[A[i]] ~= Variant(A[i + 1]);
273 }
274 return ret;
275 }
276 }
277
278
279 /**
280 A run-time attribute implementation that stores the attribute data in
281 typed fields named fieldN, where N is the index of the original attribute data element.
282 */
283 abstract class MetaTypedAttribute : MetaAttribute
284 {
285 private this() {}
286
287 static class Impl(A) : typeof(this)
288 {
289 private this() {}
290
291 mixin tupleToMembers!("field", 0, A);
292 }
293
294 static MetaAttribute create(string category, AttributeOptions opts, A...)()
295 {
296 auto ret = new Impl!A;
297 super.construct(category, opts, A);
298 return ret;
299 }
300 }
301
302 /**
303 A run-time attribute implementation that stores the attribute data in
304 typed fields by interpreting the original attribute data as name-value pairs.
305 */
306 abstract class MetaTypedDictAttribute : MetaAttribute
307 {
308 private this() {}
309
310 static class Impl(A) : typeof(this)
311 {
312 private this() {}
313
314 mixin nameValueTupleToMembers!("", A);
315 }
316
317 static MetaAttribute create(string category, AttributeOptions opts, A...)()
318 {
319 auto ret = new Impl!A;
320 super.construct(category, opts, A);
321 return ret;
322 }
323 }
324
325 version (QtdUnittest)
326 {
327 unittest
328 {
329 static void foo() {}
330
331 static class C
332 {
333 mixin InnerAttribute!("someAttribute", MetaVariantAttribute, "22", foo, 33);
334 }
335
336 auto attr = cast(MetaVariantAttribute) meta!(C).attributes[0];
337 assert(attr.name == "someAttribute");
338 assert(qttr.length == 2);
339 assert(attr.values[0] == "22");
340 assert(attr.values[1] == 33);
341 }
342 }
259 343
260 private string stringOfFunction(alias symbol)() 344 private string stringOfFunction(alias symbol)()
261 { 345 {
262 auto ptrType = typeof(&symbol).stringof; 346 auto ptrType = typeof(&symbol).stringof;
263 auto paramList = ParameterTypeTuple!(symbol).stringof; 347 auto paramList = ParameterTypeTuple!(symbol).stringof;