1 /// Serialize a D type to a message buffer.
2 module serialize;
3 
4 import std.conv, std.traits, std.outbuffer, std.string;
5 
6 import leb128, common, oneof;
7 
8 /// Serialize top-level, either array, associative array or struct.
9 /// For aggregate types like structs, serialize each member.
10 /// Return: serialized data as `OutBuffer`
11 auto toMsgBuffer(MsgBufferType E = MsgBufferType.Var, T)(const ref T val) {
12 	return toMsgBuffer!(E, T)(val, new OutBuffer);
13 }	// toMsgBuffer()
14 
15 /// Serialise an integer to `buf`.
16 pragma(inline, true)
17 void serializeInt(MsgBufferType E = MsgBufferType.Var, T)(const T val, OutBuffer buf) {
18 	static if (E == MsgBufferType.Flat) {
19 		buf.alignSize(T.alignof);
20 		buf.write(to!T(val));
21 	} else {
22 		toLEB128(buf, to!T(val));
23 	}
24 }	// serializeInt()
25 
26 /// Serialize a single value, either a string, a floating point value or a scalar, e.g.
27 /// int, bool, long, etc. If it is neither, then forward to top-level serialize.
28 auto serializeValue(MsgBufferType E = MsgBufferType.Var, T, C = int)(const ref T val, OutBuffer buf, void delegate(const ref C val, OutBuffer buf) cb = null ) {
29 	static if (isSomeString!(T)) {
30 		serializeInt!(E)(to!uint(val.length), buf);
31 		buf.write(val);
32 	} else static if (isBoolean!(T)) {
33 		static assert(ubyte.sizeof == bool.sizeof);
34 		buf.write(to!ubyte(val));
35 	} else static if (is(T == enum)) {
36 		static if (__traits(compiles, to!int(EnumMembers!T[0]))) {
37 			static if (E == MsgBufferType.Flat) {
38 				buf.alignSize(T.alignof);
39 				buf.write(to!int(val));
40 			} else {
41 				toLEB128(buf, to!int(val));
42 			}
43 		} else {
44 			static assert(0, "only integral enums supported for enum " ~ T.stringof);
45 		}
46 	} else static if (isScalarType!(T) || isFloatingPoint!(T)) {
47 		static if (!isFloatingPoint!(T) && T.sizeof > 1 && E == MsgBufferType.Var) {
48 			toLEB128!T(buf, val);
49 		} else {
50 			buf.alignSize(T.alignof);
51 			buf.write(val);
52 		}
53 	} else static if (isArray!(T)) {
54 		serializeInt!(E)(to!uint(val.length), buf);
55 		static if (isScalarType!(typeof(val[0])) || isFloatingPoint!(typeof(val[0]))) {
56 			// Performance optimisation for scalar/floating point arrays
57 			static if (!isFloatingPoint!(typeof(val[0])) && typeof(val[0]).sizeof > 1 && E == MsgBufferType.Var) {
58 				toLEB128!T(buf, val);
59 			} else {
60 				buf.reserve(val.length * typeof(val[0]).sizeof);
61 				buf.alignSize(T.alignof);
62 				buf.write(cast(ubyte[])val);
63 			}
64 		} else {
65 			static foreach (k; val)
66 				serializeValue!(E, typeof(k))(k, buf);
67 		}
68 	} else static if (isAssociativeArray!(T)) {
69 		// TODO: the performance for AAs is underwhelming, maybe there is a way to make "forbidden"
70 		// optimisations, given the implementation details of AAs here:
71 		// https://github.com/dlang/dmd/blob/7f4620f4e1fe29641b28648c5c4c93d9fafdf90f/src/dmd/backend/aarray.d (dmd)
72 		// or
73 		// https://github.com/ldc-developers/ldc/blob/196a9eb9ad6374c0837588588f36715a3cf8e302/dmd/root/aav.d (ldc2)
74 		// However, this might not work for all compilers, so maybe there is another way I have not seen yet...
75 		serializeInt!(E)(to!uint(val.length), buf);
76 		foreach (k; val.keys) {
77 			serializeValue!(E, typeof(k))(k, buf);
78 			serializeValue!(E, typeof(val[k]))(val[k], buf);
79 		}
80 	} else static if (is(T == struct) && T.stringof.startsWith("Oneof")) {
81 		bool hasValue;
82 		static foreach (idx, s; T.tupleof) {{
83 			alias OneofMemberType = typeof(s);
84 			enum OneofMemberName = __traits(identifier, s);
85 
86 			static if (OneofMemberName.startsWith("___data_field")) {
87 
88 				if (has!(OneofMemberType)(val)) {
89 					immutable oneOfValue = get!(OneofMemberType)(val);
90 					serializeInt!(E)(to!ubyte(idx+1), buf);
91 					serializeValue!(E, OneofMemberType)(oneOfValue, buf);
92 					hasValue = true;
93 				}
94 			}
95 		}}
96 		if (!hasValue) {
97 			// Oneof was empty.
98 			serializeInt!(E)(to!ubyte(0), buf);
99 		}
100 	} else {
101 		toMsgBuffer!(E, T)(val, buf);
102 	}
103 }	// serializeValue()
104 
105 /// Serialize top-level, either array, associative array or struct.
106 /// For aggregate types like structs, serialize each member.
107 /// Return: serialized data as `OutBuffer`
108 auto toMsgBuffer(MsgBufferType E = MsgBufferType.Var, T)(const ref T val, OutBuffer buf) {
109 	static if (isAggregateType!(T) && !is(T == class)) {
110 		static foreach (v; T.tupleof)
111 			serializeValue!(E, typeof(v))(mixin("val." ~ __traits(identifier, v)), buf);
112 	} else static if (isArray!(T) || isAssociativeArray!(T)) {
113 		serializeValue!(E, T)(val, buf);
114 	} else {
115 		static assert(0, "expected struct or array, not " ~ T.stringof);
116 	}
117 	return buf;
118 }	// toMsgBuffer()