Dune Core Modules (2.8.0)

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#include <type_traits>
10#include <utility>
11
12#include <dune/common/hybridutilities.hh>
14
15namespace Dune {
16
39 template<class F, class ArgTuple, class I, I... i>
40 decltype(auto) applyPartial(F&& f, ArgTuple&& args, std::integer_sequence<I, i...> /*indices*/)
41 {
42 return f(std::get<i>(args)...);
43 }
44
45 template<class T>
46 struct TupleAccessTraits
47 {
48 typedef typename std::add_const<T>::type& ConstType;
49 typedef T& NonConstType;
50 typedef const typename std::remove_const<T>::type& ParameterType;
51 };
52
53 template<class T>
54 struct TupleAccessTraits<T*>
55 {
56 typedef typename std::add_const<T>::type* ConstType;
57 typedef T* NonConstType;
58 typedef T* ParameterType;
59 };
60
61 template<class T>
62 struct TupleAccessTraits<T&>
63 {
64 typedef T& ConstType;
65 typedef T& NonConstType;
66 typedef T& ParameterType;
67 };
68
76 template<class T>
78
79 template<class... Args>
80 struct NullPointerInitialiser<std::tuple<Args...> >
81 {
82 typedef std::tuple<Args...> ResultType;
83 static ResultType apply()
84 {
85 return ResultType(static_cast<Args>(nullptr)...);
86 }
87 };
88
113 template<template <class> class TE, class T>
115
116 template<template <class> class TE, class... Args>
117 struct ForEachType<TE, std::tuple<Args...> >
118 {
119 typedef std::tuple<typename TE<Args>::Type...> Type;
120 };
121
122#ifndef DOXYGEN
123 template<class Tuple, class Functor, std::size_t... I>
124 inline auto genericTransformTupleBackendImpl(Tuple& t, Functor& f, const std::index_sequence<I...>& )
125 -> std::tuple<decltype(f(std::get<I>(t)))...>
126 {
127 return std::tuple<decltype(f(std::get<I>(t)))...>(f(std::get<I>(t))...);
128 }
129
130 template<class... Args, class Functor>
131 auto genericTransformTupleBackend(std::tuple<Args...>& t, Functor& f) ->
132 decltype(genericTransformTupleBackendImpl(t, f,std::index_sequence_for<Args...>{}))
133 {
134 return genericTransformTupleBackendImpl(t, f,std::index_sequence_for<Args...>{});
135 }
136
137 template<class... Args, class Functor>
138 auto genericTransformTupleBackend(const std::tuple<Args...>& t, Functor& f) ->
139 decltype(genericTransformTupleBackendImpl(t, f, std::index_sequence_for<Args...>{}))
140 {
141 return genericTransformTupleBackendImpl(t, f, std::index_sequence_for<Args...>{});
142 }
143#endif
144
183 template<class Tuple, class Functor>
184 auto genericTransformTuple(Tuple&& t, Functor&& f) ->
185 decltype(genericTransformTupleBackend(t, f))
186 {
187 return genericTransformTupleBackend(t, f);
188 }
189
222 template<template<class> class TE, class... Args>
224 {
225 mutable std::tuple<Args&...> tup;
226
227 template<class T, std::size_t... I>
228 inline auto apply(T&& t, const std::index_sequence<I...>& ) ->
229 decltype(TE<T>::apply(t,std::get<I>(tup)...)) const
230 {
231 return TE<T>::apply(t,std::get<I>(tup)...);
232 }
233
234 public:
235 template<class T>
236 struct TypeEvaluator : public TE<T>
237 {};
238
239 TransformTupleFunctor(Args&&... args)
240 : tup(args...)
241 { }
242
243 template<class T>
244 inline auto operator()(T&& t) ->
245 decltype(this->apply(t,std::index_sequence_for<Args...>{})) const
246 {
247 return apply(t,std::index_sequence_for<Args...>{});
248 }
249 };
250
251 template<template<class> class TE, class... Args>
252 TransformTupleFunctor<TE, Args...> makeTransformTupleFunctor(Args&&... args)
253 {
254 return TransformTupleFunctor<TE, Args...>(args...);
255 }
256
289 template<template<class> class TypeEvaluator, class Tuple, class... Args>
290 auto transformTuple(Tuple&& orig, Args&&... args) ->
291 decltype(genericTransformTuple(orig, makeTransformTupleFunctor<TypeEvaluator>(args...)))
292 {
293 return genericTransformTuple(orig, makeTransformTupleFunctor<TypeEvaluator>(args...));
294 }
295
297
301 template<class T>
303 {
304 typedef T& Type;
305 static Type apply(T& t)
306 {
307 return t;
308 }
309 };
310
312
316 template<class T>
318 {
319 typedef typename std::remove_reference<T>::type* Type;
320 static Type apply(T& t)
321 {
322 return &t;
323 }
324 };
325
326 // Specialization, in case the type is already a reference
327 template<class T>
328 struct AddPtrTypeEvaluator<T&>
329 {
330 typedef typename std::remove_reference<T>::type* Type;
331 static Type apply(T& t)
332 {
333 return &t;
334 }
335 };
336
342 template<int N, class Tuple>
343 struct AtType
344 {
345 typedef typename std::tuple_element<std::tuple_size<Tuple>::value - N - 1, Tuple>::type Type;
346 };
347
355 template<int N>
356 struct At
357 {
358 template<typename Tuple>
359 static typename TupleAccessTraits<typename AtType<N, Tuple>::Type>::NonConstType
360 get(Tuple& t)
361 {
362 return std::get<std::tuple_size<Tuple>::value - N - 1>(t);
363 }
364
365 template<typename Tuple>
366 static typename TupleAccessTraits<typename AtType<N, Tuple>::Type>::ConstType
367 get(const Tuple& t)
368 {
369 return std::get<std::tuple_size<Tuple>::value - N - 1>(t);
370 }
371 };
372
376 template<class Tuple>
378 {
379 template<typename... Ts>
380 static void apply(std::tuple<Ts...>& t)
381 {
382 Hybrid::forEach(t,[&](auto&& ti){delete ti; ti=nullptr;});
383 }
384 };
385
409 template<class Tuple, template<class> class Predicate, std::size_t start = 0,
410 std::size_t size = std::tuple_size<Tuple>::value>
412 public std::conditional<Predicate<typename std::tuple_element<start,
413 Tuple>::type>::value,
414 std::integral_constant<std::size_t, start>,
415 FirstPredicateIndex<Tuple, Predicate, start+1> >::type
416 {
417 static_assert(std::tuple_size<Tuple>::value == size, "The \"size\" "
418 "template parameter of FirstPredicateIndex is an "
419 "implementation detail and should never be set "
420 "explicitly!");
421 };
422
423#ifndef DOXYGEN
424 template<class Tuple, template<class> class Predicate, std::size_t size>
425 class FirstPredicateIndex<Tuple, Predicate, size, size>
426 {
427 static_assert(AlwaysFalse<Tuple>::value, "None of the std::tuple element "
428 "types matches the predicate!");
429 };
430#endif // !DOXYGEN
431
441 template<class T>
442 struct IsType
443 {
445 template<class U>
446 struct Predicate : public std::is_same<T, U> {};
447 };
448
462 template<class Tuple, class T, std::size_t start = 0>
464 public FirstPredicateIndex<Tuple, IsType<T>::template Predicate, start>
465 { };
466
473 template<class Tuple, class T>
475
476 template<class... Args, class T>
477 struct PushBackTuple<typename std::tuple<Args...>, T>
478 {
479 typedef typename std::tuple<Args..., T> type;
480 };
481
488 template<class Tuple, class T>
490
491 template<class... Args, class T>
492 struct PushFrontTuple<typename std::tuple<Args...>, T>
493 {
494 typedef typename std::tuple<T, Args...> type;
495 };
496
509 template<
510 template <class, class> class F,
511 class Tuple,
512 class Seed=std::tuple<>,
513 int N=std::tuple_size<Tuple>::value>
515 {
516 typedef typename ReduceTuple<F, Tuple, Seed, N-1>::type Accumulated;
517 typedef typename std::tuple_element<N-1, Tuple>::type Value;
518
520 typedef typename F<Accumulated, Value>::type type;
521 };
522
533 template<
534 template <class, class> class F,
535 class Tuple,
536 class Seed>
537 struct ReduceTuple<F, Tuple, Seed, 0>
538 {
540 typedef Seed type;
541 };
542
552 template<class Head, class Tail>
554 {
557 };
558
567 template<class Tuple>
569 {
572 };
573
575}
576
577#endif
Finding the index of a certain type in a std::tuple.
Definition: tupleutility.hh:416
Definition: tupleutility.hh:224
Traits for type conversions and type information.
constexpr void forEach(Range &&range, F &&f)
Range based for loop.
Definition: hybridutilities.hh:266
auto transformTuple(Tuple &&orig, Args &&... args) -> decltype(genericTransformTuple(orig, makeTransformTupleFunctor< TypeEvaluator >(args...)))
Definition: tupleutility.hh:290
Seed type
Result of the reduce operation.
Definition: tupleutility.hh:540
ReduceTuple< JoinTuples, Tuple >::type type
Result of the flatten operation.
Definition: tupleutility.hh:571
F< Accumulated, Value >::type type
Result of the reduce operation.
Definition: tupleutility.hh:520
ReduceTuple< PushBackTuple, Tail, Head >::type type
Result of the join operation.
Definition: tupleutility.hh:556
auto genericTransformTuple(Tuple &&t, Functor &&f) -> decltype(genericTransformTupleBackend(t, f))
Definition: tupleutility.hh:184
decltype(auto) applyPartial(F &&f, ArgTuple &&args, std::integer_sequence< I, i... >)
Apply function with arguments from a given tuple.
Definition: tupleutility.hh:40
Dune namespace.
Definition: alignedallocator.hh:11
STL namespace.
TypeEvaluator to turn a type T into a pointer to T
Definition: tupleutility.hh:318
TypeEvaluator to turn a type T into a reference to T
Definition: tupleutility.hh:303
template which always yields a false value
Definition: typetraits.hh:124
Type for reverse element access.
Definition: tupleutility.hh:344
Reverse element access.
Definition: tupleutility.hh:357
Find the first occurrence of a type in a std::tuple.
Definition: tupleutility.hh:465
Flatten a std::tuple of std::tuple's.
Definition: tupleutility.hh:569
Helper template to clone the type definition of a std::tuple with the storage types replaced by a use...
Definition: tupleutility.hh:114
The actual predicate.
Definition: tupleutility.hh:446
Generator for predicates accepting one particular type.
Definition: tupleutility.hh:443
Join two std::tuple's.
Definition: tupleutility.hh:554
A helper template that initializes a std::tuple consisting of pointers to nullptr.
Definition: tupleutility.hh:77
Deletes all objects pointed to in a std::tuple of pointers.
Definition: tupleutility.hh:378
Helper template to append a type to a std::tuple.
Definition: tupleutility.hh:474
Helper template to prepend a type to a std::tuple.
Definition: tupleutility.hh:489
Apply reduce with meta binary function to template.
Definition: tupleutility.hh:515
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.111.3 (Nov 21, 23:30, 2024)