Devel Lib
SerializableVector.h
1 #pragma once
2 
3 #include "Serializing/Core/SerializerStream.h"
4 
7 namespace Devel::Serializing {
8 #pragma pack(push, 1)
9 
19  template<typename Type = int, bool IsHidden = false, typename TSize = uint, typename T = std::vector<Type>>
20  class IVector : public IData<T, IsHidden> {
21  private:
31  template<typename TCopy = Type, typename std::enable_if_t<std::is_base_of_v<Serializing::IField, TCopy>> * = nullptr>
32  void _serialize(IO::CWriteStream &i_oStream) {
33  for (TCopy &tType: this->m_tValue) {
34  tType.doSerialize(i_oStream);
35  }
36  }
37 
46  template<typename TCopy = Type, typename std::enable_if_t<std::is_base_of_v<Serializing::IStruct, TCopy>> * = nullptr>
47  void _serialize(IO::CWriteStream &i_oStream) {
48  for (TCopy &tType: this->m_tValue) {
49  Serializing::SerializeStream(tType, i_oStream);
50  }
51  }
52 
60  template<typename TCopy = Type,
61  typename std::enable_if_t<!std::is_base_of_v<Serializing::IField, TCopy>> * = nullptr,
62  typename std::enable_if_t<!std::is_base_of_v<Serializing::IStruct, TCopy>> * = nullptr>
63  void _serialize(IO::CWriteStream &i_oStream) {
64  for (const TCopy &tType: this->m_tValue) {
65  i_oStream.push<TCopy>(tType);
66  }
67  }
68 
73  bool serialize(IO::CWriteStream &i_oStream) override {
74  i_oStream.push<TSize>(static_cast<TSize>(this->m_tValue.size()));
75  this->_serialize(i_oStream);
76  return true;
77  }
78 
79  private:
90  template<typename TCopy = Type, typename std::enable_if_t<std::is_base_of_v<Serializing::IField, TCopy>> * = nullptr>
91  void _deserialize(IO::CReadStream &i_oStream, size_t i_nSize) {
92  for (; i_nSize > 0; i_nSize--) {
93  this->m_tValue.emplace_back();
94  (--this->m_tValue.end())->doDeserialize(i_oStream);
95  }
96  }
97 
107  template<typename TCopy = Type, typename std::enable_if_t<std::is_base_of_v<Serializing::IStruct, TCopy>> * = nullptr>
108  void _deserialize(IO::CReadStream &i_oStream, size_t i_nSize) {
109  for (; i_nSize > 0; i_nSize--) {
110  this->m_tValue.emplace_back();
111  Serializing::DeserializeStream(*(--this->m_tValue.end()), i_oStream);
112  }
113  }
114 
123  template<typename TCopy = Type,
124  typename std::enable_if_t<!std::is_base_of_v<Serializing::IField, TCopy>> * = nullptr,
125  typename std::enable_if_t<!std::is_base_of_v<Serializing::IStruct, TCopy>> * = nullptr>
126  void _deserialize(IO::CReadStream &i_oStream, size_t i_nSize) {
127  for (; i_nSize > 0; i_nSize--) {
128  this->m_tValue.push_back(i_oStream.get<Type>());
129  }
130  }
131 
136  bool deserialize(IO::CReadStream &i_oStream) override {
137  const TSize nSize = i_oStream.get<TSize>();
138  this->m_tValue.reserve(nSize > 30 ? 30 : nSize);
139 
140  this->_deserialize(i_oStream, nSize);
141  return true;
142  }
143 
144  public:
145  CreateSerializeOperators(T, IsHidden);
146  };
147 
148 #pragma pack(pop)
149 }
A class for reading data from a buffer. This class provides functionality to read data from a buffer....
Definition: ReadStream.h:37
T get(const size_t i_nPosition, const bool i_fSetPosition)
Reads a value of type T from the buffer.
Definition: ReadStream.h:344
A class for writing data to a buffer. This class provides functionality to write data to a buffer....
Definition: WriteStream.h:42
void push(const void *i_pBuffer, size_t i_nSize)
Pushes the specified data to the buffer.
Definition: WriteStream.cpp:30
Template class representing a data field.
Definition: SerializableData.h:86
bool doDeserialize(IO::CReadStream &i_oStream)
Deserializes the data from a read stream.
Definition: SerializableData.h:117
bool doSerialize(IO::CWriteStream &i_oStream) const
Serializes the data to a write stream.
Definition: SerializableData.h:97
T m_tValue
The value.
Definition: SerializableData.h:78
A class representing a vector for serialization.
Definition: SerializableVector.h:20
The namespace encapsulating serializing related functionality in the Devel framework.
bool SerializeStream(const T &i_oStruct, IO::CWriteStream &i_oStream)
Serializes a struct to a write stream.
Definition: SerializerStream.h:20
bool DeserializeStream(T &i_oStruct, IO::CReadStream &i_oStream, size_t i_nStructSize=sizeof(T))
Deserializes a struct from a read stream.
Definition: SerializerStream.h:39