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
7#ifdef JPH_OBJECT_STREAM
8
10
11class RTTI;
12class IObjectStreamIn;
14
16enum class EOSDataType
17{
19 Declare,
20 Object,
21 Instance,
22 Pointer,
23 Array,
24
25 // Basic types (primitives)
26 #define JPH_DECLARE_PRIMITIVE(name) T_##name,
27
28 // This file uses the JPH_DECLARE_PRIMITIVE macro to define all types
30
31 // Error values for read functions
32 Invalid,
33};
34
37{
38public:
40 using pGetMemberPrimitiveType = const RTTI * (*)();
41 using pIsType = bool (*)(int inArrayDepth, EOSDataType inDataType, const char *inClassName);
42 using pReadData = bool (*)(IObjectStreamIn &ioStream, void *inObject);
43 using pWriteData = void (*)(IObjectStreamOut &ioStream, const void *inObject);
44 using pWriteDataType = void (*)(IObjectStreamOut &ioStream);
45
47 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) { }
48
50 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) { }
51
53 void SetName(const char *inName) { mName = inName; }
54 const char * GetName() const { return mName; }
55
57 template <class T>
58 inline T * GetMemberPointer(void *inObject) const { return reinterpret_cast<T *>(reinterpret_cast<uint8 *>(inObject) + mMemberOffset); }
59 template <class T>
60 inline const T * GetMemberPointer(const void *inObject) const { return reinterpret_cast<const T *>(reinterpret_cast<const uint8 *>(inObject) + mMemberOffset); }
61
64 {
65 return mGetMemberPrimitiveType();
66 }
67
69 bool IsType(int inArrayDepth, EOSDataType inDataType, const char *inClassName) const
70 {
71 return mIsType(inArrayDepth, inDataType, inClassName);
72 }
73
75 bool ReadData(IObjectStreamIn &ioStream, void *inObject) const
76 {
77 return mReadData(ioStream, GetMemberPointer<void>(inObject));
78 }
79
81 void WriteData(IObjectStreamOut &ioStream, const void *inObject) const
82 {
83 mWriteData(ioStream, GetMemberPointer<void>(inObject));
84 }
85
87 void WriteDataType(IObjectStreamOut &ioStream) const
88 {
89 mWriteDataType(ioStream);
90 }
91
92private:
93 // Name of the attribute
94 const char * mName;
95
96 // Offset of the member relative to the class
97 uint mMemberOffset;
98
99 // In case this attribute contains an RTTI type, return it (note that a Array<sometype> will return the rtti of sometype)
100 pGetMemberPrimitiveType mGetMemberPrimitiveType;
101
102 // Serialization operations
103 pIsType mIsType;
104 pReadData mReadData;
105 pWriteData mWriteData;
106 pWriteDataType mWriteDataType;
107};
108
110
111#endif // JPH_OBJECT_STREAM
std::uint8_t uint8
Definition: Core.h:453
unsigned int uint
Definition: Core.h:452
#define JPH_NAMESPACE_END
Definition: Core.h:378
#define JPH_NAMESPACE_BEGIN
Definition: Core.h:372
EOSDataType
Data type.
Definition: SerializableAttribute.h:17
@ 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:42
Interface class for writing to an object stream.
Definition: ObjectStream.h:76
Definition: RTTI.h:122
Attributes are members of classes that need to be serialized.
Definition: SerializableAttribute.h:37
void(*)(IObjectStreamOut &ioStream) pWriteDataType
Definition: SerializableAttribute.h:44
void WriteData(IObjectStreamOut &ioStream, const void *inObject) const
Write the data for this attribute from attribute containing class inObject.
Definition: SerializableAttribute.h:81
void WriteDataType(IObjectStreamOut &ioStream) const
Write the data type of this attribute to a stream.
Definition: SerializableAttribute.h:87
bool(*)(int inArrayDepth, EOSDataType inDataType, const char *inClassName) pIsType
Definition: SerializableAttribute.h:41
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:63
const T * GetMemberPointer(const void *inObject) const
Definition: SerializableAttribute.h:60
T * GetMemberPointer(void *inObject) const
Access to the memory location that contains the member.
Definition: SerializableAttribute.h:58
void SetName(const char *inName)
Name of the attribute.
Definition: SerializableAttribute.h:53
SerializableAttribute(const char *inName, uint inMemberOffset, pGetMemberPrimitiveType inGetMemberPrimitiveType, pIsType inIsType, pReadData inReadData, pWriteData inWriteData, pWriteDataType inWriteDataType)
Constructor.
Definition: SerializableAttribute.h:47
bool IsType(int inArrayDepth, EOSDataType inDataType, const char *inClassName) const
Check if this attribute is of a specific type.
Definition: SerializableAttribute.h:69
bool(*)(IObjectStreamIn &ioStream, void *inObject) pReadData
Definition: SerializableAttribute.h:42
bool ReadData(IObjectStreamIn &ioStream, void *inObject) const
Read the data for this attribute into attribute containing class inObject.
Definition: SerializableAttribute.h:75
SerializableAttribute(const SerializableAttribute &inOther, int inBaseOffset)
Construct from other attribute with base class offset.
Definition: SerializableAttribute.h:50
const char * GetName() const
Definition: SerializableAttribute.h:54
const RTTI *(*)() pGetMemberPrimitiveType
@ Serialization functions
Definition: SerializableAttribute.h:40
void(*)(IObjectStreamOut &ioStream, const void *inObject) pWriteData
Definition: SerializableAttribute.h:43