| 1 |
/// \file |
| 2 |
// Range v3 library |
| 3 |
// |
| 4 |
// Copyright Eric Niebler 2013-present |
| 5 |
// |
| 6 |
// Use, modification and distribution is subject to the |
| 7 |
// Boost Software License, Version 1.0. (See accompanying |
| 8 |
// file LICENSE_1_0.txt or copy at |
| 9 |
// http://www.boost.org/LICENSE_1_0.txt) |
| 10 |
// |
| 11 |
// Project home: https://github.com/ericniebler/range-v3 |
| 12 |
// |
| 13 |
#ifndef RANGES_V3_FUNCTIONAL_ON_HPP |
| 14 |
#define RANGES_V3_FUNCTIONAL_ON_HPP |
| 15 |
|
| 16 |
#include <concepts/concepts.hpp> |
| 17 |
|
| 18 |
#include <range/v3/detail/config.hpp> |
| 19 |
#include <range/v3/functional/invoke.hpp> |
| 20 |
|
| 21 |
#include <range/v3/detail/prologue.hpp> |
| 22 |
|
| 23 |
namespace ranges |
| 24 |
{ |
| 25 |
/// \addtogroup group-functional |
| 26 |
/// @{ |
| 27 |
template<typename Fn1, typename Fn2> |
| 28 |
struct transformed |
| 29 |
{ |
| 30 |
private: |
| 31 |
RANGES_NO_UNIQUE_ADDRESS |
| 32 |
Fn1 first_; |
| 33 |
RANGES_NO_UNIQUE_ADDRESS |
| 34 |
Fn2 second_; |
| 35 |
|
| 36 |
public: |
| 37 |
transformed() = default; |
| 38 |
constexpr transformed(Fn1 fn1, Fn2 fn2) |
| 39 |
: first_(static_cast<Fn1 &&>(fn1)) |
| 40 |
, second_(static_cast<Fn2 &&>(fn2)) |
| 41 |
{} |
| 42 |
// clang-format off |
| 43 |
template<typename... Args> |
| 44 |
auto CPP_auto_fun(operator())(Args &&... args) |
| 45 |
( |
| 46 |
return invoke(first_, invoke(second_, static_cast<Args &&>(args)...)) |
| 47 |
) |
| 48 |
template<typename... Args> |
| 49 |
auto CPP_auto_fun(operator())(Args &&... args)(const) |
| 50 |
( |
| 51 |
return invoke((Fn1 const &)first_, |
| 52 |
invoke((Fn2 const &)second_, static_cast<Args &&>(args))...) |
| 53 |
) |
| 54 |
// clang-format on |
| 55 |
}; |
| 56 |
|
| 57 |
struct on_fn |
| 58 |
{ |
| 59 |
template<typename Fn1, typename Fn2> |
| 60 |
constexpr transformed<Fn1, Fn2> operator()(Fn1 fn1, Fn2 fn2) const |
| 61 |
{ |
| 62 |
return transformed<Fn1, Fn2>{detail::move(fn1), detail::move(fn2)}; |
| 63 |
} |
| 64 |
}; |
| 65 |
|
| 66 |
/// \ingroup group-functional |
| 67 |
/// \sa `on_fn` |
| 68 |
RANGES_INLINE_VARIABLE(on_fn, on) |
| 69 |
/// @} |
| 70 |
} // namespace ranges |
| 71 |
|
| 72 |
#include <range/v3/detail/epilogue.hpp> |
| 73 |
|
| 74 |
#endif |