Jolt Physics
A multi core friendly Game Physics Engine
Loading...
Searching...
No Matches
ObjectStreamOut.h
Go to the documentation of this file.
1// Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
2// SPDX-FileCopyrightText: 2021 Jorrit Rouwe
3// SPDX-License-Identifier: MIT
4
5#pragma once
6
8#include <Jolt/Core/RTTI.h>
11
13#include <fstream>
15
16#ifdef JPH_OBJECT_STREAM
17
19
23{
24private:
25 struct ObjectInfo;
26
27public:
29 template <class T>
30 static bool sWriteObject(ostream &inStream, ObjectStream::EStreamType inType, const T &inObject)
31 {
32 // Create the output stream
33 bool result = false;
34 ObjectStreamOut *stream = ObjectStreamOut::Open(inType, inStream);
35 if (stream)
36 {
37 // Write the object to the stream
38 result = stream->Write((void *)&inObject, GetRTTI(&inObject));
39 delete stream;
40 }
41
42 return result;
43 }
44
46 template <class T>
47 static bool sWriteObject(const char *inFileName, ObjectStream::EStreamType inType, const T &inObject)
48 {
49 std::ofstream stream;
50 stream.open(inFileName, std::ofstream::out | std::ofstream::trunc | std::ofstream::binary);
51 if (!stream.is_open())
52 return false;
53 return sWriteObject(stream, inType, inObject);
54 }
55
57 // EVERYTHING BELOW THIS SHOULD NOT DIRECTLY BE CALLED
59
61 bool Write(const void *inObject, const RTTI *inRTTI);
62 void WriteObject(const void *inObject);
63 void QueueRTTI(const RTTI *inRTTI);
64 void WriteRTTI(const RTTI *inRTTI);
65 virtual void WriteClassData(const RTTI *inRTTI, const void *inInstance) override;
66 virtual void WritePointerData(const RTTI *inRTTI, const void *inPointer) override;
67
68protected:
70 static ObjectStreamOut * Open(EStreamType inType, ostream &inStream);
71
73 explicit ObjectStreamOut(ostream &inStream);
74
75 ostream & mStream;
76
77private:
78 struct ObjectInfo
79 {
80 ObjectInfo() : mIdentifier(0), mRTTI(nullptr) { }
81 ObjectInfo(Identifier inIdentifier, const RTTI *inRTTI) : mIdentifier(inIdentifier), mRTTI(inRTTI) { }
82
83 Identifier mIdentifier;
84 const RTTI * mRTTI;
85 };
86
87 using IdentifierMap = UnorderedMap<const void *, ObjectInfo>;
88 using ClassSet = UnorderedSet<const RTTI *>;
89 using ObjectQueue = Array<const void *>;
90 using ClassQueue = Array<const RTTI *>;
91
92 Identifier mNextIdentifier = sNullIdentifier + 1;
93 IdentifierMap mIdentifierMap;
94 ObjectQueue mObjectQueue;
95 ClassSet mClassSet;
96 ClassQueue mClassQueue;
97};
98
100
101#endif // JPH_OBJECT_STREAM
#define JPH_EXPORT
Definition Core.h:271
#define JPH_SUPPRESS_WARNINGS_STD_BEGIN
Definition Core.h:419
#define JPH_SUPPRESS_WARNINGS_STD_END
Definition Core.h:431
#define JPH_NAMESPACE_END
Definition Core.h:414
#define JPH_NAMESPACE_BEGIN
Definition Core.h:408
Definition Array.h:36
Interface class for writing to an object stream.
Definition ObjectStream.h:76
virtual void WritePointerData(const RTTI *inRTTI, const void *inPointer)=0
virtual void WriteClassData(const RTTI *inRTTI, const void *inInstance)=0
uint32 Identifier
Identifier for objects.
Definition ObjectStream.h:33
EStreamType
Stream type.
Definition ObjectStream.h:23
Definition ObjectStreamOut.h:23
bool Write(const void *inObject, const RTTI *inRTTI)
Definition ObjectStreamOut.cpp:35
static bool sWriteObject(const char *inFileName, ObjectStream::EStreamType inType, const T &inObject)
Main function to write an object to a file.
Definition ObjectStreamOut.h:47
static bool sWriteObject(ostream &inStream, ObjectStream::EStreamType inType, const T &inObject)
Main function to write an object to a stream.
Definition ObjectStreamOut.h:30
static ObjectStreamOut * Open(EStreamType inType, ostream &inStream)
Static constructor.
Definition ObjectStreamOut.cpp:24
ostream & mStream
Definition ObjectStreamOut.h:75
Definition RTTI.h:122
Definition UnorderedMap.h:30
Definition UnorderedSet.h:29