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 template Promoted(N)
39     if (isScalarType!N)
40 {
41     alias Promoted = CopyTypeQualifiers!(N, typeof(N.init + N.init));
42 }
43 
44 alias CallType(alias callable, ArgTypes...) = typeof(function()
45     {
46         import std.typecons : Tuple;
47         return callable(Tuple!(ArgTypes)().expand);
48     }());
49 alias OpType(string op, T) = typeof(function()
50     {
51         T t;
52         return mixin(op ~ "t");
53     }());
54 alias OpType(T, string op, V) = typeof(function()
55     {
56         T t;
57         V v = 1; // Prevent "divide by zero" errors at CTFE
58         return mixin("t " ~ op ~ " v");
59     }());
60 
61 template precision(N)
62     if (isScalarType!N)
63 {
64     import future.bitop : bsr;
65     static if (isFloatingPoint!N)
66         enum int precision = N.mant_dig;
67     else static if (isSomeChar!N)
68         enum int precision = N.sizeof * 8; // dchar may hold values greater than dchar.max
69     else
70         enum int precision = bsr(N.max) + 1;
71 }