-
Notifications
You must be signed in to change notification settings - Fork 1
/
xx_data_ex.h
240 lines (211 loc) · 7.96 KB
/
xx_data_ex.h
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
#pragma once
#include "xx_data_shared.h"
#include "xx_ptr.h"
namespace xx {
/* for copy
struct XXXXXXXXXXX {
static constexpr uint16_t cTypeId{ 111111111 };
static constexpr uint16_t cParentTypeId{ xx::SerdeBase::cTypeId };
void WriteTo(xx::Data& d) const override;
int32_t ReadFrom(xx::Data_r& dr) override;
};
*/
struct alignas(8) SerdeBase {
static constexpr uint16_t cTypeId{};
static constexpr uint16_t cParentTypeId{};
SerdeBase() = default;
virtual ~SerdeBase() = default;
virtual void WriteTo(Data& d) const {};
virtual int32_t ReadFrom(Data_r& dr) { return __LINE__; }
uint16_t typeId; // fill by MakeShared, after constructor
// user data area
uint8_t udByte1{}, udByte2{};
int32_t indexAtContainer{ -1 };
};
struct SerdeInfo;
struct DataEx_r : Data_r {
using Data_r::Data_r;
SerdeInfo* si{}; // need fill before use
xx::Listi32<xx::Shared<SerdeBase>> ptrs; // need clear after read finish
};
struct UdPtr {
size_t* p;
UdPtr(size_t* p) : p(p) {}
UdPtr(UdPtr const&) = default;
UdPtr& operator=(UdPtr const&) = default;
UdPtr(UdPtr&& u) noexcept {
p = std::exchange(u.p, nullptr);
}
UdPtr& operator=(UdPtr&& u) noexcept {
p = std::exchange(u.p, nullptr);
return *this;
}
~UdPtr() {
*p = 0;
}
};
template<>
struct IsPod<UdPtr, void> : std::true_type {};
struct DataEx : Data {
using Data::Data;
SerdeInfo* si{}; // need fill before use
xx::Listi32<UdPtr> ptrs; // need clear after write finish
};
struct SerdeInfo {
typedef Shared<SerdeBase> (*Func) ();
inline static Shared<SerdeBase> null;
std::array<Func, 65536> fs{};
std::array<uint16_t, 65536> pids{};
void Init() {
memset(fs.data(), 0, sizeof(Func) * fs.size());
memset(pids.data(), 0, sizeof(uint16_t) * pids.size());
}
XX_INLINE bool IsBaseOf(uint16_t baseTypeId, uint16_t typeId) noexcept {
for (; typeId != baseTypeId; typeId = pids[typeId]) {
if (!typeId || typeId == pids[typeId]) return false;
}
return true;
}
template<typename BT>
XX_INLINE bool IsBaseOf(uint16_t typeId) noexcept {
static_assert(std::is_base_of_v<SerdeBase, BT>);
return IsBaseOf(BT::cTypeId, typeId);
}
template<typename BT, typename T>
XX_INLINE bool IsBaseOf() noexcept {
static_assert(std::is_base_of_v<SerdeBase, T>);
static_assert(std::is_base_of_v<SerdeBase, BT>);
return IsBaseOf(BT::cTypeId, T::cTypeId);
}
template<typename T>
XX_INLINE void Register() {
static_assert(std::is_base_of_v<SerdeBase, T>);
assert(!fs[T::cTypeId]);
pids[T::cTypeId] = T::cParentTypeId;
fs[T::cTypeId] = []() -> Shared<SerdeBase> { return ::xx::MakeShared<T>(); };
}
template<typename T = SerdeBase>
XX_INLINE Shared<T> MakeShared(uint16_t const& typeId) {
static_assert(std::is_base_of_v<SerdeBase, T>);
if (!typeId || !fs[typeId] || !IsBaseOf<T>(typeId)) return nullptr;
return fs[typeId]().Cast<T>();
}
XX_INLINE DataEx MakeDataEx() {
DataEx d;
d.si = this;
return d;
}
template<typename M>
XX_INLINE DataShared MakeDataShared(M&& msg) {
auto d = MakeDataEx();
d.Write(std::forward<M>(msg));
return std::move(d);
}
template<typename M>
XX_INLINE DataShared MakeDataShared() {
return MakeDataShared(::xx::MakeShared<M>());
}
template<typename D>
XX_INLINE DataEx_r MakeDataEx_r(D&& d) {
DataEx_r dr(std::forward<D>(d));
dr.si = this;
return dr;
}
template<typename M>
XX_INLINE Shared<M> MakeMessage(DataShared const& ds) {
auto dr = MakeDataEx_r(ds);
xx::Shared<M> msg;
if (auto r = dr.Read(msg))
return {};
if (dr.offset != dr.len)
return {};
return msg;
}
};
XX_INLINE uint16_t ReadVarUint16(void* buf, size_t offset, size_t len) {
auto u = ((uint8_t*)buf)[offset] & 0x7Fu;
if ((((uint8_t*)buf)[offset] & 0x80u) == 0) return u;
if (offset + 1 >= len) return 0;
return u | ((((uint8_t*)buf)[offset + 1] & 0x7Fu) << 7);
}
XX_INLINE uint16_t ReadTypeId(DataShared const& ds) {
auto buf = ds.GetBuf();
auto len = ds.GetLen();
if (len < 2 || buf[0] != 1) return 0;
return ReadVarUint16(buf, 1, len);
}
template<typename T>
struct DataFuncs<T, std::enable_if_t<xx::IsShared_v<T> && std::is_base_of_v<SerdeBase, typename T::ElementType>>> {
using U = typename T::ElementType;
//static_assert(TypeId_v<U> > 0);
template<bool needReserve = true>
static inline void Write(Data& d, T const& in) {
if (!in) {
d.WriteFixed<needReserve>((uint8_t)0); // index( nullptr ) same as WriteVar(size_t 0)
} else {
auto h = in.GetHeader();
if (h->ud == 0) { // first time
auto& ptrs = ((DataEx&)d).ptrs;
ptrs.Emplace(&h->ud); // for restore
h->ud = (size_t)ptrs.len; // store index
d.WriteVarInteger<needReserve>(h->ud); // index
d.WriteVarInteger<needReserve>(in->typeId); // type id
in->WriteTo(d); // content
} else { // exists
d.WriteVarInteger<needReserve>(h->ud); // index
}
}
}
static inline int Read(Data_r& dr, T& out) {
size_t idx;
if (int r = dr.Read(idx)) return r; // index
if (!idx) { // nullptr
out.Reset();
return 0;
}
--idx;
auto& ptrs = ((DataEx_r&)dr).ptrs;
auto& si = *((DataEx_r&)dr).si;
auto len = (size_t)ptrs.len;
if (idx == len) { // first time
uint16_t typeId;
if (int r = dr.Read(typeId)) return r; // type id
if (!typeId) return __LINE__;
if (!si.fs[typeId]) return __LINE__;
if (!si.IsBaseOf<U>(typeId)) return __LINE__;
if (!out || out->typeId != typeId) {
out = std::move(si.MakeShared<U>(typeId));
}
assert(out);
ptrs.Emplace(out);
return out->ReadFrom(dr);
} else {
if (idx > len) return __LINE__;
auto& o = ptrs[(int32_t)idx];
if (!si.IsBaseOf<U>(o->typeId)) return __LINE__;
out = (Shared<U>&)o;
return 0;
}
}
};
template<typename T>
struct DataFuncs<T, std::enable_if_t<xx::IsWeak_v<T> && std::is_base_of_v<SerdeBase, typename T::ElementType>>> {
using U = typename T::ElementType;
//static_assert(TypeId_v<U> > 0);
template<bool needReserve = true>
static inline void Write(Data& d, T const& in) {
if (!in) {
d.WriteFixed<needReserve>((uint8_t)0); // index( nullptr ) same as WriteVar(size_t 0)
} else {
auto p = &in.h->data;
d.Write<needReserve>((Shared<U>&)p);
}
}
static inline int Read(Data_r& dr, T& out) {
Shared<U> o;
if (int r = dr.Read(o)) return r;
out = o;
return 0;
}
};
}