PxSerializer.h

Go to the documentation of this file.
00001 //
00002 // Redistribution and use in source and binary forms, with or without
00003 // modification, are permitted provided that the following conditions
00004 // are met:
00005 //  * Redistributions of source code must retain the above copyright
00006 //    notice, this list of conditions and the following disclaimer.
00007 //  * Redistributions in binary form must reproduce the above copyright
00008 //    notice, this list of conditions and the following disclaimer in the
00009 //    documentation and/or other materials provided with the distribution.
00010 //  * Neither the name of NVIDIA CORPORATION nor the names of its
00011 //    contributors may be used to endorse or promote products derived
00012 //    from this software without specific prior written permission.
00013 //
00014 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
00015 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00016 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
00017 // PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
00018 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00019 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00020 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00021 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
00022 // OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00023 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00024 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00025 //
00026 // Copyright (c) 2008-2018 NVIDIA Corporation. All rights reserved.
00027 // Copyright (c) 2004-2008 AGEIA Technologies, Inc. All rights reserved.
00028 // Copyright (c) 2001-2004 NovodeX AG. All rights reserved.  
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            requiresObjects(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 requiresObjects(PxBase& obj, PxProcessPxBaseCallback& c) const
00166     {
00167         T& t = static_cast<T&>(obj);
00168         t.requiresObjects(c);
00169     }
00170 
00171     virtual bool isSubordinate() const
00172     {
00173         return false;
00174     }
00175         
00177     /************************************************************************************************/
00178 
00182 
00183     // object methods
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.requiresObjects(callback);
00215     }
00216 
00217     // class methods
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 } // namespace physx
00254 #endif
00255 
00257 #endif


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