assign.hpp
Go to the documentation of this file.
1 // Copyright 2024 Ekumen, Inc.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef BELUGA_ACTIONS_ASSIGN_HPP
16 #define BELUGA_ACTIONS_ASSIGN_HPP
17 
18 #include <range/v3/action/action.hpp>
19 #include <range/v3/functional/bind_back.hpp>
20 #include <range/v3/range/conversion.hpp>
21 #include <range/v3/view/view.hpp>
22 
28 namespace beluga::actions {
29 
31 
32 template <class T, class = void>
33 struct is_range_closure : std::false_type {};
34 
35 template <class T>
36 struct is_range_closure<ranges::actions::action_closure<T>> : std::true_type {};
37 
38 template <class T>
39 struct is_range_closure<ranges::views::view_closure<T>> : std::true_type {};
40 
41 template <class T>
42 inline constexpr bool is_range_closure_v = is_range_closure<T>::value;
43 
45 
46 namespace detail {
47 
49 struct assign_fn {
51  template <
52  class Range,
53  class Fn,
54  std::enable_if_t<ranges::range<Range>, int> = 0,
55  std::enable_if_t<is_range_closure_v<Fn>, int> = 0>
56  constexpr auto operator()(Range& range, Fn fn) const -> Range& {
57  auto&& view = fn(range);
58  if constexpr (!std::is_same_v<Range, std::decay_t<decltype(view)>>) {
59  // If the result of invoking the closure is not the range itself,
60  // then we need to convert the view and assign it to the input range.
61  range = std::move(view) | ranges::to<Range>;
62  }
63  return range;
64  }
65 
67 
79  template <class Fn, std::enable_if_t<is_range_closure_v<Fn>, int> = 0>
80  friend constexpr auto operator|(Fn fn, assign_fn) {
81  return ranges::make_action_closure(ranges::bind_back(assign_fn{}, std::move(fn)));
82  }
83 };
84 
85 } // namespace detail
86 
89 
99 inline constexpr detail::assign_fn assign;
100 
101 } // namespace beluga::actions
102 
103 #endif
beluga::actions::detail::assign_fn::operator()
constexpr auto operator()(Range &range, Fn fn) const -> Range &
Overload that implements the assign algorithm.
Definition: assign.hpp:56
beluga::actions
Definition: assign.hpp:28
beluga::actions::detail::assign_fn
Implementation detail for an assign range adaptor object.
Definition: assign.hpp:49
beluga::actions::assign
constexpr detail::assign_fn assign
Definition: assign.hpp:99
beluga::actions::detail::assign_fn::operator|
constexpr friend auto operator|(Fn fn, assign_fn)
Hidden friend operator overload that enables action / view composition.
Definition: assign.hpp:80


beluga
Author(s):
autogenerated on Tue Jul 16 2024 02:59:53