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
13#ifdef JPH_OBJECT_STREAM
14
16
19{
20public:
22 enum class EStreamType
23 {
24 Text,
25 Binary,
26 };
27
28protected:
30 virtual ~ObjectStream() = default;
31
34
35 static constexpr int sVersion = 1;
36 static constexpr int sRevision = 0;
37 static constexpr Identifier sNullIdentifier = 0;
38};
39
42{
43public:
45 virtual bool ReadDataType(EOSDataType &outType) = 0;
46 virtual bool ReadName(String &outName) = 0;
47 virtual bool ReadIdentifier(Identifier &outIdentifier) = 0;
48 virtual bool ReadCount(uint32 &outCount) = 0;
49
51 virtual bool ReadPrimitiveData(uint8 &outPrimitive) = 0;
52 virtual bool ReadPrimitiveData(uint16 &outPrimitive) = 0;
53 virtual bool ReadPrimitiveData(int &outPrimitive) = 0;
54 virtual bool ReadPrimitiveData(uint32 &outPrimitive) = 0;
55 virtual bool ReadPrimitiveData(uint64 &outPrimitive) = 0;
56 virtual bool ReadPrimitiveData(float &outPrimitive) = 0;
57 virtual bool ReadPrimitiveData(double &outPrimitive) = 0;
58 virtual bool ReadPrimitiveData(bool &outPrimitive) = 0;
59 virtual bool ReadPrimitiveData(String &outPrimitive) = 0;
60 virtual bool ReadPrimitiveData(Float3 &outPrimitive) = 0;
61 virtual bool ReadPrimitiveData(Double3 &outPrimitive) = 0;
62 virtual bool ReadPrimitiveData(Vec3 &outPrimitive) = 0;
63 virtual bool ReadPrimitiveData(DVec3 &outPrimitive) = 0;
64 virtual bool ReadPrimitiveData(Vec4 &outPrimitive) = 0;
65 virtual bool ReadPrimitiveData(Quat &outPrimitive) = 0;
66 virtual bool ReadPrimitiveData(Mat44 &outPrimitive) = 0;
67 virtual bool ReadPrimitiveData(DMat44 &outPrimitive) = 0;
68
70 virtual bool ReadClassData(const char *inClassName, void *inInstance) = 0;
71 virtual bool ReadPointerData(const RTTI *inRTTI, void **inPointer, int inRefCountOffset = -1) = 0;
72};
73
76{
77public:
79 virtual void WriteDataType(EOSDataType inType) = 0;
80 virtual void WriteName(const char *inName) = 0;
81 virtual void WriteIdentifier(Identifier inIdentifier) = 0;
82 virtual void WriteCount(uint32 inCount) = 0;
83
85 virtual void WritePrimitiveData(const uint8 &inPrimitive) = 0;
86 virtual void WritePrimitiveData(const uint16 &inPrimitive) = 0;
87 virtual void WritePrimitiveData(const int &inPrimitive) = 0;
88 virtual void WritePrimitiveData(const uint32 &inPrimitive) = 0;
89 virtual void WritePrimitiveData(const uint64 &inPrimitive) = 0;
90 virtual void WritePrimitiveData(const float &inPrimitive) = 0;
91 virtual void WritePrimitiveData(const double &inPrimitive) = 0;
92 virtual void WritePrimitiveData(const bool &inPrimitive) = 0;
93 virtual void WritePrimitiveData(const String &inPrimitive) = 0;
94 virtual void WritePrimitiveData(const Float3 &inPrimitive) = 0;
95 virtual void WritePrimitiveData(const Double3 &inPrimitive) = 0;
96 virtual void WritePrimitiveData(const Vec3 &inPrimitive) = 0;
97 virtual void WritePrimitiveData(const DVec3 &inPrimitive) = 0;
98 virtual void WritePrimitiveData(const Vec4 &inPrimitive) = 0;
99 virtual void WritePrimitiveData(const Quat &inPrimitive) = 0;
100 virtual void WritePrimitiveData(const Mat44 &inPrimitive) = 0;
101 virtual void WritePrimitiveData(const DMat44 &inPrimitive) = 0;
102
104 virtual void WritePointerData(const RTTI *inRTTI, const void *inPointer) = 0;
105 virtual void WriteClassData(const RTTI *inRTTI, const void *inInstance) = 0;
106
108 virtual void HintNextItem() { /* Default is do nothing */ }
109 virtual void HintIndentUp() { /* Default is do nothing */ }
110 virtual void HintIndentDown() { /* Default is do nothing */ }
111};
112
113// Define macro to declare functions for a specific primitive type
114#define JPH_DECLARE_PRIMITIVE(name) \
115 JPH_EXPORT bool OSIsType(name *, int inArrayDepth, EOSDataType inDataType, const char *inClassName); \
116 JPH_EXPORT bool OSReadData(IObjectStreamIn &ioStream, name &outPrimitive); \
117 JPH_EXPORT void OSWriteDataType(IObjectStreamOut &ioStream, name *); \
118 JPH_EXPORT void OSWriteData(IObjectStreamOut &ioStream, const name &inPrimitive);
119
120// This file uses the JPH_DECLARE_PRIMITIVE macro to define all types
122
123// Define serialization templates
124template <class T, class A>
125bool OSIsType(Array<T, A> *, int inArrayDepth, EOSDataType inDataType, const char *inClassName)
126{
127 return (inArrayDepth > 0 && OSIsType(static_cast<T *>(nullptr), inArrayDepth - 1, inDataType, inClassName));
128}
129
130template <class T, uint N>
131bool OSIsType(StaticArray<T, N> *, int inArrayDepth, EOSDataType inDataType, const char *inClassName)
132{
133 return (inArrayDepth > 0 && OSIsType(static_cast<T *>(nullptr), inArrayDepth - 1, inDataType, inClassName));
134}
135
136template <class T, uint N>
137bool OSIsType(T (*)[N], int inArrayDepth, EOSDataType inDataType, const char *inClassName)
138{
139 return (inArrayDepth > 0 && OSIsType(static_cast<T *>(nullptr), inArrayDepth - 1, inDataType, inClassName));
140}
141
142template <class T>
143bool OSIsType(Ref<T> *, int inArrayDepth, EOSDataType inDataType, const char *inClassName)
144{
145 return OSIsType(static_cast<T *>(nullptr), inArrayDepth, inDataType, inClassName);
146}
147
148template <class T>
149bool OSIsType(RefConst<T> *, int inArrayDepth, EOSDataType inDataType, const char *inClassName)
150{
151 return OSIsType(static_cast<T *>(nullptr), inArrayDepth, inDataType, inClassName);
152}
153
155template <class T, class A>
156bool OSReadData(IObjectStreamIn &ioStream, Array<T, A> &inArray)
157{
158 bool continue_reading = true;
159
160 // Read array length
161 uint32 array_length;
162 continue_reading = ioStream.ReadCount(array_length);
163
164 // Read array items
165 if (continue_reading)
166 {
167 inArray.clear();
168 inArray.resize(array_length);
169 for (uint32 el = 0; el < array_length && continue_reading; ++el)
170 continue_reading = OSReadData(ioStream, inArray[el]);
171 }
172
173 return continue_reading;
174}
175
177template <class T, uint N>
179{
180 bool continue_reading = true;
181
182 // Read array length
183 uint32 array_length;
184 continue_reading = ioStream.ReadCount(array_length);
185
186 // Check if we can fit this many elements
187 if (array_length > N)
188 return false;
189
190 // Read array items
191 if (continue_reading)
192 {
193 inArray.clear();
194 inArray.resize(array_length);
195 for (uint32 el = 0; el < array_length && continue_reading; ++el)
196 continue_reading = OSReadData(ioStream, inArray[el]);
197 }
198
199 return continue_reading;
200}
201
203template <class T, uint N>
204bool OSReadData(IObjectStreamIn &ioStream, T (&inArray)[N])
205{
206 bool continue_reading = true;
207
208 // Read array length
209 uint32 array_length;
210 continue_reading = ioStream.ReadCount(array_length);
211 if (array_length != N)
212 return false;
213
214 // Read array items
215 for (uint32 el = 0; el < N && continue_reading; ++el)
216 continue_reading = OSReadData(ioStream, inArray[el]);
217
218 return continue_reading;
219}
220
222template <class T>
223bool OSReadData(IObjectStreamIn &ioStream, Ref<T> &inRef)
224{
225 return ioStream.ReadPointerData(JPH_RTTI(T), inRef.InternalGetPointer(), T::sInternalGetRefCountOffset());
226}
227
228template <class T>
230{
231 return ioStream.ReadPointerData(JPH_RTTI(T), inRef.InternalGetPointer(), T::sInternalGetRefCountOffset());
232}
233
234// Define serialization templates for dynamic arrays
235template <class T, class A>
237{
239 OSWriteDataType(ioStream, static_cast<T *>(nullptr));
240}
241
242template <class T, class A>
243void OSWriteData(IObjectStreamOut &ioStream, const Array<T, A> &inArray)
244{
245 // Write size of array
246 ioStream.HintNextItem();
247 ioStream.WriteCount(static_cast<uint32>(inArray.size()));
248
249 // Write data in array
250 ioStream.HintIndentUp();
251 for (const T &v : inArray)
252 OSWriteData(ioStream, v);
253 ioStream.HintIndentDown();
254}
255
257template <class T, uint N>
259{
261 OSWriteDataType(ioStream, static_cast<T *>(nullptr));
262}
263
264template <class T, uint N>
265void OSWriteData(IObjectStreamOut &ioStream, const StaticArray<T, N> &inArray)
266{
267 // Write size of array
268 ioStream.HintNextItem();
269 ioStream.WriteCount(inArray.size());
270
271 // Write data in array
272 ioStream.HintIndentUp();
273 for (const typename StaticArray<T, N>::value_type &v : inArray)
274 OSWriteData(ioStream, v);
275 ioStream.HintIndentDown();
276}
277
279template <class T, uint N>
280void OSWriteDataType(IObjectStreamOut &ioStream, T (*)[N])
281{
283 OSWriteDataType(ioStream, static_cast<T *>(nullptr));
284}
285
286template <class T, uint N>
287void OSWriteData(IObjectStreamOut &ioStream, const T (&inArray)[N])
288{
289 // Write size of array
290 ioStream.HintNextItem();
291 ioStream.WriteCount(uint32(N));
292
293 // Write data in array
294 ioStream.HintIndentUp();
295 for (const T &v : inArray)
296 OSWriteData(ioStream, v);
297 ioStream.HintIndentDown();
298}
299
301template <class T>
303{
304 OSWriteDataType(ioStream, static_cast<T *>(nullptr));
305}
306
307template <class T>
308void OSWriteData(IObjectStreamOut &ioStream, const Ref<T> &inRef)
309{
310 if (inRef != nullptr)
311 ioStream.WritePointerData(GetRTTI(inRef.GetPtr()), inRef.GetPtr());
312 else
313 ioStream.WritePointerData(nullptr, nullptr);
314}
315
316template <class T>
318{
319 OSWriteDataType(ioStream, static_cast<T *>(nullptr));
320}
321
322template <class T>
323void OSWriteData(IObjectStreamOut &ioStream, const RefConst<T> &inRef)
324{
325 if (inRef != nullptr)
326 ioStream.WritePointerData(GetRTTI(inRef.GetPtr()), inRef.GetPtr());
327 else
328 ioStream.WritePointerData(nullptr, nullptr);
329}
330
332
333#endif // JPH_OBJECT_STREAM
std::uint8_t uint8
Definition Core.h:447
#define JPH_EXPORT
Definition Core.h:236
std::uint64_t uint64
Definition Core.h:450
#define JPH_NAMESPACE_END
Definition Core.h:379
std::uint32_t uint32
Definition Core.h:449
#define JPH_NAMESPACE_BEGIN
Definition Core.h:373
std::uint16_t uint16
Definition Core.h:448
bool OSIsType(Array< T, A > *, int inArrayDepth, EOSDataType inDataType, const char *inClassName)
Definition ObjectStream.h:125
bool OSReadData(IObjectStreamIn &ioStream, Array< T, A > &inArray)
Define serialization templates for dynamic arrays.
Definition ObjectStream.h:156
void OSWriteData(IObjectStreamOut &ioStream, const Array< T, A > &inArray)
Definition ObjectStream.h:243
void OSWriteDataType(IObjectStreamOut &ioStream, Array< T, A > *)
Definition ObjectStream.h:236
#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
@ Array
Used in attribute declaration, indicates that this is an array of objects.
Definition Array.h:36
void resize(size_type inNewSize)
Resize array to new length.
Definition Array.h:120
size_type size() const
Returns amount of elements in the array.
Definition Array.h:320
void clear()
Destruct all elements and set length to zero.
Definition Array.h:145
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:42
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:76
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:108
virtual void WritePrimitiveData(const uint16 &inPrimitive)=0
virtual void HintIndentUp()
Definition ObjectStream.h:109
virtual void WritePrimitiveData(const bool &inPrimitive)=0
virtual void WritePrimitiveData(const double &inPrimitive)=0
virtual void HintIndentDown()
Definition ObjectStream.h:110
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:19
uint32 Identifier
Identifier for objects.
Definition ObjectStream.h:33
virtual ~ObjectStream()=default
Destructor.
EStreamType
Stream type.
Definition ObjectStream.h:23
Definition Quat.h:33
Definition RTTI.h:122
Definition Reference.h:163
const T * GetPtr() const
Get pointer.
Definition Reference.h:197
void ** InternalGetPointer()
INTERNAL HELPER FUNCTION USED BY SERIALIZATION.
Definition Reference.h:206
Definition Reference.h:107
T * GetPtr() const
Get pointer.
Definition Reference.h:135
void ** InternalGetPointer()
INTERNAL HELPER FUNCTION USED BY SERIALIZATION.
Definition Reference.h:144
Simple variable length array backed by a fixed size buffer.
Definition StaticArray.h:14
void clear()
Destruct all elements and set length to zero.
Definition StaticArray.h:52
T value_type
Definition StaticArray.h:16
size_type size() const
Returns amount of elements in the array.
Definition StaticArray.h:89
void resize(size_type inNewSize)
Resize array to new length.
Definition StaticArray.h:101
Definition Vec3.h:17
Definition Vec4.h:14