2
0

ObjectStreamOut.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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/RTTI.h>
  7. #include <Jolt/Core/UnorderedMap.h>
  8. #include <Jolt/Core/UnorderedSet.h>
  9. JPH_SUPPRESS_WARNINGS_STD_BEGIN
  10. #include <queue>
  11. #include <fstream>
  12. JPH_SUPPRESS_WARNINGS_STD_END
  13. JPH_NAMESPACE_BEGIN
  14. template <class T> using Queue = std::queue<T, std::deque<T, STLAllocator<T>>>;
  15. /// ObjectStreamOut contains all logic for writing an object to disk. It is the base
  16. /// class for the text and binary output streams (ObjectStreamTextOut and ObjectStreamBinaryOut).
  17. class JPH_EXPORT ObjectStreamOut : public IObjectStreamOut
  18. {
  19. private:
  20. struct ObjectInfo;
  21. public:
  22. /// Main function to write an object to a stream
  23. template <class T>
  24. static bool sWriteObject(ostream &inStream, ObjectStream::EStreamType inType, const T &inObject)
  25. {
  26. // Create the output stream
  27. bool result = false;
  28. ObjectStreamOut *stream = ObjectStreamOut::Open(inType, inStream);
  29. if (stream)
  30. {
  31. // Write the object to the stream
  32. result = stream->Write((void *)&inObject, GetRTTI(&inObject));
  33. delete stream;
  34. }
  35. return result;
  36. }
  37. /// Main function to write an object to a file
  38. template <class T>
  39. static bool sWriteObject(const char *inFileName, ObjectStream::EStreamType inType, const T &inObject)
  40. {
  41. std::ofstream stream;
  42. stream.open(inFileName, std::ofstream::out | std::ofstream::trunc | std::ofstream::binary);
  43. if (!stream.is_open())
  44. return false;
  45. return sWriteObject(stream, inType, inObject);
  46. }
  47. //////////////////////////////////////////////////////
  48. // EVERYTHING BELOW THIS SHOULD NOT DIRECTLY BE CALLED
  49. //////////////////////////////////////////////////////
  50. ///@name Serialization operations
  51. bool Write(const void *inObject, const RTTI *inRTTI);
  52. void WriteObject(const void *inObject);
  53. void QueueRTTI(const RTTI *inRTTI);
  54. void WriteRTTI(const RTTI *inRTTI);
  55. virtual void WriteClassData(const RTTI *inRTTI, const void *inInstance) override;
  56. virtual void WritePointerData(const RTTI *inRTTI, const void *inPointer) override;
  57. protected:
  58. /// Static constructor
  59. static ObjectStreamOut * Open(EStreamType inType, ostream &inStream);
  60. /// Constructor
  61. explicit ObjectStreamOut(ostream &inStream);
  62. ostream & mStream;
  63. private:
  64. struct ObjectInfo
  65. {
  66. ObjectInfo() : mIdentifier(0), mRTTI(nullptr) { }
  67. ObjectInfo(Identifier inIdentifier, const RTTI *inRTTI) : mIdentifier(inIdentifier), mRTTI(inRTTI) { }
  68. Identifier mIdentifier;
  69. const RTTI * mRTTI;
  70. };
  71. using IdentifierMap = UnorderedMap<const void *, ObjectInfo>;
  72. using ClassSet = UnorderedSet<const RTTI *>;
  73. using ObjectQueue = Queue<const void *>;
  74. using ClassQueue = Queue<const RTTI *>;
  75. Identifier mNextIdentifier = sNullIdentifier + 1; ///< Next free identifier for this stream
  76. IdentifierMap mIdentifierMap; ///< Links object pointer to an identifier
  77. ObjectQueue mObjectQueue; ///< Queue of objects to be written
  78. ClassSet mClassSet; ///< List of classes already written
  79. ClassQueue mClassQueue; ///< List of classes waiting to be written
  80. };
  81. JPH_NAMESPACE_END