Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/thirdparty/jolt_physics/Jolt/ObjectStream/SerializableAttribute.h
9906 views
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
9
JPH_NAMESPACE_BEGIN
10
11
class RTTI;
12
class IObjectStreamIn;
13
class IObjectStreamOut;
14
15
/// Data type
16
enum class EOSDataType
17
{
18
/// Control codes
19
Declare, ///< Used to declare the attributes of a new object type
20
Object, ///< Start of a new object
21
Instance, ///< Used in attribute declaration, indicates that an object is an instanced attribute (no pointer)
22
Pointer, ///< Used in attribute declaration, indicates that an object is a pointer attribute
23
Array, ///< Used in attribute declaration, indicates that this is an array of objects
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
29
#include <Jolt/ObjectStream/ObjectStreamTypes.h>
30
31
// Error values for read functions
32
Invalid, ///< Next token on the stream was not a valid data type
33
};
34
35
/// Attributes are members of classes that need to be serialized.
36
class SerializableAttribute
37
{
38
public:
39
///@ Serialization functions
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
46
/// Constructor
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
49
/// Construct from other attribute with base class offset
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
52
/// Name of the attribute
53
void SetName(const char *inName) { mName = inName; }
54
const char * GetName() const { return mName; }
55
56
/// Access to the memory location that contains the member
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
62
/// In case this attribute contains an RTTI type, return it (note that a Array<sometype> will return the rtti of sometype)
63
const RTTI * GetMemberPrimitiveType() const
64
{
65
return mGetMemberPrimitiveType();
66
}
67
68
/// Check if this attribute is of a specific type
69
bool IsType(int inArrayDepth, EOSDataType inDataType, const char *inClassName) const
70
{
71
return mIsType(inArrayDepth, inDataType, inClassName);
72
}
73
74
/// Read the data for this attribute into attribute containing class inObject
75
bool ReadData(IObjectStreamIn &ioStream, void *inObject) const
76
{
77
return mReadData(ioStream, GetMemberPointer<void>(inObject));
78
}
79
80
/// Write the data for this attribute from attribute containing class inObject
81
void WriteData(IObjectStreamOut &ioStream, const void *inObject) const
82
{
83
mWriteData(ioStream, GetMemberPointer<void>(inObject));
84
}
85
86
/// Write the data type of this attribute to a stream
87
void WriteDataType(IObjectStreamOut &ioStream) const
88
{
89
mWriteDataType(ioStream);
90
}
91
92
private:
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
109
JPH_NAMESPACE_END
110
111
#endif // JPH_OBJECT_STREAM
112
113