ObjectStream.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. // Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
  2. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  3. // SPDX-License-Identifier: MIT
  4. #pragma once
  5. #include <Jolt/Core/StaticArray.h>
  6. #include <Jolt/Core/Reference.h>
  7. #include <Jolt/ObjectStream/SerializableAttribute.h>
  8. #include <Jolt/Core/RTTI.h>
  9. JPH_NAMESPACE_BEGIN
  10. /// Base class for object stream input and output streams.
  11. class JPH_EXPORT ObjectStream
  12. {
  13. public:
  14. /// Stream type
  15. enum class EStreamType
  16. {
  17. Text,
  18. Binary,
  19. };
  20. protected:
  21. /// Constructor
  22. virtual ~ObjectStream() = default;
  23. /// Identifier for objects
  24. using Identifier = uint32;
  25. static constexpr int sVersion = 1;
  26. static constexpr int sRevision = 0;
  27. static constexpr Identifier sNullIdentifier = 0;
  28. };
  29. /// Interface class for reading from an object stream
  30. class JPH_EXPORT IObjectStreamIn : public ObjectStream
  31. {
  32. public:
  33. ///@name Input type specific operations
  34. virtual bool ReadDataType(EOSDataType &outType) = 0;
  35. virtual bool ReadName(String &outName) = 0;
  36. virtual bool ReadIdentifier(Identifier &outIdentifier) = 0;
  37. virtual bool ReadCount(uint32 &outCount) = 0;
  38. ///@name Read primitives
  39. virtual bool ReadPrimitiveData(uint8 &outPrimitive) = 0;
  40. virtual bool ReadPrimitiveData(uint16 &outPrimitive) = 0;
  41. virtual bool ReadPrimitiveData(int &outPrimitive) = 0;
  42. virtual bool ReadPrimitiveData(uint32 &outPrimitive) = 0;
  43. virtual bool ReadPrimitiveData(uint64 &outPrimitive) = 0;
  44. virtual bool ReadPrimitiveData(float &outPrimitive) = 0;
  45. virtual bool ReadPrimitiveData(double &outPrimitive) = 0;
  46. virtual bool ReadPrimitiveData(bool &outPrimitive) = 0;
  47. virtual bool ReadPrimitiveData(String &outPrimitive) = 0;
  48. virtual bool ReadPrimitiveData(Float3 &outPrimitive) = 0;
  49. virtual bool ReadPrimitiveData(Double3 &outPrimitive) = 0;
  50. virtual bool ReadPrimitiveData(Vec3 &outPrimitive) = 0;
  51. virtual bool ReadPrimitiveData(DVec3 &outPrimitive) = 0;
  52. virtual bool ReadPrimitiveData(Vec4 &outPrimitive) = 0;
  53. virtual bool ReadPrimitiveData(Quat &outPrimitive) = 0;
  54. virtual bool ReadPrimitiveData(Mat44 &outPrimitive) = 0;
  55. virtual bool ReadPrimitiveData(DMat44 &outPrimitive) = 0;
  56. ///@name Read compounds
  57. virtual bool ReadClassData(const char *inClassName, void *inInstance) = 0;
  58. virtual bool ReadPointerData(const RTTI *inRTTI, void **inPointer, int inRefCountOffset = -1) = 0;
  59. };
  60. /// Interface class for writing to an object stream
  61. class JPH_EXPORT IObjectStreamOut : public ObjectStream
  62. {
  63. public:
  64. ///@name Output type specific operations
  65. virtual void WriteDataType(EOSDataType inType) = 0;
  66. virtual void WriteName(const char *inName) = 0;
  67. virtual void WriteIdentifier(Identifier inIdentifier) = 0;
  68. virtual void WriteCount(uint32 inCount) = 0;
  69. ///@name Write primitives
  70. virtual void WritePrimitiveData(const uint8 &inPrimitive) = 0;
  71. virtual void WritePrimitiveData(const uint16 &inPrimitive) = 0;
  72. virtual void WritePrimitiveData(const int &inPrimitive) = 0;
  73. virtual void WritePrimitiveData(const uint32 &inPrimitive) = 0;
  74. virtual void WritePrimitiveData(const uint64 &inPrimitive) = 0;
  75. virtual void WritePrimitiveData(const float &inPrimitive) = 0;
  76. virtual void WritePrimitiveData(const double &inPrimitive) = 0;
  77. virtual void WritePrimitiveData(const bool &inPrimitive) = 0;
  78. virtual void WritePrimitiveData(const String &inPrimitive) = 0;
  79. virtual void WritePrimitiveData(const Float3 &inPrimitive) = 0;
  80. virtual void WritePrimitiveData(const Double3 &inPrimitive) = 0;
  81. virtual void WritePrimitiveData(const Vec3 &inPrimitive) = 0;
  82. virtual void WritePrimitiveData(const DVec3 &inPrimitive) = 0;
  83. virtual void WritePrimitiveData(const Vec4 &inPrimitive) = 0;
  84. virtual void WritePrimitiveData(const Quat &inPrimitive) = 0;
  85. virtual void WritePrimitiveData(const Mat44 &inPrimitive) = 0;
  86. virtual void WritePrimitiveData(const DMat44 &inPrimitive) = 0;
  87. ///@name Write compounds
  88. virtual void WritePointerData(const RTTI *inRTTI, const void *inPointer) = 0;
  89. virtual void WriteClassData(const RTTI *inRTTI, const void *inInstance) = 0;
  90. ///@name Layout hints (for text output)
  91. virtual void HintNextItem() { /* Default is do nothing */ }
  92. virtual void HintIndentUp() { /* Default is do nothing */ }
  93. virtual void HintIndentDown() { /* Default is do nothing */ }
  94. };
  95. // Define macro to declare functions for a specific primitive type
  96. #define JPH_DECLARE_PRIMITIVE(name) \
  97. JPH_EXPORT bool OSIsType(name *, int inArrayDepth, EOSDataType inDataType, const char *inClassName); \
  98. JPH_EXPORT bool OSReadData(IObjectStreamIn &ioStream, name &outPrimitive); \
  99. JPH_EXPORT void OSWriteDataType(IObjectStreamOut &ioStream, name *); \
  100. JPH_EXPORT void OSWriteData(IObjectStreamOut &ioStream, const name &inPrimitive);
  101. // This file uses the JPH_DECLARE_PRIMITIVE macro to define all types
  102. #include <Jolt/ObjectStream/ObjectStreamTypes.h>
  103. // Define serialization templates
  104. template <class T>
  105. bool OSIsType(Array<T> *, int inArrayDepth, EOSDataType inDataType, const char *inClassName)
  106. {
  107. return (inArrayDepth > 0 && OSIsType((T *)nullptr, inArrayDepth - 1, inDataType, inClassName));
  108. }
  109. template <class T, uint N>
  110. bool OSIsType(StaticArray<T, N> *, int inArrayDepth, EOSDataType inDataType, const char *inClassName)
  111. {
  112. return (inArrayDepth > 0 && OSIsType((T *)nullptr, inArrayDepth - 1, inDataType, inClassName));
  113. }
  114. template <class T, uint N>
  115. bool OSIsType(T (*)[N], int inArrayDepth, EOSDataType inDataType, const char *inClassName)
  116. {
  117. return (inArrayDepth > 0 && OSIsType((T *)nullptr, inArrayDepth - 1, inDataType, inClassName));
  118. }
  119. template <class T>
  120. bool OSIsType(Ref<T> *, int inArrayDepth, EOSDataType inDataType, const char *inClassName)
  121. {
  122. return OSIsType((T *)nullptr, inArrayDepth, inDataType, inClassName);
  123. }
  124. template <class T>
  125. bool OSIsType(RefConst<T> *, int inArrayDepth, EOSDataType inDataType, const char *inClassName)
  126. {
  127. return OSIsType((T *)nullptr, inArrayDepth, inDataType, inClassName);
  128. }
  129. /// Define serialization templates for dynamic arrays
  130. template <class T>
  131. bool OSReadData(IObjectStreamIn &ioStream, Array<T> &inArray)
  132. {
  133. bool continue_reading = true;
  134. // Read array length
  135. uint32 array_length;
  136. continue_reading = ioStream.ReadCount(array_length);
  137. // Read array items
  138. if (continue_reading)
  139. {
  140. inArray.clear();
  141. inArray.resize(array_length);
  142. for (uint32 el = 0; el < array_length && continue_reading; ++el)
  143. continue_reading = OSReadData(ioStream, inArray[el]);
  144. }
  145. return continue_reading;
  146. }
  147. /// Define serialization templates for static arrays
  148. template <class T, uint N>
  149. bool OSReadData(IObjectStreamIn &ioStream, StaticArray<T, N> &inArray)
  150. {
  151. bool continue_reading = true;
  152. // Read array length
  153. uint32 array_length;
  154. continue_reading = ioStream.ReadCount(array_length);
  155. // Check if we can fit this many elements
  156. if (array_length > N)
  157. return false;
  158. // Read array items
  159. if (continue_reading)
  160. {
  161. inArray.clear();
  162. inArray.resize(array_length);
  163. for (uint32 el = 0; el < array_length && continue_reading; ++el)
  164. continue_reading = OSReadData(ioStream, inArray[el]);
  165. }
  166. return continue_reading;
  167. }
  168. /// Define serialization templates for C style arrays
  169. template <class T, uint N>
  170. bool OSReadData(IObjectStreamIn &ioStream, T (&inArray)[N])
  171. {
  172. bool continue_reading = true;
  173. // Read array length
  174. uint32 array_length;
  175. continue_reading = ioStream.ReadCount(array_length);
  176. if (array_length != N)
  177. return false;
  178. // Read array items
  179. for (uint32 el = 0; el < N && continue_reading; ++el)
  180. continue_reading = OSReadData(ioStream, inArray[el]);
  181. return continue_reading;
  182. }
  183. /// Define serialization templates for references
  184. template <class T>
  185. bool OSReadData(IObjectStreamIn &ioStream, Ref<T> &inRef)
  186. {
  187. return ioStream.ReadPointerData(JPH_RTTI(T), inRef.InternalGetPointer(), T::sInternalGetRefCountOffset());
  188. }
  189. template <class T>
  190. bool OSReadData(IObjectStreamIn &ioStream, RefConst<T> &inRef)
  191. {
  192. return ioStream.ReadPointerData(JPH_RTTI(T), inRef.InternalGetPointer(), T::sInternalGetRefCountOffset());
  193. }
  194. // Define serialization templates for dynamic arrays
  195. template <class T>
  196. void OSWriteDataType(IObjectStreamOut &ioStream, Array<T> *)
  197. {
  198. ioStream.WriteDataType(EOSDataType::Array);
  199. OSWriteDataType(ioStream, (T *)nullptr);
  200. }
  201. template <class T>
  202. void OSWriteData(IObjectStreamOut &ioStream, const Array<T> &inArray)
  203. {
  204. // Write size of array
  205. ioStream.HintNextItem();
  206. ioStream.WriteCount((uint32)inArray.size());
  207. // Write data in array
  208. ioStream.HintIndentUp();
  209. for (const T &v : inArray)
  210. OSWriteData(ioStream, v);
  211. ioStream.HintIndentDown();
  212. }
  213. /// Define serialization templates for static arrays
  214. template <class T, uint N>
  215. void OSWriteDataType(IObjectStreamOut &ioStream, StaticArray<T, N> *)
  216. {
  217. ioStream.WriteDataType(EOSDataType::Array);
  218. OSWriteDataType(ioStream, (T *)nullptr);
  219. }
  220. template <class T, uint N>
  221. void OSWriteData(IObjectStreamOut &ioStream, const StaticArray<T, N> &inArray)
  222. {
  223. // Write size of array
  224. ioStream.HintNextItem();
  225. ioStream.WriteCount(inArray.size());
  226. // Write data in array
  227. ioStream.HintIndentUp();
  228. for (const typename StaticArray<T, N>::value_type &v : inArray)
  229. OSWriteData(ioStream, v);
  230. ioStream.HintIndentDown();
  231. }
  232. /// Define serialization templates for C style arrays
  233. template <class T, uint N>
  234. void OSWriteDataType(IObjectStreamOut &ioStream, T (*)[N])
  235. {
  236. ioStream.WriteDataType(EOSDataType::Array);
  237. OSWriteDataType(ioStream, (T *)nullptr);
  238. }
  239. template <class T, uint N>
  240. void OSWriteData(IObjectStreamOut &ioStream, const T (&inArray)[N])
  241. {
  242. // Write size of array
  243. ioStream.HintNextItem();
  244. ioStream.WriteCount((uint32)N);
  245. // Write data in array
  246. ioStream.HintIndentUp();
  247. for (const T &v : inArray)
  248. OSWriteData(ioStream, v);
  249. ioStream.HintIndentDown();
  250. }
  251. /// Define serialization templates for references
  252. template <class T>
  253. void OSWriteDataType(IObjectStreamOut &ioStream, Ref<T> *)
  254. {
  255. OSWriteDataType(ioStream, (T *)nullptr);
  256. }
  257. template <class T>
  258. void OSWriteData(IObjectStreamOut &ioStream, const Ref<T> &inRef)
  259. {
  260. if (inRef != nullptr)
  261. ioStream.WritePointerData(GetRTTI(inRef.GetPtr()), inRef.GetPtr());
  262. else
  263. ioStream.WritePointerData(nullptr, nullptr);
  264. }
  265. template <class T>
  266. void OSWriteDataType(IObjectStreamOut &ioStream, RefConst<T> *)
  267. {
  268. OSWriteDataType(ioStream, (T *)nullptr);
  269. }
  270. template <class T>
  271. void OSWriteData(IObjectStreamOut &ioStream, const RefConst<T> &inRef)
  272. {
  273. if (inRef != nullptr)
  274. ioStream.WritePointerData(GetRTTI(inRef.GetPtr()), inRef.GetPtr());
  275. else
  276. ioStream.WritePointerData(nullptr, nullptr);
  277. }
  278. JPH_NAMESPACE_END