dune-typetree  2.7.1
typetraits.hh
Go to the documentation of this file.
1 // -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 // vi: set et ts=4 sw=2 sts=2:
3 
4 #ifndef DUNE_TYPETREE_TYPETRAITS_HH
5 #define DUNE_TYPETREE_TYPETRAITS_HH
6 
7 #include <type_traits>
8 #include <dune/common/typetraits.hh>
9 
12 
13 namespace Dune {
14 
15  // Provide some more C++11 TMP helpers.
16  // These should be upstreamed to dune-common ASAP.
17 
18  template<typename... T>
19  struct first_type;
20 
21  template<typename T0, typename... T>
22  struct first_type<T0,T...>
23  {
24  typedef T0 type;
25  };
26 
27  namespace TypeTree {
28 
29  template<typename T>
30  struct has_node_tag
31  {
32  struct yes { char dummy[1]; };
33  struct no { char dummy[2]; };
34 
35  template<typename X>
36  static yes test(NodeTag<X> *);
37  template<typename X>
38  static no test(...);
39 
40  enum {
42  value = sizeof(test<T>(0)) == sizeof(yes)
43  };
44  };
45 
46  template<typename T, typename V>
48  {
49  template<int N>
50  struct maybe { char dummy[N+1]; };
51  struct yes { char dummy[2]; };
52  struct no { char dummy[1]; };
53 
54  template<typename X>
57  template<typename X>
58  static no test(...);
59 
60  enum {
62  value = sizeof(test<T>(0)) == sizeof(yes)
63  };
64  };
65 
66  template<typename T>
68  {
69  struct yes { char dummy[1]; };
70  struct no { char dummy[2]; };
71 
72  template<typename X>
74  template<typename X>
75  static no test(...);
76 
77  enum {
79  value = sizeof(test<T>(0)) == sizeof(yes)
80  };
81  };
82 
83  template<typename T, typename V>
85  {
86  template<int N>
87  struct maybe { char dummy[N+1]; };
88  struct yes { char dummy[2]; };
89  struct no { char dummy[1]; };
90 
91  template<typename X>
94  template<typename X>
95  static no test(...);
96 
97  enum {
99  value = sizeof(test<T>(0)) == sizeof(yes)
100  };
101  };
102 
103  template<typename>
104  struct AlwaysVoid
105  {
106  typedef void type;
107  };
108 
109 
111  template<typename T>
112  T* declptr();
113 
114 
115  // Support for lazy evaluation of meta functions. This is required when doing
116  // nested tag dispatch without C++11-style typedefs (based on using syntax).
117  // The standard struct-based meta functions cause premature evaluation in a
118  // context that is not SFINAE-compatible. We thus have to return the meta function
119  // without evaluating it, placing that burden on the caller. On the other hand,
120  // the lookup will often directly be the target type, so here is some helper code
121  // to automatically do the additional evaluation if necessary.
122  // Too bad that the new syntax is GCC 4.6+...
123 
124 
126 
129  struct meta_function {};
130 
132  template<typename F>
134  {
135  typedef typename F::type type;
136  };
137 
139  template<typename F>
141  {
142  typedef F type;
143  };
144 
146  template<typename F>
148  {
149  typedef typename std::conditional<
150  std::is_base_of<meta_function,F>::value,
153  >::type::type type;
154  };
155 
156  namespace impl {
157 
158  // Check if type is a or is derived from one of the tree path types
159 
160  // Default overload for types not representing a tree path
161  constexpr auto isTreePath(void*)
162  -> std::false_type
163  {
164  return std::false_type();
165  }
166 
167  // Overload for instances of HybridTreePath<...>
168  template<class... I>
169  constexpr auto isTreePath(const HybridTreePath<I...>*)
170  -> std::true_type
171  {
172  return std::true_type();
173  }
174 
175  }
176 
187  template<class T>
188  struct IsTreePath :
189  public decltype(impl::isTreePath((typename std::decay<T>::type*)(nullptr)))
190  {};
191 
198  template<class T>
199  constexpr auto isTreePath(const T&)
200  -> IsTreePath<T>
201  {
202  return IsTreePath<T>();
203  }
204 
205 
206  } // end namespace TypeTree
207 } // end namespace Dune
208 
209 #endif // DUNE_TYPETREE_TYPETRAITS_HH
typename std::decay_t< Node >::NodeTag NodeTag
Returns the node tag of the given Node.
Definition: nodeinterface.hh:62
typename std::decay_t< T >::ImplementationTag ImplementationTag
Returns the implementation tag of the given Node.
Definition: nodeinterface.hh:66
Definition: accumulate_static.hh:13
constexpr auto isTreePath(const T &) -> IsTreePath< T >
Check if given object represents a tree path.
Definition: typetraits.hh:199
T * declptr()
Helper function for generating a pointer to a value of type T in an unevaluated operand setting.
constexpr auto isTreePath(void *) -> std::false_type
Definition: typetraits.hh:161
A hybrid version of TreePath that supports both compile time and run time indices.
Definition: treepath.hh:79
Definition: typetraits.hh:19
T0 type
Definition: typetraits.hh:24
Definition: typetraits.hh:31
@ value
True if class T defines a NodeTag.
Definition: typetraits.hh:42
static yes test(NodeTag< X > *)
Definition: typetraits.hh:32
char dummy[1]
Definition: typetraits.hh:32
Definition: typetraits.hh:33
char dummy[2]
Definition: typetraits.hh:33
Definition: typetraits.hh:48
@ value
True if class T defines a NodeTag of type V.
Definition: typetraits.hh:62
static maybe< std::is_base_of< V, NodeTag< X > >::value > test(NodeTag< X > *a)
Definition: typetraits.hh:50
char dummy[N+1]
Definition: typetraits.hh:50
Definition: typetraits.hh:51
char dummy[2]
Definition: typetraits.hh:51
Definition: typetraits.hh:52
char dummy[1]
Definition: typetraits.hh:52
Definition: typetraits.hh:68
static yes test(ImplementationTag< X > *)
@ value
True if class T defines an ImplementationTag.
Definition: typetraits.hh:79
char dummy[1]
Definition: typetraits.hh:69
Definition: typetraits.hh:70
char dummy[2]
Definition: typetraits.hh:70
@ value
True if class T defines an ImplementationTag of type V.
Definition: typetraits.hh:99
static maybe< std::is_base_of< V, ImplementationTag< X > >::value > test(ImplementationTag< X > *a)
char dummy[N+1]
Definition: typetraits.hh:87
char dummy[2]
Definition: typetraits.hh:88
char dummy[1]
Definition: typetraits.hh:89
Definition: typetraits.hh:105
void type
Definition: typetraits.hh:106
Marker tag declaring a meta function.
Definition: typetraits.hh:129
Helper meta function to delay evaluation of F.
Definition: typetraits.hh:134
F::type type
Definition: typetraits.hh:135
Identity function.
Definition: typetraits.hh:141
F type
Definition: typetraits.hh:142
Meta function that evaluates its argument iff it inherits from meta_function.
Definition: typetraits.hh:148
std::conditional< std::is_base_of< meta_function, F >::value, lazy_evaluate< F >, lazy_identity< F > >::type::type type
Definition: typetraits.hh:153
Check if type represents a tree path.
Definition: typetraits.hh:190