CHROMA
typemanip.h
Go to the documentation of this file.
1 // -*- C++ -*-
2 /*! @file
3  * @brief Type manipulator support
4  */
5 
6 #ifndef __typemanip_h__
7 #define __typemanip_h__
8 
9 namespace Chroma
10 {
11 
12 ////////////////////////////////////////////////////////////////////////////////
13 // class template Int2Type
14 // Converts each integral constant into a unique type
15 // Invocation: Int2Type<v> where v is a compile-time constant integral
16 // Defines 'value', an enum that evaluates to v
17 ////////////////////////////////////////////////////////////////////////////////
18 
19  template <int v>
20  struct Int2Type
21  {
22  enum { value = v };
23  };
24 
25 ////////////////////////////////////////////////////////////////////////////////
26 // class template Type2Type
27 // Converts each type into a unique, insipid type
28 // Invocation Type2Type<T> where T is a type
29 // Defines the type OriginalType which std::maps back to T
30 ////////////////////////////////////////////////////////////////////////////////
31 
32  template <typename T>
33  struct Type2Type
34  {
35  typedef T OriginalType;
36  };
37 
38 ////////////////////////////////////////////////////////////////////////////////
39 // class template Select
40 // Selects one of two types based upon a boolean constant
41 // Invocation: Select<flag, T, U>::Result
42 // where:
43 // flag is a compile-time boolean constant
44 // T and U are types
45 // Result evaluates to T if flag is true, and to U otherwise.
46 ////////////////////////////////////////////////////////////////////////////////
47 
48  template <bool flag, typename T, typename U>
49  struct Select
50  {
51  typedef T Result;
52  };
53  template <typename T, typename U>
54  struct Select<false, T, U>
55  {
56  typedef U Result;
57  };
58 
59 ////////////////////////////////////////////////////////////////////////////////
60 // Helper types Small and Big - guarantee that sizeof(Small) < sizeof(Big)
61 ////////////////////////////////////////////////////////////////////////////////
62 
63  namespace Private
64  {
65  template <class T, class U>
67  {
68  typedef char Small;
69  struct Big { char dummy[2]; };
70  static Big Test(...);
71  static Small Test(U);
72  static T MakeT();
73  };
74  }
75 
76 ////////////////////////////////////////////////////////////////////////////////
77 // class template Conversion
78 // Figures out the conversion relationships between two types
79 // Invocations (T and U are types):
80 // a) Conversion<T, U>::exists
81 // returns (at compile time) true if there is an implicit conversion from T
82 // to U (example: Derived to Base)
83 // b) Conversion<T, U>::exists2Way
84 // returns (at compile time) true if there are both conversions from T
85 // to U and from U to T (example: int to char and back)
86 // c) Conversion<T, U>::sameType
87 // returns (at compile time) true if T and U represent the same type
88 //
89 // Caveat: might not work if T and U are in a private inheritance hierarchy.
90 ////////////////////////////////////////////////////////////////////////////////
91 
92  template <class T, class U>
93  struct Conversion
94  {
96 #ifndef __MWERKS__
97  enum { exists = sizeof(typename H::Small) == sizeof(H::Test(H::MakeT())) };
98 #else
99  enum { exists = false };
100 #endif
102  enum { sameType = false };
103  };
104 
105  template <class T>
106  struct Conversion<T, T>
107  {
108  enum { exists = 1, exists2Way = 1,sameType = 1 };
109  };
110 
111  template <class T>
112  struct Conversion<void, T>
113  {
114  enum { exists = 1, exists2Way = 0,sameType = 0 };
115  };
116 
117  template <class T>
118  struct Conversion<T, void>
119  {
120  enum { exists = 1, exists2Way = 0,sameType = 0 };
121  };
122 
123  template <>
124  class Conversion<void, void>
125  {
126  public:
127  enum { exists = 1, exists2Way = 1,sameType = 1 };
128  };
129 }
130 
131 ////////////////////////////////////////////////////////////////////////////////
132 // macro SUPERSUBCLASS
133 // Invocation: SUPERSUBCLASS(B, D) where B and D are types.
134 // Returns true if B is a public base of D, or if B and D are aliases of the
135 // same type.
136 //
137 // Caveat: might not work if T and U are in a private inheritance hierarchy.
138 ////////////////////////////////////////////////////////////////////////////////
139 
140 #define SUPERSUBCLASS(T, U) \
141  (::Chroma::Conversion<const U*, const T*>::exists && \
142  !::Chroma::Conversion<const T*, const void*>::sameType)
143 
144 ////////////////////////////////////////////////////////////////////////////////
145 // macro SUPERSUBCLASS
146 // Invocation: SUPERSUBCLASS(B, D) where B and D are types.
147 // Returns true if B is a public base of D.
148 //
149 // Caveat: might not work if T and U are in a private inheritance hierarchy.
150 ////////////////////////////////////////////////////////////////////////////////
151 
152 #define SUPERSUBCLASS_STRICT(T, U) \
153  (SUPERSUBCLASS(T, U) && \
154  !::Chroma::Conversion<const T, const U>::sameType)
155 
156 #endif
Asqtad Staggered-Dirac operator.
Definition: klein_gord.cc:10
LinOpSysSolverMGProtoClover::T T
Private::ConversionHelper< T, U > H
Definition: typemanip.h:95
multi1d< LatticeColorMatrix > U