CSerializer.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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 = 807;
  15. const ui32 MINIMAL_SERIALIZATION_VERSION = 805;
  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 boost::enable_if<boost::is_convertible<T,si32> >::type * dummy = 0)
  49. {
  50. return t;
  51. }
  52. template<typename T, typename NT>
  53. static NT idToNumber(const BaseForID<T, NT> &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. typedef std::map<const std::type_info *, boost::any, TypeComparer> TTypeVecMap;
  68. TTypeVecMap vectors; //entry must be a pointer to vector containing pointers to the objects of key type
  69. public:
  70. bool smartVectorMembersSerialization;
  71. bool sendStackInstanceByIds;
  72. CSerializer();
  73. ~CSerializer();
  74. virtual void reportState(vstd::CLoggerBase * out){};
  75. template <typename T, typename U>
  76. const VectorizedObjectInfo<T, U> *getVectorizedTypeInfo()
  77. {
  78. const std::type_info *myType = nullptr;
  79. myType = &typeid(T);
  80. TTypeVecMap::iterator i = vectors.find(myType);
  81. if(i == vectors.end())
  82. return nullptr;
  83. else
  84. {
  85. assert(!i->second.empty());
  86. #ifndef __APPLE__
  87. assert(i->second.type() == typeid(VectorizedObjectInfo<T, U>));
  88. #endif
  89. VectorizedObjectInfo<T, U> *ret = &(boost::any_cast<VectorizedObjectInfo<T, U>&>(i->second));
  90. return ret;
  91. }
  92. }
  93. template <typename T, typename U>
  94. T* getVectorItemFromId(const VectorizedObjectInfo<T, U> &oInfo, U id) const
  95. {
  96. si32 idAsNumber = idToNumber(id);
  97. assert(oInfo.vector);
  98. assert(static_cast<si32>(oInfo.vector->size()) > idAsNumber);
  99. return const_cast<T*>((*oInfo.vector)[idAsNumber].get());
  100. }
  101. template <typename T, typename U>
  102. U getIdFromVectorItem(const VectorizedObjectInfo<T, U> &oInfo, const T* obj) const
  103. {
  104. if(!obj)
  105. return U(-1);
  106. return oInfo.idRetriever(*obj);
  107. }
  108. void addStdVecItems(CGameState *gs, LibClasses *lib = VLC);
  109. };
  110. /// Helper to detect classes with user-provided serialize(S&, int version) method
  111. template<class S, class T>
  112. struct is_serializeable
  113. {
  114. typedef char (&Yes)[1];
  115. typedef char (&No)[2];
  116. template<class U>
  117. static Yes test(U * data, S* arg1 = 0,
  118. typename std::enable_if<std::is_void<
  119. decltype(data->serialize(*arg1, int(0)))
  120. >::value>::type * = 0);
  121. static No test(...);
  122. static const bool value = sizeof(Yes) == sizeof(is_serializeable::test((typename std::remove_reference<typename std::remove_cv<T>::type>::type*)0));
  123. };
  124. template <typename T> //metafunction returning CGObjectInstance if T is its derivate or T elsewise
  125. struct VectorizedTypeFor
  126. {
  127. typedef typename
  128. //if
  129. boost::mpl::eval_if<std::is_same<CGHeroInstance,T>,
  130. boost::mpl::identity<CGHeroInstance>,
  131. //else if
  132. boost::mpl::eval_if<std::is_base_of<CGObjectInstance,T>,
  133. boost::mpl::identity<CGObjectInstance>,
  134. //else
  135. boost::mpl::identity<T>
  136. > >::type type;
  137. };
  138. template <typename U>
  139. struct VectorizedIDType
  140. {
  141. typedef typename
  142. //if
  143. boost::mpl::eval_if<std::is_same<CArtifact,U>,
  144. boost::mpl::identity<ArtifactID>,
  145. //else if
  146. boost::mpl::eval_if<std::is_same<CCreature,U>,
  147. boost::mpl::identity<CreatureID>,
  148. //else if
  149. boost::mpl::eval_if<std::is_same<CHero,U>,
  150. boost::mpl::identity<HeroTypeID>,
  151. //else if
  152. boost::mpl::eval_if<std::is_same<CArtifactInstance,U>,
  153. boost::mpl::identity<ArtifactInstanceID>,
  154. //else if
  155. boost::mpl::eval_if<std::is_same<CGHeroInstance,U>,
  156. boost::mpl::identity<HeroTypeID>,
  157. //else if
  158. boost::mpl::eval_if<std::is_base_of<CGObjectInstance,U>,
  159. boost::mpl::identity<ObjectInstanceID>,
  160. //else
  161. boost::mpl::identity<si32>
  162. > > > > > >::type type;
  163. };
  164. /// Base class for deserializers
  165. class DLL_LINKAGE IBinaryReader : public virtual CSerializer
  166. {
  167. public:
  168. virtual int read(void * data, unsigned size) = 0;
  169. };
  170. /// Base class for serializers
  171. class DLL_LINKAGE IBinaryWriter : public virtual CSerializer
  172. {
  173. public:
  174. virtual int write(const void * data, unsigned size) = 0;
  175. };
  176. VCMI_LIB_NAMESPACE_END