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