00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031 #ifndef PX_SERIALIZER_H
00032 #define PX_SERIALIZER_H
00033
00037 #include "PxSerialFramework.h"
00038 #include "PxCollection.h"
00039 #include "foundation/PxAssert.h"
00040
00041 #if !PX_DOXYGEN
00042 namespace physx
00043 {
00044 #endif
00045
00061 class PxSerializer
00062 {
00063 public:
00064
00065
00066
00070
00076 virtual const char* getConcreteTypeName() const = 0;
00077
00085 virtual void requires(PxBase&, PxProcessPxBaseCallback&) const = 0;
00086
00096 virtual bool isSubordinate() const = 0;
00097
00099
00100
00101
00102
00106
00110 virtual void exportExtraData(PxBase&, PxSerializationContext&) const = 0;
00111
00115 virtual void exportData(PxBase&, PxSerializationContext&) const = 0;
00116
00120 virtual void registerReferences(PxBase& obj, PxSerializationContext& s) const = 0;
00121
00127 virtual size_t getClassSize() const = 0;
00128
00136 virtual PxBase* createObject(PxU8*& address, PxDeserializationContext& context) const = 0;
00137
00139
00140 virtual ~PxSerializer() {}
00141 };
00142
00143
00147 template<class T>
00148 class PxSerializerDefaultAdapter : public PxSerializer
00149 {
00150 public:
00151
00152
00153
00157
00158 PxSerializerDefaultAdapter(const char* name) : mTypeName(name){}
00159
00160 virtual const char* getConcreteTypeName() const
00161 {
00162 return mTypeName;
00163 }
00164
00165 virtual void requires(PxBase& obj, PxProcessPxBaseCallback& c) const
00166 {
00167 T& t = static_cast<T&>(obj);
00168 t.requires(c);
00169 }
00170
00171 virtual bool isSubordinate() const
00172 {
00173 return false;
00174 }
00175
00177
00178
00182
00183
00184
00185 virtual void exportExtraData(PxBase& obj, PxSerializationContext& s) const
00186 {
00187 T& t = static_cast<T&>(obj);
00188 t.exportExtraData(s);
00189 }
00190
00191 virtual void exportData(PxBase& obj, PxSerializationContext& s) const
00192 {
00193 s.writeData(&obj, sizeof(T));
00194 }
00195
00196 virtual void registerReferences(PxBase& obj, PxSerializationContext& s) const
00197 {
00198 T& t = static_cast<T&>(obj);
00199
00200 s.registerReference(obj, PX_SERIAL_REF_KIND_PXBASE, size_t(&obj));
00201
00202 struct RequiresCallback : public PxProcessPxBaseCallback
00203 {
00204 RequiresCallback(PxSerializationContext& c) : context(c) {}
00205 RequiresCallback& operator=(RequiresCallback&) { PX_ASSERT(0); return *this; }
00206 void process(physx::PxBase& base)
00207 {
00208 context.registerReference(base, PX_SERIAL_REF_KIND_PXBASE, size_t(&base));
00209 }
00210 PxSerializationContext& context;
00211 };
00212
00213 RequiresCallback callback(s);
00214 t.requires(callback);
00215 }
00216
00217
00218
00219 virtual size_t getClassSize() const
00220 {
00221 return sizeof(T);
00222 }
00223
00224 virtual PxBase* createObject(PxU8*& address, PxDeserializationContext& context) const
00225 {
00226 return T::createObject(address, context);
00227 }
00228
00229
00231
00232
00233 private:
00234 const char* mTypeName;
00235 };
00236
00242 #define PX_NEW_SERIALIZER_ADAPTER(x) \
00243 *new( PxGetFoundation().getAllocatorCallback().allocate(sizeof(PxSerializerDefaultAdapter<x>), \
00244 "PxSerializerDefaultAdapter", __FILE__, __LINE__ )) PxSerializerDefaultAdapter<x>(#x)
00245
00249 #define PX_DELETE_SERIALIZER_ADAPTER(x) \
00250 { PxSerializer* s = x; if (s) { s->~PxSerializer(); PxGetFoundation().getAllocatorCallback().deallocate(s); } }
00251
00252 #if !PX_DOXYGEN
00253 }
00254 #endif
00255
00257 #endif