Horizon
split_when.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 
14 #ifndef RANGES_V3_ACTION_SPLIT_WHEN_HPP
15 #define RANGES_V3_ACTION_SPLIT_WHEN_HPP
16 
17 #include <vector>
18 
19 #include <meta/meta.hpp>
20 
21 #include <range/v3/range_fwd.hpp>
22 
30 #include <range/v3/utility/static_const.hpp>
32 
33 #include <range/v3/detail/prologue.hpp>
34 
35 namespace ranges
36 {
39  namespace actions
40  {
42  {
43  template<typename Rng>
44  using split_value_t =
45  meta::if_c<(bool)ranges::container<Rng>, //
46  uncvref_t<Rng>, std::vector<range_value_t<Rng>>>;
47 
48  template<typename Fun>
49  constexpr auto operator()(Fun fun) const
50  {
51  return make_action_closure(
52  bind_back(split_when_fn{}, static_cast<Fun &&>(fun)));
53  }
54 
55  // BUGBUG something is not right with the actions. It should be possible
56  // to move a container into a split and have elements moved into the result.
57  template(typename Rng, typename Fun)(
58  requires forward_range<Rng> AND
59  invocable<Fun &, iterator_t<Rng>, sentinel_t<Rng>> AND
61  copy_constructible<Fun> AND
62  convertible_to<invoke_result_t<Fun &, iterator_t<Rng>,
63  sentinel_t<Rng>>,
64  std::pair<bool, iterator_t<Rng>>>)
65  std::vector<split_value_t<Rng>> operator()(Rng && rng, Fun fun) const
66  {
67  return views::split_when(rng, std::move(fun)) |
68  to<std::vector<split_value_t<Rng>>>();
69  }
70 
71  template(typename Rng, typename Fun)(
72  requires forward_range<Rng> AND
73  predicate<Fun const &, range_reference_t<Rng>> AND
74  copy_constructible<Fun>)
75  std::vector<split_value_t<Rng>> operator()(Rng && rng, Fun fun) const
76  {
77  return views::split_when(rng, std::move(fun)) |
78  to<std::vector<split_value_t<Rng>>>();
79  }
80  };
81 
84  } // namespace actions
86 } // namespace ranges
87 
88 #include <range/v3/detail/epilogue.hpp>
89 
90 #endif
CPP_concept invocable
\concept invocable
Definition: concepts.hpp:48
CPP_concept predicate
\concept predicate
Definition: concepts.hpp:73
decltype(begin(declval(Rng &))) iterator_t
Definition: access.hpp:698
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
defer< bind_back, Fn, Ts... > bind_back
Definition: meta.hpp:994
list< F, S > pair
A list with exactly two elements.
Definition: meta.hpp:2246
Tiny meta-programming library.
Definition: split_when.hpp:42