Mercurial > projects > qtd
comparison d2/qtd/meta/Compiletime.d @ 357:9784459f0750
An attempt (failed due to optlink) to improve locality of declarations exported from QtD executables
Q_CLASSINFO implementation
Now Qtd can be built on Windows
author | Max Samukha <maxter@spambox.com> |
---|---|
date | Wed, 02 Jun 2010 19:38:05 +0300 |
parents | 59d847a814e3 |
children | a084e2df3776 |
comparison
equal
deleted
inserted
replaced
356:12cec2d14e1c | 357:9784459f0750 |
---|---|
11 std.variant, | 11 std.variant, |
12 std.typetuple; | 12 std.typetuple; |
13 | 13 |
14 import std.string : startsWith; | 14 import std.string : startsWith; |
15 | 15 |
16 /* | 16 /** |
17 uint startsWith(string s, string pattern) | 17 */ |
18 { | |
19 if (pattern.length <= s.length && s[0..pattern.length] == pattern) | |
20 return pattern.length; | |
21 | |
22 return 0; | |
23 } | |
24 */ | |
25 | |
26 | |
27 enum standardNamespace = "qtd"; | 18 enum standardNamespace = "qtd"; |
28 | 19 |
29 template Alias(A...) if (A.length == 1) | 20 template Alias(A...) if (A.length == 1) |
30 { | 21 { |
31 static if (__traits(compiles, { alias A[0] x; })) | 22 static if (__traits(compiles, { alias A[0] x; })) |
39 flattening when they are passed to a variadic template. | 30 flattening when they are passed to a variadic template. |
40 */ | 31 */ |
41 template TypeTupleWrapper(A...) | 32 template TypeTupleWrapper(A...) |
42 { | 33 { |
43 alias A tuple; | 34 alias A tuple; |
44 } | |
45 | |
46 // returns the type of a template parameter if there is one | |
47 template templateParam(U : V!(U), alias V) | |
48 { | |
49 alias U templateParam; | |
50 } | 35 } |
51 | 36 |
52 | 37 |
53 /** | 38 /** |
54 Returns a tuple with T repeated count times. | 39 Returns a tuple with T repeated count times. |
495 alias TypeTuple!() result; | 480 alias TypeTuple!() result; |
496 } | 481 } |
497 | 482 |
498 version (QtdUnittest) | 483 version (QtdUnittest) |
499 { | 484 { |
500 mixin template MyAttribute(alias symbol, A...) | 485 mixin template QtdCustomAttribute(alias symbol, A...) |
501 { | 486 { |
502 mixin Attribute!(symbol, "MyAttribute", AttributeOptions.allowMultiple, A); | 487 mixin Attribute!(symbol, "QtdCustomAttribute", AttributeOptions.allowMultiple, A); |
503 } | 488 } |
504 | 489 |
505 mixin template ClassInfo(string name, alias value) | 490 mixin template QtdCustomInnerAttribute(string name, alias value) |
506 { | 491 { |
507 mixin InnerAttribute!("ClassInfo", AttributeOptions.allowMultiple, name, value); | 492 mixin InnerAttribute!("QtdCustomInnerAttribute", AttributeOptions.allowMultiple, name, value); |
508 } | 493 } |
509 | 494 |
510 unittest | 495 unittest |
511 { | 496 { |
512 static class C | 497 static class C |
513 { | 498 { |
514 // inner C attributes | 499 // inner C attributes |
515 mixin InnerAttribute!("Inner", 33); // generic | 500 mixin InnerAttribute!("Inner", 33); // generic |
516 mixin ClassInfo!("version", 123); | 501 mixin QtdCustomInnerAttribute!("version", 123); |
517 mixin ClassInfo!("author", "James Bond"); | 502 mixin QtdCustomInnerAttribute!("author", "James Bond"); |
518 | 503 |
519 | 504 |
520 void foo() {}; | 505 void foo() {}; |
521 // foo attributes | 506 // foo attributes |
522 mixin Attribute!(foo, "SomeAttribute", 42); | 507 mixin Attribute!(foo, "SomeAttribute", 42); |
523 mixin MyAttribute!(foo, 1, 2); | 508 mixin QtdCustomAttribute!(foo, 1, 2); |
524 mixin MyAttribute!(foo, 3, 4); | 509 mixin QtdCustomAttribute!(foo, 3, 4); |
525 | 510 |
526 alias GetAttributes!(typeof(this), "Inner") innerAttrs; | 511 alias GetAttributes!(typeof(this), "Inner") innerAttrs; |
527 static assert(innerAttrs[0].tuple[0] == "Inner"); | 512 static assert(innerAttrs[0].tuple[0] == "Inner"); |
528 } | 513 } |
529 // outer C attribute | 514 // outer C attribute |
530 mixin MyAttribute!(C, 24); | 515 mixin QtdCustomAttribute!(C, 24); |
531 | 516 |
532 alias GetAttributes!(C, "Inner") innerAttrs; | 517 alias GetAttributes!(C, "Inner") innerAttrs; |
533 static assert(innerAttrs[0].tuple[0] == "Inner" && innerAttrs[0].tuple[2] == 33); | 518 static assert(innerAttrs[0].tuple[0] == "Inner" && innerAttrs[0].tuple[2] == 33); |
534 | 519 |
535 alias GetAttributes!(C, "ClassInfo") ciAttrs; | 520 alias GetAttributes!(C, "QtdCustomInnerAttribute") ciAttrs; |
536 static assert(ciAttrs[0].tuple[2] == "version" && ciAttrs[0].tuple[3] == 123); | 521 static assert(ciAttrs[0].tuple[2] == "version" && ciAttrs[0].tuple[3] == 123); |
537 | 522 |
523 /+ Fails on Windows but passes on Linux | |
538 alias GetAttributes!(C.foo, "SomeAttribute") someAttr; | 524 alias GetAttributes!(C.foo, "SomeAttribute") someAttr; |
539 static assert(someAttr.length == 1); | 525 static assert(someAttr.length == 1); |
540 static assert(someAttr[0].tuple[0] == "SomeAttribute"); | 526 static assert(someAttr[0].tuple[0] == "SomeAttribute"); |
541 | 527 |
542 alias GetAttributes!(C.foo, "MyAttribute") myAttrs; | 528 alias GetAttributes!(C.foo, "QtdCustomAttribute") myAttrs; |
543 | 529 |
544 //COMPILER BUG: cannot 'alias myAttrs[0].tuple myAttrs_0'; | 530 //COMPILER BUG: cannot 'alias myAttrs[0].tuple myAttrs_0'; |
545 static assert(myAttrs[0].tuple[0] == "MyAttribute"); | 531 |
532 static assert(myAttrs[0].tuple[0] == "QtdCustomAttribute"); | |
546 static assert(myAttrs[0].tuple[2] == 1 && myAttrs[0].tuple[3] == 2); | 533 static assert(myAttrs[0].tuple[2] == 1 && myAttrs[0].tuple[3] == 2); |
547 | 534 |
548 static assert(myAttrs[1].tuple[0] == "MyAttribute"); | 535 static assert(myAttrs[1].tuple[0] == "QtdCustomAttribute"); |
549 static assert(myAttrs[1].tuple[2] == 3 && myAttrs[1].tuple[3] == 4); | 536 static assert(myAttrs[1].tuple[2] == 3 && myAttrs[1].tuple[3] == 4); |
537 +/ | |
550 | 538 |
551 /+ BUG: Fails: local declarations cannot be accessed as parent.localDecl | 539 /+ BUG: Fails: local declarations cannot be accessed as parent.localDecl |
552 alias GetAttributes!(C, "MyAttribute") myAttrs2; | 540 alias GetAttributes!(C, "QtdCustomAttribute") myAttrs2; |
553 static assert(myAttrs2[0].tuple[0] == "MyAttribute"); | 541 static assert(myAttrs2[0].tuple[0] == "QtdCustomAttribute"); |
554 static assert(myAttrs2[0].tuple[1] == 24); | 542 static assert(myAttrs2[0].tuple[1] == 24); |
555 +/ | 543 +/ |
556 } | 544 |
557 } | 545 } |
546 } |