-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSerializationContract.h
More file actions
189 lines (148 loc) · 5.69 KB
/
SerializationContract.h
File metadata and controls
189 lines (148 loc) · 5.69 KB
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
// Implementation of 'SERIALIZATION_CONTRACT'
#pragma once
#include "SerializationContractData.h"
namespace SerializationContract {
//
// Processor
//
template <const char* Name, typename T>
struct Processor;
template <const char* Name, typename... Params>
struct Processor<Name, std::function<void(Params...)>> {
template <typename IsConstParams, typename ...Ts>
auto CreateTupleWithParamsProxy(Ts&&... ts) {
auto tuple = std::tuple<Ts&...>(std::forward<Ts&>(ts)...);
return TupleWithParamsProxy<IsConstParams, decltype(tuple)>{std::move(tuple)};
}
// Used only for serialization.
auto operator ()(const Params&... params) {
return CreateTupleWithParamsProxy<std::true_type>(std::forward<const Params&>(params)...);
}
// Used for serialization and unserialization.
auto operator ()(Params&... params) {
return CreateTupleWithParamsProxy<std::false_type>(std::forward<Params&>(params)...);
}
template <typename IsConstParams, typename TupleWithParams>
struct TupleWithParamsProxy {
static constexpr auto LastTupleIndex = std::tuple_size_v<TupleWithParams> -1;
// Serialization
void operator >> (std::vector<uint8_t>& bytes) {
Serializer serializer(bytes);
SerializeParams<0>(serializer);
}
template<int Index>
void SerializeParams(Serializer& serializer) {
if constexpr (Index == 0) {
serializer << std::string(Name);
}
serializer << std::get<Index>(tupleWithParams_);
if constexpr (Index < LastTupleIndex) {
SerializeParams<Index + 1>(serializer);
}
}
// Unserialization
void operator << (const std::vector<uint8_t>& bytes) {
static_assert(std::is_same_v<IsConstParams, std::false_type>, "Cannot unserialize to const");
Unserializer unserializer(bytes);
UnserializeParams<0>(unserializer);
}
template<int Index>
void UnserializeParams(Unserializer& unserializer) {
if constexpr (Index == 0) {
std::string name;
unserializer >> name;
}
unserializer >> std::get<Index>(tupleWithParams_);
if constexpr (Index < LastTupleIndex) {
UnserializeParams<Index + 1>(unserializer);
}
}
TupleWithParams tupleWithParams_;
};
};
//
// UnserializeDispatcher
//
class UnserializeDispatcher {
public:
static UnserializeDispatcher& Instance() {
static UnserializeDispatcher s_unserializeDispatcher;
return s_unserializeDispatcher;
}
struct IDispatcher {
virtual ~IDispatcher() = default;
virtual bool Dispatch(const std::string& contractName, Unserializer& unserializer) = 0;
};
template <typename F, const char* Name, typename... Params>
class TDispatcher : public IDispatcher {
public:
TDispatcher(F f)
: f_(f)
{}
template <typename ...> struct ArgsCollector;
template <typename T, typename ...Ts>
struct ArgsCollector<T, Ts...> {
template <typename ...Args>
static void CollectArgs(F f, Unserializer& unserializer, const Args&...args) {
T arg;
unserializer >> arg;
if constexpr (0 == sizeof...(Ts)) {
f(args..., (const T&)arg);
} else {
ArgsCollector<Ts...>::template CollectArgs<>(f, unserializer, args..., arg);
}
}
};
bool Dispatch(const std::string& contractName, Unserializer& unserializer) override {
if (contractName != Name) {
return false;
}
ArgsCollector<Params...>::template CollectArgs<>(f_, unserializer);
return true;
}
F f_;
};
bool Dispatch(const std::vector<uint8_t>& bytes) {
Unserializer unserializer(bytes);
std::string contractName;
unserializer >> contractName;
bool dispatched = false;
for (const auto& pDispatcher : vDispatcher_) {
if (pDispatcher->Dispatch(contractName, unserializer)) {
dispatched = true;
break;
}
}
return dispatched;
}
template <const char* Name, typename... Params, typename F>
void Subscribe(const Processor<Name, std::function<void(Params...)>>&, F f) {
vDispatcher_.emplace_back(std::make_unique<TDispatcher<F, Name, Params...>>(f));
}
std::vector<std::unique_ptr<IDispatcher>> vDispatcher_;
};
//
// UnserializeDispatcherProxy
//
template <const char* Name, typename... Params>
class UnserializeDispatcherProxy {
public:
UnserializeDispatcherProxy(const Processor<Name, std::function<void(Params...)>>& processor)
: processor_(processor)
{}
template <typename F>
bool operator = (F f)
{
UnserializeDispatcher::Instance().Subscribe(processor_, f);
return true;
}
Processor<Name, std::function<void(Params...)>> processor_;
};
}
#define SERIALIZATION_CONTRACT(x, ...) \
[[maybe_unused]] inline static constexpr char s_contractName##x[] = #x; \
[[maybe_unused]] static auto x = SerializationContract::Processor<s_contractName##x, std::function<void(__VA_ARGS__)>>(); \
#define ON_SERIALIZATION_CONTRACT(x) \
[[maybe_unused]] static bool s_onContract##x = SerializationContract::UnserializeDispatcherProxy(x) =
#define PROCESS_SERIALIZATION_CONTRACT(bytes) \
SerializationContract::UnserializeDispatcher::Instance().Dispatch(bytes);