typetraits.h
Engine/source/platform/typetraits.h
Template definitions for introspecting type properties.
Classes:
class
class
class
class
class
class
class
class
class
Delete an array of objects policy.
class
Delete a single object policy.
class
class
class
class
class
class
class
class
class
class
class
class
class
class
class
class
class
class
Public Functions
constructArray(T * ptr, U32 num)
constructArray(T * ptr, U32 num, A a)
constructSingle(A a)
constructSingle(A a, B b)
destructArray(T * ptr, U32 num)
destructSingle(T & val)
bool
bool
bool
bool
Detailed Description
Template definitions for introspecting type properties.
Public Functions
constructArray(T * ptr, U32 num)
constructArray(T * ptr, U32 num, A a)
constructSingle()
constructSingle(A a)
constructSingle(A a, B b)
Deref(T & val)
Deref(T * ptr)
destructArray(T * ptr, U32 num)
destructSingle(T & val)
IsFalseType()
IsFalseType< FalseType>()
IsTrueType()
IsTrueType< TrueType>()
1 2//----------------------------------------------------------------------------- 3// Copyright (c) 2012 GarageGames, LLC 4// 5// Permission is hereby granted, free of charge, to any person obtaining a copy 6// of this software and associated documentation files (the "Software"), to 7// deal in the Software without restriction, including without limitation the 8// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 9// sell copies of the Software, and to permit persons to whom the Software is 10// furnished to do so, subject to the following conditions: 11// 12// The above copyright notice and this permission notice shall be included in 13// all copies or substantial portions of the Software. 14// 15// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 20// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 21// IN THE SOFTWARE. 22//----------------------------------------------------------------------------- 23 24#ifndef _TYPETRAITS_H_ 25#define _TYPETRAITS_H_ 26 27#ifndef _PLATFORM_H_ 28# include "platform/platform.h" 29#endif 30 31 32/// @file 33/// Template definitions for introspecting type properties. 34 35 36//-------------------------------------------------------------------------- 37// Type Predicating. 38//-------------------------------------------------------------------------- 39 40struct TrueType {}; 41struct FalseType {}; 42 43template< typename T > 44inline bool IsTrueType() 45{ 46 return false; 47} 48template<> 49inline bool IsTrueType< TrueType>() 50{ 51 return true; 52} 53 54template< typename T > 55inline bool IsFalseType() 56{ 57 return false; 58} 59template<> 60inline bool IsFalseType< FalseType>() 61{ 62 return true; 63} 64 65 66template< typename T, typename IfTrue, typename IfFalse > 67struct IfTrueType : public IfFalse {}; 68template< typename IfTrue, typename IfFalse > 69struct IfTrueType< TrueType, IfTrue, IfFalse > : public IfTrue {}; 70 71template< typename T, typename IfTrue, typename IfFalse > 72struct IfFalseType : public IfTrue {}; 73template< typename IfTrue, typename IfFalse > 74struct IfFalseType< FalseType, IfTrue, IfFalse > : public IfFalse {}; 75 76//-------------------------------------------------------------------------- 77// Construct. 78//-------------------------------------------------------------------------- 79 80struct _ConstructDefault 81{ 82 template< typename T > 83 static T single() 84 { 85 return T(); 86 } 87 template< typename T, typename A > 88 static T single( A a ) 89 { 90 return T( a ); 91 } 92 template< typename T, typename A, typename B > 93 static T single( A a, B b ) 94 { 95 return T( a, b ); 96 } 97 template< typename T > 98 static void array( T* ptr, U32 num ) 99 { 100 constructArrayInPlace< T >( ptr, num ); 101 } 102 template< typename T, typename A > 103 static void array( T* ptr, U32 num, A a ) 104 { 105 for( U32 i = 0; i < num; ++ i ) 106 ptr[ i ] = single< T >( a ); 107 } 108}; 109struct _ConstructPrim 110{ 111 template< typename T > 112 static T single() 113 { 114 return 0; 115 } 116 template< typename T, typename A > 117 static T single( T a ) 118 { 119 return a; 120 } 121 template< typename T > 122 static void array( T* ptr, U32 num ) 123 { 124 dMemset( ptr, 0, num * sizeof( T ) ); 125 } 126 template< typename T, typename A > 127 static void array( T* ptr, U32 num, T a ) 128 { 129 for( U32 i = 0; i < num; ++ i ) 130 ptr[ i ] = a; 131 } 132}; 133struct _ConstructPtr 134{ 135 template< typename T > 136 static T* single() 137 { 138 return new T; 139 } 140 template< typename T, typename A > 141 static T* single( A a ) 142 { 143 return new T( a ); 144 } 145 template< typename T, typename A, typename B > 146 static T* single( A a, B b ) 147 { 148 return new T( a, b ); 149 } 150 template< typename T > 151 static void array( T** ptr, U32 num ) 152 { 153 for( U32 i = 0; i < num; ++ i ) 154 ptr[ i ] = single< T >(); 155 } 156 template< typename T, typename A > 157 static void array( T** ptr, U32 num, A a ) 158 { 159 for( U32 i = 0; i < num; ++ i ) 160 ptr[ i ] = single< T >( a ); 161 } 162}; 163 164//-------------------------------------------------------------------------- 165// Destruct. 166//-------------------------------------------------------------------------- 167 168struct _DestructDefault 169{ 170 template< typename T > 171 static void single( T& val ) 172 { 173 val.~T(); 174 } 175 template< typename T > 176 static void array( T* ptr, U32 num ) 177 { 178 for( U32 i = 0; i < num; ++ i ) 179 single< T >( ptr[ i ] ); 180 } 181}; 182struct _DestructPrim 183{ 184 template< typename T > 185 static void single( T& val ) {} 186 template< typename T > 187 static void array( T* ptr, U32 num ) {} 188}; 189struct _DestructPtr 190{ 191 template< typename T > 192 static void single( T*& val ) 193 { 194 delete val; 195 val = NULL; 196 } 197 template< typename T > 198 static void array( T* ptr, U32 num ) 199 { 200 for( U32 i = 0; i < num; ++ i ) 201 single< T >( ptr[ i ] ); 202 } 203}; 204 205//-------------------------------------------------------------------------- 206// TypeTraits. 207//-------------------------------------------------------------------------- 208 209template< typename T > 210struct _TypeTraits 211{ 212 typedef T BaseType; 213 typedef const T ConstType; 214 typedef _ConstructDefault Construct; 215 typedef _DestructDefault Destruct; 216}; 217template< typename T > 218struct _TypeTraits< T* > 219{ 220 typedef T BaseType; 221 typedef const T ConstType; 222 typedef _ConstructPtr Construct; 223 typedef _DestructPtr Destruct; 224 225 template< typename A > 226 static bool isTaggedPtr( A* ptr ) { return ( uintptr_t( ptr ) & 0x1 ); } 227 template< typename A > 228 static A* getTaggedPtr( A* ptr ) { return ( A* ) ( uintptr_t( ptr ) | 0x1 ); } 229 template< typename A > 230 static A* getUntaggedPtr( A* ptr ) { return ( A* ) ( uintptr_t( ptr ) & (~0x1) ); } 231}; 232 233template< typename T > 234struct TypeTraits : public TypeTraits< typename T::Parent > 235{ 236 typedef T BaseType; 237 typedef const T ConstType; 238}; 239template< typename T > 240struct TypeTraits< T* > : public TypeTraits< typename T::Parent*> 241{ 242 typedef T BaseType; 243 typedef const T ConstType; 244}; 245template< typename T > 246struct TypeTraits< T* const > : public TypeTraits< typename T::Parent*> 247{ 248 typedef T BaseType; 249 typedef const T ConstType; 250}; 251template<> 252struct TypeTraits< void > : public _TypeTraits< void > {}; 253template<> 254struct TypeTraits< void* > : public _TypeTraits< void*> {}; 255template<> 256struct TypeTraits< void* const > : public _TypeTraits< void*> {}; 257 258// Type traits for primitive types. 259 260template<> 261struct TypeTraits< bool > : public _TypeTraits< bool > 262{ 263 typedef _ConstructPrim Construct; 264 typedef _DestructPrim Destruct; 265}; 266template<> 267struct TypeTraits< S8 > : public _TypeTraits< S8 > 268{ 269 static const S8 MIN = S8_MIN; 270 static const S8 MAX = S8_MAX; 271 static const S8 ZERO = 0; 272 typedef _ConstructPrim Construct; 273 typedef _DestructPrim Destruct; 274}; 275template<> 276struct TypeTraits< U8 > : public _TypeTraits< U8 > 277{ 278 static const U8 MIN = 0; 279 static const U8 MAX = U8_MAX; 280 static const U8 ZERO = 0; 281 typedef _ConstructPrim Construct; 282 typedef _DestructPrim Destruct; 283}; 284template<> 285struct TypeTraits< S16 > : public _TypeTraits< S16 > 286{ 287 static const S16 MIN = S16_MIN; 288 static const S16 MAX = S16_MAX; 289 static const S16 ZERO = 0; 290 typedef _ConstructPrim Construct; 291 typedef _DestructPrim Destruct; 292}; 293template<> 294struct TypeTraits< U16 > : public _TypeTraits< U16 > 295{ 296 static const U16 MIN = 0; 297 static const U16 MAX = U16_MAX; 298 static const U16 ZERO = 0; 299 typedef _ConstructPrim Construct; 300 typedef _DestructPrim Destruct; 301}; 302template<> 303struct TypeTraits< S32 > : public _TypeTraits< S32 > 304{ 305 static const S32 MIN = S32_MIN; 306 static const S32 MAX = S32_MAX; 307 static const S32 ZERO = 0; 308 typedef _ConstructPrim Construct; 309 typedef _DestructPrim Destruct; 310}; 311template<> 312struct TypeTraits< U32 > : public _TypeTraits< U32 > 313{ 314 static const U32 MIN = 0; 315 static const U32 MAX = U32_MAX; 316 static const U32 ZERO = 0; 317 typedef _ConstructPrim Construct; 318 typedef _DestructPrim Destruct; 319}; 320template<> 321struct TypeTraits< F32 > : public _TypeTraits< F32 > 322{ 323 static const F32 MIN; 324 static const F32 MAX; 325 static const F32 ZERO; 326 typedef _ConstructPrim Construct; 327 typedef _DestructPrim Destruct; 328}; 329 330//-------------------------------------------------------------------------- 331// Utilities. 332//-------------------------------------------------------------------------- 333 334template< typename T > 335inline T constructSingle() 336{ 337 typedef typename TypeTraits< T >::BaseType Type; 338 typedef typename TypeTraits< T >::Construct Construct; 339 return Construct::template single< Type >(); 340} 341template< typename T, typename A > 342inline T constructSingle( A a ) 343{ 344 typedef typename TypeTraits< T >::BaseType BaseType; 345 typedef typename TypeTraits< T >::Construct Construct; 346 return Construct::template single< BaseType >( a ); 347} 348template< typename T, typename A, typename B > 349inline T constructSingle( A a, B b ) 350{ 351 typedef typename TypeTraits< T >::BaseType BaseType; 352 typedef typename TypeTraits< T >::Construct Construct; 353 return Construct::template single< BaseType >( a, b ); 354} 355template< typename T > 356inline void constructArray( T* ptr, U32 num ) 357{ 358 typedef typename TypeTraits< T >::BaseType BaseType; 359 typedef typename TypeTraits< T >::Construct Construct; 360 Construct::template array< BaseType >( ptr, num ); 361} 362template< typename T, typename A > 363inline void constructArray( T* ptr, U32 num, A a ) 364{ 365 typedef typename TypeTraits< T >::BaseType BaseType; 366 typedef typename TypeTraits< T >::Construct Construct; 367 Construct::template array< BaseType >( ptr, num, a ); 368} 369template< typename T > 370inline void destructSingle( T& val ) 371{ 372 typedef typename TypeTraits< T >::BaseType BaseType; 373 typedef typename TypeTraits< T >::Destruct Destruct; 374 Destruct::template single< BaseType >( val ); 375} 376template< typename T > 377inline void destructArray( T* ptr, U32 num ) 378{ 379 typedef typename TypeTraits< T >::BaseType BaseType; 380 typedef typename TypeTraits< T >::Destruct Destruct; 381 Destruct::template array< BaseType >( ptr, num ); 382} 383 384template< typename T> 385inline T& Deref( T& val ) 386{ 387 return val; 388} 389template< typename T > 390inline T& Deref( T* ptr ) 391{ 392 return *ptr; 393} 394 395/// Delete a single object policy. 396struct DeleteSingle 397{ 398 template<class T> 399 static void destroy(T *ptr) { delete ptr; } 400}; 401 402/// Delete an array of objects policy. 403struct DeleteArray 404{ 405 template<class T> 406 static void destroy(T *ptr) { delete [] ptr; } 407}; 408 409/// 410template< typename T > 411struct ValueHolder 412{ 413 T value; 414 415 ValueHolder( const T& value ) 416 : value( value ) {} 417 418 operator T() const { return value; } 419}; 420template<> 421struct ValueHolder< void > 422{ 423 ValueHolder() {} 424}; 425 426#endif // _TYPETRAITS_H_ 427