root/trunk/infrastructure/meta/Default.d

Revision 69, 4.7 kB (checked in by KirkMcDonald, 2 years ago)

Inroads on Tango support. (Not there yet, though.)

Line 
1 /**
2     Templates not found in Tango, but included in Phobos.
3 */
4 module meta.Default;
5
6 template ReturnType(alias dg) {
7     alias ReturnType!(typeof(dg)) ReturnType;
8 }
9 template ReturnType(dg) {
10     static if (is(dg R == return))
11         alias R ReturnType;
12     else
13         static assert(false, "argument has no return type");
14 }
15
16 template ParameterTypeTuple(alias dg) {
17     alias ParameterTypeTuple!(typeof(dg)) ParameterTypeTuple;
18 }
19 template ParameterTypeTuple(dg) {
20     static if (is(dg P == function))
21         alias P ParameterTypeTuple;
22     else static if (is(dg P == delegate))
23         alias ParameterTypeTuple!(P) ParameterTypeTuple;
24     else static if (is(dg P == P*))
25         alias ParameterTypeTuple!(P) ParameterTypeTuple;
26     else
27         static assert(false, "argument has no parameters");
28 }
29
30 /**
31     Derives the minimum number of arguments the given function may be called with.
32
33     This has some cases in which it can fail, or at least not behave exactly as
34     expected. For instance, it cannot distinguish between the following:
35
36     void foo(int i);
37     void foo(int i, real j);
38
39     and
40
41     void foo(int i, real j=2.0);
42
43     In the first case, calling minArgs!(foo, void function(int, real)) will
44     result in 1, which is arguably incorrect. However, minArgs should be "good
45     enough" in most cases.
46 */
47 template minArgs(alias fn, fn_t = typeof(&fn)) {
48     const uint minArgs = minArgsT!(fn, fn_t).minArgs;
49 }
50
51 template minArgsT(alias fn, fn_t = typeof(&fn)) {
52     alias ParameterTypeTuple!(fn_t) T;
53
54     T[i] I(uint i)() {
55         return T[i].init;
56     }
57
58     static if (is(typeof(fn())))
59         const uint minArgs = 0;
60     else static if (is(typeof(fn(I!(0)()))))
61         const uint minArgs = 1;
62     else static if (is(typeof(fn(I!(0)(), I!(1)()))))
63         const uint minArgs = 2;
64     else static if (is(typeof(fn(I!(0)(), I!(1)(), I!(2)()))))
65         const uint minArgs = 3;
66     else static if (is(typeof(fn(I!(0)(), I!(1)(), I!(2)(), I!(3)()))))
67         const uint minArgs = 4;
68     else static if (is(typeof(fn(I!(0)(), I!(1)(), I!(2)(), I!(3)(), I!(4)()))))
69         const uint minArgs = 5;
70     else static if (is(typeof(fn(I!(0)(), I!(1)(), I!(2)(), I!(3)(), I!(4)(), I!(5)()))))
71         const uint minArgs = 6;
72     else static if (is(typeof(fn(I!(0)(), I!(1)(), I!(2)(), I!(3)(), I!(4)(), I!(5)(), I!(6)()))))
73         const uint minArgs = 7;
74     else static if (is(typeof(fn(I!(0)(), I!(1)(), I!(2)(), I!(3)(), I!(4)(), I!(5)(), I!(6)(), I!(7)()))))
75         const uint minArgs = 8;
76     else static if (is(typeof(fn(I!(0)(), I!(1)(), I!(2)(), I!(3)(), I!(4)(), I!(5)(), I!(6)(), I!(7)(), I!(8)()))))
77         const uint minArgs = 9;
78     else static if (is(typeof(fn(I!(0)(), I!(1)(), I!(2)(), I!(3)(), I!(4)(), I!(5)(), I!(6)(), I!(7)(), I!(8)(), I!(9)()))))
79         const uint minArgs = 10;
80     else static if (is(typeof(fn(I!(0)(), I!(1)(), I!(2)(), I!(3)(), I!(4)(), I!(5)(), I!(6)(), I!(7)(), I!(8)(), I!(9)(), I!(10)()))))
81         const uint minArgs = 11;
82     else static if (is(typeof(fn(I!(0)(), I!(1)(), I!(2)(), I!(3)(), I!(4)(), I!(5)(), I!(6)(), I!(7)(), I!(8)(), I!(9)(), I!(10)(), I!(11)()))))
83         const uint minArgs = 12;
84     else static if (is(typeof(fn(I!(0)(), I!(1)(), I!(2)(), I!(3)(), I!(4)(), I!(5)(), I!(6)(), I!(7)(), I!(8)(), I!(9)(), I!(10)(), I!(11)(), I!(12)()))))
85         const uint minArgs = 13;
86     else static if (is(typeof(fn(I!(0)(), I!(1)(), I!(2)(), I!(3)(), I!(4)(), I!(5)(), I!(6)(), I!(7)(), I!(8)(), I!(9)(), I!(10)(), I!(11)(), I!(12)(), I!(13)()))))
87         const uint minArgs = 14;
88     else static if (is(typeof(fn(I!(0)(), I!(1)(), I!(2)(), I!(3)(), I!(4)(), I!(5)(), I!(6)(), I!(7)(), I!(8)(), I!(9)(), I!(10)(), I!(11)(), I!(12)(), I!(13)(), I!(14)()))))
89         const uint minArgs = 15;
90     else static if (is(typeof(fn(I!(0)(), I!(1)(), I!(2)(), I!(3)(), I!(4)(), I!(5)(), I!(6)(), I!(7)(), I!(8)(), I!(9)(), I!(10)(), I!(11)(), I!(12)(), I!(13)(), I!(14)(), I!(15)()))))
91         const uint minArgs = 16;
92     else static if (is(typeof(fn(I!(0)(), I!(1)(), I!(2)(), I!(3)(), I!(4)(), I!(5)(), I!(6)(), I!(7)(), I!(8)(), I!(9)(), I!(10)(), I!(11)(), I!(12)(), I!(13)(), I!(14)(), I!(15)(), I!(16)()))))
93         const uint minArgs = 17;
94     else static if (is(typeof(fn(I!(0)(), I!(1)(), I!(2)(), I!(3)(), I!(4)(), I!(5)(), I!(6)(), I!(7)(), I!(8)(), I!(9)(), I!(10)(), I!(11)(), I!(12)(), I!(13)(), I!(14)(), I!(15)(), I!(16)(), I!(17)()))))
95         const uint minArgs = 18;
96     else static if (is(typeof(fn(I!(0)(), I!(1)(), I!(2)(), I!(3)(), I!(4)(), I!(5)(), I!(6)(), I!(7)(), I!(8)(), I!(9)(), I!(10)(), I!(11)(), I!(12)(), I!(13)(), I!(14)(), I!(15)(), I!(16)(), I!(17)(), I!(18)()))))
97         const uint minArgs = 19;
98     else static if (is(typeof(fn(I!(0)(), I!(1)(), I!(2)(), I!(3)(), I!(4)(), I!(5)(), I!(6)(), I!(7)(), I!(8)(), I!(9)(), I!(10)(), I!(11)(), I!(12)(), I!(13)(), I!(14)(), I!(15)(), I!(16)(), I!(17)(), I!(18)(), I!(19)()))))
99         const uint minArgs = 20;
100 }
Note: See TracBrowser for help on using the browser.