1 /** 
2  * Template-oriented routines
3  *
4  * Authors: Tristan Brice Velloza Kildaire
5  */
6 module niknaks.meta;
7 
8 /** 
9  * Determines if the
10  * type `T` is a class
11  * type
12  *
13  * Returns: `true` if
14  * so, `false` otherwise
15  */
16 public bool isClassType(T)()
17 {
18     return __traits(compiles, __traits(classInstanceSize, T));
19 }
20 
21 /** 
22  * Determines if the
23  * type `T` is a struct
24  * type
25  *
26  * Returns: `true` if
27  * so, `false` otherwise
28  */
29 public bool isStructType(T)()
30 {
31     // FIXME: This isn't the best test yet
32     // Primtiive types I believe are POD, so we need to also exlcude those
33     import std.traits : isBasicType;
34     pragma(msg, __traits(isPOD, T));
35     pragma(msg, !isBasicType!(T));
36     return __traits(isPOD, T) && !isBasicType!(T);
37 }
38 
39 /** 
40  * Ensures that the given variadic arguments
41  * are all of the given type
42  *
43  * Returns: `true` if so, `false` otherwise
44  */
45 public bool isVariadicArgsOf(T_should, VarArgs...)()
46 {
47 	pragma(msg, "All variadic args should be of type: '", T_should, "'");
48 	pragma(msg, "Variadic args: ", VarArgs);
49 
50 	static foreach(va; VarArgs)
51 	{
52 		static if(!__traits(isSame, va, T_should))
53 		{
54 			pragma(msg, "Var-arg '", va, "' not of type '", T_should, "'");
55 			return false;
56 		}
57 	}
58 	
59 	return true;
60 }
61 
62 /**
63  * A function is implemented which
64  * wants to ensure its variadic
65  * arguments are all of the same
66  * type.
67  *
68  * This tests out two positive cases
69  * and one failing case.
70  */
71 unittest
72 {
73     enum SomeType
74     {
75         UM,
76         DOIS,
77         TRES,
78         QUATRO
79     }
80 
81     void myFunc(T...)(T, string somethingElse)
82     if(isVariadicArgsOf!(SomeType, T)())
83     {
84         
85     }
86     static assert(__traits(compiles, myFunc(SomeType.UM, SomeType.DOIS, "Halo")));
87 
88     static assert(__traits(compiles, myFunc(SomeType.UM, "Halo")));
89     static assert(!__traits(compiles, myFunc(1, "Halo")));
90 }