Jolt Physics
A multi core friendly Game Physics Engine
Loading...
Searching...
No Matches
SerializableAttributeEnum.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
11
13
15// Macros to add properties to be serialized
17
18template <class MemberType>
19inline void AddSerializableAttributeEnum(RTTI &inRTTI, uint inOffset, const char *inName)
20{
21 inRTTI.AddAttribute(SerializableAttribute(inName, inOffset,
22 []() -> const RTTI *
23 {
24 return nullptr;
25 },
26 [](int inArrayDepth, EOSDataType inDataType, [[maybe_unused]] const char *inClassName)
27 {
28 return inArrayDepth == 0 && inDataType == EOSDataType::T_uint32;
29 },
30 [](IObjectStreamIn &ioStream, void *inObject)
31 {
32 uint32 temporary;
33 if (OSReadData(ioStream, temporary))
34 {
35 *reinterpret_cast<MemberType *>(inObject) = static_cast<MemberType>(temporary);
36 return true;
37 }
38 return false;
39 },
40 [](IObjectStreamOut &ioStream, const void *inObject)
41 {
42 static_assert(sizeof(MemberType) <= sizeof(uint32));
43 uint32 temporary = uint32(*reinterpret_cast<const MemberType *>(inObject));
44 OSWriteData(ioStream, temporary);
45 },
46 [](IObjectStreamOut &ioStream)
47 {
48 ioStream.WriteDataType(EOSDataType::T_uint32);
49 }));
50}
51
52// JPH_ADD_ENUM_ATTRIBUTE_WITH_ALIAS
53#define JPH_ADD_ENUM_ATTRIBUTE_WITH_ALIAS(class_name, member_name, alias_name) \
54 AddSerializableAttributeEnum<decltype(class_name::member_name)>(inRTTI, offsetof(class_name, member_name), alias_name);
55
56// JPH_ADD_ENUM_ATTRIBUTE
57#define JPH_ADD_ENUM_ATTRIBUTE(class_name, member_name) \
58 JPH_ADD_ENUM_ATTRIBUTE_WITH_ALIAS(class_name, member_name, #member_name);
59
61
62#else
63
64#define JPH_ADD_ENUM_ATTRIBUTE_WITH_ALIAS(...)
65#define JPH_ADD_ENUM_ATTRIBUTE(...)
66
67#endif // JPH_OBJECT_STREAM
unsigned int uint
Definition Core.h:453
#define JPH_NAMESPACE_END
Definition Core.h:379
std::uint32_t uint32
Definition Core.h:456
#define JPH_NAMESPACE_BEGIN
Definition Core.h:373
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
EOSDataType
Data type.
Definition SerializableAttribute.h:17
JPH_NAMESPACE_BEGIN void AddSerializableAttributeEnum(RTTI &inRTTI, uint inOffset, const char *inName)
Definition SerializableAttributeEnum.h:19
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
void AddAttribute(const SerializableAttribute &inAttribute)
Attribute access.
Definition RTTI.cpp:132
Attributes are members of classes that need to be serialized.
Definition SerializableAttribute.h:37