CHROMA
typetraits.h
Go to the documentation of this file.
1 // -*- C++ -*-
2 /*! @file
3  * @brief Type traits support
4  */
5 
6 #ifndef __typetraits_h__
7 #define __typetraits_h__
8 
9 #include "typelist.h"
10 
11 namespace Chroma
12 {
13 ////////////////////////////////////////////////////////////////////////////////
14 // class template IsCustomUnsignedInt
15 // Offers a means to integrate nonstandard built-in unsigned integral types
16 // (such as unsigned __int64 or unsigned long long int) with the TypeTraits
17 // class template defined below.
18 // Invocation: IsCustomUnsignedInt<T> where T is any type
19 // Defines 'value', an enum that is 1 iff T is a custom built-in unsigned
20 // integral type
21 // Specialize this class template for nonstandard unsigned integral types
22 // and define value = 1 in those specializations
23 ////////////////////////////////////////////////////////////////////////////////
24 
25  template <typename T>
27  {
28  enum { value = 0 };
29  };
30 
31 ////////////////////////////////////////////////////////////////////////////////
32 // class template IsCustomSignedInt
33 // Offers a means to integrate nonstandard built-in unsigned integral types
34 // (such as unsigned __int64 or unsigned long long int) with the TypeTraits
35 // class template defined below.
36 // Invocation: IsCustomSignedInt<T> where T is any type
37 // Defines 'value', an enum that is 1 iff T is a custom built-in signed
38 // integral type
39 // Specialize this class template for nonstandard unsigned integral types
40 // and define value = 1 in those specializations
41 ////////////////////////////////////////////////////////////////////////////////
42 
43  template <typename T>
45  {
46  enum { value = 0 };
47  };
48 
49 ////////////////////////////////////////////////////////////////////////////////
50 // class template IsCustomFloat
51 // Offers a means to integrate nonstandard floating point types with the
52 // TypeTraits class template defined below.
53 // Invocation: IsCustomFloat<T> where T is any type
54 // Defines 'value', an enum that is 1 iff T is a custom built-in
55 // floating point type
56 // Specialize this class template for nonstandard unsigned integral types
57 // and define value = 1 in those specializations
58 ////////////////////////////////////////////////////////////////////////////////
59 
60  template <typename T>
62  {
63  enum { value = 0 };
64  };
65 
66 ////////////////////////////////////////////////////////////////////////////////
67 // Helper types for class template TypeTraits defined below
68 ////////////////////////////////////////////////////////////////////////////////
69 
70  namespace Private
71  {
72  typedef TYPELIST_4(unsigned char, unsigned short int,
73  unsigned int, unsigned long int) StdUnsignedInts;
74  typedef TYPELIST_4(signed char, short int,
75  int, long int) StdSignedInts;
76  typedef TYPELIST_3(bool, char, wchar_t) StdOtherInts;
77  typedef TYPELIST_3(float, double, long double) StdFloats;
78  }
79 
80 ////////////////////////////////////////////////////////////////////////////////
81 // class template TypeTraits
82 // Figures out various properties of any given type
83 // Invocations (T is a type):
84 // a) TypeTraits<T>::isPointer
85 // returns (at compile time) true if T is a pointer type
86 // b) TypeTraits<T>::PointeeType
87 // returns the type to which T points is T is a pointer type, NullType otherwise
88 // a) TypeTraits<T>::isReference
89 // returns (at compile time) true if T is a reference type
90 // b) TypeTraits<T>::ReferredType
91 // returns the type to which T refers is T is a reference type, NullType
92 // otherwise
93 // c) TypeTraits<T>::isMemberPointer
94 // returns (at compile time) true if T is a pointer to member type
95 // d) TypeTraits<T>::isStdUnsignedInt
96 // returns (at compile time) true if T is a standard unsigned integral type
97 // e) TypeTraits<T>::isStdSignedInt
98 // returns (at compile time) true if T is a standard signed integral type
99 // f) TypeTraits<T>::isStdIntegral
100 // returns (at compile time) true if T is a standard integral type
101 // g) TypeTraits<T>::isStdFloat
102 // returns (at compile time) true if T is a standard floating-point type
103 // h) TypeTraits<T>::isStdArith
104 // returns (at compile time) true if T is a standard arithmetic type
105 // i) TypeTraits<T>::isStdFundamental
106 // returns (at compile time) true if T is a standard fundamental type
107 // j) TypeTraits<T>::isUnsignedInt
108 // returns (at compile time) true if T is a unsigned integral type
109 // k) TypeTraits<T>::isSignedInt
110 // returns (at compile time) true if T is a signed integral type
111 // l) TypeTraits<T>::isIntegral
112 // returns (at compile time) true if T is a integral type
113 // m) TypeTraits<T>::isFloat
114 // returns (at compile time) true if T is a floating-point type
115 // n) TypeTraits<T>::isArith
116 // returns (at compile time) true if T is a arithmetic type
117 // o) TypeTraits<T>::isFundamental
118 // returns (at compile time) true if T is a fundamental type
119 // p) TypeTraits<T>::ParameterType
120 // returns the optimal type to be used as a parameter for functions that take Ts
121 // q) TypeTraits<T>::isConst
122 // returns (at compile time) true if T is a const-qualified type
123 // r) TypeTraits<T>::NonConstType
124 // removes the 'const' qualifier from T, if any
125 // s) TypeTraits<T>::isVolatile
126 // returns (at compile time) true if T is a volatile-qualified type
127 // t) TypeTraits<T>::NonVolatileType
128 // removes the 'volatile' qualifier from T, if any
129 // u) TypeTraits<T>::UnqualifiedType
130 // removes both the 'const' and 'volatile' qualifiers from T, if any
131 ////////////////////////////////////////////////////////////////////////////////
132 
133  template <typename T>
135  {
136  private:
137  template <class U> struct PointerTraits
138  {
139  enum { result = false };
141  };
142 
143  template <class U> struct PointerTraits<U*>
144  {
145  enum { result = true };
146  typedef U PointeeType;
147  };
148 
149  template <class U> struct ReferenceTraits
150  {
151  enum { result = false };
152  typedef U ReferredType;
153  };
154 
155  template <class U> struct ReferenceTraits<U&>
156  {
157  enum { result = true };
158  typedef U ReferredType;
159  };
160 
161  template <class U> struct PToMTraits
162  {
163  enum { result = false };
164  };
165 
166  template <class U, class V>
167  struct PToMTraits<U V::*>
168  {
169  enum { result = true };
170  };
171 
172  template <class U> struct UnConst
173  {
174  typedef U Result;
175  enum { isConst = 0 };
176  };
177 
178  template <class U> struct UnConst<const U>
179  {
180  typedef U Result;
181  enum { isConst = 1 };
182  };
183 
184  template <class U> struct UnVolatile
185  {
186  typedef U Result;
187  enum { isVolatile = 0 };
188  };
189 
190  template <class U> struct UnVolatile<volatile U>
191  {
192  typedef U Result;
193  enum { isVolatile = 1 };
194  };
195 
196  public:
199 
202 
204 
205  enum { isStdUnsignedInt =
207  enum { isStdSignedInt =
208  TL::IndexOf<Private::StdSignedInts, T>::value >= 0 };
209  enum { isStdIntegral = isStdUnsignedInt || isStdSignedInt ||
210  TL::IndexOf<Private::StdOtherInts, T>::value >= 0 };
211  enum { isStdFloat = TL::IndexOf<Private::StdFloats, T>::value >= 0 };
212  enum { isStdArith = isStdIntegral || isStdFloat };
213  enum { isStdFundamental = isStdArith || isStdFloat ||
215 
216  enum { isUnsignedInt = isStdUnsignedInt || IsCustomUnsignedInt<T>::value };
217  enum { isSignedInt = isStdSignedInt || IsCustomSignedInt<T>::value };
218  enum { isIntegral = isStdIntegral || isUnsignedInt || isSignedInt };
219  enum { isFloat = isStdFloat || IsCustomFloat<T>::value };
220  enum { isArith = isIntegral || isFloat };
222 
223  typedef typename Select<isStdArith || isPointer || isMemberPointer,
224  T, ReferredType&>::Result
226 
231  typedef typename UnVolatile<typename UnConst<T>::Result>::Result
233  };
234 }
235 
236 
237 #endif
ReferenceTraits< T >::ReferredType ReferredType
Definition: typetraits.h:201
UnVolatile< T >::Result NonVolatileType
Definition: typetraits.h:230
Select< isStdArith||isPointer||isMemberPointer, T, ReferredType & >::Result ParameterType
Definition: typetraits.h:225
PointerTraits< T >::PointeeType PointeeType
Definition: typetraits.h:198
UnVolatile< typename UnConst< T >::Result >::Result UnqualifiedType
Definition: typetraits.h:232
UnConst< T >::Result NonConstType
Definition: typetraits.h:228
typedef TYPELIST_3(bool, char, wchar_t) StdOtherInts
typedef TYPELIST_4(unsigned char, unsigned short int, unsigned int, unsigned long int) StdUnsignedInts
Asqtad Staggered-Dirac operator.
Definition: klein_gord.cc:10
LinOpSysSolverMGProtoClover::T T
multi1d< LatticeColorMatrix > U
Typelist support.