Jolt Physics
A multi core friendly Game Physics Engine
Loading...
Searching...
No Matches
ObjectStream.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>
12
14
17{
18public:
20 enum class EStreamType
21 {
22 Text,
23 Binary,
24 };
25
26protected:
28 virtual ~ObjectStream() = default;
29
32
33 static constexpr int sVersion = 1;
34 static constexpr int sRevision = 0;
35 static constexpr Identifier sNullIdentifier = 0;
36};
37
40{
41public:
43 virtual bool ReadDataType(EOSDataType &outType) = 0;
44 virtual bool ReadName(String &outName) = 0;
45 virtual bool ReadIdentifier(Identifier &outIdentifier) = 0;
46 virtual bool ReadCount(uint32 &outCount) = 0;
47
49 virtual bool ReadPrimitiveData(uint8 &outPrimitive) = 0;
50 virtual bool ReadPrimitiveData(uint16 &outPrimitive) = 0;
51 virtual bool ReadPrimitiveData(int &outPrimitive) = 0;
52 virtual bool ReadPrimitiveData(uint32 &outPrimitive) = 0;
53 virtual bool ReadPrimitiveData(uint64 &outPrimitive) = 0;
54 virtual bool ReadPrimitiveData(float &outPrimitive) = 0;
55 virtual bool ReadPrimitiveData(double &outPrimitive) = 0;
56 virtual bool ReadPrimitiveData(bool &outPrimitive) = 0;
57 virtual bool ReadPrimitiveData(String &outPrimitive) = 0;
58 virtual bool ReadPrimitiveData(Float3 &outPrimitive) = 0;
59 virtual bool ReadPrimitiveData(Double3 &outPrimitive) = 0;
60 virtual bool ReadPrimitiveData(Vec3 &outPrimitive) = 0;
61 virtual bool ReadPrimitiveData(DVec3 &outPrimitive) = 0;
62 virtual bool ReadPrimitiveData(Vec4 &outPrimitive) = 0;
63 virtual bool ReadPrimitiveData(Quat &outPrimitive) = 0;
64 virtual bool ReadPrimitiveData(Mat44 &outPrimitive) = 0;
65 virtual bool ReadPrimitiveData(DMat44 &outPrimitive) = 0;
66
68 virtual bool ReadClassData(const char *inClassName, void *inInstance) = 0;
69 virtual bool ReadPointerData(const RTTI *inRTTI, void **inPointer, int inRefCountOffset = -1) = 0;
70};
71
74{
75public:
77 virtual void WriteDataType(EOSDataType inType) = 0;
78 virtual void WriteName(const char *inName) = 0;
79 virtual void WriteIdentifier(Identifier inIdentifier) = 0;
80 virtual void WriteCount(uint32 inCount) = 0;
81
83 virtual void WritePrimitiveData(const uint8 &inPrimitive) = 0;
84 virtual void WritePrimitiveData(const uint16 &inPrimitive) = 0;
85 virtual void WritePrimitiveData(const int &inPrimitive) = 0;
86 virtual void WritePrimitiveData(const uint32 &inPrimitive) = 0;
87 virtual void WritePrimitiveData(const uint64 &inPrimitive) = 0;
88 virtual void WritePrimitiveData(const float &inPrimitive) = 0;
89 virtual void WritePrimitiveData(const double &inPrimitive) = 0;
90 virtual void WritePrimitiveData(const bool &inPrimitive) = 0;
91 virtual void WritePrimitiveData(const String &inPrimitive) = 0;
92 virtual void WritePrimitiveData(const Float3 &inPrimitive) = 0;
93 virtual void WritePrimitiveData(const Double3 &inPrimitive) = 0;
94 virtual void WritePrimitiveData(const Vec3 &inPrimitive) = 0;
95 virtual void WritePrimitiveData(const DVec3 &inPrimitive) = 0;
96 virtual void WritePrimitiveData(const Vec4 &inPrimitive) = 0;
97 virtual void WritePrimitiveData(const Quat &inPrimitive) = 0;
98 virtual void WritePrimitiveData(const Mat44 &inPrimitive) = 0;
99 virtual void WritePrimitiveData(const DMat44 &inPrimitive) = 0;
100
102 virtual void WritePointerData(const RTTI *inRTTI, const void *inPointer) = 0;
103 virtual void WriteClassData(const RTTI *inRTTI, const void *inInstance) = 0;
104
106 virtual void HintNextItem() { /* Default is do nothing */ }
107 virtual void HintIndentUp() { /* Default is do nothing */ }
108 virtual void HintIndentDown() { /* Default is do nothing */ }
109};
110
111// Define macro to declare functions for a specific primitive type
112#define JPH_DECLARE_PRIMITIVE(name) \
113 JPH_EXPORT bool OSIsType(name *, int inArrayDepth, EOSDataType inDataType, const char *inClassName); \
114 JPH_EXPORT bool OSReadData(IObjectStreamIn &ioStream, name &outPrimitive); \
115 JPH_EXPORT void OSWriteDataType(IObjectStreamOut &ioStream, name *); \
116 JPH_EXPORT void OSWriteData(IObjectStreamOut &ioStream, const name &inPrimitive);
117
118// This file uses the JPH_DECLARE_PRIMITIVE macro to define all types
120
121// Define serialization templates
122template <class T>
123bool OSIsType(Array<T> *, int inArrayDepth, EOSDataType inDataType, const char *inClassName)
124{
125 return (inArrayDepth > 0 && OSIsType(static_cast<T *>(nullptr), inArrayDepth - 1, inDataType, inClassName));
126}
127
128template <class T, uint N>
129bool OSIsType(StaticArray<T, N> *, int inArrayDepth, EOSDataType inDataType, const char *inClassName)
130{
131 return (inArrayDepth > 0 && OSIsType(static_cast<T *>(nullptr), inArrayDepth - 1, inDataType, inClassName));
132}
133
134template <class T, uint N>
135bool OSIsType(T (*)[N], int inArrayDepth, EOSDataType inDataType, const char *inClassName)
136{
137 return (inArrayDepth > 0 && OSIsType(static_cast<T *>(nullptr), inArrayDepth - 1, inDataType, inClassName));
138}
139
140template <class T>
141bool OSIsType(Ref<T> *, int inArrayDepth, EOSDataType inDataType, const char *inClassName)
142{
143 return OSIsType(static_cast<T *>(nullptr), inArrayDepth, inDataType, inClassName);
144}
145
146template <class T>
147bool OSIsType(RefConst<T> *, int inArrayDepth, EOSDataType inDataType, const char *inClassName)
148{
149 return OSIsType(static_cast<T *>(nullptr), inArrayDepth, inDataType, inClassName);
150}
151
153template <class T>
154bool OSReadData(IObjectStreamIn &ioStream, Array<T> &inArray)
155{
156 bool continue_reading = true;
157
158 // Read array length
159 uint32 array_length;
160 continue_reading = ioStream.ReadCount(array_length);
161
162 // Read array items
163 if (continue_reading)
164 {
165 inArray.clear();
166 inArray.resize(array_length);
167 for (uint32 el = 0; el < array_length && continue_reading; ++el)
168 continue_reading = OSReadData(ioStream, inArray[el]);
169 }
170
171 return continue_reading;
172}
173
175template <class T, uint N>
177{
178 bool continue_reading = true;
179
180 // Read array length
181 uint32 array_length;
182 continue_reading = ioStream.ReadCount(array_length);
183
184 // Check if we can fit this many elements
185 if (array_length > N)
186 return false;
187
188 // Read array items
189 if (continue_reading)
190 {
191 inArray.clear();
192 inArray.resize(array_length);
193 for (uint32 el = 0; el < array_length && continue_reading; ++el)
194 continue_reading = OSReadData(ioStream, inArray[el]);
195 }
196
197 return continue_reading;
198}
199
201template <class T, uint N>
202bool OSReadData(IObjectStreamIn &ioStream, T (&inArray)[N])
203{
204 bool continue_reading = true;
205
206 // Read array length
207 uint32 array_length;
208 continue_reading = ioStream.ReadCount(array_length);
209 if (array_length != N)
210 return false;
211
212 // Read array items
213 for (uint32 el = 0; el < N && continue_reading; ++el)
214 continue_reading = OSReadData(ioStream, inArray[el]);
215
216 return continue_reading;
217}
218
220template <class T>
221bool OSReadData(IObjectStreamIn &ioStream, Ref<T> &inRef)
222{
223 return ioStream.ReadPointerData(JPH_RTTI(T), inRef.InternalGetPointer(), T::sInternalGetRefCountOffset());
224}
225
226template <class T>
228{
229 return ioStream.ReadPointerData(JPH_RTTI(T), inRef.InternalGetPointer(), T::sInternalGetRefCountOffset());
230}
231
232// Define serialization templates for dynamic arrays
233template <class T>
235{
237 OSWriteDataType(ioStream, static_cast<T *>(nullptr));
238}
239
240template <class T>
241void OSWriteData(IObjectStreamOut &ioStream, const Array<T> &inArray)
242{
243 // Write size of array
244 ioStream.HintNextItem();
245 ioStream.WriteCount(static_cast<uint32>(inArray.size()));
246
247 // Write data in array
248 ioStream.HintIndentUp();
249 for (const T &v : inArray)
250 OSWriteData(ioStream, v);
251 ioStream.HintIndentDown();
252}
253
255template <class T, uint N>
257{
259 OSWriteDataType(ioStream, static_cast<T *>(nullptr));
260}
261
262template <class T, uint N>
263void OSWriteData(IObjectStreamOut &ioStream, const StaticArray<T, N> &inArray)
264{
265 // Write size of array
266 ioStream.HintNextItem();
267 ioStream.WriteCount(inArray.size());
268
269 // Write data in array
270 ioStream.HintIndentUp();
271 for (const typename StaticArray<T, N>::value_type &v : inArray)
272 OSWriteData(ioStream, v);
273 ioStream.HintIndentDown();
274}
275
277template <class T, uint N>
278void OSWriteDataType(IObjectStreamOut &ioStream, T (*)[N])
279{
281 OSWriteDataType(ioStream, static_cast<T *>(nullptr));
282}
283
284template <class T, uint N>
285void OSWriteData(IObjectStreamOut &ioStream, const T (&inArray)[N])
286{
287 // Write size of array
288 ioStream.HintNextItem();
289 ioStream.WriteCount(uint32(N));
290
291 // Write data in array
292 ioStream.HintIndentUp();
293 for (const T &v : inArray)
294 OSWriteData(ioStream, v);
295 ioStream.HintIndentDown();
296}
297
299template <class T>
301{
302 OSWriteDataType(ioStream, static_cast<T *>(nullptr));
303}
304
305template <class T>
306void OSWriteData(IObjectStreamOut &ioStream, const Ref<T> &inRef)
307{
308 if (inRef != nullptr)
309 ioStream.WritePointerData(GetRTTI(inRef.GetPtr()), inRef.GetPtr());
310 else
311 ioStream.WritePointerData(nullptr, nullptr);
312}
313
314template <class T>
316{
317 OSWriteDataType(ioStream, static_cast<T *>(nullptr));
318}
319
320template <class T>
321void OSWriteData(IObjectStreamOut &ioStream, const RefConst<T> &inRef)
322{
323 if (inRef != nullptr)
324 ioStream.WritePointerData(GetRTTI(inRef.GetPtr()), inRef.GetPtr());
325 else
326 ioStream.WritePointerData(nullptr, nullptr);
327}
328
std::uint8_t uint8
Definition: Core.h:440
#define JPH_EXPORT
Definition: Core.h:227
std::uint64_t uint64
Definition: Core.h:443
#define JPH_NAMESPACE_END
Definition: Core.h:367
std::uint32_t uint32
Definition: Core.h:442
#define JPH_NAMESPACE_BEGIN
Definition: Core.h:361
std::uint16_t uint16
Definition: Core.h:441
bool OSIsType(Array< T > *, int inArrayDepth, EOSDataType inDataType, const char *inClassName)
Definition: ObjectStream.h:123
void OSWriteData(IObjectStreamOut &ioStream, const Array< T > &inArray)
Definition: ObjectStream.h:241
bool OSReadData(IObjectStreamIn &ioStream, Array< T > &inArray)
Define serialization templates for dynamic arrays.
Definition: ObjectStream.h:154
void OSWriteDataType(IObjectStreamOut &ioStream, Array< T > *)
Definition: ObjectStream.h:234
#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
@ Array
Used in attribute declaration, indicates that this is an array of objects.
Holds a 4x4 matrix of floats with the last column consisting of doubles.
Definition: DMat44.h:13
Definition: DVec3.h:14
Class that holds 3 doubles. Used as a storage class. Convert to DVec3 for calculations.
Definition: Double3.h:13
Class that holds 3 floats. Used as a storage class. Convert to Vec3 for calculations.
Definition: Float3.h:13
Interface class for reading from an object stream.
Definition: ObjectStream.h:40
virtual bool ReadPrimitiveData(Quat &outPrimitive)=0
virtual bool ReadDataType(EOSDataType &outType)=0
virtual bool ReadPrimitiveData(double &outPrimitive)=0
virtual bool ReadPointerData(const RTTI *inRTTI, void **inPointer, int inRefCountOffset=-1)=0
virtual bool ReadPrimitiveData(Vec3 &outPrimitive)=0
virtual bool ReadPrimitiveData(String &outPrimitive)=0
virtual bool ReadPrimitiveData(uint64 &outPrimitive)=0
virtual bool ReadIdentifier(Identifier &outIdentifier)=0
virtual bool ReadPrimitiveData(Float3 &outPrimitive)=0
virtual bool ReadPrimitiveData(uint32 &outPrimitive)=0
virtual bool ReadPrimitiveData(float &outPrimitive)=0
virtual bool ReadPrimitiveData(DMat44 &outPrimitive)=0
virtual bool ReadPrimitiveData(int &outPrimitive)=0
virtual bool ReadName(String &outName)=0
virtual bool ReadPrimitiveData(Mat44 &outPrimitive)=0
virtual bool ReadPrimitiveData(DVec3 &outPrimitive)=0
virtual bool ReadCount(uint32 &outCount)=0
virtual bool ReadPrimitiveData(uint16 &outPrimitive)=0
virtual bool ReadClassData(const char *inClassName, void *inInstance)=0
virtual bool ReadPrimitiveData(Double3 &outPrimitive)=0
virtual bool ReadPrimitiveData(uint8 &outPrimitive)=0
virtual bool ReadPrimitiveData(Vec4 &outPrimitive)=0
virtual bool ReadPrimitiveData(bool &outPrimitive)=0
Interface class for writing to an object stream.
Definition: ObjectStream.h:74
virtual void WritePrimitiveData(const int &inPrimitive)=0
virtual void WritePrimitiveData(const float &inPrimitive)=0
virtual void WritePrimitiveData(const Vec3 &inPrimitive)=0
virtual void WritePointerData(const RTTI *inRTTI, const void *inPointer)=0
virtual void WritePrimitiveData(const DVec3 &inPrimitive)=0
virtual void WritePrimitiveData(const String &inPrimitive)=0
virtual void WritePrimitiveData(const Quat &inPrimitive)=0
virtual void WritePrimitiveData(const uint8 &inPrimitive)=0
virtual void WritePrimitiveData(const Float3 &inPrimitive)=0
virtual void WritePrimitiveData(const Mat44 &inPrimitive)=0
virtual void WritePrimitiveData(const DMat44 &inPrimitive)=0
virtual void WritePrimitiveData(const uint32 &inPrimitive)=0
virtual void WriteClassData(const RTTI *inRTTI, const void *inInstance)=0
virtual void WriteIdentifier(Identifier inIdentifier)=0
virtual void WritePrimitiveData(const Vec4 &inPrimitive)=0
virtual void WriteDataType(EOSDataType inType)=0
virtual void WriteCount(uint32 inCount)=0
virtual void WriteName(const char *inName)=0
virtual void HintNextItem()
Definition: ObjectStream.h:106
virtual void WritePrimitiveData(const uint16 &inPrimitive)=0
virtual void HintIndentUp()
Definition: ObjectStream.h:107
virtual void WritePrimitiveData(const bool &inPrimitive)=0
virtual void WritePrimitiveData(const double &inPrimitive)=0
virtual void HintIndentDown()
Definition: ObjectStream.h:108
virtual void WritePrimitiveData(const uint64 &inPrimitive)=0
virtual void WritePrimitiveData(const Double3 &inPrimitive)=0
Holds a 4x4 matrix of floats, but supports also operations on the 3x3 upper left part of the matrix.
Definition: Mat44.h:13
Class that makes another class non-copyable. Usage: Inherit from NonCopyable.
Definition: NonCopyable.h:11
Base class for object stream input and output streams.
Definition: ObjectStream.h:17
uint32 Identifier
Identifier for objects.
Definition: ObjectStream.h:31
virtual ~ObjectStream()=default
Destructor.
EStreamType
Stream type.
Definition: ObjectStream.h:21
Definition: Quat.h:33
Definition: RTTI.h:122
Definition: Reference.h:151
const T * GetPtr() const
Get pointer.
Definition: Reference.h:185
void ** InternalGetPointer()
INTERNAL HELPER FUNCTION USED BY SERIALIZATION.
Definition: Reference.h:188
Definition: Reference.h:101
T * GetPtr() const
Get pointer.
Definition: Reference.h:129
void ** InternalGetPointer()
INTERNAL HELPER FUNCTION USED BY SERIALIZATION.
Definition: Reference.h:132
Simple variable length array backed by a fixed size buffer.
Definition: StaticArray.h:12
void clear()
Destruct all elements and set length to zero.
Definition: StaticArray.h:50
T value_type
Definition: StaticArray.h:14
size_type size() const
Returns amount of elements in the array.
Definition: StaticArray.h:87
void resize(size_type inNewSize)
Resize array to new length.
Definition: StaticArray.h:99
Definition: Vec3.h:16
Definition: Vec4.h:14