ObjectStreamIn.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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/ObjectStream/ObjectStream.h>
  6. #include <Jolt/Core/Reference.h>
  7. #include <Jolt/Core/RTTI.h>
  8. #include <Jolt/Core/UnorderedMap.h>
  9. JPH_SUPPRESS_WARNINGS_STD_BEGIN
  10. #include <fstream>
  11. JPH_SUPPRESS_WARNINGS_STD_END
  12. #ifdef JPH_OBJECT_STREAM
  13. JPH_NAMESPACE_BEGIN
  14. /// ObjectStreamIn contains all logic for reading an object from disk. It is the base
  15. /// class for the text and binary input streams (ObjectStreamTextIn and ObjectStreamBinaryIn).
  16. class JPH_EXPORT ObjectStreamIn : public IObjectStreamIn
  17. {
  18. private:
  19. struct ClassDescription;
  20. public:
  21. /// Main function to read an object from a stream
  22. template <class T>
  23. static bool sReadObject(istream &inStream, T *&outObject)
  24. {
  25. // Create the input stream
  26. bool result = false;
  27. ObjectStreamIn *stream = ObjectStreamIn::Open(inStream);
  28. if (stream)
  29. {
  30. // Read the object
  31. outObject = (T *)stream->Read(JPH_RTTI(T));
  32. result = (outObject != nullptr);
  33. delete stream;
  34. }
  35. return result;
  36. }
  37. /// Main function to read an object from a stream (reference counting pointer version)
  38. template <class T>
  39. static bool sReadObject(istream &inStream, Ref<T> &outObject)
  40. {
  41. T *object = nullptr;
  42. bool result = sReadObject(inStream, object);
  43. outObject = object;
  44. return result;
  45. }
  46. /// Main function to read an object from a file
  47. template <class T>
  48. static bool sReadObject(const char *inFileName, T *&outObject)
  49. {
  50. std::ifstream stream;
  51. stream.open(inFileName, std::ifstream::in | std::ifstream::binary);
  52. if (!stream.is_open())
  53. return false;
  54. return sReadObject(stream, outObject);
  55. }
  56. /// Main function to read an object from a file (reference counting pointer version)
  57. template <class T>
  58. static bool sReadObject(const char *inFileName, Ref<T> &outObject)
  59. {
  60. T *object = nullptr;
  61. bool result = sReadObject(inFileName, object);
  62. outObject = object;
  63. return result;
  64. }
  65. //////////////////////////////////////////////////////
  66. // EVERYTHING BELOW THIS SHOULD NOT DIRECTLY BE CALLED
  67. //////////////////////////////////////////////////////
  68. ///@name Serialization operations
  69. void * Read(const RTTI *inRTTI);
  70. void * ReadObject(const RTTI *& outRTTI);
  71. bool ReadRTTI();
  72. virtual bool ReadClassData(const char *inClassName, void *inInstance) override;
  73. bool ReadClassData(const ClassDescription &inClassDesc, void *inInstance);
  74. virtual bool ReadPointerData(const RTTI *inRTTI, void **inPointer, int inRefCountOffset = -1) override;
  75. bool SkipAttributeData(int inArrayDepth, EOSDataType inDataType, const char *inClassName);
  76. protected:
  77. /// Constructor
  78. explicit ObjectStreamIn(istream &inStream);
  79. /// Determine the type and version of an object stream
  80. static bool GetInfo(istream &inStream, EStreamType &outType, int &outVersion, int &outRevision);
  81. /// Static constructor
  82. static ObjectStreamIn * Open(istream &inStream);
  83. istream & mStream;
  84. private:
  85. /// Class descriptions
  86. struct AttributeDescription
  87. {
  88. int mArrayDepth = 0;
  89. EOSDataType mSourceType = EOSDataType::Invalid;
  90. EOSDataType mDestinationType = EOSDataType::Invalid;
  91. String mClassName;
  92. int mIndex = -1;
  93. };
  94. struct ClassDescription
  95. {
  96. ClassDescription() = default;
  97. explicit ClassDescription(const RTTI *inRTTI) : mRTTI(inRTTI) { }
  98. const RTTI * mRTTI = nullptr;
  99. Array<AttributeDescription> mAttributes;
  100. };
  101. struct ObjectInfo
  102. {
  103. ObjectInfo() = default;
  104. ObjectInfo(void *inInstance, const RTTI *inRTTI) : mInstance(inInstance), mRTTI(inRTTI) { }
  105. void * mInstance = nullptr;
  106. const RTTI * mRTTI = nullptr;
  107. };
  108. struct Link
  109. {
  110. void ** mPointer;
  111. int mRefCountOffset;
  112. Identifier mIdentifier;
  113. const RTTI * mRTTI;
  114. };
  115. using IdentifierMap = UnorderedMap<Identifier, ObjectInfo>;
  116. using ClassDescriptionMap = UnorderedMap<String, ClassDescription>;
  117. ClassDescriptionMap mClassDescriptionMap;
  118. IdentifierMap mIdentifierMap; ///< Links identifier to an object pointer
  119. Array<Link> mUnresolvedLinks; ///< All pointers (links) are resolved after reading the entire file, e.g. when all object exist
  120. };
  121. JPH_NAMESPACE_END
  122. #endif // JPH_OBJECT_STREAM