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