| 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 |  | 
| 34 | /** \addtogroup common | 
| 35 | @{ | 
| 36 | */ | 
| 37 |  | 
| 38 | #include "foundation/PxFlags.h" | 
| 39 | #include "common/PxSerialFramework.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 |  | 
| 51 | /** | 
| 52 | \brief Flags for PxBase. | 
| 53 | */ | 
| 54 | struct PxBaseFlag | 
| 55 | {	 | 
| 56 | 	enum Enum | 
| 57 | 	{ | 
| 58 | 		eOWNS_MEMORY			= (1<<0), | 
| 59 | 		eIS_RELEASABLE			= (1<<1) | 
| 60 | 	}; | 
| 61 | }; | 
| 62 |  | 
| 63 | typedef PxFlags<PxBaseFlag::Enum, PxU16> PxBaseFlags; | 
| 64 | PX_FLAGS_OPERATORS(PxBaseFlag::Enum, PxU16) | 
| 65 |  | 
| 66 | /** | 
| 67 | \brief Base class for objects that can be members of a PxCollection. | 
| 68 |  | 
| 69 | All PxBase sub-classes can be serialized. | 
| 70 |  | 
| 71 | @see PxCollection  | 
| 72 | */ | 
| 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: | 
| 82 | 	/** | 
| 83 | 	\brief Releases the PxBase instance, please check documentation of release in derived class. | 
| 84 | 	*/ | 
| 85 | 	virtual     void                        release()										= 0;	 | 
| 86 |  | 
| 87 | 	/** | 
| 88 | 	\brief Returns string name of dynamic type. | 
| 89 | 	\return	Class name of most derived type of this object. | 
| 90 | 	*/ | 
| 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 |  | 
| 113 | 	/** | 
| 114 | 	\brief	Returns concrete type of object. | 
| 115 | 	\return	PxConcreteType::Enum of serialized object | 
| 116 |  | 
| 117 | 	@see PxConcreteType | 
| 118 | 	*/ | 
| 119 | 	PX_FORCE_INLINE	PxType					getConcreteType() const							{ return mConcreteType;	} | 
| 120 | 				 | 
| 121 | 	/** | 
| 122 | 	\brief Set PxBaseFlag	 | 
| 123 |  | 
| 124 | 	\param[in] flag The flag to be set | 
| 125 | 	\param[in] value The flags new value | 
| 126 | 	*/ | 
| 127 | 	PX_FORCE_INLINE	void					setBaseFlag(PxBaseFlag::Enum flag, bool value)	{ mBaseFlags = value ? mBaseFlags|flag : mBaseFlags&~flag; } | 
| 128 | 	 | 
| 129 | 	/** | 
| 130 | 	\brief Set PxBaseFlags	 | 
| 131 |  | 
| 132 | 	\param[in] inFlags The flags to be set | 
| 133 |  | 
| 134 | 	@see PxBaseFlags | 
| 135 | 	*/ | 
| 136 | 	PX_FORCE_INLINE	void					setBaseFlags(PxBaseFlags inFlags)				{ mBaseFlags = inFlags; } | 
| 137 | 	 | 
| 138 | 	/** | 
| 139 | 	\brief Returns PxBaseFlags  | 
| 140 |  | 
| 141 | 	\return	PxBaseFlags | 
| 142 |  | 
| 143 | 	@see PxBaseFlags | 
| 144 | 	*/ | 
| 145 | 	PX_FORCE_INLINE	PxBaseFlags				getBaseFlags() const							{ return mBaseFlags; } | 
| 146 |  | 
| 147 | 	/** | 
| 148 | 	\brief Whether the object is subordinate. | 
| 149 | 	 | 
| 150 | 	A class is subordinate, if it can only be instantiated in the context of another class. | 
| 151 |  | 
| 152 | 	\return	Whether the class is subordinate | 
| 153 | 	 | 
| 154 | 	@see PxSerialization::isSerializable | 
| 155 | 	*/ | 
| 156 | 	virtual		bool						isReleasable() const							{ return mBaseFlags & PxBaseFlag::eIS_RELEASABLE; } | 
| 157 |  | 
| 158 | protected: | 
| 159 | 	/** | 
| 160 | 	\brief Constructor setting concrete type and base flags. | 
| 161 | 	*/ | 
| 162 | 	PX_INLINE								PxBase(PxType concreteType, PxBaseFlags baseFlags) | 
| 163 | 												: mConcreteType(concreteType), mBaseFlags(baseFlags) {} | 
| 164 |  | 
| 165 | 	/** | 
| 166 | 	\brief Deserialization constructor setting base flags. | 
| 167 | 	*/ | 
| 168 | 	PX_INLINE								PxBase(PxBaseFlags baseFlags) : mBaseFlags(baseFlags) {} | 
| 169 |  | 
| 170 | 	/** | 
| 171 | 	\brief Destructor. | 
| 172 | 	*/ | 
| 173 | 	virtual									~PxBase()										{} | 
| 174 |  | 
| 175 | 	/** | 
| 176 | 	\brief Returns whether a given type name matches with the type of this instance | 
| 177 | 	*/	 | 
| 178 | 	virtual				bool				isKindOf(const char* superClass) const { return !::strcmp(s1: superClass, s2: "PxBase" ); } | 
| 179 |  | 
| 180 | 	template<class T>	bool				typeMatch() const | 
| 181 | 											{ | 
| 182 | 												return PxU32(PxTypeInfo<T>::eFastTypeId)!=PxU32(PxConcreteType::eUNDEFINED) ?  | 
| 183 | 													PxU32(getConcreteType()) == PxU32(PxTypeInfo<T>::eFastTypeId) : isKindOf(superClass: 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 |  | 
| 200 | /** @} */ | 
| 201 | #endif | 
| 202 |  |