CSerializer.h 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. /*
  2. * CSerializer.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. VCMI_LIB_NAMESPACE_BEGIN
  12. const std::string SAVEGAME_MAGIC = "VCMISVG";
  13. /// Helper to detect classes with user-provided serialize(S&, int version) method
  14. template<class S, class T>
  15. struct is_serializeable
  16. {
  17. using Yes = char (&)[1];
  18. using No = char (&)[2];
  19. template<class U>
  20. static Yes test(U * data, S* arg1 = nullptr, typename std::enable_if_t<std::is_void_v<decltype(data->serialize(*arg1))>> * = nullptr);
  21. static No test(...);
  22. static const bool value = sizeof(Yes) == sizeof(is_serializeable::test((typename std::remove_reference_t<typename std::remove_cv_t<T>>*)nullptr));
  23. };
  24. /// Base class for deserializers
  25. class IBinaryReader
  26. {
  27. public:
  28. virtual ~IBinaryReader() = default;
  29. virtual int read(std::byte * data, unsigned size) = 0;
  30. };
  31. /// Base class for serializers
  32. class IBinaryWriter
  33. {
  34. public:
  35. virtual ~IBinaryWriter() = default;
  36. virtual int write(const std::byte * data, unsigned size) = 0;
  37. };
  38. VCMI_LIB_NAMESPACE_END