BsRTTIPrerequisites.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  1. #pragma once
  2. namespace BansheeEngine
  3. {
  4. /**
  5. * @brief Helper method when serializing known data types that have valid
  6. * RTTIPlainType specialization.
  7. *
  8. * Returns the size of the element. If elements serializable type is
  9. * specialized with hasDynamicSize == true, the dynamic size is calculated,
  10. * otherwise sizeof() is used.
  11. */
  12. template<class ElemType>
  13. UINT32 rttiGetElemSize(const ElemType& data)
  14. {
  15. if(RTTIPlainType<ElemType>::hasDynamicSize == 1)
  16. return RTTIPlainType<ElemType>::getDynamicSize(data);
  17. else
  18. return sizeof(ElemType);
  19. }
  20. /**
  21. * @brief Helper method when serializing known data types that have valid
  22. * RTTIPlainType specialization.
  23. *
  24. * Writes the specified data into memory, advances the memory pointer by the
  25. * bytes written and returns pointer to new memory.
  26. */
  27. template<class ElemType>
  28. char* rttiWriteElem(const ElemType& data, char* memory)
  29. {
  30. RTTIPlainType<ElemType>::toMemory(data, memory);
  31. return memory + rttiGetElemSize(data);
  32. }
  33. /**
  34. * @brief Helper method when serializing known data types that have valid
  35. * RTTIPlainType specialization.
  36. *
  37. * Writes the specified data into memory, advances the memory pointer by the
  38. * bytes written and returns pointer to new memory. Also increases the size
  39. * value by the size of the written element.
  40. */
  41. template<class ElemType>
  42. char* rttiWriteElem(const ElemType& data, char* memory, UINT32& size)
  43. {
  44. RTTIPlainType<ElemType>::toMemory(data, memory);
  45. UINT32 elemSize = rttiGetElemSize(data);
  46. size += elemSize;
  47. return memory + elemSize;
  48. }
  49. /**
  50. * @brief Helper method when serializing known data types that have valid
  51. * RTTIPlainType specialization.
  52. *
  53. * Reads the specified data into memory, advances the memory pointer by the
  54. * bytes read and returns pointer to new memory.
  55. */
  56. template<class ElemType>
  57. char* rttiReadElem(ElemType& data, char* memory)
  58. {
  59. RTTIPlainType<ElemType>::fromMemory(data, memory);
  60. return memory + rttiGetElemSize(data);
  61. }
  62. /**
  63. * @brief Helper method when serializing known data types that have valid
  64. * RTTIPlainType specialization.
  65. *
  66. * Reads the specified data into memory, advances the memory pointer by the
  67. * bytes read and returns pointer to new memory. Also increases the size
  68. * value by the size of the read element.
  69. */
  70. template<class ElemType>
  71. char* rttiReadElem(ElemType& data, char* memory, UINT32& size)
  72. {
  73. RTTIPlainType<ElemType>::fromMemory(data, memory);
  74. UINT32 elemSize = rttiGetElemSize(data);
  75. size += elemSize;
  76. return memory + elemSize;
  77. }
  78. /**
  79. * @brief Template that you may specialize with a class if you want to provide
  80. * simple serialization for it.
  81. *
  82. * Any type that uses the "plain" field in the RTTI system must specialize this class.
  83. *
  84. * @note Normally you will want to implement IReflectable interface if you want to provide serialization
  85. * as that interface properly handles versioning, nested objects, pointer handling and more.
  86. *
  87. * This class is useful for types you can easily serialize using a memcpy (built-in types like int/float/etc), or
  88. * types you cannot modify so they implement IReflectable interface (like std::string or std::vector).
  89. *
  90. * @see RTTIType
  91. * @see RTTIField
  92. */
  93. template<class T>
  94. struct RTTIPlainType
  95. {
  96. static_assert(std::is_pod<T>::value,
  97. "Provided type isn't plain-old-data. You need to specialize RTTIPlainType template in order to serialize this type. "\
  98. " (Or call BS_ALLOW_MEMCPY_SERIALIZATION(type) macro if you are sure the type can be properly serialized using just memcpy.)");
  99. enum { id = 0 /**< Unique id for the serializable type. */ };
  100. enum { hasDynamicSize = 0 /**< 0 (Object has static size less than 255 bytes, e.g. int) or 1 (Dynamic size with no size restriction, e.g. string) */ };
  101. /**
  102. * @brief Serializes the provided object into the provided pre-allocated
  103. * memory buffer.
  104. */
  105. static void toMemory(const T& data, char* memory)
  106. {
  107. memcpy(memory, &data, sizeof(T));
  108. }
  109. /**
  110. * @brief Deserializes a previously allocated object from the provided
  111. * memory buffer. Return the number of bytes read from the memory buffer.
  112. */
  113. static UINT32 fromMemory(T& data, char* memory)
  114. {
  115. memcpy(&data, memory, sizeof(T));
  116. return sizeof(T);
  117. }
  118. /**
  119. * @brief Returns the size of the provided object. (Works for both
  120. * static and dynamic size types)
  121. */
  122. static UINT32 getDynamicSize(const T& data)
  123. {
  124. return sizeof(T);
  125. }
  126. };
  127. /**
  128. * @brief Tell the RTTI system that the specified type may be serialized just by
  129. * using a memcpy.
  130. *
  131. * @note Internally this creates a basic RTTIPlainType specialization for the type.
  132. *
  133. * @see RTTIPlainType
  134. */
  135. #define BS_ALLOW_MEMCPY_SERIALIZATION(type) \
  136. template<> struct RTTIPlainType<##type##> \
  137. { enum { id=0 }; enum { hasDynamicSize = 0 }; \
  138. static void toMemory(const type& data, char* memory) \
  139. { memcpy(memory, &data, sizeof(##type##)); } \
  140. static UINT32 fromMemory(##type##& data, char* memory) \
  141. { memcpy(&data, memory, sizeof(##type##)); return sizeof(##type##); } \
  142. static UINT32 getDynamicSize(const type& data) \
  143. { return sizeof(##type##); } \
  144. };
  145. /**
  146. * @brief RTTIPlainType for std::vector.
  147. *
  148. * @see RTTIPlainType
  149. */
  150. template<class T> struct RTTIPlainType<std::vector<T, StdAlloc<T>>>
  151. {
  152. enum { id = TID_Vector }; enum { hasDynamicSize = 1 };
  153. /**
  154. * @copydoc RTTIPlainType::toMemory
  155. */
  156. static void toMemory(const std::vector<T, StdAlloc<T>>& data, char* memory)
  157. {
  158. UINT32 size = sizeof(UINT32);
  159. char* memoryStart = memory;
  160. memory += sizeof(UINT32);
  161. UINT32 numElements = (UINT32)data.size();
  162. memcpy(memory, &numElements, sizeof(UINT32));
  163. memory += sizeof(UINT32);
  164. size += sizeof(UINT32);
  165. for(auto iter = data.begin(); iter != data.end(); ++iter)
  166. {
  167. UINT32 elementSize = RTTIPlainType<T>::getDynamicSize(*iter);
  168. RTTIPlainType<T>::toMemory(*iter, memory);
  169. memory += elementSize;
  170. size += elementSize;
  171. }
  172. memcpy(memoryStart, &size, sizeof(UINT32));
  173. }
  174. /**
  175. * @copydoc RTTIPlainType::toMemory
  176. */
  177. static UINT32 fromMemory(std::vector<T, StdAlloc<T>>& data, char* memory)
  178. {
  179. UINT32 size = 0;
  180. memcpy(&size, memory, sizeof(UINT32));
  181. memory += sizeof(UINT32);
  182. UINT32 numElements;
  183. memcpy(&numElements, memory, sizeof(UINT32));
  184. memory += sizeof(UINT32);
  185. for(UINT32 i = 0; i < numElements; i++)
  186. {
  187. T element;
  188. UINT32 elementSize = RTTIPlainType<T>::fromMemory(element, memory);
  189. data.push_back(element);
  190. memory += elementSize;
  191. }
  192. return size;
  193. }
  194. /**
  195. * @copydoc RTTIPlainType::toMemory
  196. */
  197. static UINT32 getDynamicSize(const std::vector<T, StdAlloc<T>>& data)
  198. {
  199. UINT64 dataSize = sizeof(UINT32) * 2;
  200. for(auto iter = data.begin(); iter != data.end(); ++iter)
  201. dataSize += RTTIPlainType<T>::getDynamicSize(*iter);
  202. assert(dataSize <= std::numeric_limits<UINT32>::max());
  203. return (UINT32)dataSize;
  204. }
  205. };
  206. /**
  207. * @brief RTTIPlainType for std::map.
  208. *
  209. * @see RTTIPlainType
  210. */
  211. template<class Key, class Value> struct RTTIPlainType<std::map<Key, Value, std::less<Key>, StdAlloc<std::pair<const Key, Value>>>>
  212. {
  213. enum { id = TID_Map }; enum { hasDynamicSize = 1 };
  214. /**
  215. * @copydoc RTTIPlainType::toMemory
  216. */
  217. static void toMemory(const std::map<Key, Value, std::less<Key>, StdAlloc<std::pair<const Key, Value>>>& data, char* memory)
  218. {
  219. UINT32 size = sizeof(UINT32);
  220. char* memoryStart = memory;
  221. memory += sizeof(UINT32);
  222. UINT32 numElements = (UINT32)data.size();
  223. memcpy(memory, &numElements, sizeof(UINT32));
  224. memory += sizeof(UINT32);
  225. size += sizeof(UINT32);
  226. for(auto iter = data.begin(); iter != data.end(); ++iter)
  227. {
  228. UINT32 keySize = RTTIPlainType<Key>::getDynamicSize(iter->first);
  229. RTTIPlainType<Key>::toMemory(iter->first, memory);
  230. memory += keySize;
  231. size += keySize;
  232. UINT32 valueSize = RTTIPlainType<Value>::getDynamicSize(iter->second);
  233. RTTIPlainType<Value>::toMemory(iter->second, memory);
  234. memory += valueSize;
  235. size += valueSize;
  236. }
  237. memcpy(memoryStart, &size, sizeof(UINT32));
  238. }
  239. /**
  240. * @copydoc RTTIPlainType::fromMemory
  241. */
  242. static UINT32 fromMemory(std::map<Key, Value, std::less<Key>, StdAlloc<std::pair<const Key, Value>>>& data, char* memory)
  243. {
  244. UINT32 size = 0;
  245. memcpy(&size, memory, sizeof(UINT32));
  246. memory += sizeof(UINT32);
  247. UINT32 numElements;
  248. memcpy(&numElements, memory, sizeof(UINT32));
  249. memory += sizeof(UINT32);
  250. for(UINT32 i = 0; i < numElements; i++)
  251. {
  252. Key key;
  253. UINT32 keySize = RTTIPlainType<Key>::fromMemory(key, memory);
  254. memory += keySize;
  255. Value value;
  256. UINT32 valueSize = RTTIPlainType<Value>::fromMemory(value, memory);
  257. memory += valueSize;
  258. data[key] = value;
  259. }
  260. return size;
  261. }
  262. /**
  263. * @copydoc RTTIPlainType::getDynamicSize
  264. */
  265. static UINT32 getDynamicSize(const std::map<Key, Value, std::less<Key>, StdAlloc<std::pair<const Key, Value>>>& data)
  266. {
  267. UINT64 dataSize = sizeof(UINT32) * 2;
  268. for(auto iter = data.begin(); iter != data.end(); ++iter)
  269. {
  270. dataSize += RTTIPlainType<Key>::getDynamicSize(iter->first);
  271. dataSize += RTTIPlainType<Value>::getDynamicSize(iter->second);
  272. }
  273. assert(dataSize <= std::numeric_limits<UINT32>::max());
  274. return (UINT32)dataSize;
  275. }
  276. };
  277. /**
  278. * @brief RTTIPlainType for std::unordered_map.
  279. *
  280. * @see RTTIPlainType
  281. */
  282. template<class Key, class Value>
  283. struct RTTIPlainType<std::unordered_map<Key, Value, std::hash<Key>, std::equal_to<Key>, StdAlloc<std::pair<const Key, Value>>>>
  284. {
  285. enum { id = TID_UnorderedMap }; enum { hasDynamicSize = 1 };
  286. typedef std::unordered_map<Key, Value, std::hash<Key>, std::equal_to<Key>, StdAlloc<std::pair<const Key, Value>>> MapType;
  287. /**
  288. * @copydoc RTTIPlainType::toMemory
  289. */
  290. static void toMemory(MapType& data, char* memory)
  291. {
  292. UINT32 size = sizeof(UINT32);
  293. char* memoryStart = memory;
  294. memory += sizeof(UINT32);
  295. UINT32 numElements = (UINT32)data.size();
  296. memcpy(memory, &numElements, sizeof(UINT32));
  297. memory += sizeof(UINT32);
  298. size += sizeof(UINT32);
  299. for (auto iter = data.begin(); iter != data.end(); ++iter)
  300. {
  301. UINT32 keySize = RTTIPlainType<Key>::getDynamicSize(iter->first);
  302. RTTIPlainType<Key>::toMemory(iter->first, memory);
  303. memory += keySize;
  304. size += keySize;
  305. UINT32 valueSize = RTTIPlainType<Value>::getDynamicSize(iter->second);
  306. RTTIPlainType<Value>::toMemory(iter->second, memory);
  307. memory += valueSize;
  308. size += valueSize;
  309. }
  310. memcpy(memoryStart, &size, sizeof(UINT32));
  311. }
  312. /**
  313. * @copydoc RTTIPlainType::fromMemory
  314. */
  315. static UINT32 fromMemory(MapType& data, char* memory)
  316. {
  317. UINT32 size = 0;
  318. memcpy(&size, memory, sizeof(UINT32));
  319. memory += sizeof(UINT32);
  320. UINT32 numElements;
  321. memcpy(&numElements, memory, sizeof(UINT32));
  322. memory += sizeof(UINT32);
  323. for (UINT32 i = 0; i < numElements; i++)
  324. {
  325. Key key;
  326. UINT32 keySize = RTTIPlainType<Key>::fromMemory(key, memory);
  327. memory += keySize;
  328. Value value;
  329. UINT32 valueSize = RTTIPlainType<Value>::fromMemory(value, memory);
  330. memory += valueSize;
  331. data[key] = value;
  332. }
  333. return size;
  334. }
  335. /**
  336. * @copydoc RTTIPlainType::getDynamicSize
  337. */
  338. static UINT32 getDynamicSize(const MapType& data)
  339. {
  340. UINT64 dataSize = sizeof(UINT32)* 2;
  341. for (auto iter = data.begin(); iter != data.end(); ++iter)
  342. {
  343. dataSize += RTTIPlainType<Key>::getDynamicSize(iter->first);
  344. dataSize += RTTIPlainType<Value>::getDynamicSize(iter->second);
  345. }
  346. assert(dataSize <= std::numeric_limits<UINT32>::max());
  347. return (UINT32)dataSize;
  348. }
  349. };
  350. /**
  351. * @brief RTTIPlainType for std::pair.
  352. *
  353. * @see RTTIPlainType
  354. */
  355. template<class A, class B> struct RTTIPlainType<std::pair<A, B>>
  356. {
  357. enum { id = TID_Pair }; enum { hasDynamicSize = 1 };
  358. /**
  359. * @copydoc RTTIPlainType::toMemory
  360. */
  361. static void toMemory(const std::pair<A, B>& data, char* memory)
  362. {
  363. UINT32 size = sizeof(UINT32);
  364. char* memoryStart = memory;
  365. memory += sizeof(UINT32);
  366. UINT32 firstSize = RTTIPlainType<A>::getDynamicSize(data.first);
  367. RTTIPlainType<A>::toMemory(data.first, memory);
  368. memory += firstSize;
  369. size += firstSize;
  370. UINT32 secondSize = RTTIPlainType<B>::getDynamicSize(data.second);
  371. RTTIPlainType<B>::toMemory(data.second, memory);
  372. memory += secondSize;
  373. size += secondSize;
  374. memcpy(memoryStart, &size, sizeof(UINT32));
  375. }
  376. /**
  377. * @copydoc RTTIPlainType::fromMemory
  378. */
  379. static UINT32 fromMemory(std::pair<A, B>& data, char* memory)
  380. {
  381. UINT32 size = 0;
  382. memcpy(&size, memory, sizeof(UINT32));
  383. memory += sizeof(UINT32);
  384. UINT32 firstSize = RTTIPlainType<A>::fromMemory(data.first, memory);
  385. memory += firstSize;
  386. UINT32 secondSize = RTTIPlainType<B>::fromMemory(data.second, memory);
  387. memory += secondSize;
  388. return size;
  389. }
  390. /**
  391. * @copydoc RTTIPlainType::getDynamicSize
  392. */
  393. static UINT32 getDynamicSize(const std::pair<A, B>& data)
  394. {
  395. UINT64 dataSize = sizeof(UINT32);
  396. dataSize += RTTIPlainType<A>::getDynamicSize(data.first);
  397. dataSize += RTTIPlainType<B>::getDynamicSize(data.second);
  398. assert(dataSize <= std::numeric_limits<UINT32>::max());
  399. return (UINT32)dataSize;
  400. }
  401. };
  402. }