PxBase.h
Go to the documentation of this file.
1 //
2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions
4 // are met:
5 // * Redistributions of source code must retain the above copyright
6 // notice, this list of conditions and the following disclaimer.
7 // * Redistributions in binary form must reproduce the above copyright
8 // notice, this list of conditions and the following disclaimer in the
9 // documentation and/or other materials provided with the distribution.
10 // * Neither the name of NVIDIA CORPORATION nor the names of its
11 // contributors may be used to endorse or promote products derived
12 // from this software without specific prior written permission.
13 //
14 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
15 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
18 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
22 // OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 //
26 // Copyright (c) 2008-2021 NVIDIA Corporation. All rights reserved.
27 // Copyright (c) 2004-2008 AGEIA Technologies, Inc. All rights reserved.
28 // Copyright (c) 2001-2004 NovodeX AG. All rights reserved.
29 
30 
31 #ifndef PX_PHYSICS_PX_BASE
32 #define PX_PHYSICS_PX_BASE
33 
38 #include "foundation/PxFlags.h"
40 #include "common/PxCollection.h"
41 #include "common/PxTypeInfo.h"
42 #include <string.h> // For strcmp
43 
44 #if !PX_DOXYGEN
45 namespace physx
46 {
47 #endif
48 
49 typedef PxU16 PxType;
50 
54 struct PxBaseFlag
55 {
56  enum Enum
57  {
58  eOWNS_MEMORY = (1<<0),
59  eIS_RELEASABLE = (1<<1)
60  };
61 };
62 
64 PX_FLAGS_OPERATORS(PxBaseFlag::Enum, PxU16)
65 
66 
73 class PxBase
74 {
75 //= ATTENTION! =====================================================================================
76 // Changing the data layout of this class breaks the binary serialization format. See comments for
77 // PX_BINARY_SERIAL_VERSION. If a modification is required, please adjust the getBinaryMetaData
78 // function. If the modification is made on a custom branch, please change PX_BINARY_SERIAL_VERSION
79 // accordingly.
80 //==================================================================================================
81 public:
85  virtual void release() = 0;
86 
91  virtual const char* getConcreteTypeName() const = 0;
92 
93  /* brief Implements dynamic cast functionality.
94 
95  Example use:
96 
97  if(actor->is<PxRigidDynamic>()) {...}
98 
99  \return A pointer to the specified type if object matches, otherwise NULL
100  */
101  template<class T> T* is() { return typeMatch<T>() ? static_cast<T*>(this) : NULL; }
102 
103  /* brief Implements dynamic cast functionality for const objects.
104 
105  Example use:
106 
107  if(actor->is<PxRigidDynamic>()) {...}
108 
109  \return A pointer to the specified type if object matches, otherwise NULL
110  */
111  template<class T> const T* is() const { return typeMatch<T>() ? static_cast<const T*>(this) : NULL; }
112 
119  PX_FORCE_INLINE PxType getConcreteType() const { return mConcreteType; }
120 
127  PX_FORCE_INLINE void setBaseFlag(PxBaseFlag::Enum flag, bool value) { mBaseFlags = value ? mBaseFlags|flag : mBaseFlags&~flag; }
128 
136  PX_FORCE_INLINE void setBaseFlags(PxBaseFlags inFlags) { mBaseFlags = inFlags; }
137 
145  PX_FORCE_INLINE PxBaseFlags getBaseFlags() const { return mBaseFlags; }
146 
156  virtual bool isReleasable() const { return mBaseFlags & PxBaseFlag::eIS_RELEASABLE; }
157 
158 protected:
162  PX_INLINE PxBase(PxType concreteType, PxBaseFlags baseFlags)
163  : mConcreteType(concreteType), mBaseFlags(baseFlags) {}
164 
168  PX_INLINE PxBase(PxBaseFlags baseFlags) : mBaseFlags(baseFlags) {}
169 
173  virtual ~PxBase() {}
174 
178  virtual bool isKindOf(const char* superClass) const { return !::strcmp(superClass, "PxBase"); }
179 
180  template<class T> bool typeMatch() const
181  {
183  PxU32(getConcreteType()) == PxU32(PxTypeInfo<T>::eFastTypeId) : isKindOf(PxTypeInfo<T>::name());
184  }
185 
186 
187 private:
188  friend void getBinaryMetaData_PxBase(PxOutputStream& stream);
189 
190 protected:
191  PxType mConcreteType; // concrete type identifier - see PxConcreteType.
192  PxBaseFlags mBaseFlags; // internal flags
193 
194 };
195 
196 #if !PX_DOXYGEN
197 } // namespace physx
198 #endif
199 
201 #endif
Definition: GuContactBuffer.h:37
a structure containing per-type information for types inheriting from PxBase
Definition: PxTypeInfo.h:91
#define PX_FORCE_INLINE
Definition: PxPreprocessor.h:351
bool typeMatch() const
Definition: PxBase.h:180
virtual bool isKindOf(const char *superClass) const
Returns whether a given type name matches with the type of this instance.
Definition: PxBase.h:178
uint32_t PxU32
Definition: PxSimpleTypes.h:71
virtual ~PxBase()
Destructor.
Definition: PxBase.h:173
Definition: PxBase.h:59
PX_FORCE_INLINE void setBaseFlags(PxBaseFlags inFlags)
Set PxBaseFlags.
Definition: PxBase.h:136
PxU16 PxType
Definition: PxBase.h:49
Output stream class for I/O.
Definition: PxIO.h:114
PxType mConcreteType
Definition: PxBase.h:191
PX_FORCE_INLINE PxBaseFlags getBaseFlags() const
Returns PxBaseFlags.
Definition: PxBase.h:145
PX_FORCE_INLINE void setBaseFlag(PxBaseFlag::Enum flag, bool value)
Set PxBaseFlag.
Definition: PxBase.h:127
Flags for PxBase.
Definition: PxBase.h:54
Enum
Definition: PxBase.h:56
const T * is() const
Definition: PxBase.h:111
PX_INLINE PxBase(PxType concreteType, PxBaseFlags baseFlags)
Constructor setting concrete type and base flags.
Definition: PxBase.h:162
T * is()
Definition: PxBase.h:101
PxBaseFlags mBaseFlags
Definition: PxBase.h:192
PxFlags< PxBaseFlag::Enum, PxU16 > PxBaseFlags
Definition: PxBase.h:63
PX_INLINE PxBase(PxBaseFlags baseFlags)
Deserialization constructor setting base flags.
Definition: PxBase.h:168
uint16_t PxU16
Definition: PxSimpleTypes.h:73
Base class for objects that can be members of a PxCollection.
Definition: PxBase.h:73
Definition: PxTypeInfo.h:57
PX_FORCE_INLINE PxType getConcreteType() const
Returns concrete type of object.
Definition: PxBase.h:119
#define PX_INLINE
Definition: PxPreprocessor.h:336
virtual bool isReleasable() const
Whether the object is subordinate.
Definition: PxBase.h:156