ZeroErr
Loading...
Searching...
No Matches
serialization.h
Go to the documentation of this file.
1#pragma once
3
5
6#include <cstdint>
7#include <cstring>
8#include <string>
9#include <type_traits>
10#include <vector>
11
13
14namespace zeroerr {
15
43struct IRObject {
44 IRObject() { std::memset(this, 0, sizeof(IRObject)); }
46 IRObject(const IRObject& other) { *this = other; }
47 IRObject(IRObject&& other) { *this = std::move(other); }
48 IRObject& operator=(const IRObject& other) {
49 std::memcpy(this, &other, sizeof(IRObject));
50 return *this;
51 }
53 std::memcpy(this, &other, sizeof(IRObject));
54 std::memset(&other, 0, sizeof(IRObject));
55 return *this;
56 }
57
59
60 union {
61 int64_t i;
62 double f;
63 char* s;
64 char ss[8];
65 IRObject* o; // first must be the number of elements
66 };
67 char others[7];
68 unsigned type;
69
70 template <typename T>
71 typename std::enable_if<std::is_integral<T>::value, T>::type GetScalar() {
72 return static_cast<T>(i);
73 }
74
75 template <typename T>
76 typename std::enable_if<std::is_floating_point<T>::value, T>::type GetScalar() {
77 return static_cast<T>(f);
78 }
79
80 template <typename T>
81 typename std::enable_if<std::is_enum<T>::value, T>::type GetScalar() {
82 return GetScalar<typename std::underlying_type<T>::type>();
83 }
84
85 template <typename T>
86 typename std::enable_if<std::is_same<T, std::string>::value, T>::type GetScalar() {
87 if (type == Type::String)
88 return std::string(s);
89 else if (type == Type::ShortString)
90 return std::string(ss);
91 return "";
92 }
93
94 template <typename T>
95 typename std::enable_if<std::is_integral<T>::value, void>::type SetScalar(T val) {
96 i = static_cast<int64_t>(val);
98 }
99
100 template <typename T>
101 typename std::enable_if<std::is_floating_point<T>::value, void>::type SetScalar(T val) {
102 f = static_cast<double>(val);
104 }
105
106 template <typename T>
107 typename std::enable_if<std::is_enum<T>::value, void>::type SetScalar(T val) {
108 SetScalar<typename std::underlying_type<T>::type>(val);
109 }
110
111 template <typename T>
112 typename std::enable_if<std::is_same<T, std::string>::value, void>::type SetScalar(T val) {
113 size_t size = val.size();
114 if (size > 14) {
115 s = alloc_str(size);
116 strcpy(s, val.c_str());
118 } else {
119 strcpy(ss, val.c_str());
120 ss[size] = 0;
122 }
123 }
124
125 void SetScalar(const IRObject& obj) { *this = obj; }
126
127 struct Childrens {
128 int64_t size;
130 };
131 Childrens GetChildren() { return {o->i, o + 1}; }
132
133 void SetChildren(IRObject* children) {
134 o = children - 1;
136 }
137
138 // ================================================================
139
140 template <typename T>
141 static
142 typename std::enable_if<std::is_integral<T>::value || std::is_floating_point<T>::value ||
143 std::is_same<T, std::string>::value || std::is_enum<T>::value,
145 FromCorpus(T val) {
146 IRObject obj;
147 obj.SetScalar(val);
148 return obj;
149 }
150
151 template <typename T>
152 static typename std::enable_if<
153 detail::is_container<T>::value && !std::is_same<T, std::string>::value, IRObject>::type
154 FromCorpus(const T& val) {
155 IRObject* children = alloc(val.size());
156 IRObject obj;
157 obj.SetChildren(children);
158
159 for (const auto& elem : val) {
160 *children++ = IRObject::FromCorpus(elem);
161 }
162 return obj;
163 }
164
165
166 template <class TupType, unsigned... I>
167 inline static void handle_tuple(const TupType& _tup, IRObject* children, detail::seq<I...>) {
168 int _[] = {((children + I)->SetScalar(FromCorpus(std::get<I>(_tup))), 0)...};
169 (void)_;
170 }
171
172 template <typename... Args>
173 static IRObject FromCorpus(const std::tuple<Args...>& val) {
174 unsigned size = sizeof...(Args);
175 IRObject obj;
176 IRObject* children = alloc(size);
177 obj.SetChildren(children);
178
179 handle_tuple(val, children, detail::gen_seq<sizeof...(Args)>{});
180 return obj;
181 }
182
183 template <typename T1, typename T2>
184 static IRObject FromCorpus(const std::pair<T1, T2>& val) {
185 IRObject obj;
186 IRObject* children = alloc(2);
187 obj.SetChildren(children);
188 children[0] = IRObject::FromCorpus(val.first);
189 children[1] = IRObject::FromCorpus(val.second);
190 return obj;
191 }
192
193 template <typename T>
194 static typename std::enable_if<std::is_integral<T>::value || std::is_enum<T>::value, T>::type
196 if (obj.type != IRObject::Int) {
197 throw std::runtime_error("Invalid type conversion.");
198 }
199 return obj.GetScalar<T>();
200 }
201
202 template <typename T>
203 static typename std::enable_if<std::is_floating_point<T>::value, T>::type ToCorpus(
204 IRObject obj) {
205 if (obj.type != IRObject::Float) {
206 throw std::runtime_error("Invalid type conversion.");
207 }
208 return obj.GetScalar<T>();
209 }
210
211 template <typename T>
212 static typename std::enable_if<std::is_same<T, std::string>::value, T>::type ToCorpus(
213 IRObject obj) {
214 if (obj.type != IRObject::String && obj.type != IRObject::ShortString) {
215 throw std::runtime_error("Invalid type conversion.");
216 }
217 return obj.GetScalar<T>();
218 }
219
220
221 template <typename TupType, unsigned... I>
222 static TupType parse_tuple(IRObject* children, detail::seq<I...>) {
223 return std::make_tuple(
224 ToCorpus<typename std::tuple_element<I, TupType>::type>(*(children + I))...);
225 }
226
227 template <typename T>
228 static typename std::enable_if<
229 detail::is_container<T>::value && !std::is_same<T, std::string>::value, T>::type
231 if (obj.type != IRObject::Object) {
232 throw std::runtime_error("Invalid type conversion.");
233 }
234
235 auto c = obj.GetChildren();
236
237 T val;
238 for (int i = 0; i < c.size; i++) {
239 val.insert(val.end(), ToCorpus<typename T::value_type>(c.children[i]));
240 }
241 return val;
242 }
243
244
245 template <typename T>
246 static typename std::enable_if<detail::is_specialization<T, std::tuple>::value, T>::type
248 if (obj.type != IRObject::Object || obj.GetChildren().size != std::tuple_size<T>::value) {
249 throw std::runtime_error("Invalid type conversion.");
250 }
251 return parse_tuple<T>(obj.o + 1, detail::gen_seq<std::tuple_size<T>::value>{});
252 }
253
254 template <typename T>
255 static typename std::enable_if<detail::is_specialization<T, std::pair>::value, T>::type
257 if (obj.type != IRObject::Object || obj.GetChildren().size != 2) {
258 throw std::runtime_error("Invalid type conversion.");
259 }
260 return std::make_pair(ToCorpus<typename T::first_type>(obj.GetChildren().children[0]),
261 ToCorpus<typename T::second_type>(obj.GetChildren().children[1]));
262 }
263
264
265 static char* alloc_str(size_t size);
266 static IRObject* alloc(size_t size);
267
268 // Serialize the object as a string.
269 static std::string ToString(IRObject obj);
270 static IRObject FromString(std::string str);
271
272 static std::vector<uint8_t> ToBinary(IRObject obj);
273 static IRObject FromBinary(std::vector<uint8_t> bin);
274};
275
276
277} // namespace zeroerr
278
#define ZEROERR_SUPPRESS_COMMON_WARNINGS_POP
Definition config.h:265
#define ZEROERR_SUPPRESS_COMMON_WARNINGS_PUSH
Definition config.h:218
Definition benchmark.cpp:17
Definition serialization.h:127
int64_t size
Definition serialization.h:128
IRObject * children
Definition serialization.h:129
Interface for serializable objects.
Definition serialization.h:43
static IRObject FromBinary(std::vector< uint8_t > bin)
Definition serialization.cpp:151
static std::enable_if< detail::is_container< T >::value &&!std::is_same< T, std::string >::value, T >::type ToCorpus(IRObject obj)
Definition serialization.h:230
static void handle_tuple(const TupType &_tup, IRObject *children, detail::seq< I... >)
Definition serialization.h:167
IRObject()
Definition serialization.h:44
void SetChildren(IRObject *children)
Definition serialization.h:133
static IRObject FromCorpus(const std::pair< T1, T2 > &val)
Definition serialization.h:184
IRObject & operator=(IRObject &&other)
Definition serialization.h:52
std::enable_if< std::is_enum< T >::value, void >::type SetScalar(T val)
Definition serialization.h:107
Childrens GetChildren()
Definition serialization.h:131
static TupType parse_tuple(IRObject *children, detail::seq< I... >)
Definition serialization.h:222
void SetScalar(const IRObject &obj)
Definition serialization.h:125
static std::string ToString(IRObject obj)
Definition serialization.cpp:133
static std::vector< uint8_t > ToBinary(IRObject obj)
Definition serialization.cpp:146
static std::enable_if< std::is_integral< T >::value||std::is_enum< T >::value, T >::type ToCorpus(IRObject obj)
Definition serialization.h:195
static std::enable_if< std::is_floating_point< T >::value, T >::type ToCorpus(IRObject obj)
Definition serialization.h:203
IRObject * o
Definition serialization.h:65
static IRObject * alloc(size_t size)
Definition serialization.cpp:7
std::enable_if< std::is_integral< T >::value, void >::type SetScalar(T val)
Definition serialization.h:95
static IRObject FromString(std::string str)
Definition serialization.cpp:139
IRObject(IRObject &&other)
Definition serialization.h:47
char others[7]
Definition serialization.h:67
std::enable_if< std::is_floating_point< T >::value, T >::type GetScalar()
Definition serialization.h:76
static std::enable_if< std::is_same< T, std::string >::value, T >::type ToCorpus(IRObject obj)
Definition serialization.h:212
std::enable_if< std::is_floating_point< T >::value, void >::type SetScalar(T val)
Definition serialization.h:101
static char * alloc_str(size_t size)
Definition serialization.cpp:13
~IRObject()
Definition serialization.h:45
char * s
Definition serialization.h:63
double f
Definition serialization.h:62
IRObject(const IRObject &other)
Definition serialization.h:46
std::enable_if< std::is_enum< T >::value, T >::type GetScalar()
Definition serialization.h:81
static std::enable_if< detail::is_container< T >::value &&!std::is_same< T, std::string >::value, IRObject >::type FromCorpus(const T &val)
Definition serialization.h:154
char ss[8]
Definition serialization.h:64
IRObject & operator=(const IRObject &other)
Definition serialization.h:48
int64_t i
Definition serialization.h:61
static std::enable_if< detail::is_specialization< T, std::pair >::value, T >::type ToCorpus(IRObject obj)
Definition serialization.h:256
std::enable_if< std::is_integral< T >::value, T >::type GetScalar()
Definition serialization.h:71
std::enable_if< std::is_same< T, std::string >::value, void >::type SetScalar(T val)
Definition serialization.h:112
static std::enable_if< detail::is_specialization< T, std::tuple >::value, T >::type ToCorpus(IRObject obj)
Definition serialization.h:247
static std::enable_if< std::is_integral< T >::value||std::is_floating_point< T >::value||std::is_same< T, std::string >::value||std::is_enum< T >::value, IRObject >::type FromCorpus(T val)
Definition serialization.h:145
static IRObject FromCorpus(const std::tuple< Args... > &val)
Definition serialization.h:173
std::enable_if< std::is_same< T, std::string >::value, T >::type GetScalar()
Definition serialization.h:86
Type
Definition serialization.h:58
@ Undefined
Definition serialization.h:58
@ ShortString
Definition serialization.h:58
@ Int
Definition serialization.h:58
@ String
Definition serialization.h:58
@ Float
Definition serialization.h:58
@ Object
Definition serialization.h:58
unsigned type
Definition serialization.h:68
Definition typetraits.h:77
Definition typetraits.h:110
Definition typetraits.h:74