Program Listing for include/common/PxSerialFramework.h

↰ Return to documentation for include/common/PxSerialFramework.h

// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
//  * Redistributions of source code must retain the above copyright
//    notice, this list of conditions and the following disclaimer.
//  * Redistributions in binary form must reproduce the above copyright
//    notice, this list of conditions and the following disclaimer in the
//    documentation and/or other materials provided with the distribution.
//  * Neither the name of NVIDIA CORPORATION nor the names of its
//    contributors may be used to endorse or promote products derived
//    from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
// PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
// OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// Copyright (c) 2008-2022 NVIDIA Corporation. All rights reserved.
// Copyright (c) 2004-2008 AGEIA Technologies, Inc. All rights reserved.
// Copyright (c) 2001-2004 NovodeX AG. All rights reserved.

#ifndef PX_SERIAL_FRAMEWORK_H
#define PX_SERIAL_FRAMEWORK_H

#include "common/PxPhysXCommonConfig.h"

#if !PX_DOXYGEN
namespace physx
{
#endif

typedef PxU16 PxType;
class PxBase;
class PxSerializationContext;
class PxRepXSerializer;
class PxSerializer;
class PxPhysics;
class PxCollection;

#define PX_SERIAL_ALIGN 16

#define PX_SERIAL_FILE_ALIGN 128

#define PX_SERIAL_OBJECT_ID_INVALID 0

typedef PxU64 PxSerialObjectId;

#define PX_SERIAL_REF_KIND_PTR_TYPE_BIT (1u<<31)

#define PX_SERIAL_REF_KIND_PXBASE       (0 | PX_SERIAL_REF_KIND_PTR_TYPE_BIT)

#define PX_SERIAL_REF_KIND_MATERIAL_IDX (1)

#define PX_MAKE_FOURCC(a, b, c, d) ( (a) | ((b)<<8) | ((c)<<16) | ((d)<<24) )

class PxProcessPxBaseCallback
{
public:
    virtual ~PxProcessPxBaseCallback()  {}
    virtual void process(PxBase&) = 0;
};


class PxSerializationContext
{
public:

    virtual void                registerReference(PxBase& base, PxU32 kind, size_t reference)       = 0;

    virtual const PxCollection& getCollection() const                                               = 0;

    virtual void                writeData(const void* data, PxU32 size)                             = 0;

    virtual void                alignData(PxU32 alignment = PX_SERIAL_ALIGN)                        = 0;

    virtual void                writeName(const char* name)                                         = 0;

protected:

                            PxSerializationContext() {}
    virtual                 ~PxSerializationContext() {}
};


class PxDeserializationContext
{
public:

    virtual     PxBase*         resolveReference(PxU32 kind, size_t reference) const = 0;

    template<typename T>
                void            translatePxBase(T*& base) { if (base) { base = static_cast<T*>(resolveReference(PX_SERIAL_REF_KIND_PXBASE, size_t(base))); } }

    PX_INLINE   void            readName(const char*& name)
    {
        PxU32 len = *reinterpret_cast<PxU32*>(mExtraDataAddress);
        mExtraDataAddress += sizeof(len);
        name = len ? reinterpret_cast<const char*>(mExtraDataAddress) : NULL;
        mExtraDataAddress += len;
    }

    template<typename T>
    PX_INLINE   T*              readExtraData(PxU32 count=1)
    {
        T* data = reinterpret_cast<T*>(mExtraDataAddress);
        mExtraDataAddress += sizeof(T)*count;
        return data;
    }

    template<typename T, PxU32 alignment>
    PX_INLINE   T*              readExtraData(PxU32 count=1)
    {
        alignExtraData(alignment);
        return readExtraData<T>(count);
    }

    PX_INLINE   void            alignExtraData(PxU32 alignment = PX_SERIAL_ALIGN)
    {
        size_t addr = size_t(mExtraDataAddress);
        addr = (addr+alignment-1)&~size_t(alignment-1);
        mExtraDataAddress = reinterpret_cast<PxU8*>(addr);
    }

protected:

                                PxDeserializationContext() {}
    virtual                     ~PxDeserializationContext() {}

    PxU8*                       mExtraDataAddress;
};

typedef PX_DEPRECATED void (*PxBinaryMetaDataCallback)(PxOutputStream& stream);

class PxSerializationRegistry
{
public:
    /************************************************************************************************/


    virtual void                        registerSerializer(PxType type, PxSerializer& serializer) = 0;

    virtual PxSerializer*               unregisterSerializer(PxType type) = 0;

    PX_DEPRECATED virtual void          registerBinaryMetaDataCallback(PxBinaryMetaDataCallback callback) = 0;

    virtual const PxSerializer*         getSerializer(PxType type) const = 0;

    /************************************************************************************************/


    virtual void                        registerRepXSerializer(PxType type, PxRepXSerializer& serializer) = 0;

    virtual PxRepXSerializer*           unregisterRepXSerializer(PxType type) = 0;

    virtual PxRepXSerializer*           getRepXSerializer(const char* typeName) const = 0;

    /************************************************************************************************/

    virtual void release() = 0;

protected:
    virtual ~PxSerializationRegistry(){}
};

#if !PX_DOXYGEN
} // namespace physx
#endif

#endif