summaryrefslogtreecommitdiff
path: root/modules/codec/c++/schema_factory.hpp
blob: 7eb3e5c2e212599c7be8c1b32f9654996e8780a9 (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
#pragma once

#include "schema.hpp"

namespace saw {
template<typename T>
struct schema_factory {
		using Schema = T;
		static_assert(always_false<T>, "Not supported");
};

template<typename... T, string_literal... Keys>
struct schema_factory<schema::Struct<schema::Member<T,Keys>...>> {
private:
public:
		using Schema = schema::Struct<schema::Member<T,Keys>...>;
		
		template<string_literal KA>
		static constexpr bool has_key() noexcept {
			return schema_has_key<KA, Schema>::value;
		}

		template<typename TA, string_literal KA>
		static constexpr bool has_member() noexcept {
			return schema_has_member<schema::Member<TA,KA>, Schema>::value;
		}

		template<typename TA, string_literal KA>
		constexpr auto add_maybe() const noexcept {
			if constexpr (!has_key<KA>()) {
				return schema_factory<schema::Struct<schema::Member<T,Keys>..., schema::Member<TA,KA>>>{};
			} else if constexpr (has_member<TA,KA>() ){
				return schema_factory<schema::Struct<schema::Member<T,Keys>...> >{};
			} else {
				static_assert(always_false<TA>, "Struct already has the provided Key, but a mismatching Type");
			}
		}

		template<typename TA, string_literal KA>
		constexpr schema_factory<schema::Struct<schema::Member<T,Keys>...,schema::Member<TA,KA>>> add() const noexcept {
			static_assert(!has_key<KA>(), "Can't add member. Name is already in use.");
			return {};
		}
};

template<typename... T, string_literal... Keys>
struct schema_factory<schema::Union<schema::Member<T,Keys>...>> {
		using Schema = schema::Union<schema::Member<T,Keys>...>;
		
		template<string_literal KA>
		static constexpr bool has_key() noexcept {
			return schema_has_key<KA, Schema>::value;
		}

		template<typename TA, string_literal KA>
		constexpr schema_factory<schema::Union<schema::Member<T,Keys>...,schema::Member<TA,KA>>> add() const noexcept {
			static_assert(!has_key<KA>(), "Can't add member. Name is already in use.");
			return {};
		}
};

template<typename... T>
struct schema_factory<schema::Tuple<T...>> {
		using Schema = schema::Tuple<T...>;

		template<typename TA>
		constexpr schema_factory<schema::Tuple<T...,TA>> add() const noexcept {
				return {};
		}
};

/**
 * This creates the base schema. For example an empty struct,tuple,union or anything else.
 */
template<typename T>
constexpr schema_factory<T> build_schema() noexcept {
		return {};
}
}