1 /** Facilities for template metaprogramming.
2 
3     Authors:    Lars Tandle Kyllingstad
4     Copyright:  Copyright (c) 2009–2011, Lars T. Kyllingstad. All rights reserved.
5     License:    Boost License 1.0
6 */
7 module scid.core.meta;
8 
9 import std.traits;
10 import scid.core.traits;
11 import std.complex: Complex;
12 
13 
14 /** Evaluates to the zero value for a given type.
15     ---
16     assert (Zero!creal == 0.0+0.0i);
17     ---
18 */
19 template Zero(T)
20 {
21     static if (isFloatingPoint!T)
22         enum T Zero = 0.0;
23     else static if (is(T : Complex!E, E))
24         enum T Zero = T(0, 0);
25     else static if (isComplex!T)
26         enum T Zero = 0.0 + 0.0i;
27     else static if (isIntegral!T)
28         enum T Zero = 0;
29     else static assert (false, "Zero: Type has no obvious zero: "~T.stringof);
30 }
31 
32 version(unittest)
33 {
34     static assert (Zero!creal == 0.0+0.0i);
35 }
36 
37 
38 
39 
40 /** Evaluates to the unit value for a given type.
41     ---
42     assert (One!creal = 1.0+0.0i);
43     ---
44 */
45 template One(T)
46 {
47     static if (isFloatingPoint!T)
48         enum T One = 1.0;
49     else static if (is(T : Complex!E, E))
50         enum T One = T(1, 0);
51     else static if (isComplex!T)
52         enum T One = 1.0 + 0.0i;
53     else static if (isIntegral!T)
54         enum T One = 1;
55     else static assert (false, "One: Type has no obvious unit element: "
56         ~T.stringof);
57 }
58 
59 version(unittest)
60 {
61     static assert (One!creal == 1.0+0.0i);
62 }