BinarySerializer.h 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. /*
  2. * BinarySerializer.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 "CTypeList.h"
  12. #include "../mapObjects/CArmedInstance.h"
  13. class DLL_LINKAGE CSaverBase
  14. {
  15. protected:
  16. IBinaryWriter * writer;
  17. public:
  18. CSaverBase(IBinaryWriter * w): writer(w){};
  19. inline int write(const void * data, unsigned size)
  20. {
  21. return writer->write(data, size);
  22. };
  23. };
  24. /// Main class for serialization of classes into binary form
  25. /// Behaviour for various classes is following:
  26. /// Primitives: copy memory into underlying stream (defined in CSaverBase)
  27. /// Containers: custom overloaded method that decouples class into primitives
  28. /// VCMI Classes: recursively serialize them via ClassName::serialize( BinarySerializer &, int version) call
  29. class DLL_LINKAGE BinarySerializer : public CSaverBase
  30. {
  31. template <typename Handler>
  32. struct VariantVisitorSaver : boost::static_visitor<>
  33. {
  34. Handler &h;
  35. VariantVisitorSaver(Handler &H):h(H)
  36. {
  37. }
  38. template <typename T>
  39. void operator()(const T &t)
  40. {
  41. h & t;
  42. }
  43. };
  44. template<typename Ser,typename T>
  45. struct SaveIfStackInstance
  46. {
  47. static bool invoke(Ser &s, const T &data)
  48. {
  49. return false;
  50. }
  51. };
  52. template<typename Ser>
  53. struct SaveIfStackInstance<Ser, CStackInstance *>
  54. {
  55. static bool invoke(Ser &s, const CStackInstance* const &data)
  56. {
  57. assert(data->armyObj);
  58. SlotID slot;
  59. if(data->getNodeType() == CBonusSystemNode::COMMANDER)
  60. slot = SlotID::COMMANDER_SLOT_PLACEHOLDER;
  61. else
  62. slot = data->armyObj->findStack(data);
  63. assert(slot != SlotID());
  64. s & data->armyObj & slot;
  65. return true;
  66. }
  67. };
  68. template <typename T> class CPointerSaver;
  69. class CBasicPointerSaver
  70. {
  71. public:
  72. virtual void savePtr(CSaverBase &ar, const void *data) const =0;
  73. virtual ~CBasicPointerSaver(){}
  74. template<typename T> static CBasicPointerSaver *getApplier(const T * t=nullptr)
  75. {
  76. return new CPointerSaver<T>();
  77. }
  78. };
  79. template <typename T>
  80. class CPointerSaver : public CBasicPointerSaver
  81. {
  82. public:
  83. void savePtr(CSaverBase &ar, const void *data) const override
  84. {
  85. BinarySerializer &s = static_cast<BinarySerializer&>(ar);
  86. const T *ptr = static_cast<const T*>(data);
  87. //T is most derived known type, it's time to call actual serialize
  88. const_cast<T*>(ptr)->serialize(s, SERIALIZATION_VERSION);
  89. }
  90. };
  91. CApplier<CBasicPointerSaver> applier;
  92. public:
  93. std::map<const void*, ui32> savedPointers;
  94. bool smartPointerSerialization;
  95. bool saving;
  96. BinarySerializer(IBinaryWriter * w): CSaverBase(w)
  97. {
  98. saving=true;
  99. smartPointerSerialization = true;
  100. }
  101. template<typename Base, typename Derived>
  102. void registerType(const Base * b = nullptr, const Derived * d = nullptr)
  103. {
  104. applier.registerType(b, d);
  105. }
  106. template<class T>
  107. BinarySerializer & operator&(const T & t)
  108. {
  109. this->save(t);
  110. return * this;
  111. }
  112. template < typename T, typename std::enable_if < std::is_same<T, bool>::value, int >::type = 0 >
  113. void save(const T &data)
  114. {
  115. ui8 writ = static_cast<ui8>(data);
  116. save(writ);
  117. }
  118. template < typename T, typename std::enable_if < std::is_same<T, std::vector<bool> >::value, int >::type = 0 >
  119. void save(const T &data)
  120. {
  121. std::vector<ui8> convData;
  122. std::copy(data.begin(), data.end(), std::back_inserter(convData));
  123. save(convData);
  124. }
  125. template < class T, typename std::enable_if < std::is_fundamental<T>::value && !std::is_same<T, bool>::value, int >::type = 0 >
  126. void save(const T &data)
  127. {
  128. // save primitive - simply dump binary data to output
  129. this->write(&data,sizeof(data));
  130. }
  131. template < typename T, typename std::enable_if < std::is_enum<T>::value, int >::type = 0 >
  132. void save(const T &data)
  133. {
  134. si32 writ = static_cast<si32>(data);
  135. *this & writ;
  136. }
  137. template < typename T, typename std::enable_if < std::is_array<T>::value, int >::type = 0 >
  138. void save(const T &data)
  139. {
  140. ui32 size = ARRAY_COUNT(data);
  141. for(ui32 i=0; i < size; i++)
  142. *this & data[i];
  143. }
  144. template < typename T, typename std::enable_if < std::is_pointer<T>::value, int >::type = 0 >
  145. void save(const T &data)
  146. {
  147. //write if pointer is not nullptr
  148. ui8 hlp = (data!=nullptr);
  149. save(hlp);
  150. //if pointer is nullptr then we don't need anything more...
  151. if(!hlp)
  152. return;
  153. if(writer->smartVectorMembersSerialization)
  154. {
  155. typedef typename std::remove_const<typename std::remove_pointer<T>::type>::type TObjectType;
  156. typedef typename VectorizedTypeFor<TObjectType>::type VType;
  157. typedef typename VectorizedIDType<TObjectType>::type IDType;
  158. if(const auto *info = writer->getVectorizedTypeInfo<VType, IDType>())
  159. {
  160. IDType id = writer->getIdFromVectorItem<VType>(*info, data);
  161. save(id);
  162. if(id != IDType(-1)) //vector id is enough
  163. return;
  164. }
  165. }
  166. if(writer->sendStackInstanceByIds)
  167. {
  168. const bool gotSaved = SaveIfStackInstance<BinarySerializer,T>::invoke(*this, data);
  169. if(gotSaved)
  170. return;
  171. }
  172. if(smartPointerSerialization)
  173. {
  174. // We might have an object that has multiple inheritance and store it via the non-first base pointer.
  175. // Therefore, all pointers need to be normalized to the actual object address.
  176. auto actualPointer = typeList.castToMostDerived(data);
  177. std::map<const void*,ui32>::iterator i = savedPointers.find(actualPointer);
  178. if(i != savedPointers.end())
  179. {
  180. //this pointer has been already serialized - write only it's id
  181. save(i->second);
  182. return;
  183. }
  184. //give id to this pointer
  185. ui32 pid = (ui32)savedPointers.size();
  186. savedPointers[actualPointer] = pid;
  187. save(pid);
  188. }
  189. //write type identifier
  190. ui16 tid = typeList.getTypeID(data);
  191. save(tid);
  192. if(!tid)
  193. save(*data); //if type is unregistered simply write all data in a standard way
  194. else
  195. applier.getApplier(tid)->savePtr(*this, typeList.castToMostDerived(data)); //call serializer specific for our real type
  196. }
  197. template < typename T, typename std::enable_if < is_serializeable<BinarySerializer, T>::value, int >::type = 0 >
  198. void save(const T &data)
  199. {
  200. const_cast<T&>(data).serialize(*this, SERIALIZATION_VERSION);
  201. }
  202. template <typename T>
  203. void save(const std::shared_ptr<T> &data)
  204. {
  205. T *internalPtr = data.get();
  206. save(internalPtr);
  207. }
  208. template <typename T>
  209. void save(const std::unique_ptr<T> &data)
  210. {
  211. T *internalPtr = data.get();
  212. save(internalPtr);
  213. }
  214. template <typename T, typename std::enable_if < !std::is_same<T, bool >::value, int >::type = 0>
  215. void save(const std::vector<T> &data)
  216. {
  217. ui32 length = data.size();
  218. *this & length;
  219. for(ui32 i=0;i<length;i++)
  220. save(data[i]);
  221. }
  222. template <typename T, size_t N>
  223. void save(const std::array<T, N> &data)
  224. {
  225. for(ui32 i=0; i < N; i++)
  226. save(data[i]);
  227. }
  228. template <typename T>
  229. void save(const std::set<T> &data)
  230. {
  231. std::set<T> &d = const_cast<std::set<T> &>(data);
  232. ui32 length = d.size();
  233. save(length);
  234. for(typename std::set<T>::iterator i=d.begin();i!=d.end();i++)
  235. save(*i);
  236. }
  237. template <typename T, typename U>
  238. void save(const std::unordered_set<T, U> &data)
  239. {
  240. std::unordered_set<T, U> &d = const_cast<std::unordered_set<T, U> &>(data);
  241. ui32 length = d.size();
  242. *this & length;
  243. for(typename std::unordered_set<T, U>::iterator i=d.begin();i!=d.end();i++)
  244. save(*i);
  245. }
  246. template <typename T>
  247. void save(const std::list<T> &data)
  248. {
  249. std::list<T> &d = const_cast<std::list<T> &>(data);
  250. ui32 length = d.size();
  251. *this & length;
  252. for(typename std::list<T>::iterator i=d.begin();i!=d.end();i++)
  253. save(*i);
  254. }
  255. void save(const std::string &data)
  256. {
  257. save(ui32(data.length()));
  258. this->write(data.c_str(),data.size());
  259. }
  260. template <typename T1, typename T2>
  261. void save(const std::pair<T1,T2> &data)
  262. {
  263. save(data.first);
  264. save(data.second);
  265. }
  266. template <typename T1, typename T2>
  267. void save(const std::map<T1,T2> &data)
  268. {
  269. *this & ui32(data.size());
  270. for(typename std::map<T1,T2>::const_iterator i=data.begin();i!=data.end();i++)
  271. {
  272. save(i->first);
  273. save(i->second);
  274. }
  275. }
  276. template <typename T1, typename T2>
  277. void save(const std::multimap<T1, T2> &data)
  278. {
  279. *this & ui32(data.size());
  280. for(typename std::map<T1, T2>::const_iterator i = data.begin(); i != data.end(); i++)
  281. {
  282. save(i->first);
  283. save(i->second);
  284. }
  285. }
  286. template <BOOST_VARIANT_ENUM_PARAMS(typename T)>
  287. void save(const boost::variant<BOOST_VARIANT_ENUM_PARAMS(T)> &data)
  288. {
  289. si32 which = data.which();
  290. save(which);
  291. VariantVisitorSaver<BinarySerializer> visitor(*this);
  292. boost::apply_visitor(visitor, data);
  293. }
  294. template <typename T>
  295. void save(const boost::optional<T> &data)
  296. {
  297. if(data)
  298. {
  299. save((ui8)1);
  300. save(*data);
  301. }
  302. else
  303. {
  304. save((ui8)0);
  305. }
  306. }
  307. };
  308. class DLL_LINKAGE CSaveFile : public IBinaryWriter
  309. {
  310. public:
  311. BinarySerializer serializer;
  312. boost::filesystem::path fName;
  313. std::unique_ptr<FileStream> sfile;
  314. CSaveFile(const boost::filesystem::path &fname); //throws!
  315. ~CSaveFile();
  316. int write(const void * data, unsigned size) override;
  317. void openNextFile(const boost::filesystem::path &fname); //throws!
  318. void clear();
  319. void reportState(CLogger * out) override;
  320. void putMagicBytes(const std::string &text);
  321. template<class T>
  322. CSaveFile & operator<<(const T &t)
  323. {
  324. serializer & t;
  325. return * this;
  326. }
  327. };