1 /**
2 Compatibility shim to allow code written against the latest `std.traits`
3 module to compile with older versions of D.
4 
5 Copyright: Copyright Thomas Stuart Bockman 2015
6 License: $(LINK2 http://www.boost.org/LICENSE_1_0.txt, Boost License 1.0).
7 Authors: Thomas Stuart Bockman
8 **/
9 
10 /* HACK: Added '0' to the end of the name to preventdub build -b docs
11          from overwriting the output of checkedint.traits */
12 module future.traits0; 
13 
14 public import std.traits;
15 
16 enum isUnqual(T) = is(T == Unqual!T);
17 enum isFixedPoint(T) = isIntegral!T || isSomeChar!T || isBoolean!T;
18 
19 template IntFromChar(N)
20     if (isSomeChar!N)
21 {
22     static if (N.sizeof == char.sizeof)
23         alias IntFromChar = ubyte;
24     else
25     static if (N.sizeof == wchar.sizeof)
26         alias IntFromChar = ushort;
27     else
28     static if (N.sizeof == dchar.sizeof)
29         alias IntFromChar = uint;
30     else
31         static assert(false);
32 }
33 template IntFromChar(N)
34     if (isIntegral!N)
35 {
36     alias IntFromChar = Unqual!N;
37 }
38 static if(__VERSION__ < 2073)
39 {
40     template Promoted(N)
41         if (isScalarType!N)
42     {
43         alias Promoted = CopyTypeQualifiers!(N, typeof(N.init + N.init));
44     }
45 }
46 
47 alias CallType(alias callable, ArgTypes...) = typeof(function()
48     {
49         import std.typecons : Tuple;
50         return callable(Tuple!(ArgTypes)().expand);
51     }());
52 alias OpType(string op, T) = typeof(function()
53     {
54         T t;
55         return mixin(op ~ "t");
56     }());
57 alias OpType(T, string op, V) = typeof(function()
58     {
59         T t;
60         V v = 1; // Prevent "divide by zero" errors at CTFE
61         return mixin("t " ~ op ~ " v");
62     }());
63 
64 template precision(N)
65     if (isScalarType!N)
66 {
67     import core.bitop : bsr;
68     static if (isFloatingPoint!N)
69         enum int precision = N.mant_dig;
70     else static if (isSomeChar!N)
71         enum int precision = N.sizeof * 8; // dchar may hold values greater than dchar.max
72     else
73         enum int precision = bsr(N.max) + 1;
74 }