Kiwano Engine v1.3.x
Serializable.h
1// Copyright (c) 2016-2018 Kiwano - Nomango
2//
3// Permission is hereby granted, free of charge, to any person obtaining a copy
4// of this software and associated documentation files (the "Software"), to deal
5// in the Software without restriction, including without limitation the rights
6// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7// copies of the Software, and to permit persons to whom the Software is
8// furnished to do so, subject to the following conditions:
9//
10// The above copyright notice and this permission notice shall be included in
11// all copies or substantial portions of the Software.
12//
13// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19// THE SOFTWARE.
20
21#pragma once
22#include <kiwano/core/Common.h>
23#include <kiwano/math/Math.h>
24
25namespace kiwano
26{
40{
43 virtual void WriteBytes(const uint8_t* bytes, size_t size) = 0;
44
47 template <typename _Ty>
48 void WriteValue(const _Ty& value)
49 {
50 static_assert(std::is_trivial<_Ty>::value, "_Ty must be trivial type.");
51
52 size_t size = sizeof(_Ty) / sizeof(uint8_t);
53 this->WriteBytes(reinterpret_cast<const uint8_t*>(&value), size);
54 }
55
56 template <typename _Ty>
57 Serializer& operator<<(const _Ty& value)
58 {
59 this->WriteValue(value);
60 return (*this);
61 }
62};
63
67{
68 ByteSerializer(Vector<uint8_t>& bytes)
69 : bytes_(bytes)
70 {
71 }
72
73 void WriteBytes(const uint8_t* bytes, size_t size) override
74 {
75 size_t old_size = bytes_.size();
76 this->GenerateSize(size);
77 std::memcpy(&bytes_[old_size], bytes, size);
78 }
79
80private:
81 void GenerateSize(size_t size)
82 {
83 size_t old_size = bytes_.size();
84 size_t total_size = old_size + size;
85 if (total_size > bytes_.capacity())
86 {
87 if (total_size < 1024)
88 {
89 size_t expected_size = size_t(std::floor(double(old_size) * 0.5));
90 bytes_.reserve(old_size + std::max(size, expected_size));
91 }
92 else if (size < old_size * 2)
93 {
94 size_t expected_size = size_t(std::floor(double(old_size) * 0.25));
95 bytes_.reserve(old_size + std::max(size, expected_size));
96 }
97 else
98 {
99 bytes_.reserve(old_size + size);
100 }
101 }
102 bytes_.resize(total_size);
103 }
104
105 Vector<uint8_t>& bytes_;
106};
107
111{
112 StreamSerializer(std::basic_ostream<char>& stream)
113 : stream_(stream)
114 {
115 }
116
117 void WriteBytes(const uint8_t* bytes, size_t size) override
118 {
119 stream_.write(reinterpret_cast<const char*>(bytes), size);
120 }
121
122private:
123 std::basic_ostream<char>& stream_;
124};
125
129{
130 FileSerializer(FILE* file)
131 : file_(file)
132 {
133 }
134
135 void WriteBytes(const uint8_t* bytes, size_t size) override
136 {
137 std::fwrite(bytes, sizeof(uint8_t), size, file_);
138 }
139
140private:
141 FILE* file_;
142};
143
147{
150 virtual void ReadBytes(uint8_t* bytes, size_t size) = 0;
151
154 template <typename _Ty>
155 void ReadValue(_Ty* value)
156 {
157 static_assert(std::is_trivial<_Ty>::value, "_Ty must be trivial type.");
158
159 size_t size = sizeof(_Ty) / sizeof(uint8_t);
160 this->ReadBytes(reinterpret_cast<uint8_t*>(value), size);
161 }
162
163 template <typename _Ty>
164 Deserializer& operator>>(_Ty& value)
165 {
166 this->ReadValue(&value);
167 return (*this);
168 }
169};
170
174{
175 ByteDeserializer(const Vector<uint8_t>& bytes)
176 : bytes_(bytes)
177 , index_(0)
178 {
179 }
180
181 void ReadBytes(uint8_t* bytes, size_t size) override
182 {
183 if (index_ + size > bytes_.size())
184 throw std::ios_base::failure("ByteDeserializer::ReadBytes");
185
186 const uint8_t* ptr = reinterpret_cast<const uint8_t*>(&bytes_[index_]);
187 std::memcpy(bytes, ptr, size);
188 index_ += size;
189 }
190
191private:
192 size_t index_;
193 const Vector<uint8_t>& bytes_;
194};
195
199{
200 StreamDeserializer(std::basic_istream<char>& stream)
201 : stream_(stream)
202 {
203 }
204
205 void ReadBytes(uint8_t* bytes, size_t size) override
206 {
207 stream_.read(reinterpret_cast<char*>(bytes), size);
208 }
209
210private:
211 std::basic_istream<char>& stream_;
212};
213
217{
218 FileDeserializer(FILE* file)
219 : file_(file)
220 {
221 }
222
223 void ReadBytes(uint8_t* bytes, size_t size) override
224 {
225 size_t count = std::fread(bytes, sizeof(uint8_t), size, file_);
226 if (count < size)
227 {
228 throw std::ios_base::failure("FileDeserializer::ReadBytes");
229 }
230 }
231
232private:
233 FILE* file_;
234};
235
239{
240public:
243 inline Vector<uint8_t> Serialize() const
244 {
245 Vector<uint8_t> data;
246 ByteSerializer serializer(data);
247 this->DoSerialize(&serializer);
248 return data;
249 }
250
253 inline void Deserialize(const Vector<uint8_t>& data)
254 {
255 ByteDeserializer deserializer(data);
256 this->DoDeserialize(&deserializer);
257 }
258
261 virtual void DoSerialize(Serializer* serializer) const = 0;
262
265 virtual void DoDeserialize(Deserializer* deserializer) = 0;
266};
267
268//
269// operator<< for Serializer
270//
271inline Serializer& operator<<(Serializer& serializer, const char* str)
272{
273 size_t len = std::char_traits<char>::length(str);
274 serializer.WriteValue(len);
275 if (len)
276 {
277 serializer.WriteBytes(reinterpret_cast<const uint8_t*>(str), len);
278 }
279 return serializer;
280}
281
282inline Serializer& operator<<(Serializer& serializer, StringView str)
283{
284 return serializer << str.data();
285}
286
287template <typename _Ty>
288inline Serializer& operator<<(Serializer& serializer, const Vector<_Ty>& arr)
289{
290 size_t size = arr.size();
291 serializer.WriteValue(size);
292 for (const auto& v : arr)
293 {
294 serializer << v;
295 }
296 return serializer;
297}
298
299template <typename _Ty>
300inline Serializer& operator<<(Serializer& serializer, const List<_Ty>& list)
301{
302 size_t size = list.size();
303 serializer.WriteValue(size);
304 for (const auto& v : list)
305 {
306 serializer << v;
307 }
308 return serializer;
309}
310
311template <typename _Ty>
312inline Serializer& operator<<(Serializer& serializer, const Set<_Ty>& set)
313{
314 size_t size = set.size();
315 serializer.WriteValue(size);
316 for (const auto& v : set)
317 {
318 serializer << v;
319 }
320 return serializer;
321}
322
323template <typename _KTy, typename _Ty>
324inline Serializer& operator<<(Serializer& serializer, const Map<_KTy, _Ty>& map)
325{
326 size_t size = map.size();
327 serializer.WriteValue(size);
328 for (const auto& p : map)
329 {
330 serializer << p.first << p.second;
331 }
332 return serializer;
333}
334
335inline Serializer& operator<<(Serializer& serializer, const math::Vec2T<float>& vec)
336{
337 return serializer << vec.x << vec.y;
338}
339
340inline Serializer& operator<<(Serializer& serializer, const math::RectT<float>& rect)
341{
342 return serializer << rect.left_top << rect.right_bottom;
343}
344
345inline Serializer& operator<<(Serializer& serializer, const math::TransformT<float>& transform)
346{
347 return serializer << transform.position << transform.rotation << transform.scale << transform.skew;
348}
349
350//
351// operator>> for Deserializer
352//
353inline Deserializer& operator>>(Deserializer& deserializer, char* str)
354{
355 size_t len = 0;
356 deserializer.ReadValue(&len);
357 if (len)
358 {
359 deserializer.ReadBytes(reinterpret_cast<uint8_t*>(str), len);
360 }
361 return deserializer;
362}
363
364inline Deserializer& operator>>(Deserializer& deserializer, String& str)
365{
366 size_t len = 0;
367 deserializer.ReadValue(&len);
368 if (len)
369 {
370 str.resize(len);
371 deserializer.ReadBytes(reinterpret_cast<uint8_t*>(&str[0]), len);
372 }
373 return deserializer;
374}
375
376template <typename _Ty>
377inline Deserializer& operator>>(Deserializer& deserializer, Vector<_Ty>& arr)
378{
379 size_t len = 0;
380 deserializer.ReadValue(&len);
381 for (size_t i = 0; i < len; ++i)
382 {
383 _Ty value;
384 deserializer >> value;
385 arr.push_back(value);
386 }
387 return deserializer;
388}
389
390template <typename _Ty>
391inline Deserializer& operator>>(Deserializer& deserializer, List<_Ty>& list)
392{
393 size_t len = 0;
394 deserializer.ReadValue(&len);
395 for (size_t i = 0; i < len; ++i)
396 {
397 _Ty value;
398 deserializer >> value;
399 list.push_back(value);
400 }
401 return deserializer;
402}
403
404template <typename _Ty>
405inline Deserializer& operator>>(Deserializer& deserializer, Set<_Ty>& set)
406{
407 size_t len = 0;
408 deserializer.ReadValue(&len);
409 for (size_t i = 0; i < len; ++i)
410 {
411 _Ty value;
412 deserializer >> value;
413 set.insert(value);
414 }
415 return deserializer;
416}
417
418template <typename _KTy, typename _Ty>
419inline Deserializer& operator>>(Deserializer& deserializer, Map<_KTy, _Ty>& map)
420{
421 size_t len = 0;
422 deserializer.ReadValue(&len);
423 for (size_t i = 0; i < len; ++i)
424 {
425 _KTy key;
426 _Ty value;
427 deserializer >> key >> value;
428 map[key] = value;
429 }
430 return deserializer;
431}
432
433inline Deserializer& operator>>(Deserializer& deserializer, math::Vec2T<float>& vec)
434{
435 return deserializer >> vec.x >> vec.y;
436}
437
438inline Deserializer& operator>>(Deserializer& deserializer, math::RectT<float>& rect)
439{
440 return deserializer >> rect.left_top >> rect.right_bottom;
441}
442
443inline Deserializer& operator>>(Deserializer& deserializer, math::TransformT<float>& transform)
444{
445 return deserializer >> transform.position >> transform.rotation >> transform.scale >> transform.skew;
446}
447
450} // namespace kiwano
可序列化对象
Definition: Serializable.h:239
Vector< uint8_t > Serialize() const
序列化为字节串
Definition: Serializable.h:243
virtual void DoDeserialize(Deserializer *deserializer)=0
执行反序列化
virtual void DoSerialize(Serializer *serializer) const =0
执行序列化
void Deserialize(const Vector< uint8_t > &data)
从字节串反序列化
Definition: Serializable.h:253
字节串反序列化器
Definition: Serializable.h:174
void ReadBytes(uint8_t *bytes, size_t size) override
读取字节序列
Definition: Serializable.h:181
字节串序列化器
Definition: Serializable.h:67
void WriteBytes(const uint8_t *bytes, size_t size) override
写入字节序列
Definition: Serializable.h:73
反序列化器
Definition: Serializable.h:147
virtual void ReadBytes(uint8_t *bytes, size_t size)=0
读取字节序列
void ReadValue(_Ty *value)
读取值
Definition: Serializable.h:155
文件反序列化器
Definition: Serializable.h:217
void ReadBytes(uint8_t *bytes, size_t size) override
读取字节序列
Definition: Serializable.h:223
文件序列化器
Definition: Serializable.h:129
void WriteBytes(const uint8_t *bytes, size_t size) override
写入字节序列
Definition: Serializable.h:135
序列化器
Definition: Serializable.h:40
void WriteValue(const _Ty &value)
写入值
Definition: Serializable.h:48
virtual void WriteBytes(const uint8_t *bytes, size_t size)=0
写入字节序列
流反序列化器
Definition: Serializable.h:199
void ReadBytes(uint8_t *bytes, size_t size) override
读取字节序列
Definition: Serializable.h:205
流序列化器
Definition: Serializable.h:111
void WriteBytes(const uint8_t *bytes, size_t size) override
写入字节序列
Definition: Serializable.h:117