Jolt Physics
A multi core friendly Game Physics Engine
Loading...
Searching...
No Matches
ObjectStreamIn.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
9#include <Jolt/Core/RTTI.h>
11
13#include <fstream>
15
16#ifdef JPH_OBJECT_STREAM
17
19
23{
24private:
25 struct ClassDescription;
26
27public:
29 template <class T>
30 static bool sReadObject(istream &inStream, T *&outObject)
31 {
32 // Create the input stream
33 bool result = false;
34 ObjectStreamIn *stream = ObjectStreamIn::Open(inStream);
35 if (stream)
36 {
37 // Read the object
38 outObject = (T *)stream->Read(JPH_RTTI(T));
39 result = (outObject != nullptr);
40 delete stream;
41 }
42 return result;
43 }
44
46 template <class T>
47 static bool sReadObject(istream &inStream, Ref<T> &outObject)
48 {
49 T *object = nullptr;
50 bool result = sReadObject(inStream, object);
51 outObject = object;
52 return result;
53 }
54
56 template <class T>
57 static bool sReadObject(const char *inFileName, T *&outObject)
58 {
59 std::ifstream stream;
60 stream.open(inFileName, std::ifstream::in | std::ifstream::binary);
61 if (!stream.is_open())
62 return false;
63 return sReadObject(stream, outObject);
64 }
65
67 template <class T>
68 static bool sReadObject(const char *inFileName, Ref<T> &outObject)
69 {
70 T *object = nullptr;
71 bool result = sReadObject(inFileName, object);
72 outObject = object;
73 return result;
74 }
75
77 // EVERYTHING BELOW THIS SHOULD NOT DIRECTLY BE CALLED
79
81 void * Read(const RTTI *inRTTI);
82 void * ReadObject(const RTTI *& outRTTI);
83 bool ReadRTTI();
84 virtual bool ReadClassData(const char *inClassName, void *inInstance) override;
85 bool ReadClassData(const ClassDescription &inClassDesc, void *inInstance);
86 virtual bool ReadPointerData(const RTTI *inRTTI, void **inPointer, int inRefCountOffset = -1) override;
87 bool SkipAttributeData(int inArrayDepth, EOSDataType inDataType, const char *inClassName);
88
89protected:
91 explicit ObjectStreamIn(istream &inStream);
92
94 static bool GetInfo(istream &inStream, EStreamType &outType, int &outVersion, int &outRevision);
95
97 static ObjectStreamIn * Open(istream &inStream);
98
99 istream & mStream;
100
101private:
103 struct AttributeDescription
104 {
105 int mArrayDepth = 0;
106 EOSDataType mSourceType = EOSDataType::Invalid;
107 EOSDataType mDestinationType = EOSDataType::Invalid;
108 String mClassName;
109 int mIndex = -1;
110 };
111
112 struct ClassDescription
113 {
114 ClassDescription() = default;
115 explicit ClassDescription(const RTTI *inRTTI) : mRTTI(inRTTI) { }
116
117 const RTTI * mRTTI = nullptr;
118 Array<AttributeDescription> mAttributes;
119 };
120
121 struct ObjectInfo
122 {
123 ObjectInfo() = default;
124 ObjectInfo(void *inInstance, const RTTI *inRTTI) : mInstance(inInstance), mRTTI(inRTTI) { }
125
126 void * mInstance = nullptr;
127 const RTTI * mRTTI = nullptr;
128 };
129
130 struct Link
131 {
132 void ** mPointer;
133 int mRefCountOffset;
134 Identifier mIdentifier;
135 const RTTI * mRTTI;
136 };
137
138 using IdentifierMap = UnorderedMap<Identifier, ObjectInfo>;
139 using ClassDescriptionMap = UnorderedMap<String, ClassDescription>;
140
141 ClassDescriptionMap mClassDescriptionMap;
142 IdentifierMap mIdentifierMap;
143 Array<Link> mUnresolvedLinks;
144};
145
147
148#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
#define JPH_RTTI(class_name)
Definition: RTTI.h:319
std::basic_string< char, std::char_traits< char >, STLAllocator< char > > String
Definition: STLAllocator.h:107
EOSDataType
Data type.
Definition: SerializableAttribute.h:17
std::unordered_map< Key, T, Hash, KeyEqual, STLAllocator< pair< const Key, T > > > UnorderedMap
Definition: UnorderedMap.h:13
Definition: Array.h:36
Interface class for reading from an object stream.
Definition: ObjectStream.h:42
virtual bool ReadPointerData(const RTTI *inRTTI, void **inPointer, int inRefCountOffset=-1)=0
virtual bool ReadClassData(const char *inClassName, void *inInstance)=0
Definition: ObjectStreamIn.h:23
static ObjectStreamIn * Open(istream &inStream)
Static constructor.
Definition: ObjectStreamIn.cpp:53
void * Read(const RTTI *inRTTI)
Definition: ObjectStreamIn.cpp:80
static bool sReadObject(const char *inFileName, T *&outObject)
Main function to read an object from a file.
Definition: ObjectStreamIn.h:57
static bool sReadObject(istream &inStream, Ref< T > &outObject)
Main function to read an object from a stream (reference counting pointer version)
Definition: ObjectStreamIn.h:47
static bool sReadObject(istream &inStream, T *&outObject)
Main function to read an object from a stream.
Definition: ObjectStreamIn.h:30
static bool sReadObject(const char *inFileName, Ref< T > &outObject)
Main function to read an object from a file (reference counting pointer version)
Definition: ObjectStreamIn.h:68
istream & mStream
Definition: ObjectStreamIn.h:99
Definition: RTTI.h:122
Definition: Reference.h:107