-
Notifications
You must be signed in to change notification settings - Fork 1
/
ser.hpp
156 lines (135 loc) · 3.1 KB
/
ser.hpp
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
#pragma once
#include "is_serializable.hpp"
#include "istrm.hpp"
#include "ostrm.hpp"
#include <memory>
#include <optional>
#include <string>
#include <vector>
class Ser;
class Deser;
class Ser
{
public:
constexpr Ser(OStrm &strm) : strm(strm) {}
template <typename T>
constexpr auto operator()(const char *, const T &value) -> void
{
if constexpr (internal::IsSerializableClassV<T>)
value.ser(*this);
else
serVal(value);
}
template <typename T>
constexpr auto serVal(const T &value) noexcept
-> std::enable_if_t<std::is_arithmetic_v<T> || std::is_enum_v<T>>
{
strm.write((char *)&value, sizeof(value));
}
auto serVal(const std::string &value) noexcept -> void;
template <typename T>
constexpr auto serVal(const std::vector<T> &value) -> void
{
int32_t sz = value.size();
operator()("sz", sz);
for (auto &&v : value)
operator()("v", v);
}
template <typename T>
constexpr auto serVal(const std::unique_ptr<T> &value) -> void
{
int32_t isNull = value ? 0 : 1;
operator()("isNull", isNull);
if (value)
operator()("*value", *value);
}
template <typename T>
constexpr auto serVal(const std::optional<T> &value) -> void
{
int32_t isNull = value ? 0 : 1;
operator()("isNull", isNull);
if (value)
operator()("*value", *value);
}
private:
OStrm &strm;
};
class Deser
{
public:
constexpr Deser(IStrm &strm) : strm(strm) {}
template <typename T>
constexpr auto deserVal(T &value) noexcept
-> std::enable_if_t<std::is_arithmetic_v<T> || std::is_enum_v<T>>
{
strm.read((char *)&value, sizeof(value));
}
auto deserVal(std::string &value) noexcept -> void;
template <typename T>
constexpr auto operator()(const char *, T &value) -> void
{
if constexpr (internal::IsSerializableClassV<T>)
value.deser(*this);
else
deserVal(value);
}
template <typename T>
constexpr auto deserVal(std::vector<T> &value) -> void
{
value.clear();
int32_t sz{};
operator()("sz", sz);
value.reserve(sz);
for (auto i = 0; i < sz; ++i)
{
T &v = value.emplace_back();
operator()("v", v);
}
}
template <typename T>
constexpr auto deserVal(std::unique_ptr<T> &value) -> void
{
int32_t isNull{};
operator()("isNull", isNull);
if (isNull == 1)
{
value = nullptr;
return;
}
value = std::make_unique<T>();
operator()("*value", *value);
}
template <typename T>
constexpr auto deserVal(std::optional<T> &value) -> void
{
int32_t isNull{};
operator()("isNull", isNull);
if (isNull == 1)
{
value = std::nullopt;
return;
}
value = T{};
operator()("*value", *value);
}
private:
IStrm &strm;
};
template <typename T>
constexpr auto ser(OStrm &strm, const T &value) -> void
{
Ser s(strm);
if constexpr (internal::IsSerializableClassV<T>)
value.ser(s);
else
s("value", value);
}
template <typename T>
constexpr auto deser(IStrm &strm, T &value) -> void
{
Deser s(strm);
if constexpr (internal::IsSerializableClassV<T>)
value.deser(s);
else
s("value", value);
}