1 module mathed.utils.traits; 2 3 version (unittest) 4 { 5 private 6 { 7 import mathed.types.matrix : Matrix2i, Matrix3i, Matrix3f; 8 import mathed.types.vector : Vector3i, Vector3f; 9 } 10 } 11 12 /** 13 * Tests type to be a matrix. 14 */ 15 template isMatrix (Test) 16 { 17 enum isMatrix = isMatrixImpl!Test; 18 } 19 20 /** 21 * Tests variable to be a matrix. 22 */ 23 template isMatrix (alias Variable) 24 { 25 static if (!__traits (compiles, typeof (Variable))) 26 enum isMatrix = isMatrixImpl!Variable; 27 else 28 enum isMatrix = isMatrixImpl!(typeof(Variable)); 29 } 30 31 /// 32 unittest 33 { 34 auto v = Matrix2i (1, 2, 3, 4); 35 auto i = 3; 36 assert (isMatrix!v); 37 assert (!isMatrix!i); 38 } 39 40 /** 41 * Tests type to be a vector. 42 */ 43 template isVector (Test) 44 { 45 enum isVector = isVectorImpl!Test; 46 } 47 48 /** 49 * Tests variable to be a vector. 50 */ 51 template isVector (alias Variable) 52 { 53 static if (!__traits (compiles, typeof (Variable))) 54 enum isVector = isVectorImpl!Variable; 55 else 56 enum isVector = isVectorImpl!(typeof(Variable)); 57 } 58 59 /// 60 unittest 61 { 62 auto v = Vector3i (1, 2, 3); 63 auto i = 3; 64 assert (isVector!v); 65 assert (!isVector!i); 66 } 67 68 private: 69 70 template isMatrixImpl (Test) 71 { 72 import mathed.types.matrix : Matrix; 73 74 enum isMatrixImpl = is (typeof (Impl!(Test.lines, Test.cols, Test.type)(Test.init))); 75 76 private void Impl (size_t Lines, size_t Cols, Type) 77 (Matrix!(Lines, Cols, Type)){} 78 } 79 80 template isVectorImpl (Test) 81 { 82 import mathed.types.vector : Vector; 83 84 enum isVectorImpl = is (typeof (Impl!(Test.size, Test.type, Test.accessors, Test.orientation)(Test.init))); 85 86 private void Impl (size_t Size, Type, string Accessors, string Orientation) 87 (Vector!(Size, Type, Accessors, Orientation)){} 88 }