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 <queue>
14#include <fstream>
16
17#ifdef JPH_OBJECT_STREAM
18
20
21template <class T> using Queue = std::queue<T, std::deque<T, STLAllocator<T>>>;
22
26{
27private:
28 struct ObjectInfo;
29
30public:
32 template <class T>
33 static bool sWriteObject(ostream &inStream, ObjectStream::EStreamType inType, const T &inObject)
34 {
35 // Create the output stream
36 bool result = false;
37 ObjectStreamOut *stream = ObjectStreamOut::Open(inType, inStream);
38 if (stream)
39 {
40 // Write the object to the stream
41 result = stream->Write((void *)&inObject, GetRTTI(&inObject));
42 delete stream;
43 }
44
45 return result;
46 }
47
49 template <class T>
50 static bool sWriteObject(const char *inFileName, ObjectStream::EStreamType inType, const T &inObject)
51 {
52 std::ofstream stream;
53 stream.open(inFileName, std::ofstream::out | std::ofstream::trunc | std::ofstream::binary);
54 if (!stream.is_open())
55 return false;
56 return sWriteObject(stream, inType, inObject);
57 }
58
60 // EVERYTHING BELOW THIS SHOULD NOT DIRECTLY BE CALLED
62
64 bool Write(const void *inObject, const RTTI *inRTTI);
65 void WriteObject(const void *inObject);
66 void QueueRTTI(const RTTI *inRTTI);
67 void WriteRTTI(const RTTI *inRTTI);
68 virtual void WriteClassData(const RTTI *inRTTI, const void *inInstance) override;
69 virtual void WritePointerData(const RTTI *inRTTI, const void *inPointer) override;
70
71protected:
73 static ObjectStreamOut * Open(EStreamType inType, ostream &inStream);
74
76 explicit ObjectStreamOut(ostream &inStream);
77
78 ostream & mStream;
79
80private:
81 struct ObjectInfo
82 {
83 ObjectInfo() : mIdentifier(0), mRTTI(nullptr) { }
84 ObjectInfo(Identifier inIdentifier, const RTTI *inRTTI) : mIdentifier(inIdentifier), mRTTI(inRTTI) { }
85
86 Identifier mIdentifier;
87 const RTTI * mRTTI;
88 };
89
90 using IdentifierMap = UnorderedMap<const void *, ObjectInfo>;
91 using ClassSet = UnorderedSet<const RTTI *>;
92 using ObjectQueue = Queue<const void *>;
93 using ClassQueue = Queue<const RTTI *>;
94
95 Identifier mNextIdentifier = sNullIdentifier + 1;
96 IdentifierMap mIdentifierMap;
97 ObjectQueue mObjectQueue;
98 ClassSet mClassSet;
99 ClassQueue mClassQueue;
100};
101
103
104#endif // JPH_OBJECT_STREAM
#define JPH_EXPORT
Definition: Core.h:236
#define JPH_SUPPRESS_WARNINGS_STD_BEGIN
Definition: Core.h:383
#define JPH_SUPPRESS_WARNINGS_STD_END
Definition: Core.h:395
#define JPH_NAMESPACE_END
Definition: Core.h:378
#define JPH_NAMESPACE_BEGIN
Definition: Core.h:372
std::queue< T, std::deque< T, STLAllocator< T > > > Queue
Definition: ObjectStreamOut.h:21
std::unordered_map< Key, T, Hash, KeyEqual, STLAllocator< pair< const Key, T > > > UnorderedMap
Definition: UnorderedMap.h:13
std::unordered_set< Key, Hash, KeyEqual, STLAllocator< Key > > UnorderedSet
Definition: UnorderedSet.h:13
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:26
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:50
static bool sWriteObject(ostream &inStream, ObjectStream::EStreamType inType, const T &inObject)
Main function to write an object to a stream.
Definition: ObjectStreamOut.h:33
static ObjectStreamOut * Open(EStreamType inType, ostream &inStream)
Static constructor.
Definition: ObjectStreamOut.cpp:24
ostream & mStream
Definition: ObjectStreamOut.h:78
Definition: RTTI.h:122