Dune Core Modules (2.9.0)

typetraits.hh
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>
9
10#include <dune/typetree/treepath.hh>
11#include <dune/typetree/nodeinterface.hh>
12
13namespace 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>
45 struct has_node_tag_value
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>
53 static maybe<std::is_base_of<V, NodeTag<X>>::value>
54 test(NodeTag<X> * a);
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>
63 struct has_implementation_tag
64 {
65 struct yes { char dummy[1]; };
66 struct no { char dummy[2]; };
67
68 template<typename X>
69 static yes test(ImplementationTag<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>
78 struct has_implementation_tag_value
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>
86 static maybe<std::is_base_of<V, ImplementationTag<X>>::value>
87 test(ImplementationTag<X> * a);
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&)
193 {
194 return IsTreePath<T>();
195 }
196
197
198 } // end namespace TypeTree
199} // end namespace Dune
200
201#endif // DUNE_TYPETREE_TYPETRAITS_HH
Traits for type conversions and type information.
Dune namespace.
Definition: alignedallocator.hh:13
Check if type represents a tree path.
Definition: typetraits.hh:182
Meta function that evaluates its argument iff it inherits from meta_function.
Definition: typetraits.hh:140
Helper meta function to delay evaluation of F.
Definition: typetraits.hh:126
Identity function.
Definition: typetraits.hh:133
Marker tag declaring a meta function.
Definition: typetraits.hh:121
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.111.3 (Dec 21, 23:30, 2024)