ObjectStreamIn.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  2. // SPDX-License-Identifier: MIT
  3. #pragma once
  4. #include <Jolt/ObjectStream/ObjectStream.h>
  5. #include <Jolt/Core/Reference.h>
  6. #include <Jolt/Core/RTTI.h>
  7. JPH_SUPPRESS_WARNINGS_STD_BEGIN
  8. #include <fstream>
  9. #include <unordered_map>
  10. JPH_SUPPRESS_WARNINGS_STD_END
  11. JPH_NAMESPACE_BEGIN
  12. /// ObjectStreamIn contains all logic for reading an object from disk. It is the base
  13. /// class for the text and binary input streams (ObjectStreamTextIn and ObjectStreamBinaryIn).
  14. class ObjectStreamIn : public ObjectStream
  15. {
  16. private:
  17. struct ClassDescription;
  18. public:
  19. /// Main function to read an object from a stream
  20. template <class T>
  21. static bool sReadObject(istream &inStream, T *&outObject)
  22. {
  23. // Create the input stream
  24. bool result = false;
  25. ObjectStreamIn *stream = ObjectStreamIn::Open(inStream);
  26. if (stream)
  27. {
  28. // Read the object
  29. outObject = (T *)stream->Read(JPH_RTTI(T));
  30. result = (outObject != nullptr);
  31. delete stream;
  32. }
  33. return result;
  34. }
  35. /// Main function to read an object from a stream (reference counting pointer version)
  36. template <class T>
  37. static bool sReadObject(istream &inStream, Ref<T> &outObject)
  38. {
  39. T *object = nullptr;
  40. bool result = sReadObject(inStream, object);
  41. outObject = object;
  42. return result;
  43. }
  44. /// Main function to read an object from a file
  45. template <class T>
  46. static bool sReadObject(const char *inFileName, T *&outObject)
  47. {
  48. ifstream stream;
  49. stream.open(inFileName, ifstream::in | ifstream::binary);
  50. if (!stream.is_open())
  51. return false;
  52. return sReadObject(stream, outObject);
  53. }
  54. /// Main function to read an object from a file (reference counting pointer version)
  55. template <class T>
  56. static bool sReadObject(const char *inFileName, Ref<T> &outObject)
  57. {
  58. T *object = nullptr;
  59. bool result = sReadObject(inFileName, object);
  60. outObject = object;
  61. return result;
  62. }
  63. //////////////////////////////////////////////////////
  64. // EVERYTHING BELOW THIS SHOULD NOT DIRECTLY BE CALLED
  65. //////////////////////////////////////////////////////
  66. ///@name Serialization operations
  67. void * Read(const RTTI *inRTTI);
  68. void * ReadObject(const RTTI *& outRTTI);
  69. bool ReadRTTI();
  70. bool ReadClassData(const char *inClassName, void *inInstance);
  71. bool ReadClassData(const ClassDescription &inClassDesc, void *inInstance);
  72. bool ReadPointerData(const RTTI *inRTTI, void **inPointer, int inRefCountOffset = -1);
  73. bool SkipAttributeData(int inArrayDepth, EDataType inDataType, const char *inClassName);
  74. ///@name Input type specific operations
  75. virtual bool ReadDataType(EDataType &outType) = 0;
  76. virtual bool ReadName(string &outName) = 0;
  77. virtual bool ReadIdentifier(Identifier &outIdentifier) = 0;
  78. virtual bool ReadCount(uint32 &outCount) = 0;
  79. virtual bool ReadPrimitiveData(uint8 &outPrimitive) = 0;
  80. virtual bool ReadPrimitiveData(uint16 &outPrimitive) = 0;
  81. virtual bool ReadPrimitiveData(int &outPrimitive) = 0;
  82. virtual bool ReadPrimitiveData(uint32 &outPrimitive) = 0;
  83. virtual bool ReadPrimitiveData(uint64 &outPrimitive) = 0;
  84. virtual bool ReadPrimitiveData(float &outPrimitive) = 0;
  85. virtual bool ReadPrimitiveData(bool &outPrimitive) = 0;
  86. virtual bool ReadPrimitiveData(string &outPrimitive) = 0;
  87. virtual bool ReadPrimitiveData(Float3 &outPrimitive) = 0;
  88. virtual bool ReadPrimitiveData(Vec3 &outPrimitive) = 0;
  89. virtual bool ReadPrimitiveData(Vec4 &outPrimitive) = 0;
  90. virtual bool ReadPrimitiveData(Quat &outPrimitive) = 0;
  91. virtual bool ReadPrimitiveData(Mat44 &outPrimitive) = 0;
  92. protected:
  93. /// Constructor
  94. explicit ObjectStreamIn(istream &inStream);
  95. /// Determine the type and version of an object stream
  96. static bool GetInfo(istream &inStream, EStreamType &outType, int &outVersion, int &outRevision);
  97. /// Static constructor
  98. static ObjectStreamIn * Open(istream &inStream);
  99. istream & mStream;
  100. private:
  101. /// Class descriptions
  102. struct AttributeDescription
  103. {
  104. int mArrayDepth = 0;
  105. EDataType mDataType = EDataType::Invalid;
  106. string mClassName;
  107. int mIndex = -1;
  108. };
  109. struct ClassDescription
  110. {
  111. ClassDescription() = default;
  112. explicit ClassDescription(const RTTI *inRTTI) : mRTTI(inRTTI) { }
  113. const RTTI * mRTTI = nullptr;
  114. vector<AttributeDescription> mAttributes;
  115. };
  116. struct ObjectInfo
  117. {
  118. ObjectInfo() = default;
  119. ObjectInfo(void *inInstance, const RTTI *inRTTI) : mInstance(inInstance), mRTTI(inRTTI) { }
  120. void * mInstance = nullptr;
  121. const RTTI * mRTTI = nullptr;
  122. };
  123. struct Link
  124. {
  125. void ** mPointer;
  126. int mRefCountOffset;
  127. Identifier mIdentifier;
  128. const RTTI * mRTTI;
  129. };
  130. using IdentifierMap = unordered_map<Identifier, ObjectInfo>;
  131. using ClassDescriptionMap = unordered_map<string, ClassDescription>;
  132. ClassDescriptionMap mClassDescriptionMap;
  133. IdentifierMap mIdentifierMap; ///< Links identifier to an object pointer
  134. vector<Link> mUnresolvedLinks; ///< All pointers (links) are resolved after reading the entire file, e.g. when all object exist
  135. };
  136. // Define macro to declare functions for a specific primitive type
  137. #define JPH_DECLARE_PRIMITIVE(name) \
  138. bool OSReadData(ObjectStreamIn &ioStream, name &outPrimitive);
  139. // This file uses the JPH_DECLARE_PRIMITIVE macro to define all types
  140. #include <Jolt/ObjectStream/ObjectStreamTypes.h>
  141. /// Define serialization templates for dynamic arrays
  142. template <class T>
  143. bool OSReadData(ObjectStreamIn &ioStream, vector<T> &inArray)
  144. {
  145. bool continue_reading = true;
  146. // Read array length
  147. uint32 array_length;
  148. continue_reading = ioStream.ReadCount(array_length);
  149. // Read array items
  150. if (continue_reading)
  151. {
  152. inArray.resize(array_length);
  153. for (uint32 el = 0; el < array_length && continue_reading; ++el)
  154. continue_reading = OSReadData(ioStream, inArray[el]);
  155. }
  156. return continue_reading;
  157. }
  158. /// Define serialization templates for static arrays
  159. template <class T, uint N>
  160. bool OSReadData(ObjectStreamIn &ioStream, StaticArray<T, N> &inArray)
  161. {
  162. bool continue_reading = true;
  163. // Read array length
  164. uint32 array_length;
  165. continue_reading = ioStream.ReadCount(array_length);
  166. // Check if we can fit this many elements
  167. if (array_length > N)
  168. return false;
  169. // Read array items
  170. if (continue_reading)
  171. {
  172. inArray.resize(array_length);
  173. for (uint32 el = 0; el < array_length && continue_reading; ++el)
  174. continue_reading = OSReadData(ioStream, inArray[el]);
  175. }
  176. return continue_reading;
  177. }
  178. /// Define serialization templates for C style arrays
  179. template <class T, uint N>
  180. bool OSReadData(ObjectStreamIn &ioStream, T (&inArray)[N])
  181. {
  182. bool continue_reading = true;
  183. // Read array length
  184. uint32 array_length;
  185. continue_reading = ioStream.ReadCount(array_length);
  186. if (array_length != N)
  187. return false;
  188. // Read array items
  189. for (uint32 el = 0; el < N && continue_reading; ++el)
  190. continue_reading = OSReadData(ioStream, inArray[el]);
  191. return continue_reading;
  192. }
  193. /// Define serialization templates for references
  194. template <class T>
  195. bool OSReadData(ObjectStreamIn &ioStream, Ref<T> &inRef)
  196. {
  197. return ioStream.ReadPointerData(JPH_RTTI(T), inRef.InternalGetPointer(), T::sInternalGetRefCountOffset());
  198. }
  199. template <class T>
  200. bool OSReadData(ObjectStreamIn &ioStream, RefConst<T> &inRef)
  201. {
  202. return ioStream.ReadPointerData(JPH_RTTI(T), inRef.InternalGetPointer(), T::sInternalGetRefCountOffset());
  203. }
  204. JPH_NAMESPACE_END