CSerializer.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. const ui32 SERIALIZATION_VERSION = 792;
  14. const ui32 MINIMAL_SERIALIZATION_VERSION = 753;
  15. const std::string SAVEGAME_MAGIC = "VCMISVG";
  16. class CHero;
  17. class CGHeroInstance;
  18. class CGObjectInstance;
  19. class CGameState;
  20. class LibClasses;
  21. extern DLL_LINKAGE LibClasses * VLC;
  22. struct TypeComparer
  23. {
  24. bool operator()(const std::type_info *a, const std::type_info *b) const
  25. {
  26. //#ifndef __APPLE__
  27. // return a->before(*b);
  28. //#else
  29. return strcmp(a->name(), b->name()) < 0;
  30. //#endif
  31. }
  32. };
  33. template <typename ObjType, typename IdType>
  34. struct VectorizedObjectInfo
  35. {
  36. const std::vector<ConstTransitivePtr<ObjType> > *vector; //pointer to the appropriate vector
  37. std::function<IdType(const ObjType &)> idRetriever;
  38. VectorizedObjectInfo(const std::vector< ConstTransitivePtr<ObjType> > *Vector, std::function<IdType(const ObjType &)> IdGetter)
  39. :vector(Vector), idRetriever(IdGetter)
  40. {
  41. }
  42. };
  43. /// Base class for serializers capable of reading or writing data
  44. class DLL_LINKAGE CSerializer
  45. {
  46. template<typename T>
  47. static si32 idToNumber(const T &t, typename boost::enable_if<boost::is_convertible<T,si32> >::type * dummy = 0)
  48. {
  49. return t;
  50. }
  51. template<typename T, typename NT>
  52. static NT idToNumber(const BaseForID<T, NT> &t)
  53. {
  54. return t.getNum();
  55. }
  56. template <typename T, typename U>
  57. void registerVectoredType(const std::vector<T*> *Vector, const std::function<U(const T&)> &idRetriever)
  58. {
  59. vectors[&typeid(T)] = VectorizedObjectInfo<T, U>(Vector, idRetriever);
  60. }
  61. template <typename T, typename U>
  62. void registerVectoredType(const std::vector<ConstTransitivePtr<T> > *Vector, const std::function<U(const T&)> &idRetriever)
  63. {
  64. vectors[&typeid(T)] = VectorizedObjectInfo<T, U>(Vector, idRetriever);
  65. }
  66. typedef std::map<const std::type_info *, boost::any, TypeComparer> TTypeVecMap;
  67. TTypeVecMap vectors; //entry must be a pointer to vector containing pointers to the objects of key type
  68. public:
  69. bool smartVectorMembersSerialization;
  70. bool sendStackInstanceByIds;
  71. CSerializer();
  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. TTypeVecMap::iterator i = vectors.find(myType);
  80. if(i == vectors.end())
  81. return nullptr;
  82. else
  83. {
  84. assert(!i->second.empty());
  85. #ifndef __APPLE__
  86. assert(i->second.type() == typeid(VectorizedObjectInfo<T, U>));
  87. #endif
  88. VectorizedObjectInfo<T, U> *ret = &(boost::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. typedef char (&Yes)[1];
  114. typedef char (&No)[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. typedef 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 type;
  136. };
  137. template <typename U>
  138. struct VectorizedIDType
  139. {
  140. typedef 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 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. };