[3332] | 1 | //////////////////////////////////////////////////////////////////////////////// |
---|
| 2 | // The Loki Library |
---|
| 3 | // Copyright (c) 2001 by Andrei Alexandrescu |
---|
| 4 | // This code accompanies the book: |
---|
[7266] | 5 | // Alexandrescu, Andrei. "Modern C++ Design: Generic Programming and Design |
---|
[3332] | 6 | // Patterns Applied". Copyright (c) 2001. Addison-Wesley. |
---|
[7266] | 7 | // Permission to use, copy, modify, distribute and sell this software for any |
---|
| 8 | // purpose is hereby granted without fee, provided that the above copyright |
---|
| 9 | // notice appear in all copies and that both that copyright notice and this |
---|
[3332] | 10 | // permission notice appear in supporting documentation. |
---|
[7266] | 11 | // The author or Addison-Wesley Longman make no representations about the |
---|
| 12 | // suitability of this software for any purpose. It is provided "as is" |
---|
[3332] | 13 | // without express or implied warranty. |
---|
| 14 | // |
---|
[7266] | 15 | // Changes by Orxonox (Reto Grieder) |
---|
[3332] | 16 | // Removed all stdInt, etc. type traits and function pointer traits |
---|
| 17 | // and added UnqualifiedReferredType. |
---|
| 18 | //////////////////////////////////////////////////////////////////////////////// |
---|
| 19 | #ifndef LOKI_TYPETRAITS_INC_ |
---|
| 20 | #define LOKI_TYPETRAITS_INC_ |
---|
| 21 | |
---|
[7266] | 22 | // $Id: TypeTraits.h 1069 2010-04-19 03:09:59Z rich_sposato $ |
---|
[3332] | 23 | |
---|
| 24 | |
---|
[7266] | 25 | #include <loki/NullType.h> |
---|
| 26 | |
---|
[3332] | 27 | #if (defined _MSC_VER) && (_MSC_VER < 1400) |
---|
| 28 | #include <string> |
---|
| 29 | #endif |
---|
| 30 | |
---|
| 31 | |
---|
| 32 | #ifdef _MSC_VER |
---|
[7266] | 33 | #pragma warning( push ) |
---|
[3332] | 34 | #pragma warning( disable : 4180 ) //qualifier applied to function type has no meaning; ignored |
---|
| 35 | #endif |
---|
| 36 | |
---|
| 37 | namespace Loki |
---|
| 38 | { |
---|
| 39 | //////////////////////////////////////////////////////////////////////////////// |
---|
| 40 | // class template IsCustomUnsignedInt |
---|
| 41 | // Offers a means to integrate nonstandard built-in unsigned integral types |
---|
[7266] | 42 | // (such as unsigned __int64 or unsigned long long int) with the TypeTraits |
---|
[3332] | 43 | // class template defined below. |
---|
| 44 | // Invocation: IsCustomUnsignedInt<T> where T is any type |
---|
| 45 | // Defines 'value', an enum that is 1 iff T is a custom built-in unsigned |
---|
| 46 | // integral type |
---|
| 47 | // Specialize this class template for nonstandard unsigned integral types |
---|
| 48 | // and define value = 1 in those specializations |
---|
| 49 | //////////////////////////////////////////////////////////////////////////////// |
---|
| 50 | |
---|
| 51 | template <typename T> |
---|
| 52 | struct IsCustomUnsignedInt |
---|
| 53 | { |
---|
| 54 | enum { value = 0 }; |
---|
[7266] | 55 | }; |
---|
[3332] | 56 | |
---|
| 57 | //////////////////////////////////////////////////////////////////////////////// |
---|
| 58 | // class template IsCustomSignedInt |
---|
| 59 | // Offers a means to integrate nonstandard built-in unsigned integral types |
---|
[7266] | 60 | // (such as unsigned __int64 or unsigned long long int) with the TypeTraits |
---|
[3332] | 61 | // class template defined below. |
---|
| 62 | // Invocation: IsCustomSignedInt<T> where T is any type |
---|
| 63 | // Defines 'value', an enum that is 1 iff T is a custom built-in signed |
---|
| 64 | // integral type |
---|
| 65 | // Specialize this class template for nonstandard unsigned integral types |
---|
| 66 | // and define value = 1 in those specializations |
---|
| 67 | //////////////////////////////////////////////////////////////////////////////// |
---|
| 68 | |
---|
| 69 | template <typename T> |
---|
| 70 | struct IsCustomSignedInt |
---|
| 71 | { |
---|
| 72 | enum { value = 0 }; |
---|
[7266] | 73 | }; |
---|
[3332] | 74 | |
---|
| 75 | //////////////////////////////////////////////////////////////////////////////// |
---|
| 76 | // class template IsCustomFloat |
---|
| 77 | // Offers a means to integrate nonstandard floating point types with the |
---|
| 78 | // TypeTraits class template defined below. |
---|
| 79 | // Invocation: IsCustomFloat<T> where T is any type |
---|
| 80 | // Defines 'value', an enum that is 1 iff T is a custom built-in |
---|
| 81 | // floating point type |
---|
| 82 | // Specialize this class template for nonstandard unsigned integral types |
---|
| 83 | // and define value = 1 in those specializations |
---|
| 84 | //////////////////////////////////////////////////////////////////////////////// |
---|
| 85 | |
---|
| 86 | template <typename T> |
---|
| 87 | struct IsCustomFloat |
---|
| 88 | { |
---|
| 89 | enum { value = 0 }; |
---|
[7266] | 90 | }; |
---|
[3332] | 91 | |
---|
| 92 | //////////////////////////////////////////////////////////////////////////////// |
---|
| 93 | // Helper types for class template TypeTraits defined below |
---|
| 94 | //////////////////////////////////////////////////////////////////////////////// |
---|
| 95 | |
---|
| 96 | namespace Private |
---|
| 97 | { |
---|
| 98 | template <typename U> struct AddPointer |
---|
| 99 | { |
---|
| 100 | typedef U* Result; |
---|
| 101 | }; |
---|
| 102 | |
---|
| 103 | template <typename U> struct AddPointer<U&> |
---|
| 104 | { |
---|
| 105 | typedef U* Result; |
---|
| 106 | }; |
---|
| 107 | |
---|
| 108 | template <class U> struct AddReference |
---|
| 109 | { |
---|
| 110 | typedef U & Result; |
---|
| 111 | }; |
---|
| 112 | |
---|
| 113 | template <class U> struct AddReference<U &> |
---|
| 114 | { |
---|
| 115 | typedef U & Result; |
---|
| 116 | }; |
---|
| 117 | |
---|
| 118 | template <> struct AddReference<void> |
---|
| 119 | { |
---|
| 120 | typedef NullType Result; |
---|
| 121 | }; |
---|
| 122 | |
---|
| 123 | template <class U> struct AddParameterType |
---|
| 124 | { |
---|
| 125 | typedef const U & Result; |
---|
| 126 | }; |
---|
| 127 | |
---|
| 128 | template <class U> struct AddParameterType<U &> |
---|
| 129 | { |
---|
| 130 | typedef U & Result; |
---|
| 131 | }; |
---|
| 132 | |
---|
| 133 | template <> struct AddParameterType<void> |
---|
| 134 | { |
---|
| 135 | typedef NullType Result; |
---|
| 136 | }; |
---|
| 137 | }// namespace Private |
---|
[7266] | 138 | |
---|
[3332] | 139 | //////////////////////////////////////////////////////////////////////////////// |
---|
| 140 | // class template TypeTraits |
---|
| 141 | // |
---|
| 142 | // Figures out at compile time various properties of any given type |
---|
| 143 | // Invocations (T is a type, TypeTraits<T>::Property): |
---|
| 144 | // |
---|
| 145 | // - isPointer : returns true if T is a pointer type |
---|
[7266] | 146 | // - PointeeType : returns the type to which T points if T is a pointer |
---|
[3332] | 147 | // type, NullType otherwise |
---|
| 148 | // - isReference : returns true if T is a reference type |
---|
[7266] | 149 | // - ReferredType : returns the type to which T refers if T is a reference |
---|
[3332] | 150 | // type, NullType otherwise |
---|
[7266] | 151 | // - ParameterType : returns the optimal type to be used as a parameter for |
---|
[3332] | 152 | // functions that take Ts |
---|
| 153 | // - isConst : returns true if T is a const-qualified type |
---|
| 154 | // - NonConstType : Type with removed 'const' qualifier from T, if any |
---|
| 155 | // - isVolatile : returns true if T is a volatile-qualified type |
---|
| 156 | // - NonVolatileType : Type with removed 'volatile' qualifier from T, if any |
---|
[7266] | 157 | // - UnqualifiedType : Type with removed 'const' and 'volatile' qualifiers from |
---|
[3332] | 158 | // T, if any |
---|
[7266] | 159 | // - ParameterType : returns the optimal type to be used as a parameter |
---|
[3332] | 160 | // for functions that take 'const T's |
---|
| 161 | // |
---|
| 162 | //////////////////////////////////////////////////////////////////////////////// |
---|
| 163 | |
---|
| 164 | template <typename T> |
---|
| 165 | class TypeTraits |
---|
| 166 | { |
---|
| 167 | private: |
---|
[7266] | 168 | |
---|
[3332] | 169 | template <class U> struct ReferenceTraits |
---|
| 170 | { |
---|
| 171 | enum { result = false }; |
---|
| 172 | typedef U ReferredType; |
---|
| 173 | }; |
---|
[7266] | 174 | |
---|
[3332] | 175 | template <class U> struct ReferenceTraits<U&> |
---|
| 176 | { |
---|
| 177 | enum { result = true }; |
---|
| 178 | typedef U ReferredType; |
---|
| 179 | }; |
---|
[7266] | 180 | |
---|
[3332] | 181 | template <class U> struct PointerTraits |
---|
| 182 | { |
---|
| 183 | enum { result = false }; |
---|
| 184 | typedef NullType PointeeType; |
---|
| 185 | }; |
---|
[7266] | 186 | |
---|
[3332] | 187 | template <class U> struct PointerTraits<U*> |
---|
| 188 | { |
---|
| 189 | enum { result = true }; |
---|
| 190 | typedef U PointeeType; |
---|
| 191 | }; |
---|
[7266] | 192 | |
---|
[3332] | 193 | template <class U> struct PointerTraits<U*&> |
---|
| 194 | { |
---|
| 195 | enum { result = true }; |
---|
| 196 | typedef U PointeeType; |
---|
| 197 | }; |
---|
[7266] | 198 | |
---|
[3332] | 199 | template <class U> struct PToMTraits |
---|
| 200 | { |
---|
| 201 | enum { result = false }; |
---|
| 202 | }; |
---|
[7266] | 203 | |
---|
[3332] | 204 | template <class U, class V> struct PToMTraits<U V::*> |
---|
| 205 | { |
---|
| 206 | enum { result = true }; |
---|
| 207 | }; |
---|
[7266] | 208 | |
---|
[3332] | 209 | template <class U, class V> struct PToMTraits<U V::*&> |
---|
| 210 | { |
---|
| 211 | enum { result = true }; |
---|
| 212 | }; |
---|
[7266] | 213 | |
---|
[3332] | 214 | template <class U> struct UnConst |
---|
| 215 | { |
---|
| 216 | typedef U Result; |
---|
| 217 | enum { isConst = 0 }; |
---|
| 218 | }; |
---|
[7266] | 219 | |
---|
[3332] | 220 | template <class U> struct UnConst<const U> |
---|
| 221 | { |
---|
| 222 | typedef U Result; |
---|
| 223 | enum { isConst = 1 }; |
---|
| 224 | }; |
---|
| 225 | |
---|
| 226 | template <class U> struct UnConst<const U&> |
---|
| 227 | { |
---|
| 228 | typedef U& Result; |
---|
| 229 | enum { isConst = 1 }; |
---|
| 230 | }; |
---|
[7266] | 231 | |
---|
[3332] | 232 | template <class U> struct UnVolatile |
---|
| 233 | { |
---|
| 234 | typedef U Result; |
---|
| 235 | enum { isVolatile = 0 }; |
---|
| 236 | }; |
---|
[7266] | 237 | |
---|
[3332] | 238 | template <class U> struct UnVolatile<volatile U> |
---|
| 239 | { |
---|
| 240 | typedef U Result; |
---|
| 241 | enum { isVolatile = 1 }; |
---|
| 242 | }; |
---|
| 243 | |
---|
| 244 | template <class U> struct UnVolatile<volatile U&> |
---|
| 245 | { |
---|
| 246 | typedef U& Result; |
---|
| 247 | enum { isVolatile = 1 }; |
---|
| 248 | }; |
---|
[7266] | 249 | |
---|
[3332] | 250 | public: |
---|
[7266] | 251 | typedef typename UnConst<T>::Result |
---|
[3332] | 252 | NonConstType; |
---|
[7266] | 253 | typedef typename UnVolatile<T>::Result |
---|
[3332] | 254 | NonVolatileType; |
---|
[7266] | 255 | typedef typename UnVolatile<typename UnConst<T>::Result>::Result |
---|
[3332] | 256 | UnqualifiedType; |
---|
[7266] | 257 | typedef typename PointerTraits<UnqualifiedType>::PointeeType |
---|
[3332] | 258 | PointeeType; |
---|
[7266] | 259 | typedef typename ReferenceTraits<T>::ReferredType |
---|
[3332] | 260 | ReferredType; |
---|
| 261 | typedef typename ReferenceTraits<typename UnVolatile<typename UnConst<T>::Result>::Result>::ReferredType |
---|
| 262 | UnqualifiedReferredType; |
---|
| 263 | |
---|
| 264 | enum { isConst = UnConst<T>::isConst }; |
---|
| 265 | enum { isVolatile = UnVolatile<T>::isVolatile }; |
---|
| 266 | enum { isReference = ReferenceTraits<UnqualifiedType>::result }; |
---|
| 267 | enum { isPointer = PointerTraits< |
---|
| 268 | typename ReferenceTraits<UnqualifiedType>::ReferredType >::result}; |
---|
[7266] | 269 | |
---|
[3332] | 270 | }; |
---|
| 271 | } |
---|
| 272 | |
---|
| 273 | #ifdef _MSC_VER |
---|
| 274 | #pragma warning( pop ) |
---|
| 275 | #endif // _MSC_VER |
---|
| 276 | |
---|
| 277 | |
---|
| 278 | #endif // end file guardian |
---|
| 279 | |
---|