1/*
2 * Copyright (c) 2008-2015, NVIDIA CORPORATION. All rights reserved.
3 *
4 * NVIDIA CORPORATION and its licensors retain all intellectual property
5 * and proprietary rights in and to this software, related documentation
6 * and any modifications thereto. Any use, reproduction, disclosure or
7 * distribution of this software and related documentation without an express
8 * license agreement from NVIDIA CORPORATION is strictly prohibited.
9 */
10// Copyright (c) 2004-2008 AGEIA Technologies, Inc. All rights reserved.
11// Copyright (c) 2001-2004 NovodeX AG. All rights reserved.
12
13
14#ifndef PX_PHYSICS_PX_BASE
15#define PX_PHYSICS_PX_BASE
16
17/** \addtogroup common
18@{
19*/
20
21#include "PxSerialFramework.h"
22#include "PxCollection.h"
23
24#ifndef PX_DOXYGEN
25namespace physx
26{
27#endif
28
29typedef PxU16 PxType;
30
31/**
32\brief Flags for PxBase.
33*/
34struct PxBaseFlag
35{
36 enum Enum
37 {
38 eOWNS_MEMORY = (1<<0),
39 eIS_RELEASABLE = (1<<1)
40 };
41};
42
43typedef PxFlags<PxBaseFlag::Enum, PxU16> PxBaseFlags;
44PX_FLAGS_OPERATORS(PxBaseFlag::Enum, PxU16)
45
46/**
47\brief Base class for objects that can be members of a PxCollection.
48
49All PxBase sub-classes can be serialized.
50
51@see PxCollection
52*/
53class PxBase
54{
55//= ATTENTION! =====================================================================================
56// Changing the data layout of this class breaks the binary serialization format. See comments for
57// PX_BINARY_SERIAL_VERSION. If a modification is required, please adjust the getBinaryMetaData
58// function. If the modification is made on a custom branch, please change PX_BINARY_SERIAL_VERSION
59// accordingly.
60//==================================================================================================
61public:
62 /**
63 \brief Releases the PxBase instance, please check documentation of release in derived class.
64 */
65 virtual void release() = 0;
66
67 /**
68 \brief Returns string name of dynamic type.
69 \return Class name of most derived type of this object.
70 */
71 virtual const char* getConcreteTypeName() const { return NULL; }
72
73 /* brief Implements dynamic cast functionality.
74
75 Example use:
76
77 if(actor->is<PxRigidDynamic>()) {...}
78
79 \return A pointer to the specified type if object matches, otherwise NULL
80 */
81 template<class T> T* is() { return typeMatch<T>() ? static_cast<T*>(this) : NULL; }
82
83 /* brief Implements dynamic cast functionality for const objects.
84
85 Example use:
86
87 if(actor->is<PxRigidDynamic>()) {...}
88
89 \return A pointer to the specified type if object matches, otherwise NULL
90 */
91 template<class T> const T* is() const { return typeMatch<T>() ? static_cast<const T*>(this) : NULL; }
92
93 /**
94 \brief Returns concrete type of object.
95 \return PxConcreteType::Enum of serialized object
96
97 @see PxConcreteType
98 */
99 PX_INLINE PxType getConcreteType() const { return mConcreteType; }
100
101 /**
102 \brief Set PxBaseFlag
103
104 \param[in] flag The flag to be set
105 \param[in] value The flags new value
106 */
107 PX_INLINE void setBaseFlag(PxBaseFlag::Enum flag, bool value) { mBaseFlags = value ? mBaseFlags|flag : mBaseFlags&~flag; }
108
109 /**
110 \brief Set PxBaseFlags
111
112 \param[in] inFlags The flags to be set
113
114 @see PxBaseFlags
115 */
116 PX_INLINE void setBaseFlags(PxBaseFlags inFlags ) { mBaseFlags = inFlags; }
117
118 /**
119 \brief Returns PxBaseFlags
120
121 \return PxBaseFlags
122
123 @see PxBaseFlags
124 */
125 PX_INLINE PxBaseFlags getBaseFlags() const { return mBaseFlags; }
126
127 /**
128 \brief Whether the object is subordinate.
129
130 A class is subordinate, if it can only be instantiated in the context of another class.
131
132 \return Whether the class is subordinate
133
134 @see PxSerialization::isSerializable
135 */
136 virtual bool isReleasable() const { return mBaseFlags & PxBaseFlag::eIS_RELEASABLE; }
137
138protected:
139 /**
140 \brief Constructor setting concrete type and base flags.
141 */
142 PX_INLINE PxBase(PxType concreteType, PxBaseFlags baseFlags)
143 : mConcreteType(concreteType), mBaseFlags(baseFlags) {}
144
145 /**
146 \brief Deserialization constructor setting base flags.
147 */
148 PX_INLINE PxBase(PxBaseFlags baseFlags) : mBaseFlags(baseFlags) {}
149
150 /**
151 \brief Destructor.
152 */
153 virtual ~PxBase() {}
154
155 /**
156 \brief Returns whether a given type name matches with the type of this instance
157 */
158 virtual bool isKindOf(const char* superClass) const { return !strcmp(superClass, "PxBase"); }
159
160 template<class T> bool typeMatch() const
161 {
162 return PxU32(PxTypeInfo<T>::eFastTypeId)!=PxU32(PxConcreteType::eUNDEFINED) ?
163 PxU32(getConcreteType()) == PxU32(PxTypeInfo<T>::eFastTypeId) : isKindOf(PxTypeInfo<T>::name());
164 }
165
166
167private:
168 friend void getBinaryMetaData_PxBase(PxOutputStream& stream);
169
170protected:
171 PxType mConcreteType; // concrete type identifier - see PxConcreteType.
172 PxBaseFlags mBaseFlags; // internal flags
173
174};
175
176#ifndef PX_DOXYGEN
177} // namespace physx
178#endif
179
180/** @} */
181#endif
182