ObjectStreamIn.h 7.3 KB

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