CMemorySerializer.h 1019 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. /*
  2. * CMemorySerializer.h, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #pragma once
  11. #include "BinarySerializer.h"
  12. #include "BinaryDeserializer.h"
  13. VCMI_LIB_NAMESPACE_BEGIN
  14. /// Serializer that stores objects in the dynamic buffer. Allows performing deep object copies.
  15. class DLL_LINKAGE CMemorySerializer
  16. : public IBinaryReader, public IBinaryWriter
  17. {
  18. std::vector<std::byte> buffer;
  19. size_t readPos; //index of the next byte to be read
  20. public:
  21. BinaryDeserializer iser;
  22. BinarySerializer oser;
  23. int read(std::byte * data, unsigned size) override; //throws!
  24. int write(const std::byte * data, unsigned size) override;
  25. CMemorySerializer();
  26. template <typename T>
  27. static std::unique_ptr<T> deepCopy(const T &data)
  28. {
  29. CMemorySerializer mem;
  30. mem.oser & &data;
  31. std::unique_ptr<T> ret;
  32. mem.iser & ret;
  33. return ret;
  34. }
  35. };
  36. VCMI_LIB_NAMESPACE_END