PxSerializer.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2008-2015, NVIDIA CORPORATION. All rights reserved.
3  *
4  * NVIDIA CORPORATION and its licensors retain all intellectual property
5  * and proprietary rights in and to this software, related documentation
6  * and any modifications thereto. Any use, reproduction, disclosure or
7  * distribution of this software and related documentation without an express
8  * license agreement from NVIDIA CORPORATION is strictly prohibited.
9  */
10 // Copyright (c) 2004-2008 AGEIA Technologies, Inc. All rights reserved.
11 // Copyright (c) 2001-2004 NovodeX AG. All rights reserved.
12 
13 
14 #ifndef PX_SERIALIZER_H
15 #define PX_SERIALIZER_H
16 
20 #include "PxSerialFramework.h"
21 #include "PxCollection.h"
22 #include "foundation/PxAssert.h"
23 
24 #ifndef PX_DOXYGEN
25 namespace physx
26 {
27 #endif
28 
45 {
46 public:
47 
48  /**********************************************************************************************************************/
49 
53 
59  virtual const char* getConcreteTypeName() const = 0;
60 
68  virtual void requires(PxBase&, PxProcessPxBaseCallback&) const = 0;
69 
79  virtual bool isSubordinate() const = 0;
80 
82  /**********************************************************************************************************************/
83 
84  /**********************************************************************************************************************/
85 
89 
93  virtual void exportExtraData(PxBase&, PxSerializationContext&) const = 0;
94 
98  virtual void exportData(PxBase&, PxSerializationContext&) const = 0;
99 
103  virtual void registerReferences(PxBase& obj, PxSerializationContext& s) const = 0;
104 
110  virtual size_t getClassSize() const = 0;
111 
119  virtual PxBase* createObject(PxU8*& address, PxDeserializationContext& context) const = 0;
120 
122  /**********************************************************************************************************************/
123  virtual ~PxSerializer() {}
124 };
125 
126 
130 template<class T>
132 {
133 public:
134 
135  /************************************************************************************************/
136 
140 
141  PxSerializerDefaultAdapter(const char* name) : mTypeName(name){}
142 
143  virtual const char* getConcreteTypeName() const
144  {
145  return mTypeName;
146  }
147 
148  virtual void requires(PxBase& obj, PxProcessPxBaseCallback& c) const
149  {
150  T& t = static_cast<T&>(obj);
151  t.requires(c);
152  }
153 
154  virtual bool isSubordinate() const
155  {
156  return false;
157  }
158 
160  /************************************************************************************************/
161 
165 
166  // object methods
167 
168  virtual void exportExtraData(PxBase& obj, PxSerializationContext& s) const
169  {
170  T& t = static_cast<T&>(obj);
171  t.exportExtraData(s);
172  }
173 
174  virtual void exportData(PxBase& obj, PxSerializationContext& s) const
175  {
176  s.writeData(&obj, sizeof(T));
177  }
178 
179  virtual void registerReferences(PxBase& obj, PxSerializationContext& s) const
180  {
181  T& t = static_cast<T&>(obj);
182 
183  s.registerReference(obj, PX_SERIAL_REF_KIND_PXBASE, size_t(&obj));
184 
185  struct RequiresCallback : public PxProcessPxBaseCallback
186  {
187  RequiresCallback(PxSerializationContext& c) : context(c) {}
188  RequiresCallback& operator=(RequiresCallback&) { PX_ASSERT(0); return *this; }
189  void process(physx::PxBase& base)
190  {
191  context.registerReference(base, PX_SERIAL_REF_KIND_PXBASE, size_t(&base));
192  }
193  PxSerializationContext& context;
194  };
195 
196  RequiresCallback callback(s);
197  t.requires(callback);
198  }
199 
200  // class methods
201 
202  virtual size_t getClassSize() const
203  {
204  return sizeof(T);
205  }
206 
207  virtual PxBase* createObject(PxU8*& address, PxDeserializationContext& context) const
208  {
209  return T::createObject(address, context);
210  }
211 
212 
214  /************************************************************************************************/
215 
216 private:
217  const char* mTypeName;
218 };
219 
225 #define PX_NEW_SERIALIZER_ADAPTER(x) \
226  *new( PxGetFoundation().getAllocatorCallback().allocate(sizeof(PxSerializerDefaultAdapter<x>), \
227  "PxSerializerDefaultAdapter", __FILE__, __LINE__ )) PxSerializerDefaultAdapter<x>(#x)
228 
232 #define PX_DELETE_SERIALIZER_ADAPTER(x) \
233  { PxSerializer* s = x; if (s) { s->~PxSerializer(); PxGetFoundation().getAllocatorCallback().deallocate(s); } }
234 
235 #ifndef PX_DOXYGEN
236 } // namespace physx
237 #endif
238 
240 #endif


Copyright © 2008-2015 NVIDIA Corporation, 2701 San Tomas Expressway, Santa Clara, CA 95050 U.S.A. All rights reserved. www.nvidia.com