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