DefineTemplateVars

mixin template DefineTemplateVars (
alias Trg
List...
) if (
allSatisfy!(isTemplateVarDef, List)
) {}

Examples

1 import std.string;
2 
3 static struct X(Args...)
4 {
5     void func( Args args )
6     {
7         static if( Args.length == 1 && is( Args[0] == string ) )
8             assert( args[0] == "hello" );
9         else static if( Args.length == 2 && is( Args[0] == float ) && is( Args[1] == int ) )
10         {
11             import std.math;
12             assert( abs(args[0] - 3.14) < float.epsilon );
13             assert( args[1] == 12 );
14         }
15         else static assert(0,"undefined for this unittest");
16     }
17 }
18 
19 static class ZZ
20 {
21     mixin DefineTemplateVars!( X, TemplateVarDef!("ok",string),
22                                   TemplateVarDef!("da",float,int),
23             );
24 }
25 
26 auto zz = new ZZ;
27 static assert( is( typeof(zz.ok) == X!string ) );
28 static assert( is( typeof(zz.da) == X!(float,int) ) );
29 zz.ok.func( "hello" );
30 zz.da.func( 3.14, 12 );
1 enum mark;
2 
3 static class A
4 {
5     void s1() @mark {}
6     void f2() {}
7     @mark
8     {
9         void s3( int, string ) {}
10         void s4( float x ) {}
11     }
12 }
13 
14 template isVoidMarked(T)
15 {
16     alias isVoidMarked = isVoidMarkedFunc;
17 
18     template isVoidMarkedFunc(string n)
19     {
20         static if( __traits(compiles,impl!(__traits(getMember,T,n))) )
21             enum isVoidMarkedFunc = impl!(__traits(getMember,T,n));
22         else enum isVoidMarkedFunc = false;
23 
24         template impl(alias f)
25         {
26             enum impl = isCallable!f &&
27                         is( ReturnType!f == void ) &&
28                         hasAttrib!(mark,f);
29         }
30     }
31 }
32 
33 template TemplateVarDefFromMethod(T)
34 {
35     template TemplateVarDefFromMethod(string name)
36     {
37         alias TemplateVarDefFromMethod = TemplateVarDef!(name,ParameterTypeTuple!(__traits(getMember,T,name)));
38     }
39 }
40 
41 alias tvd = staticMap!( TemplateVarDefFromMethod!A, staticFilter!(isVoidMarked!A,__traits(allMembers,A)) );
42 static assert( tvd.length == 3 );
43 alias exp = TypeTuple!( TemplateVarDef!("s1"), TemplateVarDef!("s3",int,string), TemplateVarDef!("s4",float) );
44 static assert( is(tvd[0] == exp[0]) );
45 static assert( is(tvd[1] == exp[1]) );
46 static assert( is(tvd[2] == exp[2]) );

Meta