Loading...
Searching...
No Matches
utility.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-FileCopyrightText: Copyright © DUNE Project contributors, see file AUTHORS.md
// SPDX-License-Identifier: LicenseRef-GPL-2.0-only-with-DUNE-exception OR LGPL-3.0-or-later
#ifndef DUNE_FUNCTIONS_COMMON_UTILITY_HH
#define DUNE_FUNCTIONS_COMMON_UTILITY_HH
#include <utility>
#include <type_traits>
#include <dune/common/overloadset.hh>
#include <dune/common/indices.hh>
#include <dune/functions/common/functionconcepts.hh>
namespace Dune {
namespace Functions {
template<class F, class size_type, size_type firstValue, class... Args>
[[deprecated("This function will be removed after Dune 2.11")]]
auto forwardAsStaticInteger(std::integer_sequence<size_type, firstValue> values, const size_type& i, F&& f, Args&&... args)
->decltype(f(std::integral_constant<size_type, firstValue>(), std::forward<Args>(args)...))
{
return f(std::integral_constant<size_type, firstValue>(), std::forward<Args>(args)...);
}
template<class F, class size_type, size_type firstValue, size_type secondValue, size_type... otherValues, class... Args>
[[deprecated("This function will be removed after Dune 2.11")]]
auto forwardAsStaticInteger(std::integer_sequence<size_type, firstValue, secondValue, otherValues...> values, const size_type i, F&& f, Args&&... args)
->decltype(f(std::integral_constant<size_type, firstValue>(), std::forward<Args>(args)...))
{
if (i==firstValue)
return f(std::integral_constant<size_type, firstValue>(), std::forward<Args>(args)...);
return forwardAsStaticInteger(std::integer_sequence<size_type, secondValue, otherValues...>(), i, std::forward<F>(f), std::forward<Args>(args)...);
}
template<std::size_t end, class F, class size_type, class... Args>
[[deprecated("This function will be removed after Dune 2.11, use Dune::Hybrid::switchCases.")]]
auto forwardAsStaticIndex(const size_type& i, F&& f, Args&&... args)
->decltype(f(Dune::Indices::_0, std::forward<Args>(args)...))
{
return forwardAsStaticInteger(std::make_index_sequence<end>{}, i, std::forward<F>(f), std::forward<Args>(args)...);
}
namespace Imp {
template<template<class...> class T, class List>
struct ExpandTupleHelper
{};
template<template<class...> class T, template<class...> class ListType, class... Args>
struct ExpandTupleHelper<T, ListType<Args...>>
{
using Type = T<Args...>;
};
} // end namespace Imp
template<template<class...> class T, class ArgTuple>
using ExpandTuple = typename Imp::ExpandTupleHelper<T, ArgTuple>::Type;
namespace Imp {
template<template<class...> class T, class... Tuple>
struct TransformTupleHelper
{};
template<template<class...> class T, class... Args1>
struct TransformTupleHelper<T, typename std::tuple<Args1...>>
{
using Type = std::tuple<T<Args1>...>;
};
template<template<class...> class T, class... Args1, class... Args2>
struct TransformTupleHelper<T, typename std::tuple<Args1...>, typename std::tuple<Args2...>>
{
using Type = std::tuple<T<Args1, Args2>...>;
};
} // end namespace Imp
template<template<class...> class F, class... Tuples>
using TransformTuple = typename Imp::TransformTupleHelper<F, Tuples...>::Type;
namespace Imp {
template<class F, class... T, std::size_t... k>
auto transformTupleHelper(F&& f, const std::tuple<T...>& tuple, std::index_sequence<k...>)
-> decltype(std::make_tuple(f(std::get<k>(tuple))...))
{
return std::make_tuple(f(std::get<k>(tuple))...);
}
template<class F, class... T1, class...T2, std::size_t... k>
auto transformTupleHelper(F&& f, const std::tuple<T1...>& tuple1, const std::tuple<T2...>& tuple2, std::index_sequence<k...>)
-> decltype(std::make_tuple(f(std::get<k>(tuple1), std::get<k>(tuple2))...))
{
return std::make_tuple(f(std::get<k>(tuple1), std::get<k>(tuple2))...);
}
} // end namespace Imp
template<class F, class... T>
auto transformTuple(F&& f, const std::tuple<T...>& tuple)
-> decltype(Imp::transformTupleHelper(std::forward<F>(f), tuple, std::index_sequence_for<T...>{}))
{
return Imp::transformTupleHelper(std::forward<F>(f), tuple, std::index_sequence_for<T...>{});
}
template<class F, class... T1, class... T2>
auto transformTuple(F&& f, const std::tuple<T1...>& tuple1, const std::tuple<T2...>& tuple2)
-> decltype(Imp::transformTupleHelper(std::forward<F>(f), tuple1, tuple2, std::index_sequence_for<T1...>{}))
{
return Imp::transformTupleHelper(std::forward<F>(f), tuple1, tuple2, std::index_sequence_for<T1...>{});
}
namespace Imp {
template<class IntegerSequence>
struct IntegerSequenceTupleHelper
{};
template<class I, I... k>
struct IntegerSequenceTupleHelper<std::integer_sequence<I, k...>>
{
using Type = std::tuple<std::integral_constant<I, k>...>;
};
} // end namespace Imp
template<class IntegerSequence>
using IntegerSequenceTuple= typename Imp::IntegerSequenceTupleHelper<IntegerSequence>::Type;
template<class... T>
struct LastType
{
using type = std::tuple_element_t<sizeof...(T)-1, std::tuple<T...>>;
};
namespace Imp {
template<class T, class I>
struct RotateHelper;
template<class... T, std::size_t... I>
struct RotateHelper<std::tuple<T...>, std::index_sequence<I...> >
{
using type = typename std::tuple<typename LastType<T...>::type, std::tuple_element_t<I,std::tuple<T...>>...>;
};
} // end namespace Imp
template<class... T>
struct RotateTuple
{
using type = typename Imp::RotateHelper<std::tuple<T...>, std::make_index_sequence<sizeof...(T)-1>>::type;
};
template<class Expression>
auto callableCheck(Expression f)
{
return [f](auto&&... args){
return Functions::Concept::isCallable(f, std::forward<decltype(args)>(args)...);
};
}
template<class Check>
auto negatePredicate(Check check)
{
return [check](auto&&... args){
auto negate = overload(
[](std::true_type) { return std::false_type{};},
[](std::false_type) { return std::true_type{};},
[](bool v) { return not v;});
return negate(check(std::forward<decltype(args)>(args)...));
};
}
namespace Impl {
// Wrapper to capture values in a lambda for perfect forwarding.
// This captures value types by value and reference types by reference.
template <typename T>
struct ForwardCaptureWrapper;
template <typename T>
struct ForwardCaptureWrapper
{
template <typename TT>
ForwardCaptureWrapper(TT&& t) : t_{std::forward<TT>(t)} {}
auto forward() const { return std::move(t_); }
T t_;
};
template <typename T>
struct ForwardCaptureWrapper<T&>
{
ForwardCaptureWrapper(T& t) : t_{t} {}
T& forward() const { return t_; };
T& t_;
};
template <typename T>
struct ForwardCaptureWrapper<const T&>
{
ForwardCaptureWrapper(const T& t) : t_{t} {}
const T& forward() const { return t_; };
const T& t_;
};
} // end namespace Dune::Functions::Impl
template <class T>
auto forwardCapture(T&& t)
{
return Impl::ForwardCaptureWrapper<T>(std::forward<T>(t));
}
} // namespace Dune::Functions
} // namespace Dune
#endif // DUNE_FUNCTIONS_COMMON_UTILITY_HH
functionconcepts.hh
Dune::Functions::Concept::isCallable
static constexpr auto isCallable()
Check if f is callable with given argument list.
Definition functionconcepts.hh:51
Dune::Functions::transformTuple
auto transformTuple(F &&f, const std::tuple< T... > &tuple) -> decltype(Imp::transformTupleHelper(std::forward< F >(f), tuple, std::index_sequence_for< T... >{}))
Transform tuple value using a functor.
Definition utility.hh:173
Dune::Functions::callableCheck
auto callableCheck(Expression f)
Create a predicate for checking validity of expressions.
Definition utility.hh:287
Dune::Functions::forwardAsStaticIndex
auto forwardAsStaticIndex(const size_type &i, F &&f, Args &&... args) -> decltype(f(Dune::Indices::_0, std::forward< Args >(args)...))
Transform dynamic index to static index_constant.
Definition utility.hh:68
Dune::Functions::TransformTuple
typename Imp::TransformTupleHelper< F, Tuples... >::Type TransformTuple
Transform tuple types argument using type-functor.
Definition utility.hh:139
Dune::Functions::negatePredicate
auto negatePredicate(Check check)
Negate given predicate.
Definition utility.hh:312
Dune::Functions::ExpandTuple
typename Imp::ExpandTupleHelper< T, ArgTuple >::Type ExpandTuple
Expand tuple arguments as template arguments.
Definition utility.hh:102
Dune
Definition monomialset.hh:19
Dune::Functions
Definition monomialset.hh:19
Dune::Functions::forwardCapture
auto forwardCapture(T &&t)
Create a capture object for perfect forwarding.
Definition utility.hh:380
Dune::Functions::forwardAsStaticInteger
auto forwardAsStaticInteger(std::integer_sequence< size_type, firstValue > values, const size_type &i, F &&f, Args &&... args) -> decltype(f(std::integral_constant< size_type, firstValue >(), std::forward< Args >(args)...))
Definition utility.hh:25
Dune::Functions::IntegerSequenceTuple
typename Imp::IntegerSequenceTupleHelper< IntegerSequence >::Type IntegerSequenceTuple
Transform integer_sequence<I,k...> to tuple<integral_constant<I,k>...>.
Definition utility.hh:219
Dune::Functions::LastType
Get last entry of type list.
Definition utility.hh:230
Dune::Functions::LastType::type
std::tuple_element_t< sizeof...(T) -1, std::tuple< T... > > type
Definition utility.hh:231
Dune::Functions::RotateTuple
Rotate type list by one, such that last entry is moved to first position.
Definition utility.hh:259
Dune::Functions::RotateTuple::type
typename Imp::RotateHelper< std::tuple< T... >, std::make_index_sequence< sizeof...(T) -1 > >::type type
Definition utility.hh:260