Program Listing for include/common/PxBase.h

↰ Return to documentation for include/common/PxBase.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_BASE_H
#define PX_BASE_H

#include "foundation/PxFlags.h"
#include "common/PxSerialFramework.h"
#include "common/PxCollection.h"
#include "common/PxTypeInfo.h"
#include <string.h> // For strcmp
#include "foundation/PxAssert.h"

#if !PX_DOXYGEN
namespace physx
{
#endif

typedef PxU16 PxType;

struct PxBaseFlag
{
    enum Enum
    {
        eOWNS_MEMORY    = (1<<0),
        eIS_RELEASABLE  = (1<<1)
    };
};

typedef PxFlags<PxBaseFlag::Enum, PxU16> PxBaseFlags;
PX_FLAGS_OPERATORS(PxBaseFlag::Enum, PxU16)


class PxBase
{
//= ATTENTION! =====================================================================================
// Changing the data layout of this class breaks the binary serialization format.  See comments for
// PX_BINARY_SERIAL_VERSION.  If a modification is required, please adjust the getBinaryMetaData
// function.  If the modification is made on a custom branch, please change PX_BINARY_SERIAL_VERSION
// accordingly.
//==================================================================================================
public:
    virtual     void                release()                                       = 0;

    virtual     const char*         getConcreteTypeName() const                     = 0;

    /* brief Implements dynamic cast functionality.

    Example use:

    if(actor->is<PxRigidDynamic>()) {...}

    \return A pointer to the specified type if object matches, otherwise NULL
    */
    template<class T> T*            is()                                            { return typeMatch<T>() ? static_cast<T*>(this) : NULL; }

    /* brief Implements dynamic cast functionality for const objects.

    Example use:

    if(actor->is<PxRigidDynamic>()) {...}

    \return A pointer to the specified type if object matches, otherwise NULL
    */
    template<class T> const T*      is() const                                      { return typeMatch<T>() ? static_cast<const T*>(this) : NULL; }

    PX_FORCE_INLINE PxType          getConcreteType() const                         { return mConcreteType; }

    PX_FORCE_INLINE void            setBaseFlag(PxBaseFlag::Enum flag, bool value)  { mBaseFlags = value ? mBaseFlags|flag : mBaseFlags&~flag; }

    PX_FORCE_INLINE void            setBaseFlags(PxBaseFlags inFlags)               { mBaseFlags = inFlags; }

    PX_FORCE_INLINE PxBaseFlags     getBaseFlags() const                            { return mBaseFlags; }

    virtual     bool                isReleasable() const                            { return mBaseFlags & PxBaseFlag::eIS_RELEASABLE; }

protected:
    PX_INLINE                       PxBase(PxType concreteType, PxBaseFlags baseFlags)
                                        : mConcreteType(concreteType), mBaseFlags(baseFlags), mBuiltInRefCount(1)   {}

    PX_INLINE                       PxBase(PxBaseFlags baseFlags) : mBaseFlags(baseFlags)
                                    {
                                        PX_ASSERT(mBuiltInRefCount == 1);
                                    }
    virtual                         ~PxBase()                                       {}

    virtual             bool        isKindOf(const char* superClass) const { return !::strcmp(superClass, "PxBase"); }

    template<class T>   bool        typeMatch() const
                                    {
                                        return PxU32(PxTypeInfo<T>::eFastTypeId)!=PxU32(PxConcreteType::eUNDEFINED) ?
                                            PxU32(getConcreteType()) == PxU32(PxTypeInfo<T>::eFastTypeId) : isKindOf(PxTypeInfo<T>::name());
                                    }

private:
    friend              void        getBinaryMetaData_PxBase(PxOutputStream& stream);

protected:
                        PxType      mConcreteType;          // concrete type identifier - see PxConcreteType.
                        PxBaseFlags mBaseFlags;             // internal flags
                        PxU32       mBuiltInRefCount;
};

class PxRefCounted : public PxBase
{
public:

    virtual     void                release()                   = 0;

    virtual             PxU32       getReferenceCount() const   = 0;

    virtual             void        acquireReference()          = 0;

protected:
    virtual             void        onRefCountZero()    { delete this;  }

    PX_INLINE                       PxRefCounted(PxType concreteType, PxBaseFlags baseFlags) : PxBase(concreteType, baseFlags)  {}
    PX_INLINE                       PxRefCounted(PxBaseFlags baseFlags) : PxBase(baseFlags)                                     {}
    virtual                         ~PxRefCounted()                                                                             {}
    virtual             bool        isKindOf(const char* name) const { return !::strcmp("PxRefCounted", name) || PxBase::isKindOf(name); }
};

#if !PX_DOXYGEN
} // namespace physx
#endif

#endif