Kiwano Engine  v1.2.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 
25 namespace kiwano
26 {
37 struct Serializer
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 
66 struct ByteSerializer : public Serializer
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 
80 private:
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 
122 private:
123  std::basic_ostream<char>& stream_;
124 };
125 
128 struct FileSerializer : public Serializer
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 
140 private:
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 
191 private:
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 
210 private:
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 
232 private:
233  FILE* file_;
234 };
235 
239 {
240 public:
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 //
270 // operator<< for Serializer
271 //
272 inline Serializer& operator<<(Serializer& serializer, const char* str)
273 {
274  size_t len = std::char_traits<char>::length(str);
275  serializer.WriteValue(len);
276  if (len)
277  {
278  serializer.WriteBytes(reinterpret_cast<const uint8_t*>(str), len);
279  }
280  return serializer;
281 }
282 
283 inline Serializer& operator<<(Serializer& serializer, const String& str)
284 {
285  return serializer << str.c_str();
286 }
287 
288 template <typename _Ty>
289 inline Serializer& operator<<(Serializer& serializer, const Vector<_Ty>& arr)
290 {
291  size_t size = arr.size();
292  serializer.WriteValue(size);
293  for (const auto& v : arr)
294  {
295  serializer << v;
296  }
297  return serializer;
298 }
299 
300 template <typename _Ty>
301 inline Serializer& operator<<(Serializer& serializer, const List<_Ty>& list)
302 {
303  size_t size = list.size();
304  serializer.WriteValue(size);
305  for (const auto& v : list)
306  {
307  serializer << v;
308  }
309  return serializer;
310 }
311 
312 template <typename _Ty>
313 inline Serializer& operator<<(Serializer& serializer, const Set<_Ty>& set)
314 {
315  size_t size = set.size();
316  serializer.WriteValue(size);
317  for (const auto& v : set)
318  {
319  serializer << v;
320  }
321  return serializer;
322 }
323 
324 template <typename _KTy, typename _Ty>
325 inline Serializer& operator<<(Serializer& serializer, const Map<_KTy, _Ty>& map)
326 {
327  size_t size = map.size();
328  serializer.WriteValue(size);
329  for (const auto& p : map)
330  {
331  serializer << p.first << p.second;
332  }
333  return serializer;
334 }
335 
336 inline Serializer& operator<<(Serializer& serializer, const math::Vec2T<float>& vec)
337 {
338  return serializer << vec.x << vec.y;
339 }
340 
341 inline Serializer& operator<<(Serializer& serializer, const math::RectT<float>& rect)
342 {
343  return serializer << rect.left_top << rect.right_bottom;
344 }
345 
346 inline Serializer& operator<<(Serializer& serializer, const math::TransformT<float>& transform)
347 {
348  return serializer << transform.position << transform.rotation << transform.scale << transform.skew;
349 }
350 
351 
352 //
353 // operator>> for Deserializer
354 //
355 inline Deserializer& operator>>(Deserializer& deserializer, char* str)
356 {
357  size_t len = 0;
358  deserializer.ReadValue(&len);
359  if (len)
360  {
361  deserializer.ReadBytes(reinterpret_cast<uint8_t*>(str), len);
362  }
363  return deserializer;
364 }
365 
366 inline Deserializer& operator>>(Deserializer& deserializer, String& str)
367 {
368  size_t len = 0;
369  deserializer.ReadValue(&len);
370  if (len)
371  {
372  str.resize(len);
373  deserializer.ReadBytes(reinterpret_cast<uint8_t*>(&str[0]), len);
374  }
375  return deserializer;
376 }
377 
378 template <typename _Ty>
379 inline Deserializer& operator>>(Deserializer& deserializer, Vector<_Ty>& arr)
380 {
381  size_t len = 0;
382  deserializer.ReadValue(&len);
383  for (size_t i = 0; i < len; ++i)
384  {
385  _Ty value;
386  deserializer >> value;
387  arr.push_back(value);
388  }
389  return deserializer;
390 }
391 
392 template <typename _Ty>
393 inline Deserializer& operator>>(Deserializer& deserializer, List<_Ty>& list)
394 {
395  size_t len = 0;
396  deserializer.ReadValue(&len);
397  for (size_t i = 0; i < len; ++i)
398  {
399  _Ty value;
400  deserializer >> value;
401  list.push_back(value);
402  }
403  return deserializer;
404 }
405 
406 template <typename _Ty>
407 inline Deserializer& operator>>(Deserializer& deserializer, Set<_Ty>& set)
408 {
409  size_t len = 0;
410  deserializer.ReadValue(&len);
411  for (size_t i = 0; i < len; ++i)
412  {
413  _Ty value;
414  deserializer >> value;
415  set.insert(value);
416  }
417  return deserializer;
418 }
419 
420 template <typename _KTy, typename _Ty>
421 inline Deserializer& operator>>(Deserializer& deserializer, Map<_KTy, _Ty>& map)
422 {
423  size_t len = 0;
424  deserializer.ReadValue(&len);
425  for (size_t i = 0; i < len; ++i)
426  {
427  _KTy key;
428  _Ty value;
429  deserializer >> key >> value;
430  map[key] = value;
431  }
432  return deserializer;
433 }
434 
435 inline Deserializer& operator>>(Deserializer& deserializer, math::Vec2T<float>& vec)
436 {
437  return deserializer >> vec.x >> vec.y;
438 }
439 
440 inline Deserializer& operator>>(Deserializer& deserializer, math::RectT<float>& rect)
441 {
442  return deserializer >> rect.left_top >> rect.right_bottom;
443 }
444 
445 inline Deserializer& operator>>(Deserializer& deserializer, math::TransformT<float>& transform)
446 {
447  return deserializer >> transform.position >> transform.rotation >> transform.scale >> transform.skew;
448 }
449 
453 } // namespace kiwano
void WriteBytes(const uint8_t *bytes, size_t size) override
写入字节序列
Definition: Serializable.h:135
void ReadBytes(uint8_t *bytes, size_t size) override
读取字节序列
Definition: Serializable.h:223
可序列化对象
Definition: Serializable.h:238
流序列化器
Definition: Serializable.h:110
Vec2T< ValueType > scale
缩放
Definition: Transform.hpp:42
文件反序列化器
Definition: Serializable.h:216
序列化器
Definition: Serializable.h:39
void WriteBytes(const uint8_t *bytes, size_t size) override
写入字节序列
Definition: Serializable.h:73
void ReadBytes(uint8_t *bytes, size_t size) override
读取字节序列
Definition: Serializable.h:181
Vec2T< ValueType > position
坐标
Definition: Transform.hpp:41
void WriteValue(const _Ty &value)
写入值
Definition: Serializable.h:48
void WriteBytes(const uint8_t *bytes, size_t size) override
写入字节序列
Definition: Serializable.h:117
流反序列化器
Definition: Serializable.h:198
Vector< uint8_t > Serialize() const
序列化为字节串
Definition: Serializable.h:243
void ReadBytes(uint8_t *bytes, size_t size) override
读取字节序列
Definition: Serializable.h:205
字节串序列化器
Definition: Serializable.h:66
Definition: Actor.cpp:26
virtual void WriteBytes(const uint8_t *bytes, size_t size)=0
写入字节序列
float rotation
旋转
Definition: Transform.hpp:40
virtual void ReadBytes(uint8_t *bytes, size_t size)=0
读取字节序列
void ReadValue(_Ty *value)
读取值
Definition: Serializable.h:155
Vec2T< ValueType > skew
错切角度
Definition: Transform.hpp:43
文件序列化器
Definition: Serializable.h:128
字节串反序列化器
Definition: Serializable.h:173
反序列化器
Definition: Serializable.h:146
void Deserialize(const Vector< uint8_t > &data)
从字节串反序列化
Definition: Serializable.h:253