summaryrefslogtreecommitdiff
path: root/modules/codec/c++/schema.hpp
blob: 7df47b358ede46d97aa5b5cac29d9226115f8df0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
#pragma once

#include <forstio/common.hpp>
#include <forstio/templates.hpp>
#include <forstio/string_literal.hpp>

namespace saw {
template <class T> struct is_primitive {
	constexpr static bool value = false;
};

namespace schema {
// NOLINTBEGIN

/**
 * Void Type used for function schemas
 */
struct Void {
	static constexpr string_literal name = "Void";
};

/**
 * Boolean type
 */
struct Bool {
	static constexpr string_literal name = "Bool";
};

template <typename Schema, string_literal Literal> struct Member {
	static constexpr string_literal name = "Member";

	using ValueType = Schema;
	static constexpr string_literal KeyLiteral = Literal;
};

template <typename... T> struct Struct {
	static_assert(
		always_false<T...>,
		"This schema template doesn't support this type of template argument");
};

template <typename... Schema, string_literal... Key>
struct Struct<Member<Schema, Key>...> {
	static constexpr string_literal name = "Struct";
};

template <typename... T> struct Union {
	static_assert(
		always_false<T...>,
		"This schema template doesn't support this type of template argument");
};

template <typename... V, string_literal... K>
struct Union<Member<V, K>...> {
	static constexpr string_literal name = "Union";
};

template <typename T, size_t Dim = 1> struct Array {
	static constexpr string_literal name = "Array";
};

template<typename T, size_t... S> struct FixedArray {
	static constexpr string_literal name = "FixedArray";
};

template <typename... T> struct Tuple {
	static constexpr string_literal name = "Tuple";
};

/**
 * This acts as a separator of different encodings being mashed together
 * For example we can transport any base64 encodings in JSON
 *
 * using WrappedExample = schema::Tuple<
 *  schema::Wrapper<schema::String, encode::Base64>
 * >;
 *
 * data<WrappedExample, encode::Json> ex_data;
 */
template <typename Schema, typename Enc>
class Wrapper {
	static constexpr string_literal name = "Wrapper";
};

/**
 * The schema version of ref<T>.
 * See the Ptr<T> comment on this.
 */
template<typename Schema>
class Ref {
	static constexpr string_literal name = "Ref";
};

/**
 * The schema version of ptr<T>
 * It has the advantage that if used in the data class
 * it won't change the interface of the data instance.
 * Meaning that data<Ptr<UInt64>> acts as if it's data<UInt64>
 * in terms of arithmetic operations. It's mostly useful for arrays of primitive types.
 * In cases where Array<Float64> should be translated to a double*.
 * In that case we still want the data class as an accesor on the array, so we provide the
 * data<Ptr<Float64>> type.
 */
template<typename Schema>
class Ptr {
	static constexpr string_literal name = "Ptr";
};

struct String {
	static constexpr string_literal name = "String";
};

struct SignedInteger {
	static constexpr string_literal name = "SignedInteger";
};
struct UnsignedInteger {
	static constexpr string_literal name = "UnsignedInteger";
};
struct FloatingPoint {
	static constexpr string_literal name = "FloatingPoint";
};

template <class T, size_t N> struct Primitive {
	static_assert(((std::is_same_v<T, SignedInteger> ||
					std::is_same_v<T, UnsignedInteger>)&&(N == 1 || N == 2 ||
														  N == 4 || N == 8)) ||
					  (std::is_same_v<T, FloatingPoint> && (N == 4 || N == 8)),
				  "Primitive Type is not supported");
	static constexpr string_literal name = "Primitive";
	
	using InterfaceSchema = Primitive<T,N>;
	using StorageSchema = Primitive<T,N>;
};

using Int8 = Primitive<SignedInteger, 1>;
using Int16 = Primitive<SignedInteger, 2>;
using Int32 = Primitive<SignedInteger, 4>;
using Int64 = Primitive<SignedInteger, 8>;

using UInt8 = Primitive<UnsignedInteger, 1>;
using UInt16 = Primitive<UnsignedInteger, 2>;
using UInt32 = Primitive<UnsignedInteger, 4>;
using UInt64 = Primitive<UnsignedInteger, 8>;

using Float32 = Primitive<FloatingPoint, 4>;
using Float64 = Primitive<FloatingPoint, 8>;

/**
 * Classes allowing to distinguish Ints from VarInts
 */
template<typename T, uint64_t MaxLen>
struct VariableLengthPrimitive {
	static constexpr string_literal name = "VariableLengthPrimitive";
};

using VarInt = VariableLengthPrimitive<SignedInteger, 5>;
using VarLong = VariableLengthPrimitive<SignedInteger, 10>;

template<typename PrimA, typename PrimB>
struct MixedPrecision {
	using InterfaceSchema = PrimA;
	using StorageSchema = PrimB;

