CMemorySerializer.cpp 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. /*
  2. * CMemorySerializer.cpp, 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. #include "StdInc.h"
  11. #include "CMemorySerializer.h"
  12. VCMI_LIB_NAMESPACE_BEGIN
  13. int CMemorySerializer::read(std::byte * data, unsigned size)
  14. {
  15. if(buffer.size() < readPos + size)
  16. throw std::runtime_error(boost::str(boost::format("Cannot read past the buffer (accessing index %d, while size is %d)!") % (readPos + size - 1) % buffer.size()));
  17. std::memcpy(data, buffer.data() + readPos, size);
  18. readPos += size;
  19. return size;
  20. }
  21. int CMemorySerializer::write(const std::byte * data, unsigned size)
  22. {
  23. auto oldSize = buffer.size(); //and the pos to write from
  24. buffer.resize(oldSize + size);
  25. std::memcpy(buffer.data() + oldSize, data, size);
  26. return size;
  27. }
  28. CMemorySerializer::CMemorySerializer(): iser(this), oser(this), readPos(0)
  29. {
  30. iser.version = ESerializationVersion::CURRENT;
  31. }
  32. VCMI_LIB_NAMESPACE_END