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-2018 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 "PxSerialFramework.h"
38 #include "PxCollection.h"
39 #include "foundation/PxAssert.h"
40 
41 #if !PX_DOXYGEN
42 namespace physx
43 {
44 #endif
45 
62 {
63 public:
64 
65  /**********************************************************************************************************************/
66 
70 
76  virtual const char* getConcreteTypeName() const = 0;
77 
85  virtual void requiresObjects(PxBase&, PxProcessPxBaseCallback&) const = 0;
86 
96  virtual bool isSubordinate() const = 0;
97 
99  /**********************************************************************************************************************/
100 
101  /**********************************************************************************************************************/
102 
106 
110  virtual void exportExtraData(PxBase&, PxSerializationContext&) const = 0;
111 
115  virtual void exportData(PxBase&, PxSerializationContext&) const = 0;
116 
120  virtual void registerReferences(PxBase& obj, PxSerializationContext& s) const = 0;
121 
127  virtual size_t getClassSize() const = 0;
128 
136  virtual PxBase* createObject(PxU8*& address, PxDeserializationContext& context) const = 0;
137 
139  /**********************************************************************************************************************/
140  virtual ~PxSerializer() {}
141 };
142 
143 
147 template<class T>
149 {
150 public:
151 
152  /************************************************************************************************/
153 
157 
158  PxSerializerDefaultAdapter(const char* name) : mTypeName(name){}
159 
160  virtual const char* getConcreteTypeName() const
161  {
162  return mTypeName;
163  }
164 
165  virtual void requiresObjects(PxBase& obj, PxProcessPxBaseCallback& c) const
166  {
167  T& t = static_cast<T&>(obj);
168  t.requiresObjects(c);
169  }
170 
171  virtual bool isSubordinate() const
172  {
173  return false;
174  }
175 
177  /************************************************************************************************/
178 
182 
183  // object methods
184 
185  virtual void exportExtraData(PxBase& obj, PxSerializationContext& s) const
186  {
187  T& t = static_cast<T&>(obj);
188  t.exportExtraData(s);
189  }
190 
191  virtual void exportData(PxBase& obj, PxSerializationContext& s) const
192  {
193  s.writeData(&obj, sizeof(T));
194  }
195 
196  virtual void registerReferences(PxBase& obj, PxSerializationContext& s) const
197  {
198  T& t = static_cast<T&>(obj);
199 
200  s.registerReference(obj, PX_SERIAL_REF_KIND_PXBASE, size_t(&obj));
201 
202  struct RequiresCallback : public PxProcessPxBaseCallback
203  {
204  RequiresCallback(PxSerializationContext& c) : context(c) {}
205  RequiresCallback& operator=(RequiresCallback&) { PX_ASSERT(0); return *this; }
206  void process(physx::PxBase& base)
207  {
208  context.registerReference(base, PX_SERIAL_REF_KIND_PXBASE, size_t(&base));
209  }
210  PxSerializationContext& context;
211  };
212 
213  RequiresCallback callback(s);
214  t.requiresObjects(callback);
215  }
216 
217  // class methods
218 
219  virtual size_t getClassSize() const
220  {
221  return sizeof(T);
222  }
223 
224  virtual PxBase* createObject(PxU8*& address, PxDeserializationContext& context) const
225  {
226  return T::createObject(address, context);
227  }
228 
229 
231  /************************************************************************************************/
232 
233 private:
234  const char* mTypeName;
235 };
236 
242 #define PX_NEW_SERIALIZER_ADAPTER(x) \
243  *new( PxGetFoundation().getAllocatorCallback().allocate(sizeof(PxSerializerDefaultAdapter<x>), \
244  "PxSerializerDefaultAdapter", __FILE__, __LINE__ )) PxSerializerDefaultAdapter<x>(#x)
245 
249 #define PX_DELETE_SERIALIZER_ADAPTER(x) \
250  { PxSerializer* s = x; if (s) { s->~PxSerializer(); PxGetFoundation().getAllocatorCallback().deallocate(s); } }
251 
252 #if !PX_DOXYGEN
253 } // namespace physx
254 #endif
255 
257 #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:185
virtual void exportData(PxBase &obj, PxSerializationContext &s) const
Exports object&#39;s data to stream.
Definition: PxSerializer.h:191
virtual void requiresObjects(PxBase &obj, PxProcessPxBaseCallback &c) const
Adds required objects to the collection.
Definition: PxSerializer.h:165
Serialization interface class.
Definition: PxSerializer.h:61
uint8_t PxU8
Definition: PxSimpleTypes.h:75
virtual bool isSubordinate() const
Whether the object is subordinate.
Definition: PxSerializer.h:171
virtual PxBase * createObject(PxU8 *&address, PxDeserializationContext &context) const
Create object at a given address, resolve references and import extra data.
Definition: PxSerializer.h:224
virtual ~PxSerializer()
Definition: PxSerializer.h:140
virtual const char * getConcreteTypeName() const
Returns string name of dynamic type.
Definition: PxSerializer.h:160
Binary deserialization context class.
Definition: PxSerialFramework.h:174
virtual size_t getClassSize() const
Returns size needed to create the class instance.
Definition: PxSerializer.h:219
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:61
PxSerializerDefaultAdapter(const char *name)
Definition: PxSerializer.h:158
virtual void registerReferences(PxBase &obj, PxSerializationContext &s) const
Register references that the object maintains to other objects.
Definition: PxSerializer.h:196
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:234
Default PxSerializer implementation.
Definition: PxSerializer.h:148