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