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
11
12
13#ifndef PVD_OBJECT_MODEL_BASE_TYPES_H
14#define PVD_OBJECT_MODEL_BASE_TYPES_H
15#include "foundation/PxSimpleTypes.h"
16#include "foundation/PxAssert.h"
17
18namespace physx { namespace general_shared3 {
19 class PxMat34Legacy;
20}}
21
22namespace physx { namespace debugger {
23 namespace comm {}
24
25 using namespace physx;
26 using namespace general_shared3;
27
28 inline const char* nonNull( const char* str ) { return str ? str : ""; }
29 //strcmp will crash if passed a null string, however,
30 //so we need to make sure that doesn't happen. We do that
31 //by equating NULL and the empty string, "".
32 inline bool safeStrEq( const char* lhs, const char* rhs )
33 {
34 return strcmp( nonNull (lhs), nonNull(rhs) ) == 0;
35 }
36
37 //Does this string have useful information in it.
38 inline bool isMeaningful( const char* str ) { return *(nonNull( str ) ) > 0; }
39
40 inline PxU32 safeStrLen( const char* str )
41 {
42 str = nonNull( str );
43 return static_cast<PxU32>( strlen( str ) );
44 }
45 struct None
46 {
47 };
48
49 template<typename T>
50 class Option
51 {
52 T mValue;
53 bool mHasValue;
54
55 public:
56 Option( const T& val ) : mValue( val ), mHasValue( true ) {}
57 Option( None nothing = None() ) : mHasValue( false ) { (void)nothing; }
58 Option( const Option& other ) : mValue( other.mValue ), mHasValue( other.mHasValue ) {}
59 Option& operator=( const Option& other )
60 {
61 mValue = other.mValue;
62 mHasValue = other.mHasValue;
63 return *this;
64 }
65 bool hasValue() const { return mHasValue; }
66 const T& getValue() const { PX_ASSERT( hasValue() ); return mValue; }
67 T& getValue() { PX_ASSERT( hasValue() ); return mValue; }
68 operator const T& () const { return getValue(); }
69 operator T& () { return getValue(); }
70 T* operator->() { return &getValue(); }
71 const T* operator->() const { return &getValue(); }
72 };
73
74 class NonNegativeInteger
75 {
76 PxI32 mValue;
77 public:
78 NonNegativeInteger( PxI32 val = -1 ) : mValue( val ) {}
79 NonNegativeInteger( const NonNegativeInteger& o ) : mValue( o.mValue ) {}
80 NonNegativeInteger& operator=( const NonNegativeInteger& o ) { mValue = o.mValue; return *this; }
81 bool hasValue() const { return mValue >= 0; }
82 PxI32 getValue() const { PX_ASSERT( hasValue() ); return mValue; }
83 operator PxI32 () const { return getValue(); }
84 PxI32 unsafeGetValue() const { return mValue; }
85 bool operator==( const NonNegativeInteger& other ) { return mValue == other.mValue; }
86 };
87
88 struct PvdBaseType
89 {
90 enum Enum
91 {
92 None = 0,
93 InternalStart = 1,
94 InternalStop = 64,
95#define DECLARE_BASE_PVD_TYPE( type ) type,
96 #include "physxvisualdebuggersdk/PvdObjectModelBaseTypeDefs.h"
97 Last
98#undef DECLARE_BASE_PVD_TYPE
99 };
100 };
101
102 struct ObjectRef
103 {
104 NonNegativeInteger mInstanceId;
105 ObjectRef( NonNegativeInteger iid ) : mInstanceId( iid ) {}
106 ObjectRef( PxI32 iid = -1 ) : mInstanceId( iid ) {}
107 operator NonNegativeInteger() const { return mInstanceId; }
108 operator PxI32 () const { return mInstanceId.getValue(); }
109 bool hasValue() const { return mInstanceId.unsafeGetValue() > 0; }
110 };
111
112 struct U32Array4
113 {
114 PxU32 mD0;
115 PxU32 mD1;
116 PxU32 mD2;
117 PxU32 mD3;
118 U32Array4( PxU32 d0, PxU32 d1, PxU32 d2, PxU32 d3 )
119 : mD0( d0 ), mD1( d1 ), mD2( d2 ), mD3( d3 ) {}
120 U32Array4() : mD0( 0 ), mD1( 0 ), mD2( 0 ), mD3( 0 ) {}
121 };
122
123#define PVD_POINTER_TO_U64( ptr ) static_cast<PxU64>( reinterpret_cast<size_t>( ptr ) )
124#define PVD_U64_TO_POINTER( ptrtype, val ) reinterpret_cast<ptrtype>( static_cast<size_t>( val ) );
125 typedef bool PvdBool;
126 typedef const char* String;
127 typedef void* VoidPtr;
128
129
130
131 struct PvdColor
132 {
133 PxU8 r;
134 PxU8 g;
135 PxU8 b;
136 PxU8 a;
137 PvdColor( PxU8 _r, PxU8 _g, PxU8 _b, PxU8 _a = 255 ) : r( _r ), g( _g ), b( _b ), a( _a ) {}
138 PvdColor() : r( 0 ), g( 0 ), b( 0 ), a( 255 ) {}
139 PvdColor( PxU32 value )
140 {
141 PxU8* valPtr = reinterpret_cast<PxU8*>( &value );
142 r = valPtr[0];
143 g = valPtr[1];
144 b = valPtr[2];
145 a = valPtr[3];
146 }
147 };
148
149 struct StringHandle
150 {
151 PxU32 mHandle;
152 StringHandle( PxU32 val = 0 ) : mHandle( val ) {}
153 operator PxU32 () const { return mHandle; }
154 };
155
156 struct NamespacedName
157 {
158 String mNamespace;
159 String mName;
160 NamespacedName( String ns, String nm ) : mNamespace( ns ), mName( nm ) {}
161 NamespacedName( String nm = "" ) : mNamespace( "" ), mName( nm ) {}
162 bool operator==( const NamespacedName& other ) const
163 {
164 return safeStrEq( mNamespace, other.mNamespace )
165 && safeStrEq( mName, other.mName );
166 }
167 };
168
169
170 struct NamedValue
171 {
172 String mName;
173 PxU32 mValue;
174 NamedValue( String nm = "", PxU32 val = 0 )
175 : mName( nm )
176 , mValue( val )
177 {
178 }
179 };
180
181
182 template<typename T>
183 struct BaseDataTypeToTypeMap
184 {
185 bool compile_error;
186 };
187 template<PvdBaseType::Enum>
188 struct BaseTypeToDataTypeMap
189 {
190 bool compile_error;
191 };
192
193 //Users can extend this mapping with new datatypes.
194 template<typename T>
195 struct PvdDataTypeToNamespacedNameMap
196 {
197 bool Name;
198 };
199 //This mapping tells you the what class id to use for the base datatypes
200
201#define DECLARE_BASE_PVD_TYPE( type ) \
202 template<> struct BaseDataTypeToTypeMap<type> { enum Enum { BaseTypeEnum = PvdBaseType::type }; }; \
203 template<> struct BaseDataTypeToTypeMap<const type&> { enum Enum { BaseTypeEnum = PvdBaseType::type }; }; \
204 template<> struct BaseTypeToDataTypeMap<PvdBaseType::type> { typedef type TDataType; }; \
205 template<> struct PvdDataTypeToNamespacedNameMap<type> { NamespacedName Name; PvdDataTypeToNamespacedNameMap<type>() : Name( "physx3", #type ) {} }; \
206 template<> struct PvdDataTypeToNamespacedNameMap<const type&> { NamespacedName Name; PvdDataTypeToNamespacedNameMap<const type&>() : Name( "physx3", #type ) {} };
207
208 #include "physxvisualdebuggersdk/PvdObjectModelBaseTypeDefs.h"
209#undef DECLARE_BASE_PVD_TYPE
210
211 template< typename TDataType> inline NonNegativeInteger getPvdTypeForType() { return static_cast<PvdBaseType::Enum>(BaseDataTypeToTypeMap<TDataType>::BaseTypeEnum); }
212 template<typename TDataType> inline NamespacedName getPvdNamespacedNameForType() { return PvdDataTypeToNamespacedNameMap<TDataType>().Name; }
213
214#define DEFINE_PVD_TYPE_NAME_MAP( type, ns, name ) \
215 template<> struct PvdDataTypeToNamespacedNameMap<type> { NamespacedName Name; PvdDataTypeToNamespacedNameMap<type>() : Name( ns, name ) {} };
216
217#define DEFINE_PVD_TYPE_ALIAS( newType, oldType ) \
218 template<> struct PvdDataTypeToNamespacedNameMap<newType> { NamespacedName Name; PvdDataTypeToNamespacedNameMap<newType>() : Name( PvdDataTypeToNamespacedNameMap<oldType>().Name ) {} };
219
220DEFINE_PVD_TYPE_ALIAS( const void*, void* )
221
222
223 struct ArrayData
224 {
225 PxU8* mBegin;
226 PxU8* mEnd;
227 PxU8* mCapacity; //>= stop
228 ArrayData( PxU8* beg = NULL, PxU8* end = NULL, PxU8* cap = NULL )
229 : mBegin(beg )
230 , mEnd( end )
231 , mCapacity( cap )
232 {}
233 PxU8* begin() { return mBegin; }
234 PxU8* end() { return mEnd; }
235 PxU32 byteCapacity() { return static_cast<PxU32>( mCapacity - mBegin ); }
236 PxU32 byteSize() const { return static_cast<PxU32>( mEnd - mBegin ); } //in bytes
237 PxU32 numberOfItems( PxU32 objectByteSize ) { if ( objectByteSize ) return byteSize() / objectByteSize; return 0; }
238
239 void forgetData() { mBegin = mEnd = mCapacity = 0; }
240 };
241
242 template<typename TItemType>
243 struct PvdScopedItem
244 {
245 private:
246 PvdScopedItem( const PvdScopedItem& );
247 PvdScopedItem& operator=( const PvdScopedItem& );
248 public:
249
250 TItemType* mItem;
251 PvdScopedItem( TItemType& item ) : mItem( &item ) {}
252 PvdScopedItem( TItemType* item ) : mItem( item ) {}
253 ~PvdScopedItem() { if ( mItem ) mItem->release(); }
254 TItemType* operator->() { PX_ASSERT( mItem ); return mItem; }
255 TItemType& operator*() { PX_ASSERT( mItem ); return *mItem; }
256 operator TItemType* () { return mItem; }
257 operator const TItemType* () const { return mItem; }
258 };
259
260
261 template<typename T>
262 class DataRef
263 {
264 const T* mBegin;
265 const T* mEnd;
266 public:
267 DataRef( const T* b, PxU32 count ) : mBegin( b ), mEnd( b + count ) {}
268 DataRef( const T* b = NULL, const T* e = NULL ) : mBegin( b ), mEnd( e ) {}
269 DataRef( const DataRef& o ) : mBegin( o.mBegin ), mEnd( o.mEnd ) {}
270 DataRef& operator=( const DataRef& o ) { mBegin = o.mBegin; mEnd = o.mEnd; return *this; }
271 PxU32 size() const { return static_cast<PxU32>( mEnd - mBegin ); }
272 const T* begin() const { return mBegin; }
273 const T* end() const { return mEnd; }
274 const T& operator[]( PxU32 idx ) const { PX_ASSERT( idx < size() ); return mBegin[idx]; }
275 const T& back() const { PX_ASSERT( mEnd > mBegin ); return *(mEnd - 1); }
276 };
277
278 inline PxU64 toPaddedSize( PxU64 inOriginal, PxU32 inPageSize = 0x1000 )
279 {
280 return (inOriginal + inPageSize) - inOriginal % inPageSize;
281 }
282
283 template<PxU32 TSize>
284 struct Union
285 {
286 PxU8 mData[TSize];
287 Union(){}
288 template<typename TDataType>
289 void set( const TDataType& inValue ) { PX_COMPILE_TIME_ASSERT( sizeof( TDataType ) <= TSize ); new (mData) TDataType( inValue ); }
290 template<typename TDataType>
291 TDataType get() const { PX_COMPILE_TIME_ASSERT( sizeof( TDataType ) <= TSize ); return *reinterpret_cast<const TDataType*>( mData ); }
292 };
293
294 struct PropertyType
295 {
296 enum Enum
297 {
298 Unknown = 0,
299 Scalar,
300 Array
301 };
302 };
303
304 template<typename TObjType>
305 struct PvdRefPtr
306 {
307 mutable TObjType* mObj;
308 ~PvdRefPtr()
309 {
310 release();
311 }
312 PvdRefPtr( TObjType* obj = NULL ) : mObj( obj )
313 {
314 addRef();
315 }
316 PvdRefPtr( const PvdRefPtr<TObjType>& other )
317 {
318 mObj = other.mObj;
319 addRef();
320 }
321 PvdRefPtr<TObjType>& operator=( const PvdRefPtr<TObjType>& other )
322 {
323 if ( mObj != other.mObj )
324 release();
325 mObj = other.mObj;
326 addRef();
327 return *this;
328 }
329 void addRef() { if ( mObj ) mObj->addRef(); }
330 void release() { if ( mObj ) mObj->release(); }
331 operator TObjType* () { return mObj; }
332 operator const TObjType* () const { return mObj; }
333
334 TObjType* operator->() { PX_ASSERT( mObj ); return mObj; }
335 const TObjType* operator->() const { PX_ASSERT( mObj ); return mObj; }
336
337 TObjType& operator*() { PX_ASSERT( mObj ); return *mObj; }
338 const TObjType& operator*() const { PX_ASSERT( mObj ); return *mObj; }
339 };
340}}
341
342
343#endif
344