Horizon
pipeable.hpp
Go to the documentation of this file.
1 // 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 
22 #include <range/v3/utility/static_const.hpp>
23 
24 #include <range/v3/detail/prologue.hpp>
25 
26 namespace ranges
27 {
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 
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 
65 
67  {
68  template<typename Pipeable>
69  struct impl : Pipeable
70  {
71  using Pipeable::pipe;
72  };
73  };
74 
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 
122 
124 } // namespace ranges
125 
126 #include <range/v3/detail/epilogue.hpp>
127 
128 #endif
RANGES_INLINE_VARIABLE(detail::to_container_fn< detail::from_range< std::vector >>, to_vector) template< template< typename... > class ContT > auto to(RANGES_HIDDEN_DETAIL(detail
For initializing a container of the specified type with the elements of an Range.
Definition: conversion.hpp:399
std::integral_constant< bool, B > bool_
An integral constant wrapper for bool.
Definition: meta.hpp:168
Definition: pipeable.hpp:46
Definition: pipeable.hpp:70
Definition: pipeable.hpp:67
Definition: pipeable.hpp:76