Jolt Physics
A multi core friendly Game Physics Engine
Loading...
Searching...
No Matches
SerializableAttribute.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
9class RTTI;
10class IObjectStreamIn;
12
14enum class EOSDataType
15{
17 Declare,
18 Object,
19 Instance,
20 Pointer,
21 Array,
22
23 // Basic types (primitives)
24 #define JPH_DECLARE_PRIMITIVE(name) T_##name,
25
26 // This file uses the JPH_DECLARE_PRIMITIVE macro to define all types
28
29 // Error values for read functions
30 Invalid,
31};
32
35{
36public:
38 using pGetMemberPrimitiveType = const RTTI * (*)();
39 using pIsType = bool (*)(int inArrayDepth, EOSDataType inDataType, const char *inClassName);
40 using pReadData = bool (*)(IObjectStreamIn &ioStream, void *inObject);
41 using pWriteData = void (*)(IObjectStreamOut &ioStream, const void *inObject);
42 using pWriteDataType = void (*)(IObjectStreamOut &ioStream);
43
45 SerializableAttribute(const char *inName, uint inMemberOffset, pGetMemberPrimitiveType inGetMemberPrimitiveType, pIsType inIsType, pReadData inReadData, pWriteData inWriteData, pWriteDataType inWriteDataType) : mName(inName), mMemberOffset(inMemberOffset), mGetMemberPrimitiveType(inGetMemberPrimitiveType), mIsType(inIsType), mReadData(inReadData), mWriteData(inWriteData), mWriteDataType(inWriteDataType) { }
46
48 SerializableAttribute(const SerializableAttribute &inOther, int inBaseOffset) : mName(inOther.mName), mMemberOffset(inOther.mMemberOffset + inBaseOffset), mGetMemberPrimitiveType(inOther.mGetMemberPrimitiveType), mIsType(inOther.mIsType), mReadData(inOther.mReadData), mWriteData(inOther.mWriteData), mWriteDataType(inOther.mWriteDataType) { }
49
51 void SetName(const char *inName) { mName = inName; }
52 const char * GetName() const { return mName; }
53
55 template <class T>
56 inline T * GetMemberPointer(void *inObject) const { return reinterpret_cast<T *>(reinterpret_cast<uint8 *>(inObject) + mMemberOffset); }
57 template <class T>
58 inline const T * GetMemberPointer(const void *inObject) const { return reinterpret_cast<const T *>(reinterpret_cast<const uint8 *>(inObject) + mMemberOffset); }
59
62 {
63 return mGetMemberPrimitiveType();
64 }
65
67 bool IsType(int inArrayDepth, EOSDataType inDataType, const char *inClassName) const
68 {
69 return mIsType(inArrayDepth, inDataType, inClassName);
70 }
71
73 bool ReadData(IObjectStreamIn &ioStream, void *inObject) const
74 {
75 return mReadData(ioStream, GetMemberPointer<void>(inObject));
76 }
77
79 void WriteData(IObjectStreamOut &ioStream, const void *inObject) const
80 {
81 mWriteData(ioStream, GetMemberPointer<void>(inObject));
82 }
83
85 void WriteDataType(IObjectStreamOut &ioStream) const
86 {
87 mWriteDataType(ioStream);
88 }
89
90private:
91 // Name of the attribute
92 const char * mName;
93
94 // Offset of the member relative to the class
95 uint mMemberOffset;
96
97 // In case this attribute contains an RTTI type, return it (note that a Array<sometype> will return the rtti of sometype)
98 pGetMemberPrimitiveType mGetMemberPrimitiveType;
99
100 // Serialization operations
101 pIsType mIsType;
102 pReadData mReadData;
103 pWriteData mWriteData;
104 pWriteDataType mWriteDataType;
105};
106
std::uint8_t uint8
Definition: Core.h:440
unsigned int uint
Definition: Core.h:439
#define JPH_NAMESPACE_END
Definition: Core.h:367
#define JPH_NAMESPACE_BEGIN
Definition: Core.h:361
EOSDataType
Data type.
Definition: SerializableAttribute.h:15
@ Declare
Control codes.
@ Array
Used in attribute declaration, indicates that this is an array of objects.
@ Object
Start of a new object.
@ Invalid
Next token on the stream was not a valid data type.
@ Pointer
Used in attribute declaration, indicates that an object is a pointer attribute.
@ Instance
Used in attribute declaration, indicates that an object is an instanced attribute (no pointer)
Interface class for reading from an object stream.
Definition: ObjectStream.h:40
Interface class for writing to an object stream.
Definition: ObjectStream.h:74
Definition: RTTI.h:122
Attributes are members of classes that need to be serialized.
Definition: SerializableAttribute.h:35
void(*)(IObjectStreamOut &ioStream) pWriteDataType
Definition: SerializableAttribute.h:42
void WriteData(IObjectStreamOut &ioStream, const void *inObject) const
Write the data for this attribute from attribute containing class inObject.
Definition: SerializableAttribute.h:79
void WriteDataType(IObjectStreamOut &ioStream) const
Write the data type of this attribute to a stream.
Definition: SerializableAttribute.h:85
bool(*)(int inArrayDepth, EOSDataType inDataType, const char *inClassName) pIsType
Definition: SerializableAttribute.h:39
const RTTI * GetMemberPrimitiveType() const
In case this attribute contains an RTTI type, return it (note that a Array<sometype> will return the ...
Definition: SerializableAttribute.h:61
const T * GetMemberPointer(const void *inObject) const
Definition: SerializableAttribute.h:58
T * GetMemberPointer(void *inObject) const
Access to the memory location that contains the member.
Definition: SerializableAttribute.h:56
void SetName(const char *inName)
Name of the attribute.
Definition: SerializableAttribute.h:51
SerializableAttribute(const char *inName, uint inMemberOffset, pGetMemberPrimitiveType inGetMemberPrimitiveType, pIsType inIsType, pReadData inReadData, pWriteData inWriteData, pWriteDataType inWriteDataType)
Constructor.
Definition: SerializableAttribute.h:45
bool IsType(int inArrayDepth, EOSDataType inDataType, const char *inClassName) const
Check if this attribute is of a specific type.
Definition: SerializableAttribute.h:67
bool(*)(IObjectStreamIn &ioStream, void *inObject) pReadData
Definition: SerializableAttribute.h:40
bool ReadData(IObjectStreamIn &ioStream, void *inObject) const
Read the data for this attribute into attribute containing class inObject.
Definition: SerializableAttribute.h:73
SerializableAttribute(const SerializableAttribute &inOther, int inBaseOffset)
Construct from other attribute with base class offset.
Definition: SerializableAttribute.h:48
const char * GetName() const
Definition: SerializableAttribute.h:52
const RTTI *(*)() pGetMemberPrimitiveType
@ Serialization functions
Definition: SerializableAttribute.h:38
void(*)(IObjectStreamOut &ioStream, const void *inObject) pWriteData
Definition: SerializableAttribute.h:41