annotate 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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
717
cb8edb09108a Updated docgen (mostly) to new tango.
Jari-Matti M?kel? <jmjm@iki.fi>
parents:
diff changeset
1 /**
cb8edb09108a Updated docgen (mostly) to new tango.
Jari-Matti M?kel? <jmjm@iki.fi>
parents:
diff changeset
2 * Author: Jari-Matti Mäkelä
cb8edb09108a Updated docgen (mostly) to new tango.
Jari-Matti M?kel? <jmjm@iki.fi>
parents:
diff changeset
3 * License: GPL3
cb8edb09108a Updated docgen (mostly) to new tango.
Jari-Matti M?kel? <jmjm@iki.fi>
parents:
diff changeset
4 */
cb8edb09108a Updated docgen (mostly) to new tango.
Jari-Matti M?kel? <jmjm@iki.fi>
parents:
diff changeset
5 module docgen.misc.meta;
cb8edb09108a Updated docgen (mostly) to new tango.
Jari-Matti M?kel? <jmjm@iki.fi>
parents:
diff changeset
6
cb8edb09108a Updated docgen (mostly) to new tango.
Jari-Matti M?kel? <jmjm@iki.fi>
parents:
diff changeset
7 /// tuple literal workaround
cb8edb09108a Updated docgen (mostly) to new tango.
Jari-Matti M?kel? <jmjm@iki.fi>
parents:
diff changeset
8 template Tuple(T...) { alias T Tuple; }
cb8edb09108a Updated docgen (mostly) to new tango.
Jari-Matti M?kel? <jmjm@iki.fi>
parents:
diff changeset
9
cb8edb09108a Updated docgen (mostly) to new tango.
Jari-Matti M?kel? <jmjm@iki.fi>
parents:
diff changeset
10 /// another tuple literal workaround (can be nested & avoids at least one dmdfe bug)
cb8edb09108a Updated docgen (mostly) to new tango.
Jari-Matti M?kel? <jmjm@iki.fi>
parents:
diff changeset
11 struct STuple(T...) { alias T tuple; }
cb8edb09108a Updated docgen (mostly) to new tango.
Jari-Matti M?kel? <jmjm@iki.fi>
parents:
diff changeset
12
cb8edb09108a Updated docgen (mostly) to new tango.
Jari-Matti M?kel? <jmjm@iki.fi>
parents:
diff changeset
13
cb8edb09108a Updated docgen (mostly) to new tango.
Jari-Matti M?kel? <jmjm@iki.fi>
parents:
diff changeset
14 // (a -> b), [a] -> [b]
cb8edb09108a Updated docgen (mostly) to new tango.
Jari-Matti M?kel? <jmjm@iki.fi>
parents:
diff changeset
15 template map(alias S, T...) {
cb8edb09108a Updated docgen (mostly) to new tango.
Jari-Matti M?kel? <jmjm@iki.fi>
parents:
diff changeset
16 static if (T.length)
cb8edb09108a Updated docgen (mostly) to new tango.
Jari-Matti M?kel? <jmjm@iki.fi>
parents:
diff changeset
17 alias Tuple!(S!(T[0]), map!(S, T[1..$])) map;
cb8edb09108a Updated docgen (mostly) to new tango.
Jari-Matti M?kel? <jmjm@iki.fi>
parents:
diff changeset
18 else
cb8edb09108a Updated docgen (mostly) to new tango.
Jari-Matti M?kel? <jmjm@iki.fi>
parents:
diff changeset
19 alias T map;
cb8edb09108a Updated docgen (mostly) to new tango.
Jari-Matti M?kel? <jmjm@iki.fi>
parents:
diff changeset
20 }
cb8edb09108a Updated docgen (mostly) to new tango.
Jari-Matti M?kel? <jmjm@iki.fi>
parents:
diff changeset
21
cb8edb09108a Updated docgen (mostly) to new tango.
Jari-Matti M?kel? <jmjm@iki.fi>
parents:
diff changeset
22 /// (a -> Bool), [a] -> [a]
cb8edb09108a Updated docgen (mostly) to new tango.
Jari-Matti M?kel? <jmjm@iki.fi>
parents:
diff changeset
23 template filter(alias S, T...) {
cb8edb09108a Updated docgen (mostly) to new tango.
Jari-Matti M?kel? <jmjm@iki.fi>
parents:
diff changeset
24 static if (!T.length)
cb8edb09108a Updated docgen (mostly) to new tango.
Jari-Matti M?kel? <jmjm@iki.fi>
parents:
diff changeset
25 alias Tuple!() filter;
cb8edb09108a Updated docgen (mostly) to new tango.
Jari-Matti M?kel? <jmjm@iki.fi>
parents:
diff changeset
26 else static if (S!(T[0]))
cb8edb09108a Updated docgen (mostly) to new tango.
Jari-Matti M?kel? <jmjm@iki.fi>
parents:
diff changeset
27 alias Tuple!(T[0], filter!(S, T[1..$])) filter;
cb8edb09108a Updated docgen (mostly) to new tango.
Jari-Matti M?kel? <jmjm@iki.fi>
parents:
diff changeset
28 else
cb8edb09108a Updated docgen (mostly) to new tango.
Jari-Matti M?kel? <jmjm@iki.fi>
parents:
diff changeset
29 alias filter!(S, T[1..$]) filter;
cb8edb09108a Updated docgen (mostly) to new tango.
Jari-Matti M?kel? <jmjm@iki.fi>
parents:
diff changeset
30 }
cb8edb09108a Updated docgen (mostly) to new tango.
Jari-Matti M?kel? <jmjm@iki.fi>
parents:
diff changeset
31
cb8edb09108a Updated docgen (mostly) to new tango.
Jari-Matti M?kel? <jmjm@iki.fi>
parents:
diff changeset
32 /// Int -> Bool
cb8edb09108a Updated docgen (mostly) to new tango.
Jari-Matti M?kel? <jmjm@iki.fi>
parents:
diff changeset
33 template odd(int T) {
cb8edb09108a Updated docgen (mostly) to new tango.
Jari-Matti M?kel? <jmjm@iki.fi>
parents:
diff changeset
34 const odd = T%2 == 1;
cb8edb09108a Updated docgen (mostly) to new tango.
Jari-Matti M?kel? <jmjm@iki.fi>
parents:
diff changeset
35 }
cb8edb09108a Updated docgen (mostly) to new tango.
Jari-Matti M?kel? <jmjm@iki.fi>
parents:
diff changeset
36
cb8edb09108a Updated docgen (mostly) to new tango.
Jari-Matti M?kel? <jmjm@iki.fi>
parents:
diff changeset
37 /// Int -> Bool
cb8edb09108a Updated docgen (mostly) to new tango.
Jari-Matti M?kel? <jmjm@iki.fi>
parents:
diff changeset
38 template even(int T) {
cb8edb09108a Updated docgen (mostly) to new tango.
Jari-Matti M?kel? <jmjm@iki.fi>
parents:
diff changeset
39 const even = !odd!(T);
cb8edb09108a Updated docgen (mostly) to new tango.
Jari-Matti M?kel? <jmjm@iki.fi>
parents:
diff changeset
40 }
cb8edb09108a Updated docgen (mostly) to new tango.
Jari-Matti M?kel? <jmjm@iki.fi>
parents:
diff changeset
41
cb8edb09108a Updated docgen (mostly) to new tango.
Jari-Matti M?kel? <jmjm@iki.fi>
parents:
diff changeset
42 /// a [a] -> a -- max x y = max2 x (max y)
cb8edb09108a Updated docgen (mostly) to new tango.
Jari-Matti M?kel? <jmjm@iki.fi>
parents:
diff changeset
43 T max(T, U...)(T a, U b) {
cb8edb09108a Updated docgen (mostly) to new tango.
Jari-Matti M?kel? <jmjm@iki.fi>
parents:
diff changeset
44 static if (b.length)
cb8edb09108a Updated docgen (mostly) to new tango.
Jari-Matti M?kel? <jmjm@iki.fi>
parents:
diff changeset
45 return a > max(b) ? a : max(b);
cb8edb09108a Updated docgen (mostly) to new tango.
Jari-Matti M?kel? <jmjm@iki.fi>
parents:
diff changeset
46 else
cb8edb09108a Updated docgen (mostly) to new tango.
Jari-Matti M?kel? <jmjm@iki.fi>
parents:
diff changeset
47 return a;
cb8edb09108a Updated docgen (mostly) to new tango.
Jari-Matti M?kel? <jmjm@iki.fi>
parents:
diff changeset
48 }
cb8edb09108a Updated docgen (mostly) to new tango.
Jari-Matti M?kel? <jmjm@iki.fi>
parents:
diff changeset
49
cb8edb09108a Updated docgen (mostly) to new tango.
Jari-Matti M?kel? <jmjm@iki.fi>
parents:
diff changeset
50 /// a [a] -> a -- min x y = min2 x (min y)
cb8edb09108a Updated docgen (mostly) to new tango.
Jari-Matti M?kel? <jmjm@iki.fi>
parents:
diff changeset
51 T min(T, U...)(T a, U b) {
cb8edb09108a Updated docgen (mostly) to new tango.
Jari-Matti M?kel? <jmjm@iki.fi>
parents:
diff changeset
52 static if (b.length)
cb8edb09108a Updated docgen (mostly) to new tango.
Jari-Matti M?kel? <jmjm@iki.fi>
parents:
diff changeset
53 return a < min(b) ? a : min(b);
cb8edb09108a Updated docgen (mostly) to new tango.
Jari-Matti M?kel? <jmjm@iki.fi>
parents:
diff changeset
54 else
cb8edb09108a Updated docgen (mostly) to new tango.
Jari-Matti M?kel? <jmjm@iki.fi>
parents:
diff changeset
55 return a;
cb8edb09108a Updated docgen (mostly) to new tango.
Jari-Matti M?kel? <jmjm@iki.fi>
parents:
diff changeset
56 }
cb8edb09108a Updated docgen (mostly) to new tango.
Jari-Matti M?kel? <jmjm@iki.fi>
parents:
diff changeset
57
cb8edb09108a Updated docgen (mostly) to new tango.
Jari-Matti M?kel? <jmjm@iki.fi>
parents:
diff changeset
58 /// Upcasts derivatives of B to B
cb8edb09108a Updated docgen (mostly) to new tango.
Jari-Matti M?kel? <jmjm@iki.fi>
parents:
diff changeset
59 template UpCast(B, T) { alias T UpCast; }
cb8edb09108a Updated docgen (mostly) to new tango.
Jari-Matti M?kel? <jmjm@iki.fi>
parents:
diff changeset
60 template UpCast(B, T : B) { alias B UpCast; }
cb8edb09108a Updated docgen (mostly) to new tango.
Jari-Matti M?kel? <jmjm@iki.fi>
parents:
diff changeset
61
cb8edb09108a Updated docgen (mostly) to new tango.
Jari-Matti M?kel? <jmjm@iki.fi>
parents:
diff changeset
62 /// converts integer to ascii, base 10
cb8edb09108a Updated docgen (mostly) to new tango.
Jari-Matti M?kel? <jmjm@iki.fi>
parents:
diff changeset
63 char[] itoa(int i) {
cb8edb09108a Updated docgen (mostly) to new tango.
Jari-Matti M?kel? <jmjm@iki.fi>
parents:
diff changeset
64 char[] ret;
cb8edb09108a Updated docgen (mostly) to new tango.
Jari-Matti M?kel? <jmjm@iki.fi>
parents:
diff changeset
65 auto numbers = "0123456789ABCDEF";
cb8edb09108a Updated docgen (mostly) to new tango.
Jari-Matti M?kel? <jmjm@iki.fi>
parents:
diff changeset
66
cb8edb09108a Updated docgen (mostly) to new tango.
Jari-Matti M?kel? <jmjm@iki.fi>
parents:
diff changeset
67 do {
cb8edb09108a Updated docgen (mostly) to new tango.
Jari-Matti M?kel? <jmjm@iki.fi>
parents:
diff changeset
68 ret = numbers[i%10] ~ ret;
cb8edb09108a Updated docgen (mostly) to new tango.
Jari-Matti M?kel? <jmjm@iki.fi>
parents:
diff changeset
69 i /= 10;
cb8edb09108a Updated docgen (mostly) to new tango.
Jari-Matti M?kel? <jmjm@iki.fi>
parents:
diff changeset
70 } while (i)
cb8edb09108a Updated docgen (mostly) to new tango.
Jari-Matti M?kel? <jmjm@iki.fi>
parents:
diff changeset
71
cb8edb09108a Updated docgen (mostly) to new tango.
Jari-Matti M?kel? <jmjm@iki.fi>
parents:
diff changeset
72 return ret;
cb8edb09108a Updated docgen (mostly) to new tango.
Jari-Matti M?kel? <jmjm@iki.fi>
parents:
diff changeset
73 }
cb8edb09108a Updated docgen (mostly) to new tango.
Jari-Matti M?kel? <jmjm@iki.fi>
parents:
diff changeset
74
cb8edb09108a Updated docgen (mostly) to new tango.
Jari-Matti M?kel? <jmjm@iki.fi>
parents:
diff changeset
75 /// Enum stuff
cb8edb09108a Updated docgen (mostly) to new tango.
Jari-Matti M?kel? <jmjm@iki.fi>
parents:
diff changeset
76
cb8edb09108a Updated docgen (mostly) to new tango.
Jari-Matti M?kel? <jmjm@iki.fi>
parents:
diff changeset
77 template _genList(char[] pre, char[] post, T...) {
cb8edb09108a Updated docgen (mostly) to new tango.
Jari-Matti M?kel? <jmjm@iki.fi>
parents:
diff changeset
78 static if (T.length)
cb8edb09108a Updated docgen (mostly) to new tango.
Jari-Matti M?kel? <jmjm@iki.fi>
parents:
diff changeset
79 const _genList = pre ~ T[0] ~ post ~ (T.length>1 ? "," : "") ~
cb8edb09108a Updated docgen (mostly) to new tango.
Jari-Matti M?kel? <jmjm@iki.fi>
parents:
diff changeset
80 _genList!(pre, post, T[1..$]);
cb8edb09108a Updated docgen (mostly) to new tango.
Jari-Matti M?kel? <jmjm@iki.fi>
parents:
diff changeset
81 else
cb8edb09108a Updated docgen (mostly) to new tango.
Jari-Matti M?kel? <jmjm@iki.fi>
parents:
diff changeset
82 const _genList = ``;
cb8edb09108a Updated docgen (mostly) to new tango.
Jari-Matti M?kel? <jmjm@iki.fi>
parents:
diff changeset
83 }
cb8edb09108a Updated docgen (mostly) to new tango.
Jari-Matti M?kel? <jmjm@iki.fi>
parents:
diff changeset
84
cb8edb09108a Updated docgen (mostly) to new tango.
Jari-Matti M?kel? <jmjm@iki.fi>
parents:
diff changeset
85 /**
cb8edb09108a Updated docgen (mostly) to new tango.
Jari-Matti M?kel? <jmjm@iki.fi>
parents:
diff changeset
86 * Creates
cb8edb09108a Updated docgen (mostly) to new tango.
Jari-Matti M?kel? <jmjm@iki.fi>
parents:
diff changeset
87 * - a typedef for enum (workaround for .tupleof.stringof)
cb8edb09108a Updated docgen (mostly) to new tango.
Jari-Matti M?kel? <jmjm@iki.fi>
parents:
diff changeset
88 * - the enum structure
cb8edb09108a Updated docgen (mostly) to new tango.
Jari-Matti M?kel? <jmjm@iki.fi>
parents:
diff changeset
89 * - string array of enum items (for runtime programming)
cb8edb09108a Updated docgen (mostly) to new tango.
Jari-Matti M?kel? <jmjm@iki.fi>
parents:
diff changeset
90 * - string tuple of enum items (for metaprogramming - char[][] doesn't work)
cb8edb09108a Updated docgen (mostly) to new tango.
Jari-Matti M?kel? <jmjm@iki.fi>
parents:
diff changeset
91 */
cb8edb09108a Updated docgen (mostly) to new tango.
Jari-Matti M?kel? <jmjm@iki.fi>
parents:
diff changeset
92 template createEnum(char[] tName, char[] eName, char[] arName, char[] alName, T...) {
cb8edb09108a Updated docgen (mostly) to new tango.
Jari-Matti M?kel? <jmjm@iki.fi>
parents:
diff changeset
93 const createEnum =
cb8edb09108a Updated docgen (mostly) to new tango.
Jari-Matti M?kel? <jmjm@iki.fi>
parents:
diff changeset
94 "typedef int " ~ tName ~ ";" ~
cb8edb09108a Updated docgen (mostly) to new tango.
Jari-Matti M?kel? <jmjm@iki.fi>
parents:
diff changeset
95 "enum " ~ eName ~ ":" ~ tName ~ "{" ~ _genList!("", "", T) ~ "};" ~
cb8edb09108a Updated docgen (mostly) to new tango.
Jari-Matti M?kel? <jmjm@iki.fi>
parents:
diff changeset
96 "char[][] " ~ arName ~ "=[" ~ _genList!(`"`, `"[]`, T) ~ "];" ~
cb8edb09108a Updated docgen (mostly) to new tango.
Jari-Matti M?kel? <jmjm@iki.fi>
parents:
diff changeset
97 "alias STuple!(" ~ _genList!(`"`, `"`, T) ~ ") " ~ alName ~ ";";
cb8edb09108a Updated docgen (mostly) to new tango.
Jari-Matti M?kel? <jmjm@iki.fi>
parents:
diff changeset
98 }