dune-typetree  2.9
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 
41  constexpr static bool value = sizeof(test<T>(0)) == sizeof(yes);
42  };
43 
44  template<typename T, typename V>
46  {
47  template<int N>
48  struct maybe { char dummy[N+1]; };
49  struct yes { char dummy[2]; };
50  struct no { char dummy[1]; };
51 
52  template<typename X>
55  template<typename X>
56  static no test(...);
57 
59  constexpr static bool value = sizeof(test<T>(0)) == sizeof(yes);
60  };
61 
62  template<typename T>
64  {
65  struct yes { char dummy[1]; };
66  struct no { char dummy[2]; };
67 
68  template<typename X>
70  template<typename X>
71  static no test(...);
72 
74  constexpr static bool value = sizeof(test<T>(0)) == sizeof(yes);
75  };
76 
77  template<typename T, typename V>
79  {
80  template<int N>
81  struct maybe { char dummy[N+1]; };
82  struct yes { char dummy[2]; };
83  struct no { char dummy[1]; };
84 
85  template<typename X>
88  template<typename X>
89  static no test(...);
90 
92  constexpr static bool value = sizeof(test<T>(0)) == sizeof(yes);
93  };
94 
95  template<typename>
96  struct AlwaysVoid
97  {
98  typedef void type;
99  };
100 
101 
103  template<typename T>
104  T* declptr();
105 
106 
107  // Support for lazy evaluation of meta functions. This is required when doing
108  // nested tag dispatch without C++11-style typedefs (based on using syntax).
109  // The standard struct-based meta functions cause premature evaluation in a
110  // context that is not SFINAE-compatible. We thus have to return the meta function
111  // without evaluating it, placing that burden on the caller. On the other hand,
112  // the lookup will often directly be the target type, so here is some helper code
113  // to automatically do the additional evaluation if necessary.
114  // Too bad that the new syntax is GCC 4.6+...
115 
116 
118 
121  struct meta_function {};
122 
124  template<typename F>
126  {
127  typedef typename F::type type;
128  };
129 
131  template<typename F>
133  {
134  typedef F type;
135  };
136 
138  template<typename F>
140  {
141  typedef typename std::conditional<
142  std::is_base_of<meta_function,F>::value,
145  >::type::type type;
146  };
147 
148  namespace impl {
149 
150  // Check if type is a or is derived from one of the tree path types
151 
152  // Default overload for types not representing a tree path
153  constexpr auto isTreePath(void*)
154  -> std::false_type
155  {
156  return std::false_type();
157  }
158 
159  // Overload for instances of HybridTreePath<...>
160  template<class... I>
161  constexpr auto isTreePath(const HybridTreePath<I...>*)
162  -> std::true_type
163  {
164  return std::true_type();
165  }
166 
167  }
168 
179  template<class T>
180  struct IsTreePath :
181  public decltype(impl::isTreePath((typename std::decay<T>::type*)(nullptr)))
182  {};
183 
190  template<class T>
191  constexpr auto isTreePath(const T&)
192  -> IsTreePath<T>
193  {
194  return IsTreePath<T>();
195  }
196 
197 
198  } // end namespace TypeTree
199 } // end namespace Dune
200 
201 #endif // DUNE_TYPETREE_TYPETRAITS_HH
typename std::decay_t< Node >::NodeTag NodeTag
Returns the node tag of the given Node.
Definition: nodeinterface.hh:76
typename std::decay_t< T >::ImplementationTag ImplementationTag
Returns the implementation tag of the given Node.
Definition: nodeinterface.hh:80
Definition: accumulate_static.hh:13
constexpr auto isTreePath(const T &) -> IsTreePath< T >
Check if given object represents a tree path.
Definition: typetraits.hh:191
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:153
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
constexpr static bool value
True if class T defines a NodeTag.
Definition: typetraits.hh:41
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:46
static maybe< std::is_base_of< V, NodeTag< X > >::value > test(NodeTag< X > *a)
constexpr static bool value
True if class T defines a NodeTag of type V.
Definition: typetraits.hh:59
Definition: typetraits.hh:48
char dummy[N+1]
Definition: typetraits.hh:48
Definition: typetraits.hh:49
char dummy[2]
Definition: typetraits.hh:49
Definition: typetraits.hh:50
char dummy[1]
Definition: typetraits.hh:50
Definition: typetraits.hh:64
static yes test(ImplementationTag< X > *)
constexpr static bool value
True if class T defines an ImplementationTag.
Definition: typetraits.hh:74
char dummy[1]
Definition: typetraits.hh:65
Definition: typetraits.hh:66
char dummy[2]
Definition: typetraits.hh:66
static maybe< std::is_base_of< V, ImplementationTag< X > >::value > test(ImplementationTag< X > *a)
constexpr static bool value
True if class T defines an ImplementationTag of type V.
Definition: typetraits.hh:92
char dummy[N+1]
Definition: typetraits.hh:81
char dummy[2]
Definition: typetraits.hh:82
char dummy[1]
Definition: typetraits.hh:83
Definition: typetraits.hh:97
void type
Definition: typetraits.hh:98
Marker tag declaring a meta function.
Definition: typetraits.hh:121
Helper meta function to delay evaluation of F.
Definition: typetraits.hh:126
F::type type
Definition: typetraits.hh:127
Identity function.
Definition: typetraits.hh:133
F type
Definition: typetraits.hh:134
Meta function that evaluates its argument iff it inherits from meta_function.
Definition: typetraits.hh:140
std::conditional< std::is_base_of< meta_function, F >::value, lazy_evaluate< F >, lazy_identity< F > >::type::type type
Definition: typetraits.hh:145
Check if type represents a tree path.
Definition: typetraits.hh:182