CSerializer.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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 = 788;
  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. assert(i->second.type() == typeid(VectorizedObjectInfo<T, U>));
  86. VectorizedObjectInfo<T, U> *ret = &(boost::any_cast<VectorizedObjectInfo<T, U>&>(i->second));
  87. return ret;
  88. }
  89. }
  90. template <typename T, typename U>
  91. T* getVectorItemFromId(const VectorizedObjectInfo<T, U> &oInfo, U id) const
  92. {
  93. si32 idAsNumber = idToNumber(id);
  94. assert(oInfo.vector);
  95. assert(static_cast<si32>(oInfo.vector->size()) > idAsNumber);
  96. return const_cast<T*>((*oInfo.vector)[idAsNumber].get());
  97. }
  98. template <typename T, typename U>
  99. U getIdFromVectorItem(const VectorizedObjectInfo<T, U> &oInfo, const T* obj) const
  100. {
  101. if(!obj)
  102. return U(-1);
  103. return oInfo.idRetriever(*obj);
  104. }
  105. void addStdVecItems(CGameState *gs, LibClasses *lib = VLC);
  106. };
  107. /// Helper to detect classes with user-provided serialize(S&, int version) method
  108. template<class S, class T>
  109. struct is_serializeable
  110. {
  111. typedef char (&Yes)[1];
  112. typedef char (&No)[2];
  113. template<class U>
  114. static Yes test(U * data, S* arg1 = 0,
  115. typename std::enable_if<std::is_void<
  116. decltype(data->serialize(*arg1, int(0)))
  117. >::value>::type * = 0);
  118. static No test(...);
  119. static const bool value = sizeof(Yes) == sizeof(is_serializeable::test((typename std::remove_reference<typename std::remove_cv<T>::type>::type*)0));
  120. };
  121. template <typename T> //metafunction returning CGObjectInstance if T is its derivate or T elsewise
  122. struct VectorizedTypeFor
  123. {
  124. typedef typename
  125. //if
  126. boost::mpl::eval_if<std::is_same<CGHeroInstance,T>,
  127. boost::mpl::identity<CGHeroInstance>,
  128. //else if
  129. boost::mpl::eval_if<std::is_base_of<CGObjectInstance,T>,
  130. boost::mpl::identity<CGObjectInstance>,
  131. //else
  132. boost::mpl::identity<T>
  133. > >::type type;
  134. };
  135. template <typename U>
  136. struct VectorizedIDType
  137. {
  138. typedef typename
  139. //if
  140. boost::mpl::eval_if<std::is_same<CArtifact,U>,
  141. boost::mpl::identity<ArtifactID>,
  142. //else if
  143. boost::mpl::eval_if<std::is_same<CCreature,U>,
  144. boost::mpl::identity<CreatureID>,
  145. //else if
  146. boost::mpl::eval_if<std::is_same<CHero,U>,
  147. boost::mpl::identity<HeroTypeID>,
  148. //else if
  149. boost::mpl::eval_if<std::is_same<CArtifactInstance,U>,
  150. boost::mpl::identity<ArtifactInstanceID>,
  151. //else if
  152. boost::mpl::eval_if<std::is_same<CGHeroInstance,U>,
  153. boost::mpl::identity<HeroTypeID>,
  154. //else if
  155. boost::mpl::eval_if<std::is_base_of<CGObjectInstance,U>,
  156. boost::mpl::identity<ObjectInstanceID>,
  157. //else
  158. boost::mpl::identity<si32>
  159. > > > > > >::type type;
  160. };
  161. /// Base class for deserializers
  162. class IBinaryReader : public virtual CSerializer
  163. {
  164. public:
  165. virtual int read(void * data, unsigned size) = 0;
  166. };
  167. /// Base class for serializers
  168. class IBinaryWriter : public virtual CSerializer
  169. {
  170. public:
  171. virtual int write(const void * data, unsigned size) = 0;
  172. };