convertValue

template convertValue(DataType DT)
convertValue
pure
(
T
string file = __FILE__
size_t line = __LINE__
)
(
T val
)

Examples

1 {
2     auto r = convertValue!(DataType.BYTE)(1);
3     static assert( is( typeof(r) == byte ) );
4     static assert( convertValue!(DataType.BYTE)(2) == 2 );
5 }
6 
7 {
8     auto r = convertValue!(DataType.FLOAT)(1);
9     static assert( is( typeof(r) == float ) );
10     static assert( convertValue!(DataType.FLOAT)(1) == 1 );
11     static assert( convertValue!(DataType.FLOAT)(1.1) == 1.1 );
12 }
13 
14 {
15     auto r = convertValue!(DataType.UNORM_QUART)(1.0);
16     static assert( is( typeof(r) == ubyte ) );
17     static assert( convertValue!(DataType.UNORM_QUART)(1.0) == ubyte.max );
18     static assert( convertValue!(DataType.UNORM_QUART)(0.5) == ubyte.max/2 );
19     static assert( convertValue!(DataType.UNORM_QUART)(0.0) == 0 );
20 }
21 
22 {
23     auto r = convertValue!(DataType.UNORM_HALF)(1.0);
24     static assert( is( typeof(r) == ushort ) );
25     static assert( convertValue!(DataType.UNORM_HALF)(1.0) == ushort.max );
26     static assert( convertValue!(DataType.UNORM_HALF)(0.5) == ushort.max/2 );
27     static assert( convertValue!(DataType.UNORM_HALF)(0.0) == 0 );
28 }
29 
30 {
31     auto r = convertValue!(DataType.UNORM_FIXED)(1.0);
32     static assert( is( typeof(r) == uint ) );
33     static assert( convertValue!(DataType.UNORM_FIXED)(1.0) == uint.max );
34     static assert( convertValue!(DataType.UNORM_FIXED)(0.5) == uint.max/2 );
35     static assert( convertValue!(DataType.UNORM_FIXED)(0.0) == 0 );
36 }
37 
38 {
39     auto r = convertValue!(DataType.UNORM_DOUBLE)(1.0);
40     static assert( is( typeof(r) == ulong ) );
41     static assert( convertValue!(DataType.UNORM_DOUBLE)(1.0) == ulong.max );
42     static assert( convertValue!(DataType.UNORM_DOUBLE)(0.5) == ulong.max/2 );
43     static assert( convertValue!(DataType.UNORM_DOUBLE)(0.0) == 0 );
44 }
45 
46 {
47     auto r = convertValue!(DataType.NORM_QUART)(1.0);
48     static assert( is( typeof(r) == byte ) );
49     static assert( convertValue!(DataType.NORM_QUART)(1.0)  == byte.max );
50     static assert( convertValue!(DataType.NORM_QUART)(0.5)  == byte.max/2 );
51     static assert( convertValue!(DataType.NORM_QUART)(0.0)  == 0 );
52     static assert( convertValue!(DataType.NORM_QUART)(-0.5) == byte.min/2 );
53     static assert( convertValue!(DataType.NORM_QUART)(-1.0) == byte.min );
54 }
55 
56 {
57     auto r = convertValue!(DataType.NORM_HALF)(1.0);
58     static assert( is( typeof(r) == short ) );
59     static assert( convertValue!(DataType.NORM_HALF)(1.0)  == short.max );
60     static assert( convertValue!(DataType.NORM_HALF)(0.5)  == short.max/2 );
61     static assert( convertValue!(DataType.NORM_HALF)(0.0)  == 0 );
62     static assert( convertValue!(DataType.NORM_HALF)(-0.5) == short.min/2 );
63     static assert( convertValue!(DataType.NORM_HALF)(-1.0) == short.min );
64 }
65 
66 {
67     auto r = convertValue!(DataType.NORM_FIXED)(1.0);
68     static assert( is( typeof(r) == int ) );
69     static assert( convertValue!(DataType.NORM_FIXED)(1.0)  == int.max );
70     static assert( convertValue!(DataType.NORM_FIXED)(0.5)  == int.max/2 );
71     static assert( convertValue!(DataType.NORM_FIXED)(0.0)  == 0 );
72     static assert( convertValue!(DataType.NORM_FIXED)(-0.5) == int.min/2 );
73     static assert( convertValue!(DataType.NORM_FIXED)(-1.0) == int.min );
74 }
75 
76 {
77     auto r = convertValue!(DataType.NORM_DOUBLE)(1.0);
78     static assert( is( typeof(r) == long ) );
79     static assert( convertValue!(DataType.NORM_DOUBLE)(1.0)  == long.max );
80     static assert( convertValue!(DataType.NORM_DOUBLE)(0.5)  == long.max/2 );
81     static assert( convertValue!(DataType.NORM_DOUBLE)(0.0)  == 0 );
82     static assert( convertValue!(DataType.NORM_DOUBLE)(-0.5) == long.min/2 );
83     static assert( convertValue!(DataType.NORM_DOUBLE)(-1.0) == long.min );
84 }
85 
86 convertValue!(DataType.NORM_FIXED)(-0.1);
87 assertThrown!DataTypeException( convertValue!(DataType.NORM_FIXED)(1.1) );
88 
89 assertThrown!DataTypeException( convertValue!(DataType.UNORM_FIXED)(-0.1) );
90 assertThrown!DataTypeException( convertValue!(DataType.UNORM_FIXED)(1.1) );

Meta