1 /** 2 * Copyright: © 2014-2015 Anton Gushcha 3 * License: Subject to the terms of the Boost 1.0 license as specified in LICENSE file 4 * Authors: Anton Gushcha <ncrashed@gmail.com> 5 * 6 * Template maps - extension of std.typecons; 7 */ 8 module stribog.meta.map; 9 10 public import std.typetuple : staticMap; 11 12 import std.conv : text; 13 14 import stribog.meta.base; 15 16 template staticMapN(size_t i, alias F, T...) 17 { 18 static assert(i != 0, "staticMapN: N is zero"); 19 static assert(T.length % i == 0, text("Wrong count of arguments, expected power of ", i, " but got ", T.length)); 20 21 static if (T.length < i) 22 { 23 alias staticMapN = ExpressionList!(); 24 } 25 else static if(T.length == i) 26 { 27 alias staticMapN = ExpressionList!(F!(T[0 .. i])); 28 } 29 else 30 { 31 alias staticMapN = ExpressionList!(F!(T[0 .. i]), staticMapN!(i, F, T[i .. $])); 32 } 33 } 34 /// Example 35 unittest 36 { 37 template Test(bool a, bool b) 38 { 39 enum Test = a && b; 40 } 41 42 static assert([staticMapN!(2, Test, true, true, true, false)] == [true, false]); 43 44 template Test3(alias T1, alias T2, alias T3) 45 { 46 alias Test3 = StrictExpressionList!(T3, T2, T1); 47 } 48 49 template Dummy(T) { 50 enum toString = text("Dummy!", T.stringof); 51 } 52 53 static assert( staticEqual!( StrictExpressionList!(staticMapN!(3, Test3, Dummy!int, Dummy!float, Dummy!real)), StrictExpressionList!(StrictExpressionList!(Dummy!real, Dummy!float, Dummy!int) )) ); 54 } 55 56 /// Alias of staticMapN for 2 arguments 57 alias staticMap2(alias F, T...) = staticMapN!(2, F, T); 58 /// Example 59 unittest 60 { 61 template Test(T...) 62 { 63 enum Test = T[0] && T[1]; 64 } 65 66 static assert([staticMap2!(Test, true, true, true, false)] == [true, false]); 67 } 68 69 /// Alias of staticMapN for 3 arguments 70 alias staticMap3(alias F, T...) = staticMapN!(3, F, T); 71 /// Alias of staticMapN for 4 arguments 72 alias staticMap4(alias F, T...) = staticMapN!(4, F, T); 73 /// Alias of staticMapN for 5 arguments 74 alias staticMap5(alias F, T...) = staticMapN!(5, F, T); 75 /// Alias of staticMapN for 6 arguments 76 alias staticMap6(alias F, T...) = staticMapN!(6, F, T); 77 /// Alias of staticMapN for 7 arguments 78 alias staticMap7(alias F, T...) = staticMapN!(7, F, T); 79 /// Alias of staticMapN for 8 arguments 80 alias staticMap8(alias F, T...) = staticMapN!(8, F, T); 81 /// Alias of staticMapN for 9 arguments 82 alias staticMap9(alias F, T...) = staticMapN!(9, F, T);