| 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_PIPEABLE_HPP |
| 14 |
#define RANGES_V3_FUNCTIONAL_PIPEABLE_HPP |
| 15 |
|
| 16 |
#include <concepts/concepts.hpp> |
| 17 |
|
| 18 |
#include <range/v3/range_fwd.hpp> |
| 19 |
|
| 20 |
#include <range/v3/functional/compose.hpp> |
| 21 |
#include <range/v3/functional/concepts.hpp> |
| 22 |
#include <range/v3/utility/static_const.hpp> |
| 23 |
|
| 24 |
#include <range/v3/detail/prologue.hpp> |
| 25 |
|
| 26 |
namespace ranges |
| 27 |
{ |
| 28 |
/// \addtogroup group-functional |
| 29 |
/// @{ |
| 30 |
|
| 31 |
struct pipeable_base; |
| 32 |
|
| 33 |
template<typename T> |
| 34 |
RANGES_INLINE_VAR constexpr bool is_pipeable_v = META_IS_BASE_OF(pipeable_base, T); |
| 35 |
|
| 36 |
template<typename T> |
| 37 |
RANGES_INLINE_VAR constexpr bool is_pipeable_v<T &> = META_IS_BASE_OF(pipeable_base, |
| 38 |
T); |
| 39 |
template<typename T> |
| 40 |
RANGES_INLINE_VAR constexpr bool is_pipeable_v<T &&> = META_IS_BASE_OF(pipeable_base, |
| 41 |
T); |
| 42 |
template<typename T> |
| 43 |
using is_pipeable = meta::bool_<is_pipeable_v<T>>; |
| 44 |
|
| 45 |
struct make_pipeable_fn |
| 46 |
{ |
| 47 |
template<typename Fun, typename PipeableBase = pipeable_base> |
| 48 |
constexpr auto operator()(Fun fun) const |
| 49 |
{ |
| 50 |
struct local |
| 51 |
: Fun |
| 52 |
, PipeableBase |
| 53 |
{ |
| 54 |
constexpr explicit local(Fun && f) |
| 55 |
: Fun(static_cast<Fun &&>(f)) |
| 56 |
{} |
| 57 |
}; |
| 58 |
return local{static_cast<Fun &&>(fun)}; |
| 59 |
} |
| 60 |
}; |
| 61 |
|
| 62 |
/// \ingroup group-functional |
| 63 |
/// \sa `make_pipeable_fn` |
| 64 |
RANGES_INLINE_VARIABLE(make_pipeable_fn, make_pipeable) |
| 65 |
|
| 66 |
struct pipeable_access |
| 67 |
{ |
| 68 |
template<typename Pipeable> |
| 69 |
struct impl : Pipeable |
| 70 |
{ |
| 71 |
using Pipeable::pipe; |
| 72 |
}; |
| 73 |
}; |
| 74 |
|
| 75 |
struct pipeable_base |
| 76 |
{ |
| 77 |
private: |
| 78 |
friend pipeable_access; |
| 79 |
|
| 80 |
// Evaluate the pipe with an argument |
| 81 |
template(typename Arg, typename Pipe)( |
| 82 |
requires (!is_pipeable_v<Arg>) AND is_pipeable_v<Pipe> AND |
| 83 |
invocable<Pipe, Arg>) // clang-format off |
| 84 |
friend constexpr auto operator|(Arg &&arg, Pipe pipe) // clang-format off |
| 85 |
{ |
| 86 |
return static_cast<Pipe &&>(pipe)(static_cast<Arg &&>(arg)); |
| 87 |
} |
| 88 |
|
| 89 |
// Compose two pipes |
| 90 |
template(typename Pipe0, typename Pipe1)( |
| 91 |
requires is_pipeable_v<Pipe0> AND is_pipeable_v<Pipe1>) // clang-format off |
| 92 |
friend constexpr auto operator|(Pipe0 pipe0, Pipe1 pipe1) // clang-format on |
| 93 |
{ |
| 94 |
return make_pipeable(compose(detail::move(pipe1), detail::move(pipe0))); |
| 95 |
} |
| 96 |
|
| 97 |
template<typename Arg, typename Pipe> |
| 98 |
friend auto operator|=(Arg & arg, Pipe pipe) // |
| 99 |
-> CPP_broken_friend_ret(Arg &)( |
| 100 |
requires (is_pipeable_v<Pipe>) && |
| 101 |
(!is_pipeable_v<Arg>) && invocable<Pipe, Arg &>) |
| 102 |
{ |
| 103 |
static_cast<Pipe &&>(pipe)(arg); |
| 104 |
return arg; |
| 105 |
} |
| 106 |
|
| 107 |
// Default Pipe behavior just passes the argument to the pipe's function call |
| 108 |
// operator |
| 109 |
// clang-format off |
| 110 |
template<typename Arg, typename Pipe> |
| 111 |
static constexpr auto CPP_auto_fun(pipe)(Arg && arg, Pipe p) |
| 112 |
( |
| 113 |
return static_cast<Pipe &&>(p)(static_cast<Arg &&>(arg)) |
| 114 |
) |
| 115 |
// clang-format on |
| 116 |
}; |
| 117 |
|
| 118 |
template<typename> |
| 119 |
using pipeable RANGES_DEPRECATED("Please use pipeable_base instead") = pipeable_base; |
| 120 |
|
| 121 |
/// \endcond |
| 122 |
|
| 123 |
/// @} |
| 124 |
} // namespace ranges |
| 125 |
|
| 126 |
#include <range/v3/detail/epilogue.hpp> |
| 127 |
|
| 128 |
#endif |