2
0

ObjectStreamIn.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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 <Jolt/Core/UnorderedMap.h>
  8. JPH_SUPPRESS_WARNINGS_STD_BEGIN
  9. #include <fstream>
  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 IObjectStreamIn
  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. std::ifstream stream;
  49. stream.open(inFileName, std::ifstream::in | std::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. virtual bool ReadClassData(const char *inClassName, void *inInstance) override;
  71. bool ReadClassData(const ClassDescription &inClassDesc, void *inInstance);
  72. virtual bool ReadPointerData(const RTTI *inRTTI, void **inPointer, int inRefCountOffset = -1) override;
  73. bool SkipAttributeData(int inArrayDepth, EOSDataType inDataType, const char *inClassName);
  74. protected:
  75. /// Constructor
  76. explicit ObjectStreamIn(istream &inStream);
  77. /// Determine the type and version of an object stream
  78. static bool GetInfo(istream &inStream, EStreamType &outType, int &outVersion, int &outRevision);
  79. /// Static constructor
  80. static ObjectStreamIn * Open(istream &inStream);
  81. istream & mStream;
  82. private:
  83. /// Class descriptions
  84. struct AttributeDescription
  85. {
  86. int mArrayDepth = 0;
  87. EOSDataType mDataType = EOSDataType::Invalid;
  88. String mClassName;
  89. int mIndex = -1;
  90. };
  91. struct ClassDescription
  92. {
  93. ClassDescription() = default;
  94. explicit ClassDescription(const RTTI *inRTTI) : mRTTI(inRTTI) { }
  95. const RTTI * mRTTI = nullptr;
  96. Array<AttributeDescription> mAttributes;
  97. };
  98. struct ObjectInfo
  99. {
  100. ObjectInfo() = default;
  101. ObjectInfo(void *inInstance, const RTTI *inRTTI) : mInstance(inInstance), mRTTI(inRTTI) { }
  102. void * mInstance = nullptr;
  103. const RTTI * mRTTI = nullptr;
  104. };
  105. struct Link
  106. {
  107. void ** mPointer;
  108. int mRefCountOffset;
  109. Identifier mIdentifier;
  110. const RTTI * mRTTI;
  111. };
  112. using IdentifierMap = UnorderedMap<Identifier, ObjectInfo>;
  113. using ClassDescriptionMap = UnorderedMap<String, ClassDescription>;
  114. ClassDescriptionMap mClassDescriptionMap;
  115. IdentifierMap mIdentifierMap; ///< Links identifier to an object pointer
  116. Array<Link> mUnresolvedLinks; ///< All pointers (links) are resolved after reading the entire file, e.g. when all object exist
  117. };
  118. JPH_NAMESPACE_END