1 module fdb.tuple.tupletype;
2 
3 import
4     std.exception,
5     std.uuid;
6 
7 static const uint  floatSignMask      = 0x80_00_00_00U;
8 static const ulong doubleSignMask     = 0x80_00_00_00_00_00_00_00UL;
9 static const ulong byteArrayEndMarker = 0xff;
10 
11 enum TupleType : ubyte {
12     /**
13      * Null/Empty/Void
14      */
15     Nil     = 0,
16 
17     /**
18      * ASCII String
19      */
20     Bytes   = 1,
21 
22     /**
23      * UTF-8 String
24      */
25     Utf8    = 2,
26 
27     IntNeg8 = 12,
28     IntNeg7 = 13,
29     IntNeg6 = 14,
30     IntNeg5 = 15,
31     IntNeg4 = 16,
32     IntNeg3 = 17,
33     IntNeg2 = 18,
34     IntNeg1 = 19,
35 
36     /**
37      * Base value for integer types (20 +/- n)
38      */
39     IntBase = 20,
40     IntZero = 20,
41 
42     IntPos1 = 21,
43     IntPos2 = 22,
44     IntPos3 = 23,
45     IntPos4 = 24,
46     IntPos5 = 25,
47     IntPos6 = 26,
48     IntPos7 = 27,
49     IntPos8 = 28,
50 
51     /**
52      * Single precision decimals (32-bit, Big-Endian) [DRAFT]
53      */
54     Single  = 32,
55 
56     /**
57      * Double precision decimals (64-bit, Big-Endian) [DRAFT]
58      */
59     Double  = 33,
60 
61     /**
62      * RFC4122 UUID (128 bits) [DRAFT]
63      */
64     Uuid128 = 48,
65 }
66 
67 auto FDBsizeof(const TupleType type) pure
68 in
69 {
70     enforce(
71         type.isFDBIntegral ||
72         type.isFDBFloat ||
73         type.isFDBDouble ||
74         type.isFDBUUID);
75 }
76 body
77 {
78     if (type.isFDBIntegral)
79     {
80         if (type > TupleType.IntZero)
81             return type - TupleType.IntZero;
82         return TupleType.IntZero - type;
83     }
84     else if (type.isFDBFloat)
85         return float.sizeof;
86     else if (type.isFDBDouble)
87         return double.sizeof;
88     else if (type.isFDBUUID)
89         return UUID.sizeof;
90 
91     assert(0, "Type " ~ type ~ " is not supported");
92 }
93 
94 bool isFDBIntegral(const TupleType type) pure @nogc
95 {
96     return type >= TupleType.IntNeg8 && type <= TupleType.IntPos8;
97 }
98 
99 bool isFDBFloat(const TupleType type) pure @nogc
100 {
101     return type == TupleType.Single;
102 }
103 
104 bool isFDBDouble(const TupleType type) pure @nogc
105 {
106     return type == TupleType.Double;
107 }
108 
109 bool isFDBUUID(const TupleType type) pure @nogc
110 {
111     return type == TupleType.Uuid128;
112 }
113 
114 unittest
115 {
116     assert(TupleType.IntNeg8.FDBsizeof == 8);
117     assert(TupleType.IntNeg7.FDBsizeof == 7);
118     assert(TupleType.IntNeg6.FDBsizeof == 6);
119     assert(TupleType.IntNeg5.FDBsizeof == 5);
120     assert(TupleType.IntNeg4.FDBsizeof == 4);
121     assert(TupleType.IntNeg3.FDBsizeof == 3);
122     assert(TupleType.IntNeg2.FDBsizeof == 2);
123     assert(TupleType.IntNeg1.FDBsizeof == 1);
124 
125     assert(TupleType.IntZero.FDBsizeof == 0);
126 
127     assert(TupleType.IntPos1.FDBsizeof == 1);
128     assert(TupleType.IntPos2.FDBsizeof == 2);
129     assert(TupleType.IntPos3.FDBsizeof == 3);
130     assert(TupleType.IntPos4.FDBsizeof == 4);
131     assert(TupleType.IntPos5.FDBsizeof == 5);
132     assert(TupleType.IntPos6.FDBsizeof == 6);
133     assert(TupleType.IntPos7.FDBsizeof == 7);
134     assert(TupleType.IntPos8.FDBsizeof == 8);
135 }