| 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_COMPOSE_HPP |
| 14 |
#define RANGES_V3_FUNCTIONAL_COMPOSE_HPP |
| 15 |
|
| 16 |
#include <type_traits> |
| 17 |
#include <utility> |
| 18 |
|
| 19 |
#include <concepts/concepts.hpp> |
| 20 |
|
| 21 |
#include <range/v3/detail/config.hpp> |
| 22 |
#include <range/v3/functional/invoke.hpp> |
| 23 |
#include <range/v3/utility/static_const.hpp> |
| 24 |
|
| 25 |
#include <range/v3/detail/prologue.hpp> |
| 26 |
|
| 27 |
namespace ranges |
| 28 |
{ |
| 29 |
/// \addtogroup group-functional |
| 30 |
/// @{ |
| 31 |
template<typename Second, typename First> |
| 32 |
struct composed |
| 33 |
{ |
| 34 |
private: |
| 35 |
RANGES_NO_UNIQUE_ADDRESS |
| 36 |
First first_; |
| 37 |
RANGES_NO_UNIQUE_ADDRESS |
| 38 |
Second second_; |
| 39 |
|
| 40 |
// clang-format off |
| 41 |
template<typename A, typename B, typename... Ts> |
| 42 |
static constexpr auto // |
| 43 |
CPP_auto_fun(do_)(A &&a, B &&b, std::false_type, Ts &&... ts) |
| 44 |
( |
| 45 |
return invoke((B &&) b, invoke((A &&) a, (Ts &&) ts...)) |
| 46 |
) |
| 47 |
template<typename A, typename B, typename... Ts> |
| 48 |
static constexpr auto CPP_auto_fun(do_)(A &&a, B &&b, std::true_type, Ts &&... ts) |
| 49 |
( |
| 50 |
return (invoke((A &&) a, (Ts &&) ts...), invoke((B &&) b)) |
| 51 |
) |
| 52 |
public: |
| 53 |
composed() = default; |
| 54 |
// clang-format on |
| 55 |
constexpr composed(Second second, First first) |
| 56 |
: first_(std::move(first)) |
| 57 |
, second_(std::move(second)) |
| 58 |
{} |
| 59 |
// clang-format off |
| 60 |
template<typename... Ts> |
| 61 |
constexpr auto CPP_auto_fun(operator())(Ts &&... ts)(mutable &) |
| 62 |
( |
| 63 |
return composed::do_(first_, |
| 64 |
second_, |
| 65 |
std::is_void<invoke_result_t<First &, Ts...>>{}, |
| 66 |
(Ts &&) ts...) |
| 67 |
) |
| 68 |
template<typename... Ts> |
| 69 |
constexpr auto CPP_auto_fun(operator())(Ts &&... ts)(const &) |
| 70 |
( |
| 71 |
return composed::do_((First const &)first_, |
| 72 |
(Second const &)second_, |
| 73 |
std::is_void<invoke_result_t<First const &, Ts...>>{}, |
| 74 |
(Ts &&) ts...) |
| 75 |
) |
| 76 |
template<typename... Ts> |
| 77 |
constexpr auto CPP_auto_fun(operator())(Ts &&... ts)(mutable &&) |
| 78 |
( |
| 79 |
return composed::do_((First &&)first_, |
| 80 |
(Second &&)second_, |
| 81 |
std::is_void<invoke_result_t<First &&, Ts...>>{}, |
| 82 |
(Ts &&) ts...) |
| 83 |
) |
| 84 |
// clang-format on |
| 85 |
}; |
| 86 |
|
| 87 |
struct compose_fn |
| 88 |
{ |
| 89 |
template<typename Second, typename First> |
| 90 |
constexpr composed<Second, First> operator()(Second second, First first) const |
| 91 |
{ |
| 92 |
return {std::move(second), std::move(first)}; |
| 93 |
} |
| 94 |
}; |
| 95 |
|
| 96 |
/// \ingroup group-functional |
| 97 |
/// \sa `compose_fn` |
| 98 |
RANGES_INLINE_VARIABLE(compose_fn, compose) |
| 99 |
/// @} |
| 100 |
} // namespace ranges |
| 101 |
|
| 102 |
#include <range/v3/detail/epilogue.hpp> |
| 103 |
|
| 104 |
#endif |