Mercurial > projects > qtd
comparison d2/qtd/Meta.d @ 344:96a75b1e5b26
project structure changes
author | Max Samukha <maxter@spambox.com> |
---|---|
date | Fri, 14 May 2010 12:14:37 +0300 |
parents | qt/qtd/Meta.d@4e31cbd9e20c |
children |
comparison
equal
deleted
inserted
replaced
343:552647ec0f82 | 344:96a75b1e5b26 |
---|---|
1 module qtd.Meta; | |
2 | |
3 import std.traits; | |
4 import std.typetuple; | |
5 import std.string; | |
6 | |
7 // Various compile time utilities | |
8 | |
9 public bool ctfeStartsWith(T)(T[] source, T[] pattern) | |
10 { | |
11 return source.length >= pattern.length && source[0 .. pattern.length] == pattern[]; | |
12 } | |
13 | |
14 // compile-time toString, maybe to!string is already working in CT | |
15 string __toString(long v) | |
16 { | |
17 if (v == 0) | |
18 return "0"; | |
19 | |
20 string ret; | |
21 | |
22 bool neg; | |
23 if (v < 0) | |
24 { | |
25 neg = true; | |
26 v = -v; | |
27 } | |
28 | |
29 while (v != 0) | |
30 { | |
31 ret = cast(char)(v % 10 + '0') ~ ret; | |
32 v = cast(long)(v / 10); | |
33 } | |
34 | |
35 if (neg) | |
36 ret = "-" ~ ret; | |
37 | |
38 return ret; | |
39 } | |
40 | |
41 // returns the type of a template parameter if there is one | |
42 template templateParam(U : V!(U), alias V) | |
43 { | |
44 alias U templateParam; | |
45 } | |
46 | |
47 // to workaround buggy foreach, returns a tuple of Ts of size I | |
48 template Repeat(T, int I) | |
49 { | |
50 static if (!I) alias TypeTuple!() Repeat; | |
51 else alias TypeTuple!(T, Repeat!(T, I - 1)) Repeat; | |
52 } | |
53 | |
54 //returns number of required function arguments, optional arguments excluded | |
55 int requiredArgCount(alias fn)() { | |
56 alias ParameterTypeTuple!(typeof(&fn)) P; | |
57 P p; | |
58 static if (P.length == 0) | |
59 return 0; | |
60 | |
61 foreach(i, _; P) | |
62 { | |
63 static if (!__traits(compiles, fn(p[0..$-i-1]))) | |
64 { | |
65 return p.length - i; | |
66 } | |
67 } | |
68 return 0; | |
69 } | |
70 | |
71 template isDg(Dg) | |
72 { | |
73 enum isDg = is(Dg == delegate); | |
74 } | |
75 | |
76 template isFn(Fn) | |
77 { | |
78 enum isFn = is(typeof(*Fn.init) == function); | |
79 } | |
80 | |
81 template isFnOrDg(Dg) | |
82 { | |
83 enum isFnOrDg = isFn!(Dg) || isDg!(Dg); | |
84 } | |
85 | |
86 uint isModule(string str) | |
87 { | |
88 return startsWith(str, "module "); | |
89 } | |
90 | |
91 template qualifiedCppName(T) | |
92 { | |
93 static if(!isModule(__traits(parent, T).stringof)) | |
94 enum qualifiedCppName = qualifiedCppName!(__traits(parent, T)) ~ "::" ~ T.stringof; | |
95 else | |
96 enum qualifiedCppName = T.stringof; | |
97 } | |
98 | |
99 template qualifiedDName(T) | |
100 { | |
101 static if (!isModule(__traits(parent, T).stringof)) | |
102 enum qualifiedDName = qualifiedDName!(__traits(parent, T)) ~ "." ~ T.stringof; | |
103 else | |
104 enum qualifiedDName = T.stringof; | |
105 } | |
106 | |
107 template fullDName(T) | |
108 { | |
109 static if (is(T == enum)) | |
110 enum fullDName = qualifiedDName!T; | |
111 else | |
112 enum fullDName = T.stringof; | |
113 } |