DUNE PDELab (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// SPDX-FileCopyrightInfo: Copyright © DUNE Project contributors, see file LICENSE.md in module root
4// SPDX-License-Identifier: LicenseRef-GPL-2.0-only-with-DUNE-exception
5
6#ifndef DUNE_TUPLE_UTILITY_HH
7#define DUNE_TUPLE_UTILITY_HH
8
9#include <cstddef>
10#include <tuple>
11#include <type_traits>
12#include <utility>
13
14#include <dune/common/hybridutilities.hh>
16
17namespace Dune {
18
41 template<class F, class ArgTuple, class I, I... i>
42 decltype(auto) applyPartial(F&& f, ArgTuple&& args, std::integer_sequence<I, i...> /*indices*/)
43 {
44 return f(std::get<i>(args)...);
45 }
46
47 template<class T>
48 struct TupleAccessTraits
49 {
50 typedef typename std::add_const<T>::type& ConstType;
51 typedef T& NonConstType;
52 typedef const typename std::remove_const<T>::type& ParameterType;
53 };
54
55 template<class T>
56 struct TupleAccessTraits<T*>
57 {
58 typedef typename std::add_const<T>::type* ConstType;
59 typedef T* NonConstType;
60 typedef T* ParameterType;
61 };
62
63 template<class T>
64 struct TupleAccessTraits<T&>
65 {
66 typedef T& ConstType;
67 typedef T& NonConstType;
68 typedef T& ParameterType;
69 };
70
78 template<class T>
80
81 template<class... Args>
82 struct NullPointerInitialiser<std::tuple<Args...> >
83 {
84 typedef std::tuple<Args...> ResultType;
85 static ResultType apply()
86 {
87 return ResultType(static_cast<Args>(nullptr)...);
88 }
89 };
90
115 template<template <class> class TE, class T>
117
118 template<template <class> class TE, class... Args>
119 struct ForEachType<TE, std::tuple<Args...> >
120 {
121 typedef std::tuple<typename TE<Args>::Type...> Type;
122 };
123
124#ifndef DOXYGEN
125 template<class Tuple, class Functor, std::size_t... I>
126 inline auto genericTransformTupleBackendImpl(Tuple& t, Functor& f, const std::index_sequence<I...>& )
127 -> std::tuple<decltype(f(std::get<I>(t)))...>
128 {
129 return std::tuple<decltype(f(std::get<I>(t)))...>(f(std::get<I>(t))...);
130 }
131
132 template<class... Args, class Functor>
133 auto genericTransformTupleBackend(std::tuple<Args...>& t, Functor& f) ->
134 decltype(genericTransformTupleBackendImpl(t, f,std::index_sequence_for<Args...>{}))
135 {
136 return genericTransformTupleBackendImpl(t, f,std::index_sequence_for<Args...>{});
137 }
138
139 template<class... Args, class Functor>
140 auto genericTransformTupleBackend(const std::tuple<Args...>& t, Functor& f) ->
141 decltype(genericTransformTupleBackendImpl(t, f, std::index_sequence_for<Args...>{}))
142 {
143 return genericTransformTupleBackendImpl(t, f, std::index_sequence_for<Args...>{});
144 }
145#endif
146
185 template<class Tuple, class Functor>
186 auto genericTransformTuple(Tuple&& t, Functor&& f) ->
187 decltype(genericTransformTupleBackend(t, f))
188 {
189 return genericTransformTupleBackend(t, f);
190 }
191
224 template<template<class> class TE, class... Args>
226 {
227 mutable std::tuple<Args&...> tup;
228
229 template<class T, std::size_t... I>
230 inline auto apply(T&& t, const std::index_sequence<I...>& ) ->
231 decltype(TE<T>::apply(t,std::get<I>(tup)...)) const
232 {
233 return TE<T>::apply(t,std::get<I>(tup)...);
234 }
235
236 public:
237 template<class T>
238 struct TypeEvaluator : public TE<T>
239 {};
240
241 TransformTupleFunctor(Args&&... args)
242 : tup(args...)
243 { }
244
245 template<class T>
246 inline auto operator()(T&& t) ->
247 decltype(this->apply(t,std::index_sequence_for<Args...>{})) const
248 {
249 return apply(t,std::index_sequence_for<Args...>{});
250 }
251 };
252
253 template<template<class> class TE, class... Args>
254 TransformTupleFunctor<TE, Args...> makeTransformTupleFunctor(Args&&... args)
255 {
256 return TransformTupleFunctor<TE, Args...>(args...);
257 }
258
291 template<template<class> class TypeEvaluator, class Tuple, class... Args>
292 auto transformTuple(Tuple&& orig, Args&&... args) ->
293 decltype(genericTransformTuple(orig, makeTransformTupleFunctor<TypeEvaluator>(args...)))
294 {
295 return genericTransformTuple(orig, makeTransformTupleFunctor<TypeEvaluator>(args...));
296 }
297
299
303 template<class T>
305 {
306 typedef T& Type;
307 static Type apply(T& t)
308 {
309 return t;
310 }
311 };
312
314
318 template<class T>
320 {
321 typedef typename std::remove_reference<T>::type* Type;
322 static Type apply(T& t)
323 {
324 return &t;
325 }
326 };
327
328 // Specialization, in case the type is already a reference
329 template<class T>
330 struct AddPtrTypeEvaluator<T&>
331 {
332 typedef typename std::remove_reference<T>::type* Type;
333 static Type apply(T& t)
334 {
335 return &t;
336 }
337 };
338
344 template<int N, class Tuple>
345 struct AtType
346 {
347 typedef typename std::tuple_element<std::tuple_size<Tuple>::value - N - 1, Tuple>::type Type;
348 };
349
357 template<int N>
358 struct At
359 {
360 template<typename Tuple>
361 static typename TupleAccessTraits<typename AtType<N, Tuple>::Type>::NonConstType
362 get(Tuple& t)
363 {
364 return std::get<std::tuple_size<Tuple>::value - N - 1>(t);
365 }
366
367 template<typename Tuple>
368 static typename TupleAccessTraits<typename AtType<N, Tuple>::Type>::ConstType
369 get(const Tuple& t)
370 {
371 return std::get<std::tuple_size<Tuple>::value - N - 1>(t);
372 }
373 };
374
378 template<class Tuple>
380 {
381 template<typename... Ts>
382 static void apply(std::tuple<Ts...>& t)
383 {
384 Hybrid::forEach(t,[&](auto&& ti){delete ti; ti=nullptr;});
385 }
386 };
387
411 template<class Tuple, template<class> class Predicate, std::size_t start = 0,
412 std::size_t size = std::tuple_size<Tuple>::value>
414 public std::conditional<Predicate<typename std::tuple_element<start,
415 Tuple>::type>::value,
416 std::integral_constant<std::size_t, start>,
417 FirstPredicateIndex<Tuple, Predicate, start+1> >::type
418 {
419 static_assert(std::tuple_size<Tuple>::value == size, "The \"size\" "
420 "template parameter of FirstPredicateIndex is an "
421 "implementation detail and should never be set "
422 "explicitly!");
423 };
424
425#ifndef DOXYGEN
426 template<class Tuple, template<class> class Predicate, std::size_t size>
427 class FirstPredicateIndex<Tuple, Predicate, size, size>
428 {
429 static_assert(AlwaysFalse<Tuple>::value, "None of the std::tuple element "
430 "types matches the predicate!");
431 };
432#endif // !DOXYGEN
433
443 template<class T>
444 struct IsType
445 {
447 template<class U>
448 struct Predicate : public std::is_same<T, U> {};
449 };
450
464 template<class Tuple, class T, std::size_t start = 0>
466 public FirstPredicateIndex<Tuple, IsType<T>::template Predicate, start>
467 { };
468
475 template<class Tuple, class T>
477
478 template<class... Args, class T>
479 struct PushBackTuple<typename std::tuple<Args...>, T>
480 {
481 typedef typename std::tuple<Args..., T> type;
482 };
483
490 template<class Tuple, class T>
492
493 template<class... Args, class T>
494 struct PushFrontTuple<typename std::tuple<Args...>, T>
495 {
496 typedef typename std::tuple<T, Args...> type;
497 };
498
511 template<
512 template <class, class> class F,
513 class Tuple,
514 class Seed=std::tuple<>,
515 int N=std::tuple_size<Tuple>::value>
517 {
518 typedef typename ReduceTuple<F, Tuple, Seed, N-1>::type Accumulated;
519 typedef typename std::tuple_element<N-1, Tuple>::type Value;
520
522 typedef typename F<Accumulated, Value>::type type;
523 };
524
535 template<
536 template <class, class> class F,
537 class Tuple,
538 class Seed>
539 struct ReduceTuple<F, Tuple, Seed, 0>
540 {
542 typedef Seed type;
543 };
544
554 template<class Head, class Tail>
556 {
559 };
560
569 template<class Tuple>
571 {
574 };
575
577}
578
579#endif
Finding the index of a certain type in a std::tuple.
Definition: tupleutility.hh:418
Definition: tupleutility.hh:226
Traits for type conversions and type information.
constexpr void forEach(Range &&range, F &&f)
Range based for loop.
Definition: hybridutilities.hh:256
auto transformTuple(Tuple &&orig, Args &&... args) -> decltype(genericTransformTuple(orig, makeTransformTupleFunctor< TypeEvaluator >(args...)))
Definition: tupleutility.hh:292
Seed type
Result of the reduce operation.
Definition: tupleutility.hh:542
ReduceTuple< JoinTuples, Tuple >::type type
Result of the flatten operation.
Definition: tupleutility.hh:573
F< Accumulated, Value >::type type
Result of the reduce operation.
Definition: tupleutility.hh:522
ReduceTuple< PushBackTuple, Tail, Head >::type type
Result of the join operation.
Definition: tupleutility.hh:558
auto genericTransformTuple(Tuple &&t, Functor &&f) -> decltype(genericTransformTupleBackend(t, f))
Definition: tupleutility.hh:186
decltype(auto) applyPartial(F &&f, ArgTuple &&args, std::integer_sequence< I, i... >)
Apply function with arguments from a given tuple.
Definition: tupleutility.hh:42
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
STL namespace.
TypeEvaluator to turn a type T into a pointer to T
Definition: tupleutility.hh:320
TypeEvaluator to turn a type T into a reference to T
Definition: tupleutility.hh:305
template which always yields a false value
Definition: typetraits.hh:124
Type for reverse element access.
Definition: tupleutility.hh:346
Reverse element access.
Definition: tupleutility.hh:359
Find the first occurrence of a type in a std::tuple.
Definition: tupleutility.hh:467
Flatten a std::tuple of std::tuple's.
Definition: tupleutility.hh:571
Helper template to clone the type definition of a std::tuple with the storage types replaced by a use...
Definition: tupleutility.hh:116
The actual predicate.
Definition: tupleutility.hh:448
Generator for predicates accepting one particular type.
Definition: tupleutility.hh:445
Join two std::tuple's.
Definition: tupleutility.hh:556
A helper template that initializes a std::tuple consisting of pointers to nullptr.
Definition: tupleutility.hh:79
Deletes all objects pointed to in a std::tuple of pointers.
Definition: tupleutility.hh:380
Helper template to append a type to a std::tuple.
Definition: tupleutility.hh:476
Helper template to prepend a type to a std::tuple.
Definition: tupleutility.hh:491
Apply reduce with meta binary function to template.
Definition: tupleutility.hh:517
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.111.3 (Nov 12, 23:30, 2024)