1 module hunt.serialization.BinaryDeserializer;
2 
3 import hunt.serialization.Common;
4 import hunt.serialization.Specify;
5 import std.traits;
6 
7 import hunt.logging;
8 
9 /**
10  * 
11  */
12 struct BinaryDeserializer {
13 
14     private {
15         const(ubyte)[] _buffer;
16     }
17 
18     this(ubyte[] buffer) {
19         this._buffer = buffer;
20     }
21 
22     const(ubyte[]) bytes() const nothrow {
23         return _buffer;
24     }
25 
26     ulong bytesLeft() const {
27         return _buffer.length;
28     }
29 
30     T iArchive(SerializationOptions options, T)()
31             if (!isDynamicArray!T && !isAssociativeArray!T && !is(T == class) && __traits(compiles, T())) {
32         T obj;
33         specify!(options)(this, obj);
34         return obj;
35     }
36 
37     T iArchive(SerializationOptions options, T)()
38             if (!isDynamicArray!T && !isAssociativeArray!T && !is(T == class)
39                 && !__traits(compiles, T())) {
40         T obj = void;
41         specify!(options)(this, obj);
42         return obj;
43     }
44 
45     T iArchive(SerializationOptions options, T, A...)(A args) if (is(T == class)) {
46         T obj = new T(args);
47         specify!(options)(this, obj);
48         return obj;
49     }
50 
51     T iArchive(SerializationOptions options, T)() if (isDynamicArray!T || isAssociativeArray!T) {
52         return iArchive!(options, T, ushort)();
53     }
54 
55     T iArchive(SerializationOptions options, T, U)() if (isDynamicArray!T || isAssociativeArray!T) {
56         T obj;
57         specify!(options)(this, obj);
58         return obj;
59     }
60 
61     void putUbyte(ref ubyte val) {
62         val = _buffer[0];
63         _buffer = _buffer[1 .. $];
64     }
65 
66     void putClass(SerializationOptions options, T)(T val) if (is(T == class)) {
67         specifyClass!(options)(this, val);
68     }
69 
70     deprecated("Using take instead.")
71     alias putRaw = take;
72 
73     const(ubyte)[] take(size_t length) {
74         const(ubyte)[] res = _buffer[0 .. length];
75         _buffer = _buffer[length .. $];
76         return res;
77     }
78 
79     // T takeAs(T, SerializationOptions options)() 
80     //     if (!is(T == enum) && (isSigned!T || isBoolean!T || is(T == char) || isFloatingPoint!T)) {
81     //     T r;
82     //     // T* val = &r;
83 
84     //     // ubyte val0 = (val >> 24);
85     //     // ubyte val1 = cast(ubyte)(val >> 16);
86     //     // ubyte val2 = cast(ubyte)(val >> 8);
87     //     // ubyte val3 = val & 0xff;
88     //     // putUbyte(val0);
89     //     // putUbyte(val1);
90     //     // putUbyte(val2);
91     //     // putUbyte(val3);
92     //     // val = (val0 << 24) + (val1 << 16) + (val2 << 8) + val3;
93     //     return r;
94     // }
95 
96     // T takeAs(T, SerializationOptions options)() {
97     //     return T.init;
98     // }
99 
100     bool isNullObj() {
101         return _buffer[0 .. 4] == NULL ? true : false;
102     }
103 }