RavEngine
Loading...
Searching...
No Matches
PxBase.h
1// Redistribution and use in source and binary forms, with or without
2// modification, are permitted provided that the following conditions
3// are met:
4// * Redistributions of source code must retain the above copyright
5// notice, this list of conditions and the following disclaimer.
6// * Redistributions in binary form must reproduce the above copyright
7// notice, this list of conditions and the following disclaimer in the
8// documentation and/or other materials provided with the distribution.
9// * Neither the name of NVIDIA CORPORATION nor the names of its
10// contributors may be used to endorse or promote products derived
11// from this software without specific prior written permission.
12//
13// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
14// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
17// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21// OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24//
25// Copyright (c) 2008-2022 NVIDIA Corporation. All rights reserved.
26// Copyright (c) 2004-2008 AGEIA Technologies, Inc. All rights reserved.
27// Copyright (c) 2001-2004 NovodeX AG. All rights reserved.
28
29#ifndef PX_BASE_H
30#define PX_BASE_H
31
36#include "foundation/PxFlags.h"
37#include "common/PxSerialFramework.h"
38#include "common/PxCollection.h"
39#include "common/PxTypeInfo.h"
40#include <string.h> // For strcmp
41#include "foundation/PxAssert.h"
42
43#if !PX_DOXYGEN
44namespace physx
45{
46#endif
47
48typedef PxU16 PxType;
49
54{
55 enum Enum
56 {
57 eOWNS_MEMORY = (1<<0),
58 eIS_RELEASABLE = (1<<1)
59 };
60};
61
63PX_FLAGS_OPERATORS(PxBaseFlag::Enum, PxU16)
64
65
72class PxBase
73{
74//= ATTENTION! =====================================================================================
75// Changing the data layout of this class breaks the binary serialization format. See comments for
76// PX_BINARY_SERIAL_VERSION. If a modification is required, please adjust the getBinaryMetaData
77// function. If the modification is made on a custom branch, please change PX_BINARY_SERIAL_VERSION
78// accordingly.
79//==================================================================================================
80public:
84 virtual void release() = 0;
85
90 virtual const char* getConcreteTypeName() const = 0;
91
92 /* brief Implements dynamic cast functionality.
93
94 Example use:
95
96 if(actor->is<PxRigidDynamic>()) {...}
97
98 \return A pointer to the specified type if object matches, otherwise NULL
99 */
100 template<class T> T* is() { return typeMatch<T>() ? static_cast<T*>(this) : NULL; }
101
102 /* brief Implements dynamic cast functionality for const objects.
103
104 Example use:
105
106 if(actor->is<PxRigidDynamic>()) {...}
107
108 \return A pointer to the specified type if object matches, otherwise NULL
109 */
110 template<class T> const T* is() const { return typeMatch<T>() ? static_cast<const T*>(this) : NULL; }
111
118 PX_FORCE_INLINE PxType getConcreteType() const { return mConcreteType; }
119
126 PX_FORCE_INLINE void setBaseFlag(PxBaseFlag::Enum flag, bool value) { mBaseFlags = value ? mBaseFlags|flag : mBaseFlags&~flag; }
127
135 PX_FORCE_INLINE void setBaseFlags(PxBaseFlags inFlags) { mBaseFlags = inFlags; }
136
144 PX_FORCE_INLINE PxBaseFlags getBaseFlags() const { return mBaseFlags; }
145
155 virtual bool isReleasable() const { return mBaseFlags & PxBaseFlag::eIS_RELEASABLE; }
156
157protected:
161 PX_INLINE PxBase(PxType concreteType, PxBaseFlags baseFlags)
162 : mConcreteType(concreteType), mBaseFlags(baseFlags), mBuiltInRefCount(1) {}
163
167 PX_INLINE PxBase(PxBaseFlags baseFlags) : mBaseFlags(baseFlags)
168 {
169 PX_ASSERT(mBuiltInRefCount == 1);
170 }
174 virtual ~PxBase() {}
175
179 virtual bool isKindOf(const char* superClass) const { return !::strcmp(superClass, "PxBase"); }
180
181 template<class T> bool typeMatch() const
182 {
183 return PxU32(PxTypeInfo<T>::eFastTypeId)!=PxU32(PxConcreteType::eUNDEFINED) ?
184 PxU32(getConcreteType()) == PxU32(PxTypeInfo<T>::eFastTypeId) : isKindOf(PxTypeInfo<T>::name());
185 }
186
187private:
188 friend void getBinaryMetaData_PxBase(PxOutputStream& stream);
189
190protected:
191 PxType mConcreteType; // concrete type identifier - see PxConcreteType.
192 PxBaseFlags mBaseFlags; // internal flags
193 PxU32 mBuiltInRefCount;
194};
195
199class PxRefCounted : public PxBase
200{
201public:
202
206 virtual void release() = 0;
207
216 virtual PxU32 getReferenceCount() const = 0;
217
223 virtual void acquireReference() = 0;
224
225protected:
226 virtual void onRefCountZero() { delete this; }
227
228 PX_INLINE PxRefCounted(PxType concreteType, PxBaseFlags baseFlags) : PxBase(concreteType, baseFlags) {}
229 PX_INLINE PxRefCounted(PxBaseFlags baseFlags) : PxBase(baseFlags) {}
230 virtual ~PxRefCounted() {}
231 virtual bool isKindOf(const char* name) const { return !::strcmp("PxRefCounted", name) || PxBase::isKindOf(name); }
232};
233
234#if !PX_DOXYGEN
235} // namespace physx
236#endif
237
239#endif
Base class for objects that can be members of a PxCollection.
Definition PxBase.h:73
virtual void release()=0
Releases the PxBase instance, please check documentation of release in derived class.
PX_FORCE_INLINE PxBaseFlags getBaseFlags() const
Returns PxBaseFlags.
Definition PxBase.h:144
virtual bool isReleasable() const
Whether the object is subordinate.
Definition PxBase.h:155
PX_INLINE PxBase(PxBaseFlags baseFlags)
Deserialization constructor setting base flags.
Definition PxBase.h:167
virtual bool isKindOf(const char *superClass) const
Returns whether a given type name matches with the type of this instance.
Definition PxBase.h:179
PX_FORCE_INLINE void setBaseFlags(PxBaseFlags inFlags)
Set PxBaseFlags
Definition PxBase.h:135
PX_INLINE PxBase(PxType concreteType, PxBaseFlags baseFlags)
Constructor setting concrete type and base flags.
Definition PxBase.h:161
PX_FORCE_INLINE void setBaseFlag(PxBaseFlag::Enum flag, bool value)
Set PxBaseFlag
Definition PxBase.h:126
virtual ~PxBase()
Destructor.
Definition PxBase.h:174
virtual const char * getConcreteTypeName() const =0
Returns string name of dynamic type.
PX_FORCE_INLINE PxType getConcreteType() const
Returns concrete type of object.
Definition PxBase.h:118
Container for bitfield flag variables associated with a specific enum type.
Definition PxFlags.h:73
Base class for ref-counted objects.
Definition PxBase.h:200
virtual bool isKindOf(const char *name) const
Returns whether a given type name matches with the type of this instance.
Definition PxBase.h:231
virtual void acquireReference()=0
Acquires a counted reference to this object.
virtual void release()=0
Decrements the reference count of the object and releases it if the new reference count is zero.
virtual PxU32 getReferenceCount() const =0
Returns the reference count of the object.
#define PX_FORCE_INLINE
Definition PxPreprocessor.h:335
#define PX_INLINE
Definition PxPreprocessor.h:320
Sorts an array of objects in ascending order, assuming that the predicate implements the < operator:
Definition PxBoxController.h:39
Flags for PxBase.
Definition PxBase.h:54
a structure containing per-type information for types inheriting from PxBase
Definition PxTypeInfo.h:113