CMemorySerializer.h 953 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  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. /// Serializer that stores objects in the dynamic buffer. Allows performing deep object copies.
  14. class DLL_LINKAGE CMemorySerializer
  15. : public IBinaryReader, public IBinaryWriter
  16. {
  17. std::vector<ui8> buffer;
  18. size_t readPos; //index of the next byte to be read
  19. public:
  20. BinaryDeserializer iser;
  21. BinarySerializer oser;
  22. int read(void * data, unsigned size) override; //throws!
  23. int write(const void * data, unsigned size) override;
  24. CMemorySerializer();
  25. template <typename T>
  26. static std::unique_ptr<T> deepCopy(const T &data)
  27. {
  28. CMemorySerializer mem;
  29. mem.oser & &data;
  30. std::unique_ptr<T> ret;
  31. mem.iser & ret;
  32. return ret;
  33. }
  34. };