CSerializer.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. #include "../ConstTransitivePtr.h"
  12. #include "../GameConstants.h"
  13. VCMI_LIB_NAMESPACE_BEGIN
  14. const ui32 SERIALIZATION_VERSION = 828;
  15. const ui32 MINIMAL_SERIALIZATION_VERSION = 828;
  16. const std::string SAVEGAME_MAGIC = "VCMISVG";
  17. class CHero;
  18. class CGHeroInstance;
  19. class CGObjectInstance;
  20. class CGameState;
  21. class LibClasses;
  22. extern DLL_LINKAGE LibClasses * VLC;
  23. struct TypeComparer
  24. {
  25. bool operator()(const std::type_info *a, const std::type_info *b) const
  26. {
  27. //#ifndef __APPLE__
  28. // return a->before(*b);
  29. //#else
  30. return strcmp(a->name(), b->name()) < 0;
  31. //#endif
  32. }
  33. };
  34. template <typename ObjType, typename IdType>
  35. struct VectorizedObjectInfo
  36. {
  37. const std::vector<ConstTransitivePtr<ObjType> > *vector; //pointer to the appropriate vector
  38. std::function<IdType(const ObjType &)> idRetriever;
  39. VectorizedObjectInfo(const std::vector< ConstTransitivePtr<ObjType> > *Vector, std::function<IdType(const ObjType &)> IdGetter)
  40. :vector(Vector), idRetriever(IdGetter)
  41. {
  42. }
  43. };
  44. /// Base class for serializers capable of reading or writing data
  45. class DLL_LINKAGE CSerializer
  46. {
  47. template<typename T>
  48. static si32 idToNumber(const T &t, typename std::enable_if<std::is_convertible<T,si32>::value>::type * dummy = nullptr)
  49. {
  50. return t;
  51. }
  52. template<typename T, typename NT>
  53. static NT idToNumber(const IdentifierBase &t)
  54. {
  55. return t.getNum();
  56. }
  57. template <typename T, typename U>
  58. void registerVectoredType(const std::vector<T*> *Vector, const std::function<U(const T&)> &idRetriever)
  59. {
  60. vectors[&typeid(T)] = VectorizedObjectInfo<T, U>(Vector, idRetriever);
  61. }
  62. template <typename T, typename U>
  63. void registerVectoredType(const std::vector<ConstTransitivePtr<T> > *Vector, const std::function<U(const T&)> &idRetriever)
  64. {
  65. vectors[&typeid(T)] = VectorizedObjectInfo<T, U>(Vector, idRetriever);
  66. }
  67. using TTypeVecMap = std::map<const std::type_info *, std::any, TypeComparer>;
  68. TTypeVecMap vectors; //entry must be a pointer to vector containing pointers to the objects of key type
  69. public:
  70. bool smartVectorMembersSerialization = false;
  71. bool sendStackInstanceByIds = false;
  72. ~CSerializer();
  73. virtual void reportState(vstd::CLoggerBase * out){};
  74. template <typename T, typename U>
  75. const VectorizedObjectInfo<T, U> *getVectorizedTypeInfo()
  76. {
  77. const std::type_info *myType = nullptr;
  78. myType = &typeid(T);
  79. auto i = vectors.find(myType);
  80. if(i == vectors.end())
  81. return nullptr;
  82. else
  83. {
  84. assert(i->second.has_value());
  85. #ifndef __APPLE__
  86. assert(i->second.type() == typeid(VectorizedObjectInfo<T, U>));
  87. #endif
  88. auto *ret = std::any_cast<VectorizedObjectInfo<T, U>>(&i->second);
  89. return ret;
  90. }
  91. }
  92. template <typename T, typename U>
  93. T* getVectorItemFromId(const VectorizedObjectInfo<T, U> &oInfo, U id) const
  94. {
  95. si32 idAsNumber = idToNumber(id);
  96. assert(oInfo.vector);
  97. assert(static_cast<si32>(oInfo.vector->size()) > idAsNumber);
  98. return const_cast<T*>((*oInfo.vector)[idAsNumber].get());
  99. }
  100. template <typename T, typename U>
  101. U getIdFromVectorItem(const VectorizedObjectInfo<T, U> &oInfo, const T* obj) const
  102. {
  103. if(!obj)
  104. return U(-1);
  105. return oInfo.idRetriever(*obj);
  106. }
  107. void addStdVecItems(CGameState *gs, LibClasses *lib = VLC);
  108. };
  109. /// Helper to detect classes with user-provided serialize(S&, int version) method
  110. template<class S, class T>
  111. struct is_serializeable
  112. {
  113. using Yes = char (&)[1];
  114. using No = char (&)[2];
  115. template<class U>
  116. static Yes test(U * data, S* arg1 = 0,
  117. typename std::enable_if<std::is_void<
  118. decltype(data->serialize(*arg1, int(0)))
  119. >::value>::type * = 0);
  120. static No test(...);
  121. static const bool value = sizeof(Yes) == sizeof(is_serializeable::test((typename std::remove_reference<typename std::remove_cv<T>::type>::type*)0));
  122. };
  123. template <typename T> //metafunction returning CGObjectInstance if T is its derivate or T elsewise
  124. struct VectorizedTypeFor
  125. {
  126. using type = typename
  127. //if
  128. boost::mpl::eval_if<std::is_same<CGHeroInstance, T>,
  129. boost::mpl::identity<CGHeroInstance>,
  130. //else if
  131. boost::mpl::eval_if<std::is_base_of<CGObjectInstance, T>,
  132. boost::mpl::identity<CGObjectInstance>,
  133. //else
  134. boost::mpl::identity<T>
  135. >>::type;
  136. };
  137. template <typename U>
  138. struct VectorizedIDType
  139. {
  140. using type = typename
  141. //if
  142. boost::mpl::eval_if<std::is_same<CArtifact, U>,
  143. boost::mpl::identity<ArtifactID>,
  144. //else if
  145. boost::mpl::eval_if<std::is_same<CCreature, U>,
  146. boost::mpl::identity<CreatureID>,
  147. //else if
  148. boost::mpl::eval_if<std::is_same<CHero, U>,
  149. boost::mpl::identity<HeroTypeID>,
  150. //else if
  151. boost::mpl::eval_if<std::is_same<CArtifactInstance, U>,
  152. boost::mpl::identity<ArtifactInstanceID>,
  153. //else if
  154. boost::mpl::eval_if<std::is_same<CGHeroInstance, U>,
  155. boost::mpl::identity<HeroTypeID>,
  156. //else if
  157. boost::mpl::eval_if<std::is_base_of<CGObjectInstance, U>,
  158. boost::mpl::identity<ObjectInstanceID>,
  159. //else
  160. boost::mpl::identity<si32>
  161. >>>>>>::type;
  162. };
  163. /// Base class for deserializers
  164. class DLL_LINKAGE IBinaryReader : public virtual CSerializer
  165. {
  166. public:
  167. virtual int read(void * data, unsigned size) = 0;
  168. };
  169. /// Base class for serializers
  170. class DLL_LINKAGE IBinaryWriter : public virtual CSerializer
  171. {
  172. public:
  173. virtual int write(const void * data, unsigned size) = 0;
  174. };
  175. VCMI_LIB_NAMESPACE_END