| 1 |
|
| 2 |
/// \file |
| 3 |
// Range v3 library |
| 4 |
// |
| 5 |
// Copyright Eric Niebler 2013-present |
| 6 |
// |
| 7 |
// Use, modification and distribution is subject to the |
| 8 |
// Boost Software License, Version 1.0. (See accompanying |
| 9 |
// file LICENSE_1_0.txt or copy at |
| 10 |
// http://www.boost.org/LICENSE_1_0.txt) |
| 11 |
// |
| 12 |
// Project home: https://github.com/ericniebler/range-v3 |
| 13 |
// |
| 14 |
#ifndef RANGES_V3_FUNCTIONAL_INDIRECT_HPP |
| 15 |
#define RANGES_V3_FUNCTIONAL_INDIRECT_HPP |
| 16 |
|
| 17 |
#include <utility> |
| 18 |
|
| 19 |
#include <concepts/concepts.hpp> |
| 20 |
|
| 21 |
#include <range/v3/functional/invoke.hpp> |
| 22 |
#include <range/v3/iterator/traits.hpp> |
| 23 |
#include <range/v3/utility/move.hpp> |
| 24 |
#include <range/v3/utility/static_const.hpp> |
| 25 |
|
| 26 |
#include <range/v3/detail/prologue.hpp> |
| 27 |
|
| 28 |
namespace ranges |
| 29 |
{ |
| 30 |
/// \addtogroup group-functional |
| 31 |
/// @{ |
| 32 |
template<typename Fn> |
| 33 |
struct indirected |
| 34 |
{ |
| 35 |
private: |
| 36 |
RANGES_NO_UNIQUE_ADDRESS |
| 37 |
Fn fn_; |
| 38 |
|
| 39 |
public: |
| 40 |
indirected() = default; |
| 41 |
indirected(Fn fn) |
| 42 |
: fn_(std::move(fn)) |
| 43 |
{} |
| 44 |
// value_type (needs no impl) |
| 45 |
template<typename... Its> |
| 46 |
[[noreturn]] invoke_result_t<Fn &, iter_reference_t<Its>...> // |
| 47 |
operator()(copy_tag, Its...) const |
| 48 |
{ |
| 49 |
RANGES_EXPECT(false); |
| 50 |
} |
| 51 |
|
| 52 |
// Reference |
| 53 |
// clang-format off |
| 54 |
template<typename... Its> |
| 55 |
auto CPP_auto_fun(operator())(Its... its) |
| 56 |
( |
| 57 |
return invoke(fn_, *its...) |
| 58 |
) |
| 59 |
template<typename... Its> |
| 60 |
auto CPP_auto_fun(operator())(Its... its)(const) |
| 61 |
( |
| 62 |
return invoke((Fn const &)fn_, *its...) |
| 63 |
) |
| 64 |
|
| 65 |
// Rvalue reference |
| 66 |
template<typename... Its> |
| 67 |
auto CPP_auto_fun(operator())(move_tag, Its... its) |
| 68 |
( |
| 69 |
return static_cast< |
| 70 |
aux::move_t<invoke_result_t<Fn &, iter_reference_t<Its>...>>>( |
| 71 |
aux::move(invoke(fn_, *its...))) |
| 72 |
) |
| 73 |
template<typename... Its> |
| 74 |
auto CPP_auto_fun(operator())(move_tag, Its... its)(const) |
| 75 |
( |
| 76 |
return static_cast< |
| 77 |
aux::move_t<invoke_result_t<Fn const &, iter_reference_t<Its>...>>>( |
| 78 |
aux::move(invoke((Fn const &)fn_, *its...))) |
| 79 |
) |
| 80 |
// clang-format on |
| 81 |
}; |
| 82 |
|
| 83 |
struct indirect_fn |
| 84 |
{ |
| 85 |
template<typename Fn> |
| 86 |
constexpr indirected<Fn> operator()(Fn fn) const |
| 87 |
{ |
| 88 |
return indirected<Fn>{detail::move(fn)}; |
| 89 |
} |
| 90 |
}; |
| 91 |
|
| 92 |
/// \ingroup group-functional |
| 93 |
/// \sa `indirect_fn` |
| 94 |
RANGES_INLINE_VARIABLE(indirect_fn, indirect) |
| 95 |
/// @} |
| 96 |
} // namespace ranges |
| 97 |
|
| 98 |
#include <range/v3/detail/epilogue.hpp> |
| 99 |
|
| 100 |
#endif |