Loading...
Searching...
No Matches
tupleutility.hh
Go to the documentation of this file.
// -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
// vi: set et ts=4 sw=2 sts=2:
// SPDX-FileCopyrightInfo: Copyright © DUNE Project contributors, see file LICENSE.md in module root
// SPDX-License-Identifier: LicenseRef-GPL-2.0-only-with-DUNE-exception
#ifndef DUNE_TUPLE_UTILITY_HH
#define DUNE_TUPLE_UTILITY_HH
#include <cstddef>
#include <tuple>
#include <type_traits>
#include <utility>
#include <dune/common/hybridutilities.hh>
#include <dune/common/typetraits.hh>
namespace Dune {
template<class F, class ArgTuple, class I, I... i>
decltype(auto) applyPartial(F&& f, ArgTuple&& args, std::integer_sequence<I, i...> /*indices*/)
{
return f(std::get<i>(args)...);
}
template<class T>
struct TupleAccessTraits
{
typedef typename std::add_const<T>::type& ConstType;
typedef T& NonConstType;
typedef const typename std::remove_const<T>::type& ParameterType;
};
template<class T>
struct TupleAccessTraits<T*>
{
typedef typename std::add_const<T>::type* ConstType;
typedef T* NonConstType;
typedef T* ParameterType;
};
template<class T>
struct TupleAccessTraits<T&>
{
typedef T& ConstType;
typedef T& NonConstType;
typedef T& ParameterType;
};
template<class T>
struct NullPointerInitialiser;
template<class... Args>
struct NullPointerInitialiser<std::tuple<Args...> >
{
typedef std::tuple<Args...> ResultType;
static ResultType apply()
{
return ResultType(static_cast<Args>(nullptr)...);
}
};
template<template <class> class TE, class T>
struct ForEachType;
template<template <class> class TE, class... Args>
struct ForEachType<TE, std::tuple<Args...> >
{
typedef std::tuple<typename TE<Args>::Type...> Type;
};
#ifndef DOXYGEN
template<class Tuple, class Functor, std::size_t... I>
inline auto genericTransformTupleBackendImpl(Tuple& t, Functor& f, const std::index_sequence<I...>& )
-> std::tuple<decltype(f(std::get<I>(t)))...>
{
return std::tuple<decltype(f(std::get<I>(t)))...>(f(std::get<I>(t))...);
}
template<class... Args, class Functor>
auto genericTransformTupleBackend(std::tuple<Args...>& t, Functor& f) ->
decltype(genericTransformTupleBackendImpl(t, f,std::index_sequence_for<Args...>{}))
{
return genericTransformTupleBackendImpl(t, f,std::index_sequence_for<Args...>{});
}
template<class... Args, class Functor>
auto genericTransformTupleBackend(const std::tuple<Args...>& t, Functor& f) ->
decltype(genericTransformTupleBackendImpl(t, f, std::index_sequence_for<Args...>{}))
{
return genericTransformTupleBackendImpl(t, f, std::index_sequence_for<Args...>{});
}
#endif
template<class Tuple, class Functor>
auto genericTransformTuple(Tuple&& t, Functor&& f) ->
decltype(genericTransformTupleBackend(t, f))
{
return genericTransformTupleBackend(t, f);
}
template<template<class> class TE, class... Args>
class TransformTupleFunctor
{
mutable std::tuple<Args&...> tup;
template<class T, std::size_t... I>
inline auto apply(T&& t, const std::index_sequence<I...>& ) ->
decltype(TE<T>::apply(t,std::get<I>(tup)...)) const
{
return TE<T>::apply(t,std::get<I>(tup)...);
}
public:
template<class T>
struct TypeEvaluator : public TE<T>
{};
TransformTupleFunctor(Args&&... args)
: tup(args...)
{ }
template<class T>
inline auto operator()(T&& t) ->
decltype(this->apply(t,std::index_sequence_for<Args...>{})) const
{
return apply(t,std::index_sequence_for<Args...>{});
}
};
template<template<class> class TE, class... Args>
TransformTupleFunctor<TE, Args...> makeTransformTupleFunctor(Args&&... args)
{
return TransformTupleFunctor<TE, Args...>(args...);
}
template<template<class> class TypeEvaluator, class Tuple, class... Args>
auto transformTuple(Tuple&& orig, Args&&... args) ->
decltype(genericTransformTuple(orig, makeTransformTupleFunctor<TypeEvaluator>(args...)))
{
return genericTransformTuple(orig, makeTransformTupleFunctor<TypeEvaluator>(args...));
}
template<class T>
struct AddRefTypeEvaluator
{
typedef T& Type;
static Type apply(T& t)
{
return t;
}
};
template<class T>
struct AddPtrTypeEvaluator
{
typedef typename std::remove_reference<T>::type* Type;
static Type apply(T& t)
{
return &t;
}
};
// Specialization, in case the type is already a reference
template<class T>
struct AddPtrTypeEvaluator<T&>
{
typedef typename std::remove_reference<T>::type* Type;
static Type apply(T& t)
{
return &t;
}
};
template<int N, class Tuple>
struct AtType
{
typedef typename std::tuple_element<std::tuple_size<Tuple>::value - N - 1, Tuple>::type Type;
};
template<int N>
struct At
{
template<typename Tuple>
static typename TupleAccessTraits<typename AtType<N, Tuple>::Type>::NonConstType
get(Tuple& t)
{
return std::get<std::tuple_size<Tuple>::value - N - 1>(t);
}
template<typename Tuple>
static typename TupleAccessTraits<typename AtType<N, Tuple>::Type>::ConstType
get(const Tuple& t)
{
return std::get<std::tuple_size<Tuple>::value - N - 1>(t);
}
};
template<class Tuple>
struct PointerPairDeletor
{
template<typename... Ts>
static void apply(std::tuple<Ts...>& t)
{
Hybrid::forEach(t,[&](auto&& ti){delete ti; ti=nullptr;});
}
};
template<class Tuple, template<class> class Predicate, std::size_t start = 0,
std::size_t size = std::tuple_size<Tuple>::value>
class FirstPredicateIndex :
public std::conditional<Predicate<typename std::tuple_element<start,
Tuple>::type>::value,
std::integral_constant<std::size_t, start>,
FirstPredicateIndex<Tuple, Predicate, start+1> >::type
{
static_assert(std::tuple_size<Tuple>::value == size, "The \"size\" "
"template parameter of FirstPredicateIndex is an "
"implementation detail and should never be set "
"explicitly!");
};
#ifndef DOXYGEN
template<class Tuple, template<class> class Predicate, std::size_t size>
class FirstPredicateIndex<Tuple, Predicate, size, size>
{
static_assert(AlwaysFalse<Tuple>::value, "None of the std::tuple element "
"types matches the predicate!");
};
#endif // !DOXYGEN
template<class T>
struct IsType
{
template<class U>
struct Predicate : public std::is_same<T, U> {};
};
template<class Tuple, class T, std::size_t start = 0>
struct FirstTypeIndex :
public FirstPredicateIndex<Tuple, IsType<T>::template Predicate, start>
{ };
template<class Tuple, class T>
struct PushBackTuple;
template<class... Args, class T>
struct PushBackTuple<typename std::tuple<Args...>, T>
{
typedef typename std::tuple<Args..., T> type;
};
template<class Tuple, class T>
struct PushFrontTuple;
template<class... Args, class T>
struct PushFrontTuple<typename std::tuple<Args...>, T>
{
typedef typename std::tuple<T, Args...> type;
};
template<
template <class, class> class F,
class Tuple,
class Seed=std::tuple<>,
int N=std::tuple_size<Tuple>::value>
struct ReduceTuple
{
typedef typename ReduceTuple<F, Tuple, Seed, N-1>::type Accumulated;
typedef typename std::tuple_element<N-1, Tuple>::type Value;
typedef typename F<Accumulated, Value>::type type;
};
template<
template <class, class> class F,
class Tuple,
class Seed>
struct ReduceTuple<F, Tuple, Seed, 0>
{
typedef Seed type;
};
template<class Head, class Tail>
struct JoinTuples
{
typedef typename ReduceTuple<PushBackTuple, Tail, Head>::type type;
};
template<class Tuple>
struct FlattenTuple
{
typedef typename ReduceTuple<JoinTuples, Tuple>::type type;
};
}
#endif
typetraits.hh
Traits for type conversions and type information.
hybridutilities.hh
Dune::applyPartial
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::TupleAccessTraits::ConstType
std::add_const< T >::type & ConstType
Definition tupleutility.hh:50
Dune::PointerPairDeletor::apply
static void apply(std::tuple< Ts... > &t)
Definition tupleutility.hh:382
Dune::TupleAccessTraits< T * >::ParameterType
T * ParameterType
Definition tupleutility.hh:60
Dune::transformTuple
auto transformTuple(Tuple &&orig, Args &&... args) -> decltype(genericTransformTuple(orig, makeTransformTupleFunctor< TypeEvaluator >(args...)))
Definition tupleutility.hh:292
Dune::TupleAccessTraits< T & >::ConstType
T & ConstType
Definition tupleutility.hh:66
Dune::ReduceTuple::Accumulated
ReduceTuple< F, Tuple, Seed, N-1 >::type Accumulated
Definition tupleutility.hh:518
Dune::TupleAccessTraits< T * >::NonConstType
T * NonConstType
Definition tupleutility.hh:59
Dune::ReduceTuple< F, Tuple, Seed, 0 >::type
Seed type
Result of the reduce operation.
Definition tupleutility.hh:542
Dune::AddPtrTypeEvaluator::Type
std::remove_reference< T >::type * Type
Definition tupleutility.hh:321
Dune::AddRefTypeEvaluator::apply
static Type apply(T &t)
Definition tupleutility.hh:307
Dune::NullPointerInitialiser< std::tuple< Args... > >::apply
static ResultType apply()
Definition tupleutility.hh:85
Dune::NullPointerInitialiser< std::tuple< Args... > >::ResultType
std::tuple< Args... > ResultType
Definition tupleutility.hh:84
Dune::AddPtrTypeEvaluator< T & >::Type
std::remove_reference< T >::type * Type
Definition tupleutility.hh:332
Dune::TransformTupleFunctor::TransformTupleFunctor
TransformTupleFunctor(Args &&... args)
Definition tupleutility.hh:241
Dune::AtType::Type
std::tuple_element< std::tuple_size< Tuple >::value-N-1, Tuple >::type Type
Definition tupleutility.hh:347
Dune::AddPtrTypeEvaluator< T & >::apply
static Type apply(T &t)
Definition tupleutility.hh:333
Dune::TransformTupleFunctor::operator()
auto operator()(T &&t) -> decltype(this->apply(t, std::index_sequence_for< Args... >{})) const
Definition tupleutility.hh:246
Dune::TupleAccessTraits< T & >::NonConstType
T & NonConstType
Definition tupleutility.hh:67
Dune::TupleAccessTraits::NonConstType
T & NonConstType
Definition tupleutility.hh:51
Dune::At::get
static TupleAccessTraits< typenameAtType< N, Tuple >::Type >::ConstType get(const Tuple &t)
Definition tupleutility.hh:369
Dune::AddRefTypeEvaluator::Type
T & Type
Definition tupleutility.hh:306
Dune::At::get
static TupleAccessTraits< typenameAtType< N, Tuple >::Type >::NonConstType get(Tuple &t)
Definition tupleutility.hh:362
Dune::PushBackTuple< typename std::tuple< Args... >, T >::type
std::tuple< Args..., T > type
Definition tupleutility.hh:481
Dune::type::Value
std::tuple_element< N-1, Tuple >::type Value
Definition tupleutility.hh:519
Dune::PushFrontTuple< typename std::tuple< Args... >, T >::type
std::tuple< T, Args... > type
Definition tupleutility.hh:496
Dune::FlattenTuple::type
ReduceTuple< JoinTuples, Tuple >::type type
Result of the flatten operation.
Definition tupleutility.hh:573
Dune::AddPtrTypeEvaluator::apply
static Type apply(T &t)
Definition tupleutility.hh:322
Dune::ReduceTuple::type
F< Accumulated, Value >::type type
Result of the reduce operation.
Definition tupleutility.hh:522
Dune::JoinTuples::type
ReduceTuple< PushBackTuple, Tail, Head >::type type
Result of the join operation.
Definition tupleutility.hh:558
Dune::ForEachType< TE, std::tuple< Args... > >::Type
std::tuple< typename TE< Args >::Type... > Type
Definition tupleutility.hh:121
Dune::genericTransformTuple
auto genericTransformTuple(Tuple &&t, Functor &&f) -> decltype(genericTransformTupleBackend(t, f))
Definition tupleutility.hh:186
Dune::TupleAccessTraits< T & >::ParameterType
T & ParameterType
Definition tupleutility.hh:68
Dune::TupleAccessTraits::ParameterType
const std::remove_const< T >::type & ParameterType
Definition tupleutility.hh:52
Dune::TupleAccessTraits< T * >::ConstType
std::add_const< T >::type * ConstType
Definition tupleutility.hh:58
Dune::makeTransformTupleFunctor
TransformTupleFunctor< TE, Args... > makeTransformTupleFunctor(Args &&... args)
Definition tupleutility.hh:254
Dune::Hybrid::forEach
constexpr void forEach(Range &&range, F &&f)
Range based for loop.
Definition hybridutilities.hh:261
std
STL namespace.
Dune
Dune namespace
Definition alignedallocator.hh:13
Dune::size
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
Dune::TupleAccessTraits
Definition tupleutility.hh:49
Dune::NullPointerInitialiser
A helper template that initializes a std::tuple consisting of pointers to nullptr.
Definition tupleutility.hh:79
Dune::ForEachType
Helper template to clone the type definition of a std::tuple with the storage types replaced by a use...
Definition tupleutility.hh:116
Dune::TransformTupleFunctor
Definition tupleutility.hh:226
Dune::TransformTupleFunctor::TypeEvaluator
Definition tupleutility.hh:239
Dune::AddRefTypeEvaluator
TypeEvaluator to turn a type T into a reference to T
Definition tupleutility.hh:305
Dune::AddPtrTypeEvaluator
TypeEvaluator to turn a type T into a pointer to T
Definition tupleutility.hh:320
Dune::AtType
Type for reverse element access.
Definition tupleutility.hh:346
Dune::At
Reverse element access.
Definition tupleutility.hh:359
Dune::PointerPairDeletor
Deletes all objects pointed to in a std::tuple of pointers.
Definition tupleutility.hh:380
Dune::FirstPredicateIndex
Finding the index of a certain type in a std::tuple.
Definition tupleutility.hh:418
Dune::IsType
Generator for predicates accepting one particular type.
Definition tupleutility.hh:445
Dune::IsType::Predicate
The actual predicate.
Definition tupleutility.hh:448
Dune::FirstTypeIndex
Find the first occurrence of a type in a std::tuple.
Definition tupleutility.hh:467
Dune::PushBackTuple
Helper template to append a type to a std::tuple.
Definition tupleutility.hh:476
Dune::PushFrontTuple
Helper template to prepend a type to a std::tuple.
Definition tupleutility.hh:491
Dune::type
Dune::JoinTuples
Join two std::tuple's.
Definition tupleutility.hh:556
Dune::FlattenTuple
Flatten a std::tuple of std::tuple's.
Definition tupleutility.hh:571
Dune::AlwaysFalse
template which always yields a false value
Definition typetraits.hh:124