comparison src/docgen/misc/meta.d @ 806:bcb74c9b895c

Moved out files in the trunk folder to the root.
author Aziz K?ksal <aziz.koeksal@gmail.com>
date Sun, 09 Mar 2008 00:12:19 +0100
parents trunk/src/docgen/misc/meta.d@cb8edb09108a
children
comparison
equal deleted inserted replaced
805:a3fab8b74a7d 806:bcb74c9b895c
1 /**
2 * Author: Jari-Matti Mäkelä
3 * License: GPL3
4 */
5 module docgen.misc.meta;
6
7 /// tuple literal workaround
8 template Tuple(T...) { alias T Tuple; }
9
10 /// another tuple literal workaround (can be nested & avoids at least one dmdfe bug)
11 struct STuple(T...) { alias T tuple; }
12
13
14 // (a -> b), [a] -> [b]
15 template map(alias S, T...) {
16 static if (T.length)
17 alias Tuple!(S!(T[0]), map!(S, T[1..$])) map;
18 else
19 alias T map;
20 }
21
22 /// (a -> Bool), [a] -> [a]
23 template filter(alias S, T...) {
24 static if (!T.length)
25 alias Tuple!() filter;
26 else static if (S!(T[0]))
27 alias Tuple!(T[0], filter!(S, T[1..$])) filter;
28 else
29 alias filter!(S, T[1..$]) filter;
30 }
31
32 /// Int -> Bool
33 template odd(int T) {
34 const odd = T%2 == 1;
35 }
36
37 /// Int -> Bool
38 template even(int T) {
39 const even = !odd!(T);
40 }
41
42 /// a [a] -> a -- max x y = max2 x (max y)
43 T max(T, U...)(T a, U b) {
44 static if (b.length)
45 return a > max(b) ? a : max(b);
46 else
47 return a;
48 }
49
50 /// a [a] -> a -- min x y = min2 x (min y)
51 T min(T, U...)(T a, U b) {
52 static if (b.length)
53 return a < min(b) ? a : min(b);
54 else
55 return a;
56 }
57
58 /// Upcasts derivatives of B to B
59 template UpCast(B, T) { alias T UpCast; }
60 template UpCast(B, T : B) { alias B UpCast; }
61
62 /// converts integer to ascii, base 10
63 char[] itoa(int i) {
64 char[] ret;
65 auto numbers = "0123456789ABCDEF";
66
67 do {
68 ret = numbers[i%10] ~ ret;
69 i /= 10;
70 } while (i)
71
72 return ret;
73 }
74
75 /// Enum stuff
76
77 template _genList(char[] pre, char[] post, T...) {
78 static if (T.length)
79 const _genList = pre ~ T[0] ~ post ~ (T.length>1 ? "," : "") ~
80 _genList!(pre, post, T[1..$]);
81 else
82 const _genList = ``;
83 }
84
85 /**
86 * Creates
87 * - a typedef for enum (workaround for .tupleof.stringof)
88 * - the enum structure
89 * - string array of enum items (for runtime programming)
90 * - string tuple of enum items (for metaprogramming - char[][] doesn't work)
91 */
92 template createEnum(char[] tName, char[] eName, char[] arName, char[] alName, T...) {
93 const createEnum =
94 "typedef int " ~ tName ~ ";" ~
95 "enum " ~ eName ~ ":" ~ tName ~ "{" ~ _genList!("", "", T) ~ "};" ~
96 "char[][] " ~ arName ~ "=[" ~ _genList!(`"`, `"[]`, T) ~ "];" ~
97 "alias STuple!(" ~ _genList!(`"`, `"`, T) ~ ") " ~ alName ~ ";";
98 }