dune-common 3.0-git
tupleutility.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_TUPLE_UTILITY_HH
5#define DUNE_TUPLE_UTILITY_HH
6
7#include <cstddef>
8#include <tuple>
9
12
13#include "tuples.hh"
14
15namespace Dune {
16
34 template<class T>
36
37 template<class... Args>
38 struct NullPointerInitialiser<std::tuple<Args...> >
39 {
40 typedef std::tuple<Args...> ResultType;
42 {
43 return ResultType(static_cast<Args>(nullptr)...);
44 }
45 };
46
71 template<template <class> class TE, class T>
73
74 template<template <class> class TE, class... Args>
75 struct ForEachType<TE, std::tuple<Args...> >
76 {
77 typedef std::tuple<typename TE<Args>::Type...> Type;
78 };
79
80#ifndef DOXYGEN
81 template<class Tuple, class Functor, std::size_t... I>
82 inline auto genericTransformTupleBackendImpl(Tuple& t, Functor& f, const Std::index_sequence<I...>& )
83 -> std::tuple<decltype(f(std::get<I>(t)))...>
84 {
85 return std::tuple<decltype(f(std::get<I>(t)))...>(f(std::get<I>(t))...);
86 }
87
88 template<class... Args, class Functor>
89 auto genericTransformTupleBackend(std::tuple<Args...>& t, Functor& f) ->
90 decltype(genericTransformTupleBackendImpl(t, f,Std::index_sequence_for<Args...>{}))
91 {
92 return genericTransformTupleBackendImpl(t, f,Std::index_sequence_for<Args...>{});
93 }
94#endif
95
134 template<class Tuple, class Functor>
135 auto genericTransformTuple(Tuple&& t, Functor&& f) ->
136 decltype(genericTransformTupleBackend(t, f))
137 {
138 return genericTransformTupleBackend(t, f);
139 }
140
173 template<template<class> class TE, class... Args>
175 {
176 mutable std::tuple<Args&...> tup;
177
178 template<class T, std::size_t... I>
179 inline auto apply(T&& t, const Std::index_sequence<I...>& ) ->
180 decltype(TE<T>::apply(t,std::get<I>(tup)...)) const
181 {
182 return TE<T>::apply(t,std::get<I>(tup)...);
183 }
184
185 public:
186 template<class T>
187 struct TypeEvaluator : public TE<T>
188 {};
189
190 TransformTupleFunctor(Args&&... args)
191 : tup(args...)
192 { }
193
194 template<class T>
195 inline auto operator()(T&& t) ->
196 decltype(this->apply(t,Std::index_sequence_for<Args...>{})) const
197 {
198 return apply(t,Std::index_sequence_for<Args...>{});
199 }
200 };
201
202 template<template<class> class TE, class... Args>
204 {
205 return TransformTupleFunctor<TE, Args...>(args...);
206 }
207
240 template<template<class> class TypeEvaluator, class Tuple, class... Args>
241 auto transformTuple(Tuple&& orig, Args&&... args) ->
242 decltype(genericTransformTuple(orig, makeTransformTupleFunctor<TypeEvaluator>(args...)))
243 {
244 return genericTransformTuple(orig, makeTransformTupleFunctor<TypeEvaluator>(args...));
245 }
246
248
252 template<class T>
254 {
255 typedef T& Type;
256 static Type apply(T& t)
257 {
258 return t;
259 }
260 };
261
263
267 template<class T>
269 {
270 typedef typename std::remove_reference<T>::type* Type;
271 static Type apply(T& t)
272 {
273 return &t;
274 }
275 };
276
277 // Specialization, in case the type is already a reference
278 template<class T>
280 {
281 typedef typename std::remove_reference<T>::type* Type;
282 static Type apply(T& t)
283 {
284 return &t;
285 }
286 };
287
288 namespace
289 {
290 template<int i, typename T1,typename F>
291 struct Visitor
292 {
293 static inline void visit(F& func, T1& t1)
294 {
295 func.visit(std::get<std::tuple_size<T1>::value-i>(t1));
296 Visitor<i-1,T1,F>::visit(func, t1);
297 }
298 };
299
300 template<typename T1,typename F>
301 struct Visitor<0,T1,F>
302 {
303 static inline void visit(F&, T1&)
304 {}
305 };
306
307 template<int i, typename T1, typename T2,typename F>
308 struct PairVisitor
309 {
310 static inline void visit(F& func, T1& t1, T2& t2)
311 {
312 func.visit(std::get<std::tuple_size<T1>::value-i>(t1), std::get<std::tuple_size<T2>::value-i>(t2));
313 PairVisitor<i-1,T1,T2,F>::visit(func, t1, t2);
314 }
315 };
316
317 template<typename T1, typename T2, typename F>
318 struct PairVisitor<0,T1,T2,F>
319 {
320 static inline void visit(F&, T1&, T2&)
321 {}
322 };
323 }
324
368 template<class Tuple>
370 {
371 public:
374 ForEachValue(Tuple& t) :
375 t_(t)
376 {}
377
380 template<class Functor>
381 void apply(Functor& f) const
382 {
383 Visitor<std::tuple_size<Tuple>::value,Tuple,Functor>::visit(f, t_);
384 }
385 private:
386 Tuple& t_;
387 };
388
402 template<class Tuple1, class Tuple2>
404 {
405 public:
409 ForEachValuePair(Tuple1& t1, Tuple2& t2) :
410 t1_(t1),
411 t2_(t2)
412 {}
413
416 template<class Functor>
417 void apply(Functor& f)
418 {
419 PairVisitor<std::tuple_size<Tuple1>::value,Tuple1,Tuple2,Functor>::visit(f, t1_, t2_);
420 }
421 private:
422 Tuple1& t1_;
423 Tuple2& t2_;
424 };
425
431 template<int N, class Tuple>
432 struct AtType
433 {
434 typedef typename std::tuple_element<std::tuple_size<Tuple>::value - N - 1, Tuple>::type Type;
435 };
436
444 template<int N>
445 struct At
446 {
447 template<typename Tuple>
448 static typename TupleAccessTraits<typename AtType<N, Tuple>::Type>::NonConstType
449 get(Tuple& t)
450 {
451 return std::get<std::tuple_size<Tuple>::value - N - 1>(t);
452 }
453
454 template<typename Tuple>
455 static typename TupleAccessTraits<typename AtType<N, Tuple>::Type>::ConstType
456 get(const Tuple& t)
457 {
458 return std::get<std::tuple_size<Tuple>::value - N - 1>(t);
459 }
460 };
461
468 template<class Tuple>
470 {
471 struct Deletor
472 {
473 template<typename P>
474 void visit(const P& p)
475 {
476 delete p;
477 }
478 };
479
480 public:
481 static void apply(Tuple& t)
482 {
483 static Deletor deletor;
484 ForEachValue<Tuple>(t).apply(deletor);
485 }
486 };
487
511 template<class Tuple, template<class> class Predicate, std::size_t start = 0,
512 std::size_t size = std::tuple_size<Tuple>::value>
514 public std::conditional<Predicate<typename std::tuple_element<start,
515 Tuple>::type>::value,
516 std::integral_constant<std::size_t, start>,
517 FirstPredicateIndex<Tuple, Predicate, start+1> >::type
518 {
519 static_assert(std::tuple_size<Tuple>::value == size, "The \"size\" "
520 "template parameter of FirstPredicateIndex is an "
521 "implementation detail and should never be set "
522 "explicitly!");
523 };
524
525#ifndef DOXYGEN
526 template<class Tuple, template<class> class Predicate, std::size_t size>
527 class FirstPredicateIndex<Tuple, Predicate, size, size>
528 {
529 static_assert(Std::to_false_type<Tuple>::value, "None of the std::tuple element "
530 "types matches the predicate!");
531 };
532#endif // !DOXYGEN
533
543 template<class T>
544 struct IsType
545 {
547 template<class U>
548 struct Predicate : public std::is_same<T, U> {};
549 };
550
564 template<class Tuple, class T, std::size_t start = 0>
566 public FirstPredicateIndex<Tuple, IsType<T>::template Predicate, start>
567 { };
568
575 template<class Tuple, class T>
577
578 template<class... Args, class T>
579 struct PushBackTuple<typename std::tuple<Args...>, T>
580 {
581 typedef typename std::tuple<Args..., T> type;
582 };
583
590 template<class Tuple, class T>
592
593 template<class... Args, class T>
594 struct PushFrontTuple<typename std::tuple<Args...>, T>
595 {
596 typedef typename std::tuple<T, Args...> type;
597 };
598
611 template<
612 template <class, class> class F,
613 class Tuple,
614 class Seed=std::tuple<>,
615 int N=tuple_size<Tuple>::value>
617 {
618 typedef typename ReduceTuple<F, Tuple, Seed, N-1>::type Accumulated;
619 typedef typename std::tuple_element<N-1, Tuple>::type Value;
620
622 typedef typename F<Accumulated, Value>::type type;
623 };
624
635 template<
636 template <class, class> class F,
637 class Tuple,
638 class Seed>
639 struct ReduceTuple<F, Tuple, Seed, 0>
640 {
642 typedef Seed type;
643 };
644
654 template<class Head, class Tail>
660
669 template<class Tuple>
671 {
674 };
675
676}
677
678#endif
Traits for type conversions and type information.
Fallback implementation of the std::tuple class.
static void apply(Tuple &t)
Definition tupleutility.hh:481
ForEachValuePair(Tuple1 &t1, Tuple2 &t2)
Definition tupleutility.hh:409
auto transformTuple(Tuple &&orig, Args &&... args) -> decltype(genericTransformTuple(orig, makeTransformTupleFunctor< TypeEvaluator >(args...)))
Definition tupleutility.hh:241
ReduceTuple< F, Tuple, Seed, N-1 >::type Accumulated
Definition tupleutility.hh:618
Seed type
Result of the reduce operation.
Definition tupleutility.hh:642
std::remove_reference< T >::type * Type
Definition tupleutility.hh:270
static Type apply(T &t)
Definition tupleutility.hh:256
static ResultType apply()
Definition tupleutility.hh:41
std::tuple< Args... > ResultType
Definition tupleutility.hh:40
std::remove_reference< T >::type * Type
Definition tupleutility.hh:281
TransformTupleFunctor(Args &&... args)
Definition tupleutility.hh:190
std::tuple_element< std::tuple_size< Tuple >::value-N-1, Tuple >::type Type
Definition tupleutility.hh:434
static Type apply(T &t)
Definition tupleutility.hh:282
static TupleAccessTraits< typenameAtType< N, Tuple >::Type >::ConstType get(const Tuple &t)
Definition tupleutility.hh:456
T & Type
Definition tupleutility.hh:255
static TupleAccessTraits< typenameAtType< N, Tuple >::Type >::NonConstType get(Tuple &t)
Definition tupleutility.hh:449
void apply(Functor &f)
Definition tupleutility.hh:417
std::tuple< Args..., T > type
Definition tupleutility.hh:581
std::tuple_element< N-1, Tuple >::type Value
Definition tupleutility.hh:619
std::tuple< T, Args... > type
Definition tupleutility.hh:596
ReduceTuple< JoinTuples, Tuple >::type type
Result of the flatten operation.
Definition tupleutility.hh:673
static Type apply(T &t)
Definition tupleutility.hh:271
void visit(const P &p)
Definition tupleutility.hh:474
F< Accumulated, Value >::type type
Result of the reduce operation.
Definition tupleutility.hh:622
ForEachValue(Tuple &t)
Constructor.
Definition tupleutility.hh:374
ReduceTuple< PushBackTuple, Tail, Head >::type type
Result of the join operation.
Definition tupleutility.hh:658
void apply(Functor &f) const
Applies a function object to each storage element of the std::tuple.
Definition tupleutility.hh:381
std::tuple< typename TE< Args >::Type... > Type
Definition tupleutility.hh:77
auto genericTransformTuple(Tuple &&t, Functor &&f) -> decltype(genericTransformTupleBackend(t, f))
Definition tupleutility.hh:135
auto operator()(T &&t) -> decltype(this->apply(t, Std::index_sequence_for< Args... >{})) const
Definition tupleutility.hh:195
TransformTupleFunctor< TE, Args... > makeTransformTupleFunctor(Args &&... args)
Definition tupleutility.hh:203
STL namespace.
Dune namespace.
Definition alignment.hh:11
integer_sequence< std::size_t, Ints... > index_sequence
std::index_sequence as introduced in C++14
Definition utility.hh:54
make_index_sequence< impl::_get_pack_length< T... >{}> index_sequence_for
Definition utility.hh:101
template mapping a type to std::false_type
Definition type_traits.hh:59
Definition tuples.hh:35
A helper template that initializes a std::tuple consisting of pointers to nullptr.
Definition tupleutility.hh:35
Helper template to clone the type definition of a std::tuple with the storage types replaced by a use...
Definition tupleutility.hh:72
Definition tupleutility.hh:175
Definition tupleutility.hh:188
TypeEvaluator to turn a type T into a reference to T
Definition tupleutility.hh:254
TypeEvaluator to turn a type T into a pointer to T
Definition tupleutility.hh:269
Helper template which implements iteration over all storage elements in a std::tuple.
Definition tupleutility.hh:370
Extension of ForEachValue to two std::tuple's.
Definition tupleutility.hh:404
Type for reverse element access.
Definition tupleutility.hh:433
Reverse element access.
Definition tupleutility.hh:446
Deletes all objects pointed to in a std::tuple of pointers.
Definition tupleutility.hh:470
Finding the index of a certain type in a std::tuple.
Definition tupleutility.hh:518
Generator for predicates accepting one particular type.
Definition tupleutility.hh:545
The actual predicate.
Definition tupleutility.hh:548
Find the first occurrence of a type in a std::tuple.
Definition tupleutility.hh:567
Helper template to append a type to a std::tuple.
Definition tupleutility.hh:576
Helper template to prepend a type to a std::tuple.
Definition tupleutility.hh:591
Apply reduce with meta binary function to template.
Definition tupleutility.hh:617
Join two std::tuple's.
Definition tupleutility.hh:656
Flatten a std::tuple of std::tuple's.
Definition tupleutility.hh:671