1 // Written in the D programming language.
2 /*
3 NYSL Version 0.9982
4 
5 A. This software is "Everyone'sWare". It means:
6   Anybody who has this software can use it as if he/she is
7   the author.
8 
9   A-1. Freeware. No fee is required.
10   A-2. You can freely redistribute this software.
11   A-3. You can freely modify this software. And the source
12       may be used in any software with no limitation.
13   A-4. When you release a modified version to public, you
14       must publish it with your name.
15 
16 B. The author is not responsible for any kind of damages or loss
17   while using or misusing this software, which is distributed
18   "AS IS". No warranty of any kind is expressed or implied.
19   You use AT YOUR OWN RISK.
20 
21 C. Copyrighted to Kazuki KOMATSU
22 
23 D. Above three clauses are applied both to source and binary
24   form of this software.
25 */
26 
27 /**
28 このモジュールは、様々なtemplateを提供します。
29 */
30 
31 module carbon.templates;
32 
33 import std.typetuple;
34 
35 
36 /**
37 あるテンプレートが、テンプレートレンジかどうか判定します。
38 
39 Example:
40 -------
41 alias head = tmplt.front!();
42 alias tail = tmplt.tail!();
43 -------
44 */
45 enum isTemplateRange(alias tmplt) = is(typeof({
46   static if(!tmplt.empty){
47     alias head = tmplt.front;
48     alias tail = tmplt.tail!();
49   }
50 }));
51 
52 unittest
53 {
54     template number(size_t a, size_t b)
55     if(a <= b)
56     {
57       static if(a == b)
58         enum bool empty = true;
59       else
60       {
61         enum bool empty = false;
62 
63         enum front = a;
64 
65         template tail()
66         {
67             alias tail = number!(a+1, b);
68         }
69       }
70     }
71 
72     static assert(isTemplateRange!(number!(0, 10)));
73     static assert(isTemplateRange!(number!(10, 10)));
74 }
75 
76 
77 /**
78 ある型や値をN個並べたタプルを返します
79 */
80 template TypeNuple(A...)
81 if(A.length == 2 && is(typeof(A[1]) : size_t))
82 {
83   static if(A[1] == 0)
84     alias TypeNuple = TypeTuple!();
85   else
86     alias TypeNuple = TypeTuple!(A[0], TypeNuple!(A[0], A[1] - 1));
87 }
88 
89 unittest
90 {
91     static assert(is(TypeNuple!(int, 2) == TypeTuple!(int, int)));
92     static assert(is(TypeNuple!(long, 3) == TypeTuple!(long, long, long)));
93 }