	static_assert(is_primitive<PrimA>::value, "InterfaceSchema needs to be a Primitive");
	static_assert(is_primitive<PrimB>::value, "StorageSchema needs to be a Primitive");
};

/**
 * Classes enabling Rpc calls
 */
template <class Request, class Response>
struct Function {
	static constexpr string_literal name = "Function";

	using RequestT = Request;
	using ResponseT = Response;
};

template <class... T> struct Interface {
	static_assert(
		always_false<T...>,
		"This schema template doesn't support this type of template argument");
	
	static constexpr string_literal name = "Interface";
};

template <class... Requests, class... Responses, string_literal... Names>
struct Interface<Member<Function<Requests, Responses>,Names>...> {
	static constexpr string_literal name = "Interface";
};

// NOLINTEND
} // namespace schema

template<typename T, typename Schema> 
struct schema_has_member{
	static_assert(
		always_false<T, Schema>,
		"Not supported schema case");
};

/**
 * Checks if identical type exists in schema structure
 */
template<typename T, typename... Members>
struct schema_has_member<T, schema::Struct<Members...>> {
private:
	template<size_t i>
	static constexpr bool value_element() {
		if constexpr ( i < sizeof...(Members) ){
			using MT = typename parameter_pack_type<i, Members...>::type;
			if constexpr ( std::is_same_v<T,MT> ) {
				return true;
			} else {
				return value_element<i+1u>();
			}
		}
		return false;
	}
public:
	static constexpr bool value = value_element<0>();
};

/**
 *
 */
template<string_literal Key, typename Schema>
struct schema_has_key {
	static_assert(
		always_false<Schema>,
		"Not supported schema case");
};

template<string_literal Key, typename... Members>
struct schema_has_key<Key, schema::Struct<Members...>> {
private:
	template<size_t i>
	static constexpr bool value_element() {
		if constexpr ( i < sizeof...(Members) ){
			using MT = typename parameter_pack_type<i, Members...>::type;

			if constexpr ( Key == MT::KeyLiteral ) {
				return true;
			}else{
				return value_element<i+1u>();
			}
		}
		return false;
	}
public:
	static constexpr bool value = value_element<0>();
};

template<string_literal Name, typename Iface>
struct schema_member_index {
	static_assert(
		always_false<Iface>,
		"Not supported schema case");
};

template<string_literal Name, typename... MemberVals, string_literal... MemberKeys >
struct schema_member_index<Name, schema::Interface<schema::Member<MemberVals, MemberKeys>...>> {
	static constexpr uint64_t value = parameter_key_pack_index<Name, MemberKeys...>::value;
};

template<string_literal Name, typename Iface>
struct schema_member_type {
	static_assert(
		always_false<Iface>,
		"Not supported schema case");
};

template<string_literal Name, typename... MemberVals, string_literal... MemberKeys >
struct schema_member_type<Name, schema::Interface<schema::Member<MemberVals, MemberKeys>...>> {

	using type = typename parameter_pack_type<schema_member_index<Name, schema::Interface<schema::Member<MemberVals,MemberKeys>...>>::value, MemberVals...>::type;
};

template <class T> struct is_struct {
	constexpr static bool value = false;
};

template <class... V, string_literal... K> struct is_struct<schema::Struct<schema::Member<V,K>...>> {
	constexpr static bool value = true;
};

template <class T> struct is_string {
	constexpr static bool value = false;
};

template <> struct is_string<schema::String> {
	constexpr static bool value = true;
};

template <class T> struct is_tuple {
	constexpr static bool value = false;
};

template <class... T> struct is_tuple<schema::Tuple<T...>> {
	constexpr static bool value = true;
};

template <class T> struct is_array {
	constexpr static bool value = false;
};

template <class T, size_t Dim> struct is_array<schema::Array<T,Dim>> {
	constexpr static bool value = true;
};

template <typename T, size_t N> struct is_primitive<schema::Primitive<T,N>> {
	constexpr static bool value = true;
};
} // namespace saw