RavEngine
Loading...
Searching...
No Matches
PxSerializer.h
1// Redistribution and use in source and binary forms, with or without
2// modification, are permitted provided that the following conditions
3// are met:
4// * Redistributions of source code must retain the above copyright
5// notice, this list of conditions and the following disclaimer.
6// * Redistributions in binary form must reproduce the above copyright
7// notice, this list of conditions and the following disclaimer in the
8// documentation and/or other materials provided with the distribution.
9// * Neither the name of NVIDIA CORPORATION nor the names of its
10// contributors may be used to endorse or promote products derived
11// from this software without specific prior written permission.
12//
13// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
14// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
17// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21// OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24//
25// Copyright (c) 2008-2022 NVIDIA Corporation. All rights reserved.
26// Copyright (c) 2004-2008 AGEIA Technologies, Inc. All rights reserved.
27// Copyright (c) 2001-2004 NovodeX AG. All rights reserved.
28
29#ifndef PX_SERIALIZER_H
30#define PX_SERIALIZER_H
35#include "foundation/PxAssert.h"
36#include "foundation/PxAllocatorCallback.h"
37#include "foundation/PxFoundation.h"
38#include "common/PxSerialFramework.h"
39#include "common/PxCollection.h"
40
41
42#if !PX_DOXYGEN
43namespace physx
44{
45#endif
46
63{
64public:
65
66 /**********************************************************************************************************************/
67
71
77 virtual const char* getConcreteTypeName() const = 0;
78
87
97 virtual bool isSubordinate() const = 0;
98
100 /**********************************************************************************************************************/
101
102 /**********************************************************************************************************************/
103
107
112
116 virtual void exportData(PxBase&, PxSerializationContext&) const = 0;
117
121 virtual void registerReferences(PxBase& obj, PxSerializationContext& s) const = 0;
122
128 virtual size_t getClassSize() const = 0;
129
137 virtual PxBase* createObject(PxU8*& address, PxDeserializationContext& context) const = 0;
138
140 /**********************************************************************************************************************/
141 virtual ~PxSerializer() {}
142};
143
144
148template<class T>
150{
151public:
152
153 /************************************************************************************************/
154
158
159 PxSerializerDefaultAdapter(const char* name) : mTypeName(name){}
160
161 virtual const char* getConcreteTypeName() const
162 {
163 return mTypeName;
164 }
165
166 virtual void requiresObjects(PxBase& obj, PxProcessPxBaseCallback& c) const
167 {
168 T& t = static_cast<T&>(obj);
169 t.requiresObjects(c);
170 }
171
172 virtual bool isSubordinate() const
173 {
174 return false;
175 }
176
178 /************************************************************************************************/
179
183
184 // object methods
185
186 virtual void exportExtraData(PxBase& obj, PxSerializationContext& s) const
187 {
188 T& t = static_cast<T&>(obj);
189 t.exportExtraData(s);
190 }
191
192 virtual void exportData(PxBase& obj, PxSerializationContext& s) const
193 {
195 T* copy = reinterpret_cast<T*>(allocator.allocate(sizeof(T), "TmpAllocExportData", __FILE__, __LINE__));
196 PxMemCopy(copy, &obj, sizeof(T));
197 copy->preExportDataReset();
198 s.writeData(copy, sizeof(T));
199 allocator.deallocate(copy);
200 }
201
203 {
204 T& t = static_cast<T&>(obj);
205
206 s.registerReference(obj, PX_SERIAL_REF_KIND_PXBASE, size_t(&obj));
207
208 struct RequiresCallback : public PxProcessPxBaseCallback
209 {
210 RequiresCallback(PxSerializationContext& c) : context(c) {}
211 RequiresCallback& operator=(RequiresCallback&) { PX_ASSERT(0); return *this; }
212 void process(physx::PxBase& base)
213 {
214 context.registerReference(base, PX_SERIAL_REF_KIND_PXBASE, size_t(&base));
215 }
217 };
218
219 RequiresCallback callback(s);
220 t.requiresObjects(callback);
221 }
222
223 // class methods
224
225 virtual size_t getClassSize() const
226 {
227 return sizeof(T);
228 }
229
230 virtual PxBase* createObject(PxU8*& address, PxDeserializationContext& context) const
231 {
232 return T::createObject(address, context);
233 }
234
235
237 /************************************************************************************************/
238
239private:
240 const char* mTypeName;
241};
242
248#define PX_NEW_SERIALIZER_ADAPTER(x) \
249 *new( PxGetAllocatorCallback()->allocate(sizeof(PxSerializerDefaultAdapter<x>), \
250 "PxSerializerDefaultAdapter", __FILE__, __LINE__ )) PxSerializerDefaultAdapter<x>(#x)
251
255#define PX_DELETE_SERIALIZER_ADAPTER(x) \
256 { PxSerializer* s = x; if (s) { s->~PxSerializer(); PxGetAllocatorCallback()->deallocate(s); } }
257
258#if !PX_DOXYGEN
259} // namespace physx
260#endif
261
263#endif
Definition base.h:1940
Abstract base class for an application defined memory allocator that can be used by the Nv library.
Definition PxAllocatorCallback.h:53
virtual void deallocate(void *ptr)=0
Frees memory previously allocated by allocate().
virtual void * allocate(size_t size, const char *typeName, const char *filename, int line)=0
Allocates size bytes of memory, which must be 16-byte aligned.
Base class for objects that can be members of a PxCollection.
Definition PxBase.h:73
Binary deserialization context class.
Definition PxSerialFramework.h:174
Callback class used to process PxBase objects.
Definition PxSerialFramework.h:81
Binary serialization context class.
Definition PxSerialFramework.h:99
Default PxSerializer implementation.
Definition PxSerializer.h:150
virtual void requiresObjects(PxBase &obj, PxProcessPxBaseCallback &c) const
Adds required objects to the collection.
Definition PxSerializer.h:166
virtual bool isSubordinate() const
Whether the object is subordinate.
Definition PxSerializer.h:172
virtual size_t getClassSize() const
Returns size needed to create the class instance.
Definition PxSerializer.h:225
virtual void exportExtraData(PxBase &obj, PxSerializationContext &s) const
Exports object's extra data to stream.
Definition PxSerializer.h:186
virtual const char * getConcreteTypeName() const
Returns string name of dynamic type.
Definition PxSerializer.h:161
virtual void registerReferences(PxBase &obj, PxSerializationContext &s) const
Register references that the object maintains to other objects.
Definition PxSerializer.h:202
virtual void exportData(PxBase &obj, PxSerializationContext &s) const
Exports object's data to stream.
Definition PxSerializer.h:192
virtual PxBase * createObject(PxU8 *&address, PxDeserializationContext &context) const
Create object at a given address, resolve references and import extra data.
Definition PxSerializer.h:230
Serialization interface class.
Definition PxSerializer.h:63
virtual bool isSubordinate() const =0
Whether the object is subordinate.
virtual void registerReferences(PxBase &obj, PxSerializationContext &s) const =0
Register references that the object maintains to other objects.
virtual const char * getConcreteTypeName() const =0
Returns string name of dynamic type.
virtual void requiresObjects(PxBase &, PxProcessPxBaseCallback &) const =0
Adds required objects to the collection.
virtual void exportExtraData(PxBase &, PxSerializationContext &) const =0
Exports object's extra data to stream.
virtual PxBase * createObject(PxU8 *&address, PxDeserializationContext &context) const =0
Create object at a given address, resolve references and import extra data.
virtual void exportData(PxBase &, PxSerializationContext &) const =0
Exports object's data to stream.
virtual size_t getClassSize() const =0
Returns size needed to create the class instance.
PX_C_EXPORT PX_FOUNDATION_API physx::PxAllocatorCallback *PX_CALL_CONV PxGetAllocatorCallback()
Get the allocator callback.
Definition FdFoundation.cpp:259
Sorts an array of objects in ascending order, assuming that the predicate implements the < operator:
Definition PxBoxController.h:39
PX_FORCE_INLINE void * PxMemCopy(void *dest, const void *src, PxU32 count)
Copies the bytes of one memory block to another. The memory blocks must not overlap.
Definition PxMemory.h:83