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.ConsoleLogger;
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     bool isNullObj() {
80         return _buffer[0 .. 4] == NULL ? true : false;
81     }
82 }