PxSerializer.h
Go to the documentation of this file.
1 //
2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions
4 // are met:
5 // * Redistributions of source code must retain the above copyright
6 // notice, this list of conditions and the following disclaimer.
7 // * Redistributions in binary form must reproduce the above copyright
8 // notice, this list of conditions and the following disclaimer in the
9 // documentation and/or other materials provided with the distribution.
10 // * Neither the name of NVIDIA CORPORATION nor the names of its
11 // contributors may be used to endorse or promote products derived
12 // from this software without specific prior written permission.
13 //
14 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
15 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
18 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
22 // OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 //
26 // Copyright (c) 2008-2021 NVIDIA Corporation. All rights reserved.
27 // Copyright (c) 2004-2008 AGEIA Technologies, Inc. All rights reserved.
28 // Copyright (c) 2001-2004 NovodeX AG. All rights reserved.
29 
30 
31 #ifndef PX_SERIALIZER_H
32 #define PX_SERIALIZER_H
33 
37 #include "foundation/PxAssert.h"
40 #include "common/PxCollection.h"
41 #include "PxFoundation.h"
42 
43 
44 #if !PX_DOXYGEN
45 namespace physx
46 {
47 #endif
48 
65 {
66 public:
67 
68  /**********************************************************************************************************************/
69 
73 
79  virtual const char* getConcreteTypeName() const = 0;
80 
88  virtual void requiresObjects(PxBase&, PxProcessPxBaseCallback&) const = 0;
89 
99  virtual bool isSubordinate() const = 0;
100 
102  /**********************************************************************************************************************/
103 
104  /**********************************************************************************************************************/
105 
109 
113  virtual void exportExtraData(PxBase&, PxSerializationContext&) const = 0;
114 
118  virtual void exportData(PxBase&, PxSerializationContext&) const = 0;
119 
123  virtual void registerReferences(PxBase& obj, PxSerializationContext& s) const = 0;
124 
130  virtual size_t getClassSize() const = 0;
131 
139  virtual PxBase* createObject(PxU8*& address, PxDeserializationContext& context) const = 0;
140 
142  /**********************************************************************************************************************/
143  virtual ~PxSerializer() {}
144 };
145 
146 
150 template<class T>
152 {
153 public:
154 
155  /************************************************************************************************/
156 
160 
161  PxSerializerDefaultAdapter(const char* name) : mTypeName(name){}
162 
163  virtual const char* getConcreteTypeName() const
164  {
165  return mTypeName;
166  }
167 
168  virtual void requiresObjects(PxBase& obj, PxProcessPxBaseCallback& c) const
169  {
170  T& t = static_cast<T&>(obj);
171  t.requiresObjects(c);
172  }
173 
174  virtual bool isSubordinate() const
175  {
176  return false;
177  }
178 
180  /************************************************************************************************/
181 
185 
186  // object methods
187 
188  virtual void exportExtraData(PxBase& obj, PxSerializationContext& s) const
189  {
190  T& t = static_cast<T&>(obj);
191  t.exportExtraData(s);
192  }
193 
194  virtual void exportData(PxBase& obj, PxSerializationContext& s) const
195  {
196  PxAllocatorCallback& allocator = PxGetFoundation().getAllocatorCallback();
197  T* copy = reinterpret_cast<T*>(allocator.allocate(sizeof(T), "TmpAllocExportData", __FILE__, __LINE__));
198  PxMemCopy(copy, &obj, sizeof(T));
199  copy->preExportDataReset();
200  s.writeData(copy, sizeof(T));
201  allocator.deallocate(copy);
202  }
203 
204  virtual void registerReferences(PxBase& obj, PxSerializationContext& s) const
205  {
206  T& t = static_cast<T&>(obj);
207 
208  s.registerReference(obj, PX_SERIAL_REF_KIND_PXBASE, size_t(&obj));
209 
210  struct RequiresCallback : public PxProcessPxBaseCallback
211  {
212  RequiresCallback(PxSerializationContext& c) : context(c) {}
213  RequiresCallback& operator=(RequiresCallback&) { PX_ASSERT(0); return *this; }
214  void process(physx::PxBase& base)
215  {
216  context.registerReference(base, PX_SERIAL_REF_KIND_PXBASE, size_t(&base));
217  }
218  PxSerializationContext& context;
219  };
220 
221  RequiresCallback callback(s);
222  t.requiresObjects(callback);
223  }
224 
225  // class methods
226 
227  virtual size_t getClassSize() const
228  {
229  return sizeof(T);
230  }
231 
232  virtual PxBase* createObject(PxU8*& address, PxDeserializationContext& context) const
233  {
234  return T::createObject(address, context);
235  }
236 
237 
239  /************************************************************************************************/
240 
241 private:
242  const char* mTypeName;
243 };
244 
250 #define PX_NEW_SERIALIZER_ADAPTER(x) \
251  *new( PxGetFoundation().getAllocatorCallback().allocate(sizeof(PxSerializerDefaultAdapter<x>), \
252  "PxSerializerDefaultAdapter", __FILE__, __LINE__ )) PxSerializerDefaultAdapter<x>(#x)
253 
257 #define PX_DELETE_SERIALIZER_ADAPTER(x) \
258  { PxSerializer* s = x; if (s) { s->~PxSerializer(); PxGetFoundation().getAllocatorCallback().deallocate(s); } }
259 
260 #if !PX_DOXYGEN
261 } // namespace physx
262 #endif
263 
265 #endif
Definition: GuContactBuffer.h:37
#define PX_SERIAL_REF_KIND_PXBASE
Reference kind value for PxBase objects.
Definition: PxSerialFramework.h:68
virtual void exportExtraData(PxBase &obj, PxSerializationContext &s) const
Exports object&#39;s extra data to stream.
Definition: PxSerializer.h:188
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:84
virtual void exportData(PxBase &obj, PxSerializationContext &s) const
Exports object&#39;s data to stream.
Definition: PxSerializer.h:194
virtual void requiresObjects(PxBase &obj, PxProcessPxBaseCallback &c) const
Adds required objects to the collection.
Definition: PxSerializer.h:168
Serialization interface class.
Definition: PxSerializer.h:64
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.
uint8_t PxU8
Definition: PxSimpleTypes.h:75
virtual bool isSubordinate() const
Whether the object is subordinate.
Definition: PxSerializer.h:174
virtual PxBase * createObject(PxU8 *&address, PxDeserializationContext &context) const
Create object at a given address, resolve references and import extra data.
Definition: PxSerializer.h:232
virtual ~PxSerializer()
Definition: PxSerializer.h:143
Abstract base class for an application defined memory allocator that can be used by the Nv library...
Definition: PxAllocatorCallback.h:52
virtual const char * getConcreteTypeName() const
Returns string name of dynamic type.
Definition: PxSerializer.h:163
PX_C_EXPORT PX_FOUNDATION_API physx::PxFoundation &PX_CALL_CONV PxGetFoundation()
Retrieves the Foundation SDK after it has been created.
Binary deserialization context class.
Definition: PxSerialFramework.h:174
virtual size_t getClassSize() const
Returns size needed to create the class instance.
Definition: PxSerializer.h:227
virtual void registerReference(PxBase &base, PxU32 kind, size_t reference)=0
Registers a reference value corresponding to a PxBase object.
#define PX_ASSERT(exp)
Definition: PxAssert.h:59
PxSerializerDefaultAdapter(const char *name)
Definition: PxSerializer.h:161
virtual void registerReferences(PxBase &obj, PxSerializationContext &s) const
Register references that the object maintains to other objects.
Definition: PxSerializer.h:204
virtual void writeData(const void *data, PxU32 size)=0
Serializes object data and object extra data.
Base class for objects that can be members of a PxCollection.
Definition: PxBase.h:73
Binary serialization context class.
Definition: PxSerialFramework.h:99
Callback class used to process PxBase objects.
Definition: PxSerialFramework.h:81
const char * mTypeName
Definition: PxSerializer.h:242
Default PxSerializer implementation.
Definition: PxSerializer.h:151