22#include <kiwano/core/Common.h>
23#include <kiwano/math/Math.h>
43 virtual void WriteBytes(
const uint8_t* bytes,
size_t size) = 0;
47 template <
typename _Ty>
50 static_assert(std::is_trivial<_Ty>::value,
"_Ty must be trivial type.");
52 size_t size =
sizeof(_Ty) /
sizeof(uint8_t);
53 this->
WriteBytes(
reinterpret_cast<const uint8_t*
>(&value), size);
56 template <
typename _Ty>
73 void WriteBytes(
const uint8_t* bytes,
size_t size)
override
75 size_t old_size = bytes_.size();
76 this->GenerateSize(size);
77 std::memcpy(&bytes_[old_size], bytes, size);
81 void GenerateSize(
size_t size)
83 size_t old_size = bytes_.size();
84 size_t total_size = old_size + size;
85 if (total_size > bytes_.capacity())
87 if (total_size < 1024)
89 size_t expected_size = size_t(std::floor(
double(old_size) * 0.5));
90 bytes_.reserve(old_size + std::max(size, expected_size));
92 else if (size < old_size * 2)
94 size_t expected_size = size_t(std::floor(
double(old_size) * 0.25));
95 bytes_.reserve(old_size + std::max(size, expected_size));
99 bytes_.reserve(old_size + size);
102 bytes_.resize(total_size);
105 Vector<uint8_t>& bytes_;
119 stream_.write(
reinterpret_cast<const char*
>(bytes), size);
123 std::basic_ostream<char>& stream_;
137 std::fwrite(bytes,
sizeof(uint8_t), size, file_);
150 virtual void ReadBytes(uint8_t* bytes,
size_t size) = 0;
154 template <
typename _Ty>
157 static_assert(std::is_trivial<_Ty>::value,
"_Ty must be trivial type.");
159 size_t size =
sizeof(_Ty) /
sizeof(uint8_t);
160 this->
ReadBytes(
reinterpret_cast<uint8_t*
>(value), size);
163 template <
typename _Ty>
183 if (index_ + size > bytes_.size())
184 throw std::ios_base::failure(
"ByteDeserializer::ReadBytes");
186 const uint8_t* ptr =
reinterpret_cast<const uint8_t*
>(&bytes_[index_]);
187 std::memcpy(bytes, ptr, size);
193 const Vector<uint8_t>& bytes_;
207 stream_.read(
reinterpret_cast<char*
>(bytes), size);
211 std::basic_istream<char>& stream_;
225 size_t count = std::fread(bytes,
sizeof(uint8_t), size, file_);
228 throw std::ios_base::failure(
"FileDeserializer::ReadBytes");
245 Vector<uint8_t> data;
273 size_t len = std::char_traits<char>::length(str);
277 serializer.
WriteBytes(
reinterpret_cast<const uint8_t*
>(str), len);
282inline Serializer& operator<<(Serializer& serializer, StringView str)
284 return serializer << str.data();
287template <
typename _Ty>
288inline Serializer& operator<<(Serializer& serializer,
const Vector<_Ty>& arr)
290 size_t size = arr.size();
292 for (
const auto& v : arr)
299template <
typename _Ty>
300inline Serializer& operator<<(Serializer& serializer,
const List<_Ty>& list)
302 size_t size = list.size();
304 for (
const auto& v : list)
311template <
typename _Ty>
312inline Serializer& operator<<(Serializer& serializer,
const Set<_Ty>& set)
314 size_t size = set.size();
316 for (
const auto& v : set)
323template <
typename _KTy,
typename _Ty>
324inline Serializer& operator<<(Serializer& serializer,
const Map<_KTy, _Ty>& map)
326 size_t size = map.size();
328 for (
const auto& p : map)
330 serializer << p.first << p.second;
335inline Serializer& operator<<(Serializer& serializer,
const math::Vec2T<float>& vec)
337 return serializer << vec.x << vec.y;
340inline Serializer& operator<<(Serializer& serializer,
const math::RectT<float>& rect)
342 return serializer << rect.left_top << rect.right_bottom;
345inline Serializer& operator<<(Serializer& serializer,
const math::TransformT<float>& transform)
347 return serializer << transform.position << transform.rotation << transform.scale << transform.skew;
353inline Deserializer& operator>>(Deserializer& deserializer,
char* str)
359 deserializer.ReadBytes(
reinterpret_cast<uint8_t*
>(str), len);
364inline Deserializer& operator>>(Deserializer& deserializer, String& str)
371 deserializer.ReadBytes(
reinterpret_cast<uint8_t*
>(&str[0]), len);
376template <
typename _Ty>
377inline Deserializer& operator>>(Deserializer& deserializer, Vector<_Ty>& arr)
381 for (
size_t i = 0; i < len; ++i)
384 deserializer >> value;
385 arr.push_back(value);
390template <
typename _Ty>
391inline Deserializer& operator>>(Deserializer& deserializer, List<_Ty>& list)
395 for (
size_t i = 0; i < len; ++i)
398 deserializer >> value;
399 list.push_back(value);
404template <
typename _Ty>
405inline Deserializer& operator>>(Deserializer& deserializer, Set<_Ty>& set)
409 for (
size_t i = 0; i < len; ++i)
412 deserializer >> value;
418template <
typename _KTy,
typename _Ty>
419inline Deserializer& operator>>(Deserializer& deserializer, Map<_KTy, _Ty>& map)
423 for (
size_t i = 0; i < len; ++i)
427 deserializer >> key >> value;
433inline Deserializer& operator>>(Deserializer& deserializer, math::Vec2T<float>& vec)
435 return deserializer >> vec.x >> vec.y;
438inline Deserializer& operator>>(Deserializer& deserializer, math::RectT<float>& rect)
440 return deserializer >> rect.left_top >> rect.right_bottom;
443inline Deserializer& operator>>(Deserializer& deserializer, math::TransformT<float>& transform)
445 return deserializer >> transform.position >> transform.rotation >> transform.scale >> transform.skew;
可序列化对象
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