better support of types with const/volatile qualifiers, thanks to Kalle Rutanen

git-svn-id: svn://svn.code.sf.net/p/loki-lib/code/trunk@208 7ec92016-0320-0410-acc4-a06ded1c099a
This commit is contained in:
syntheticpp 2005-08-26 14:02:28 +00:00
parent 79abaf3ed9
commit 41536265e8

View file

@ -64,12 +64,14 @@ namespace Loki
namespace Private namespace Private
{ {
typedef TYPELIST_4(unsigned char, unsigned short int, typedef TYPELIST_4(unsigned char, unsigned short int,unsigned int, unsigned long int)
unsigned int, unsigned long int) StdUnsignedInts; StdUnsignedInts;
typedef TYPELIST_4(signed char, short int, typedef TYPELIST_4(signed char, short int,int, long int)
int, long int) StdSignedInts; StdSignedInts;
typedef TYPELIST_3(bool, char, wchar_t) StdOtherInts; typedef TYPELIST_3(bool, char, wchar_t)
typedef TYPELIST_3(float, double, long double) StdFloats; StdOtherInts;
typedef TYPELIST_3(float, double, long double)
StdFloats;
template <class U> struct AddReference template <class U> struct AddReference
{ {
@ -89,55 +91,38 @@ namespace Loki
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
// class template TypeTraits // class template TypeTraits
// Figures out various properties of any given type //
// Invocations (T is a type): // Figures out at compile time various properties of any given type
// a) TypeTraits<T>::isPointer // Invocations (T is a type, TypeTraits<T>::Propertie):
// returns (at compile time) true if T is a pointer type //
// b) TypeTraits<T>::PointeeType // a) isPointer : returns true if T is a pointer type
// returns the type to which T points is T is a pointer type, NullType otherwise // b) PointeeType : returns the type to which T points if T is a pointer
// a) TypeTraits<T>::isReference // type, NullType otherwise
// returns (at compile time) true if T is a reference type // a) isReference : returns true if T is a reference type
// b) TypeTraits<T>::ReferredType // b) ReferredType : returns the type to which T refers if T is a reference
// returns the type to which T refers is T is a reference type, NullType // type, NullType otherwise
// otherwise // c) isMemberPointer : returns true if T is a pointer to member type
// c) TypeTraits<T>::isMemberPointer // d) isStdUnsignedInt: returns true if T is a standard unsigned integral type
// returns (at compile time) true if T is a pointer to member type // e) isStdSignedInt : returns true if T is a standard signed integral type
// d) TypeTraits<T>::isStdUnsignedInt // f) isStdIntegral : returns true if T is a standard integral type
// returns (at compile time) true if T is a standard unsigned integral type // g) isStdFloat : returns true if T is a standard floating-point type
// e) TypeTraits<T>::isStdSignedInt // h) isStdArith : returns true if T is a standard arithmetic type
// returns (at compile time) true if T is a standard signed integral type // i) isStdFundamental: returns true if T is a standard fundamental type
// f) TypeTraits<T>::isStdIntegral // j) isUnsignedInt : returns true if T is a unsigned integral type
// returns (at compile time) true if T is a standard integral type // k) isSignedInt : returns true if T is a signed integral type
// g) TypeTraits<T>::isStdFloat // l) isIntegral : returns true if T is a integral type
// returns (at compile time) true if T is a standard floating-point type // m) isFloat : returns true if T is a floating-point type
// h) TypeTraits<T>::isStdArith // n) isArith : returns true if T is a arithmetic type
// returns (at compile time) true if T is a standard arithmetic type // o) isFundamental : returns true if T is a fundamental type
// i) TypeTraits<T>::isStdFundamental // p) ParameterType : returns the optimal type to be used as a parameter for
// returns (at compile time) true if T is a standard fundamental type // functions that take Ts
// j) TypeTraits<T>::isUnsignedInt // q) isConst : returns true if T is a const-qualified type
// returns (at compile time) true if T is a unsigned integral type // r) NonConstType : Type with removed 'const' qualifier from T, if any
// k) TypeTraits<T>::isSignedInt // s) isVolatile : returns true if T is a volatile-qualified type
// returns (at compile time) true if T is a signed integral type // t) NonVolatileType : Type with removed 'volatile' qualifier from T, if any
// l) TypeTraits<T>::isIntegral // u) UnqualifiedType : Type with removed 'const' and 'volatile' qualifiers from
// returns (at compile time) true if T is a integral type // T, if any
// m) TypeTraits<T>::isFloat //
// returns (at compile time) true if T is a floating-point type
// n) TypeTraits<T>::isArith
// returns (at compile time) true if T is a arithmetic type
// o) TypeTraits<T>::isFundamental
// returns (at compile time) true if T is a fundamental type
// p) TypeTraits<T>::ParameterType
// returns the optimal type to be used as a parameter for functions that take Ts
// q) TypeTraits<T>::isConst
// returns (at compile time) true if T is a const-qualified type
// r) TypeTraits<T>::NonConstType
// removes the 'const' qualifier from T, if any
// s) TypeTraits<T>::isVolatile
// returns (at compile time) true if T is a volatile-qualified type
// t) TypeTraits<T>::NonVolatileType
// removes the 'volatile' qualifier from T, if any
// u) TypeTraits<T>::UnqualifiedType
// removes both the 'const' and 'volatile' qualifiers from T, if any
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
template <typename T> template <typename T>
@ -173,8 +158,7 @@ namespace Loki
enum { result = false }; enum { result = false };
}; };
template <class U, class V> template <class U, class V> struct PToMTraits<U V::*>
struct PToMTraits<U V::*>
{ {
enum { result = true }; enum { result = true };
}; };
@ -191,6 +175,12 @@ namespace Loki
enum { isConst = 1 }; enum { isConst = 1 };
}; };
template <class U> struct UnConst<const U&>
{
typedef U& Result;
enum { isConst = 1 };
};
template <class U> struct UnVolatile template <class U> struct UnVolatile
{ {
typedef U Result; typedef U Result;
@ -203,50 +193,59 @@ namespace Loki
enum { isVolatile = 1 }; enum { isVolatile = 1 };
}; };
public: template <class U> struct UnVolatile<volatile U&>
enum { isPointer = PointerTraits<T>::result }; {
typedef typename PointerTraits<T>::PointeeType PointeeType; typedef U& Result;
enum { isVolatile = 1 };
};
enum { isReference = ReferenceTraits<T>::result }; public:
typedef typename ReferenceTraits<T>::ReferredType ReferredType; typedef typename UnConst<T>::Result
NonConstType;
enum { isMemberPointer = PToMTraits<T>::result }; typedef typename UnVolatile<T>::Result
NonVolatileType;
enum { isStdUnsignedInt =
TL::IndexOf<Private::StdUnsignedInts, T>::value >= 0 };
enum { isStdSignedInt =
TL::IndexOf<Private::StdSignedInts, T>::value >= 0 };
enum { isStdIntegral = isStdUnsignedInt || isStdSignedInt ||
TL::IndexOf<Private::StdOtherInts, T>::value >= 0 };
enum { isStdFloat = TL::IndexOf<Private::StdFloats, T>::value >= 0 };
enum { isStdArith = isStdIntegral || isStdFloat };
enum { isStdFundamental = isStdArith || isStdFloat ||
Conversion<T, void>::sameType };
enum { isUnsignedInt = isStdUnsignedInt || IsCustomUnsignedInt<T>::value };
enum { isSignedInt = isStdSignedInt || IsCustomSignedInt<T>::value };
enum { isIntegral = isStdIntegral || isUnsignedInt || isSignedInt };
enum { isFloat = isStdFloat || IsCustomFloat<T>::value };
enum { isArith = isIntegral || isFloat };
enum { isFundamental = isStdFundamental || isArith || isFloat };
typedef typename Select<isStdArith || isPointer || isMemberPointer,
T, typename Private::AddReference<T>::Result>::Result ParameterType;
enum { isConst = UnConst<T>::isConst };
typedef typename UnConst<T>::Result NonConstType;
enum { isVolatile = UnVolatile<T>::isVolatile };
typedef typename UnVolatile<T>::Result NonVolatileType;
typedef typename UnVolatile<typename UnConst<T>::Result>::Result typedef typename UnVolatile<typename UnConst<T>::Result>::Result
UnqualifiedType; UnqualifiedType;
typedef typename PointerTraits<T>::PointeeType
PointeeType;
typedef typename ReferenceTraits<T>::ReferredType
ReferredType;
enum { isConst = UnConst<T>::isConst };
enum { isVolatile = UnVolatile<T>::isVolatile };
enum { isPointer = PointerTraits<UnqualifiedType>::result };
enum { isReference = ReferenceTraits<UnqualifiedType>::result };
enum { isMemberPointer = PToMTraits<UnqualifiedType>::result };
enum { isStdUnsignedInt = TL::IndexOf<Private::StdUnsignedInts, UnqualifiedType>::value>= 0 };
enum { isStdSignedInt = TL::IndexOf<Private::StdSignedInts, UnqualifiedType>::value>= 0 };
enum { isStdIntegral = isStdUnsignedInt || isStdSignedInt || TL::IndexOf<Private::StdOtherInts,
UnqualifiedType>::value >= 0 };
enum { isStdFloat = TL::IndexOf<Private::StdFloats, UnqualifiedType>::value>= 0 };
enum { isStdArith = isStdIntegral || isStdFloat };
enum { isStdFundamental = isStdArith || isStdFloat || Conversion<T, void>::sameType };
enum { isUnsignedInt = isStdUnsignedInt || IsCustomUnsignedInt<UnqualifiedType>::value };
enum { isSignedInt = isStdSignedInt || IsCustomSignedInt<UnqualifiedType>::value };
enum { isIntegral = isStdIntegral || isUnsignedInt || isSignedInt };
enum { isFloat = isStdFloat || IsCustomFloat<UnqualifiedType>::value };
enum { isArith = isIntegral || isFloat };
enum { isFundamental = isStdFundamental || isArith || isFloat };
typedef typename Select<isStdArith || isPointer || isMemberPointer,T,
typename Private::AddReference<T>::Result>::Result
ParameterType;
}; };
} }
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
// Change log: // Change log:
// June 20, 2001: ported by Nick Thurn to gcc 2.95.3. Kudos, Nick!!! // June 20, 2001: ported by Nick Thurn to gcc 2.95.3. Kudos, Nick!!!
// September 16, 2002: ParameterType fixed, as TypeTraits<void> made // September 16, 2002: ParameterType fixed, as TypeTraits<void> made
// ParameterType give error about reference to void. T.S. // ParameterType give error about reference to void. T.S.
// August 26, 2005: better support of types with const/volatile qualifiers,
// thanks to Kalle Rutanen
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
#endif // TYPETRAITS_INC_ #endif // TYPETRAITS_INC_