Simple-Utility v2.3.1
Loading...
Searching...
No Matches
Pipe.hpp
Go to the documentation of this file.
1// Copyright Dominic Koepke 2019 - 2023.
2// Distributed under the Boost Software License, Version 1.0.
3// (See accompanying file LICENSE_1_0.txt or copy at
4// https://www.boost.org/LICENSE_1_0.txt)
5
6#ifndef SIMPLE_UTILITY_FUNCTIONAL_MIXINS_PIPE_HPP
7#define SIMPLE_UTILITY_FUNCTIONAL_MIXINS_PIPE_HPP
8
9#pragma once
10
11#include <concepts>
12#include <tuple>
13
18
19namespace sl::functional
20{
22 {
23 template <class Fn, class... Args>
24 requires std::invocable<Fn, Args...>
25 constexpr decltype(auto) operator ()(
26 std::tuple<Fn>&& fn,
27 Args&&... args
28 ) const noexcept(std::is_nothrow_invocable_v<Fn, Args...>)
29 {
30 return std::invoke(std::get<0>(std::move(fn)), std::forward<Args>(args)...);
31 }
32
33 template <concepts::tuple Fns, class... Args>
34 requires std::invocable<type_list::front_t<Fns>, Args...>
35 && std::invocable<
38 std::invoke_result_t<type_list::front_t<Fns>, Args...>>
39 constexpr decltype(auto) operator ()(
40 Fns fns,
41 Args&&... args
42 ) const
43 noexcept(std::is_nothrow_invocable_v<type_list::front_t<Fns>, Args...>
44 && std::is_nothrow_invocable_v<PipeStrategy,
46 std::invoke_result_t<type_list::front_t<Fns>, Args...>>)
47 {
48 return std::apply(
49 *this,
50 std::apply(
51 [&]<class FirstFn, class... OtherFns>(FirstFn&& first, OtherFns&&... others)
52 {
53 return std::make_tuple(
54 std::forward_as_tuple(std::forward<OtherFns>(others)...),
55 std::invoke(std::forward<FirstFn>(first), std::forward<Args>(args)...));
56 },
57 std::move(fns)));
58 }
59 };
60
61 template <concepts::unqualified Derived>
63 {
64 protected:
65 [[nodiscard]]
66 constexpr PipeOperator() noexcept
67 {
68 static_assert(std::is_base_of_v<PipeOperator, Derived>, "Derived doesn't inherit from PipeOperator.");
69 static_assert(
70 std::copy_constructible<Derived> || std::move_constructible<Derived>,
71 "Derived is neither move- nor copy-constructible.");
72 }
73
74 [[nodiscard]]
75 PipeOperator(const PipeOperator&) = default;
77
78 [[nodiscard]]
81
82 ~PipeOperator() = default;
83
84 public:
85 template <class Other>
87 && std::same_as<unwrap_functional_t<Other>, Other>
88 [[nodiscard]]
89 friend constexpr auto operator |(
90 const Derived& first,
91 Other&& other
92 ) noexcept(noexcept(make_composition<PipeStrategy>(first, std::declval<Other>())))
93 {
94 return envelop<closure_template<Derived>::template type>(
95 make_composition<PipeStrategy>(first, std::forward<Other>(other)));
96 }
97
98 template <class Other>
100 [[nodiscard]]
101 friend constexpr auto operator |(
102 const Derived& first,
103 Other&& other
104 ) noexcept(noexcept(make_composition<PipeStrategy>(first, std::declval<Other>())))
105 {
106 return envelop<closure_template<std::remove_cvref_t<Other>>::template type>(
107 make_composition<PipeStrategy>(first, std::forward<Other>(other)));
108 }
109
110 template <class Other>
112 && std::same_as<unwrap_functional_t<Other>, Other>
113 [[nodiscard]]
114 friend constexpr auto operator |(
115 Derived&& first,
116 Other&& other
117 ) noexcept(noexcept(make_composition<PipeStrategy>(std::move(first), std::declval<Other>())))
118 {
119 return envelop<closure_template<Derived>::template type>(
120 make_composition<PipeStrategy>(std::move(first), std::forward<Other>(other)));
121 }
122
123 template <class Other>
125 [[nodiscard]]
126 friend constexpr auto operator |(
127 Derived&& first,
128 Other&& other
129 ) noexcept(noexcept(make_composition<PipeStrategy>(std::move(first), std::declval<Other>())))
130 {
131 return envelop<closure_template<std::remove_cvref_t<Other>>::template type>(
132 make_composition<PipeStrategy>(std::move(first), std::forward<Other>(other)));
133 }
134 };
135}
136
137#endif
Definition: Pipe.hpp:63
PipeOperator(PipeOperator &&)=default
constexpr PipeOperator() noexcept
Definition: Pipe.hpp:66
PipeOperator & operator=(const PipeOperator &)=default
friend constexpr auto operator|(const Derived &first, Other &&other) noexcept(noexcept(make_composition< PipeStrategy >(first, std::declval< Other >())))
Definition: Pipe.hpp:89
PipeOperator(const PipeOperator &)=default
Determines whether a type can be used as a tuple-like.
Definition: General.hpp:105
Determines whether the given type satisfies the constraints of a function type.
Definition: BasicClosure.hpp:79
typename front< List >::type front_t
Convenience alias, exposing the type member alias of the front trait.
Definition: TypeList.hpp:431
typename pop_front< List >::type pop_front_t
Convenience alias, exposing the type member alias of the pop_front trait.
Definition: TypeList.hpp:780
Definition: Arithmetic.hpp:13
Definition: Pipe.hpp:22