DUNE-FEM (unstable)

tupletypetraits.hh
1#ifndef DUNE_FEM_PASS_COMMON_TUPLETYPETRAITS_HH
2#define DUNE_FEM_PASS_COMMON_TUPLETYPETRAITS_HH
3
4#include <tuple>
5#include <type_traits>
6
7#include <dune/common/hybridutilities.hh>
10
11#include <dune/fem/common/tupleutility.hh>
12
13namespace
14{
15
16 // CheckAllElements
17 // ----------------
18
19 template< class Tuple,
20 template< class > class Predicate,
21 int N = std::tuple_size< Tuple >::value
22 >
23 struct CheckAllElements
24 {
25 static const bool value = ( Predicate< typename std::tuple_element< N-1, Tuple >::type >::value
26 && CheckAllElements< Tuple, Predicate, (N-1) >::value );
27 };
28
29 template< class Tuple,
30 template< class > class Predicate
31 >
32 struct CheckAllElements< Tuple, Predicate, 0 >
33 {
34 static const bool value = true;
35 };
36
37} // namespace
38
39
40
41namespace Dune
42{
43
44 // SingleTypeTuple
45 // ---------------
46
47 /*
48 * \brief Make tuple of given length and single type.
49 */
50 template< class T,
51 int size,
52 class Seed = std::tuple<>,
53 int index = 0
54 >
55 struct SingleTypeTuple
56 {
57 typedef typename SingleTypeTuple< T, size, typename Dune::PushBackTuple< Seed, T >::type, (index+1) >::Type Type;
58 };
59
60 template< class T,
61 int size,
62 class Seed
63 >
64 struct SingleTypeTuple< T, size, Seed, size >
65 {
66 typedef Seed Type;
67 };
68
69
70
71 // EmptyTuple
72 // ----------
73
74 /*
75 * \brief a tuple consisting of Dune::Empty
76 */
77 template< int size >
78 struct EmptyTuple
79 : public SingleTypeTuple< Dune::Empty, size >
80 {};
81
82
83
84 // TupleTypeTraits
85 // ---------------
86
87 /*
88 * \brief Mimicks Dune::TypeTraits (see dune/common/typetraits.hh) for tuples.
89 */
90 template< class Tuple >
91 class TupleTypeTraits
92 {
93 template< class T >
94 struct IsPointer
95 {
96 static const bool value = std::is_pointer<T>::value;
97 };
98
99 template< class T >
100 struct IsReference
101 {
102 static const bool value = std::is_lvalue_reference<T>::value;
103 };
104
105 template< class T >
106 struct PointeeTypeEvaluator
107 {
108 typedef typename std::remove_pointer<T>::type Type;
109 };
110
111 template< class T >
112 struct ReferredTypeEvaluator
113 {
114 typedef typename std::remove_reference<T>::type Type;
115 };
116
117 public:
118 static const bool isPointerTuple = CheckAllElements< Tuple, IsPointer >::value;
119
120 typedef typename std::conditional< isPointerTuple,
122 EmptyTuple< std::tuple_size< Tuple >::value >
123 >::type PointeeTupleType;
124
125 static const bool isReferenceTuple = CheckAllElements< Tuple, IsReference >::value;
126
127 typedef typename Dune::ForEachType< ReferredTypeEvaluator, Tuple >::Type ReferredTupleType;
128 };
129
130
131
132 // PointerTuple
133 // ------------
134
140 template< class Tuple >
142 {
143 template< class T >
144 struct PointerEvaluator
145 {
146 typedef typename std::remove_pointer<T>::type * Type;
147 };
148
149 public:
151 };
152
153
154
155 // ReferenceTuple
156 // --------------
157
163 template< class Tuple >
165 {
166 template< class T >
167 struct ReferenceEvaluator
168 {
169 typedef typename std::remove_reference<T>::type & Type;
170 };
171
172 public:
174 };
175
176
177
178 // ConstTuple
179 // ----------
180
186 template< class Tuple >
188 {
189 template< class T >
190 struct ConstEvaluator
191 {
192 typedef typename std::add_const<typename std::remove_cv<T>::type>::type Type;
193 };
194
195 public:
197 };
198
199
200
201 // RemoveConstTuple
202 // ----------------
203
207 template< class Tuple >
209 {
210 template< class T >
211 struct RemoveConstEvaluator
212 {
213 typedef typename std::remove_cv< T >::type Type;
214 };
215
216 public:
218 };
219
220
221
222 // tuple_remove_const
223 // ------------------
224
225 /*
226 * \brief Convenience structure mimicking Dune::remove_const.
227 */
228 template< class Tuple >
229 struct tuple_remove_const
230 {
231 typedef typename RemoveConstTuple< Tuple >::Type type;
232 };
233
234
235
236 // ValidPointerTupleCheck
237 // ----------------------
238
244 template< class Tuple >
246 {
247 static_assert( TupleTypeTraits< Tuple >::isPointerTuple, "Can not check non-pointer tuple." );
248
249 static bool apply ( const Tuple &tuple )
250 {
251 bool check(true);
252 Hybrid::forEach( tuple, [ & ]( auto&& ti ){ check &= static_cast< bool >( ti ); } );
253 return check;
254 }
255 };
256
257
258
259 // DereferenceTuple
260 // ----------------
261
265 template< class Tuple,
266 class Seed = std::tuple<>,
267 int index = 0,
268 int size = std::tuple_size< Tuple >::value
269 >
271 {
272 template< class, class, int, int > friend class DereferenceTuple;
273
274 typedef typename std::remove_pointer< typename std::tuple_element< index, Tuple >::type >::type & AppendType;
275 typedef typename Dune::PushBackTuple< Seed, AppendType >::type AccumulatedType;
276
277 typedef DereferenceTuple< Tuple, AccumulatedType, (index+1), size > NextType;
278
279 public:
280 typedef typename Dune::ReferenceTuple<
281 typename Dune::TupleTypeTraits< Tuple >::PointeeTupleType
282 >::Type Type;
283
284 static Type apply ( Tuple &tuple )
285 {
286 Seed seed;
287 return append( tuple, seed );
288 }
289
290 private:
291 static Type append ( Tuple &tuple, Seed &seed )
292 {
293 typename std::tuple_element< index, Tuple >::type pointer = std::get< index >( tuple );
294 AppendType append = *pointer;
295 AccumulatedType next = Dune::tuple_push_back< AppendType >( seed, append );
296 return NextType::append( tuple, next );
297 }
298 };
299
300 template< class Tuple,
301 class Seed,
302 int size
303 >
304 class DereferenceTuple< Tuple, Seed, size, size >
305 {
306 template< class, class, int, int > friend class DereferenceTuple;
307
308 public:
309 typedef typename Dune::ReferenceTuple<
310 typename Dune::TupleTypeTraits< Tuple >::PointeeTupleType
311 >::Type Type;
312
313 static_assert( (std::is_same< Seed, Type >::value), "Failed to dereference pointer tuple." );
314
315 static Type apply ( Tuple & )
316 {
317 return Type();
318 }
319
320 private:
321 static Seed append ( Tuple &tuple, Seed &seed ) { return seed; }
322 };
323
324} // namespace Dune
325
326#endif // #ifndef DUNE_FEM_PASS_COMMON_TUPLETYPETRAITS_HH
Add const qualifier to all tuple elements.
Definition: tupletypetraits.hh:188
Dereference pointer tuple.
Definition: tupletypetraits.hh:271
Convert a tuple to a tuple of pointer types.
Definition: tupletypetraits.hh:142
Convert a tuple to a tuple of references.
Definition: tupletypetraits.hh:165
Remove const qualifiers from tuple.
Definition: tupletypetraits.hh:209
Contains utility classes which can be used with std::tuple.
constexpr void forEach(Range &&range, F &&f)
Range based for loop.
Definition: hybridutilities.hh:256
Dune namespace.
Definition: alignedallocator.hh:13
constexpr std::integral_constant< std::size_t, sizeof...(II)> size(std::integer_sequence< T, II... >)
Return the size of the sequence.
Definition: integersequence.hh:75
Helper template to clone the type definition of a std::tuple with the storage types replaced by a use...
Definition: tupleutility.hh:116
Helper template to append a type to a std::tuple.
Definition: tupleutility.hh:476
Check whether a pointer tuple can be dereferenced.
Definition: tupletypetraits.hh:246
Traits for type conversions and type information.
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.111.3 (Nov 21, 23:30, 2024)