18 #include <type_traits>
40 , mUseEmbeddedPoints(
true)
46 bool isStoring()
const
52 template <
typename T>
bool serialize(T &x)
70 return write(
static_cast<uint16_t
>(magic));
75 auto const ok = read(magicRead) && (
static_cast<uint16_t
>(magic) == magicRead);
85 template <
typename T,
typename SerializeBaseType, SerializeableMagic magic>
bool serializeGeneratedType(T &x)
91 return write(
static_cast<SerializeBaseType
>(x));
110 template <
typename T>
bool serializeVector(std::vector<T> &x)
114 return writeVector(x);
118 return readVector(x);
122 template <
typename T>
123 bool serializeObjectVector(std::vector<T> &x,
SerializeableMagic const &magic = SerializeableMagic::ObjectVectorType)
127 return writeObjectVector(x, magic);
131 return readObjectVector(x, magic);
135 template <
typename T,
typename U,
typename Comp>
bool serializeObjectMap(std::map<T, U, Comp> &x)
139 return writeObjectMap(x);
143 return readObjectMap(x);
147 template <
typename T,
typename U,
typename Comp>
bool serializeObjectPtrMap(std::map<T, std::shared_ptr<U>, Comp> &x)
151 return writeObjectPtrMap(x);
155 return readObjectPtrMap(x);
159 template <
typename T,
typename U,
typename Comp>
bool serializeObjectVecMap(std::map<T, std::vector<U>, Comp> &x)
163 return writeObjectVecMap(x);
167 return readObjectVecMap(x);
172 template <
typename T>
bool read(T &x);
173 template <
typename T>
bool read(T
const &x);
174 template <
typename T>
bool write(T
const &x);
176 template <
typename T>
bool readGeneratedDouble(T &x);
177 template <
typename T>
bool writeGeneratedDouble(T
const &x);
179 template <
typename T>
bool readVector(std::vector<T> &x);
180 template <
typename T>
bool writeVector(std::vector<T> &x);
182 template <
typename T>
183 bool readObjectVector(std::vector<T> &x,
SerializeableMagic const &magic = SerializeableMagic::ObjectVectorType);
184 template <
typename T>
185 bool writeObjectVector(std::vector<T> &x,
SerializeableMagic const &magic = SerializeableMagic::ObjectVectorType);
187 template <
typename T,
typename U,
typename Comp>
bool readObjectMap(std::map<T, U, Comp> &x);
188 template <
typename T,
typename U,
typename Comp>
bool writeObjectMap(std::map<T, U, Comp> &x);
190 template <
typename T,
typename U,
typename Comp>
bool readObjectPtrMap(std::map<T, std::shared_ptr<U>, Comp> &x);
191 template <
typename T,
typename U,
typename Comp>
bool writeObjectPtrMap(std::map<T, std::shared_ptr<U>, Comp> &x);
193 template <
typename T,
typename U,
typename Comp>
bool readObjectVecMap(std::map<T, std::vector<U>, Comp> &x);
194 template <
typename T,
typename U,
typename Comp>
bool writeObjectVecMap(std::map<T, std::vector<U>, Comp> &x);
197 virtual bool write(
const void *x,
size_t bytes) = 0;
198 virtual bool read(
void *x,
size_t bytes) = 0;
213 bool old_magic = mUseMagic;
214 mUseMagic = use_magic;
230 bool oldUseEmbeddedPoints = mUseEmbeddedPoints;
232 return oldUseEmbeddedPoints;
240 return mUseEmbeddedPoints;
248 if (!serialize(SerializeableMagic::ObjectVectorMapType))
274 bool mUseEmbeddedPoints;
276 typedef uint8_t EnumSerializationType;
282 template <>
inline bool ISerializer::write<bool>(
bool const &x)
284 uint8_t b =
static_cast<uint8_t
>(x ? 1 : 0);
288 template <>
inline bool ISerializer::read<bool>(
bool &x)
302 template <>
inline bool ISerializer::write<std::string>(std::string
const &value)
304 uint32_t n =
static_cast<uint32_t
>(value.size());
305 return serialize(n) && write(value.c_str(), n);
308 template <>
inline bool ISerializer::read<std::string>(std::string &value)
313 char *buffer =
new char[n + 1];
314 if (buffer !=
nullptr)
329 inline bool doSerialize(ISerializer &serializer, std::string &value)
331 return serializer.serialize(SerializeableMagic::String) && serializer.serialize(value);
334 template <
typename T>
inline bool ISerializer::write(T
const &x)
337 static_assert(std::is_trivially_copyable<T>::value,
"Type needs non-trivial serialization!");
339 if (std::is_enum<T>())
341 EnumSerializationType v =
static_cast<EnumSerializationType
>(x);
342 return write(&v,
sizeof(v));
346 return write(&x,
sizeof(x));
350 template <
typename T>
inline bool ISerializer::read(T &x)
353 static_assert(std::is_trivially_copyable<T>::value,
"Type needs non-trivial serialization!");
355 if (std::is_enum<T>())
357 EnumSerializationType v;
358 if (read(&v,
sizeof(v)))
360 x =
static_cast<T
>(v);
370 return read(&x,
sizeof(x));
374 template <
typename T>
inline bool ISerializer::writeVector(std::vector<T> &x)
376 if (serialize(SerializeableMagic::VectorType))
381 for (
size_t i = 0; i < n; i++)
394 template <
typename T>
inline bool ISerializer::readVector(std::vector<T> &x)
396 if (serialize(SerializeableMagic::VectorType))
401 for (
size_t i = 0; i < n; i++)
416 template <
typename T>
inline bool ISerializer::writeObjectVector(std::vector<T> &x,
SerializeableMagic const &magic)
418 if (serialize(magic))
423 for (
size_t i = 0; i < n; i++)
425 if (!doSerialize(*
this, x[i]))
436 template <
typename T>
inline bool ISerializer::readObjectVector(std::vector<T> &x,
SerializeableMagic const &magic)
438 if (serialize(magic))
443 for (
size_t i = 0; i < n; i++)
446 if (doSerialize(*
this, xi))
461 template <
typename T,
typename U,
typename Comp>
inline bool ISerializer::writeObjectMap(std::map<T, U, Comp> &x)
463 if (serialize(SerializeableMagic::ObjectMapType))
468 typedef std::map<T, U> Map;
469 for (
typename Map::iterator it = x.begin(); it != x.end(); ++it)
472 if (!doSerialize(*
this, key) || !doSerialize(*
this, it->second))
483 template <
typename T,
typename U,
typename Comp>
inline bool ISerializer::readObjectMap(std::map<T, U, Comp> &x)
485 if (serialize(SerializeableMagic::ObjectMapType))
490 for (
size_t i = 0; i < n; i++)
493 if (doSerialize(*
this, first))
496 if (doSerialize(*
this, second))
498 auto insertResult = x.insert({first, second});
499 if (!insertResult.second)
520 template <
typename T,
typename U,
typename Comp>
521 inline bool ISerializer::writeObjectPtrMap(std::map<T, std::shared_ptr<U>, Comp> &x)
523 if (serialize(SerializeableMagic::ObjectPtrMapType))
528 for (
auto it = x.begin(); it != x.end(); it++)
531 if (!doSerialize(*
this, key) || !doSerialize(*
this, *it->second))
542 template <
typename T,
typename U,
typename Comp>
543 inline bool ISerializer::readObjectPtrMap(std::map<T, std::shared_ptr<U>, Comp> &x)
545 if (serialize(SerializeableMagic::ObjectPtrMapType))
550 for (
size_t i = 0; i < n; i++)
553 if (doSerialize(*
this, first))
555 std::shared_ptr<U> second = std::make_shared<U>();
556 if (doSerialize(*
this, *second))
558 auto insertResult = x.insert({first, second});
559 if (!insertResult.second)
580 template <
typename T,
typename U,
typename Comp>
581 inline bool ISerializer::writeObjectVecMap(std::map<T, std::vector<U>, Comp> &x)
583 if (serialize(SerializeableMagic::ObjectVectorMapType))
588 for (
auto it = x.begin(); it != x.end(); it++)
591 if (!doSerialize(*
this, key) || !writeObjectVector(it->second))
602 template <
typename T,
typename U,
typename Comp>
603 inline bool ISerializer::readObjectVecMap(std::map<T, std::vector<U>, Comp> &x)
605 if (serialize(SerializeableMagic::ObjectVectorMapType))
610 for (
size_t i = 0; i < n; i++)
613 if (doSerialize(*
this, first))
615 auto insertResult = x.insert({first, std::vector<U>()});
616 if (!insertResult.second)
620 if (!readObjectVector(insertResult.first->second))