Jolt Physics
A multi core friendly Game Physics Engine
Loading...
Searching...
No Matches
SerializableObject.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
10
12// Helper macros
14
15// JPH_DECLARE_SERIALIZATION_FUNCTIONS
16#define JPH_DECLARE_SERIALIZATION_FUNCTIONS(linkage, prefix, class_name) \
17 linkage prefix bool OSReadData(IObjectStreamIn &ioStream, class_name &inInstance); \
18 linkage prefix bool OSReadData(IObjectStreamIn &ioStream, class_name *&inPointer); \
19 linkage prefix bool OSIsType(class_name *, int inArrayDepth, EOSDataType inDataType, const char *inClassName); \
20 linkage prefix bool OSIsType(class_name **, int inArrayDepth, EOSDataType inDataType, const char *inClassName); \
21 linkage prefix void OSWriteData(IObjectStreamOut &ioStream, const class_name &inInstance); \
22 linkage prefix void OSWriteData(IObjectStreamOut &ioStream, class_name *const &inPointer); \
23 linkage prefix void OSWriteDataType(IObjectStreamOut &ioStream, class_name *); \
24 linkage prefix void OSWriteDataType(IObjectStreamOut &ioStream, class_name **);
25
26// JPH_IMPLEMENT_SERIALIZATION_FUNCTIONS
27#define JPH_IMPLEMENT_SERIALIZATION_FUNCTIONS(class_name) \
28 bool OSReadData(IObjectStreamIn &ioStream, class_name &inInstance) \
29 { \
30 return ioStream.ReadClassData(#class_name, (void *)&inInstance); \
31 } \
32 bool OSReadData(IObjectStreamIn &ioStream, class_name *&inPointer) \
33 { \
34 return ioStream.ReadPointerData(JPH_RTTI(class_name), (void **)&inPointer); \
35 } \
36 bool OSIsType(class_name *, int inArrayDepth, EOSDataType inDataType, const char *inClassName) \
37 { \
38 return inArrayDepth == 0 && inDataType == EOSDataType::Instance && strcmp(inClassName, #class_name) == 0; \
39 } \
40 bool OSIsType(class_name **, int inArrayDepth, EOSDataType inDataType, const char *inClassName) \
41 { \
42 return inArrayDepth == 0 && inDataType == EOSDataType::Pointer && strcmp(inClassName, #class_name) == 0; \
43 } \
44 void OSWriteData(IObjectStreamOut &ioStream, const class_name &inInstance) \
45 { \
46 ioStream.WriteClassData(JPH_RTTI(class_name), (void *)&inInstance); \
47 } \
48 void OSWriteData(IObjectStreamOut &ioStream, class_name *const &inPointer) \
49 { \
50 if (inPointer) \
51 ioStream.WritePointerData(GetRTTI(inPointer), (void *)inPointer); \
52 else \
53 ioStream.WritePointerData(nullptr, nullptr); \
54 } \
55 void OSWriteDataType(IObjectStreamOut &ioStream, class_name *) \
56 { \
57 ioStream.WriteDataType(EOSDataType::Instance); \
58 ioStream.WriteName(#class_name); \
59 } \
60 void OSWriteDataType(IObjectStreamOut &ioStream, class_name **) \
61 { \
62 ioStream.WriteDataType(EOSDataType::Pointer); \
63 ioStream.WriteName(#class_name); \
64 }
65
67// Use these macros on non-virtual objects to make them serializable
69
70// JPH_DECLARE_SERIALIZABLE_NON_VIRTUAL
71#define JPH_DECLARE_SERIALIZABLE_NON_VIRTUAL(linkage, class_name) \
72public: \
73 JPH_DECLARE_RTTI_NON_VIRTUAL(linkage, class_name) \
74 JPH_DECLARE_SERIALIZATION_FUNCTIONS(linkage, friend, class_name) \
75
76// JPH_IMPLEMENT_SERIALIZABLE_NON_VIRTUAL
77#define JPH_IMPLEMENT_SERIALIZABLE_NON_VIRTUAL(class_name) \
78 JPH_IMPLEMENT_SERIALIZATION_FUNCTIONS(class_name) \
79 JPH_IMPLEMENT_RTTI_NON_VIRTUAL(class_name) \
80
82// Same as above, but when you cannot insert the declaration in the class itself
84
85// JPH_DECLARE_SERIALIZABLE_OUTSIDE_CLASS
86#define JPH_DECLARE_SERIALIZABLE_OUTSIDE_CLASS(linkage, class_name) \
87 JPH_DECLARE_RTTI_OUTSIDE_CLASS(linkage, class_name) \
88 JPH_DECLARE_SERIALIZATION_FUNCTIONS(linkage, extern, class_name) \
89
90// JPH_IMPLEMENT_SERIALIZABLE_OUTSIDE_CLASS
91#define JPH_IMPLEMENT_SERIALIZABLE_OUTSIDE_CLASS(class_name) \
92 JPH_IMPLEMENT_SERIALIZATION_FUNCTIONS(class_name) \
93 JPH_IMPLEMENT_RTTI_OUTSIDE_CLASS(class_name) \
94
96// Same as above, but for classes that have virtual functions
98
99// JPH_DECLARE_SERIALIZABLE_VIRTUAL - Use for concrete, non-base classes
100#define JPH_DECLARE_SERIALIZABLE_VIRTUAL(linkage, class_name) \
101public: \
102 JPH_DECLARE_RTTI_VIRTUAL(linkage, class_name) \
103 JPH_DECLARE_SERIALIZATION_FUNCTIONS(linkage, friend, class_name) \
104
105// JPH_IMPLEMENT_SERIALIZABLE_VIRTUAL
106#define JPH_IMPLEMENT_SERIALIZABLE_VIRTUAL(class_name) \
107 JPH_IMPLEMENT_SERIALIZATION_FUNCTIONS(class_name) \
108 JPH_IMPLEMENT_RTTI_VIRTUAL(class_name) \
109
110// JPH_DECLARE_SERIALIZABLE_ABSTRACT - Use for abstract, non-base classes
111#define JPH_DECLARE_SERIALIZABLE_ABSTRACT(linkage, class_name) \
112public: \
113 JPH_DECLARE_RTTI_ABSTRACT(linkage, class_name) \
114 JPH_DECLARE_SERIALIZATION_FUNCTIONS(linkage, friend, class_name) \
115
116// JPH_IMPLEMENT_SERIALIZABLE_ABSTRACT
117#define JPH_IMPLEMENT_SERIALIZABLE_ABSTRACT(class_name) \
118 JPH_IMPLEMENT_SERIALIZATION_FUNCTIONS(class_name) \
119 JPH_IMPLEMENT_RTTI_ABSTRACT(class_name) \
120
121// JPH_DECLARE_SERIALIZABLE_VIRTUAL_BASE - Use for concrete base classes
122#define JPH_DECLARE_SERIALIZABLE_VIRTUAL_BASE(linkage, class_name) \
123public: \
124 JPH_DECLARE_RTTI_VIRTUAL_BASE(linkage, class_name) \
125 JPH_DECLARE_SERIALIZATION_FUNCTIONS(linkage, friend, class_name) \
126
127// JPH_IMPLEMENT_SERIALIZABLE_VIRTUAL_BASE
128#define JPH_IMPLEMENT_SERIALIZABLE_VIRTUAL_BASE(class_name) \
129 JPH_IMPLEMENT_SERIALIZATION_FUNCTIONS(class_name) \
130 JPH_IMPLEMENT_RTTI_VIRTUAL_BASE(class_name) \
131
132// JPH_DECLARE_SERIALIZABLE_ABSTRACT_BASE - Use for abstract base class
133#define JPH_DECLARE_SERIALIZABLE_ABSTRACT_BASE(linkage, class_name) \
134public: \
135 JPH_DECLARE_RTTI_ABSTRACT_BASE(linkage, class_name) \
136 JPH_DECLARE_SERIALIZATION_FUNCTIONS(linkage, friend, class_name) \
137
138// JPH_IMPLEMENT_SERIALIZABLE_ABSTRACT_BASE
139#define JPH_IMPLEMENT_SERIALIZABLE_ABSTRACT_BASE(class_name) \
140 JPH_IMPLEMENT_SERIALIZATION_FUNCTIONS(class_name) \
141 JPH_IMPLEMENT_RTTI_ABSTRACT_BASE(class_name)
142
147{
149
150public:
152 virtual ~SerializableObject() = default;
153};
154
#define JPH_EXPORT
Definition: Core.h:227
#define JPH_NAMESPACE_END
Definition: Core.h:367
#define JPH_NAMESPACE_BEGIN
Definition: Core.h:361
#define JPH_DECLARE_SERIALIZABLE_ABSTRACT_BASE(linkage, class_name)
Definition: SerializableObject.h:133
Class that makes another class non-copyable. Usage: Inherit from NonCopyable.
Definition: NonCopyable.h:11
Definition: SerializableObject.h:147
virtual ~SerializableObject()=default
Constructor.