Horizon
common.hpp
Go to the documentation of this file.
1 // Range v3 library
3 //
4 // Copyright Eric Niebler 2014-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_VIEW_COMMON_HPP
14 #define RANGES_V3_VIEW_COMMON_HPP
15 
16 #include <type_traits>
17 
18 #include <range/v3/range_fwd.hpp>
19 
26 #include <range/v3/utility/static_const.hpp>
27 #include <range/v3/view/all.hpp>
29 #include <range/v3/view/view.hpp>
30 
31 #include <range/v3/detail/prologue.hpp>
32 
33 namespace ranges
34 {
37 
39  namespace detail
40  {
41  // clang-format off
44  template<typename R>
45  CPP_concept random_access_and_sized_range =
46  random_access_range<R> && sized_range<R>;
47  // clang-format on
48 
49  template<typename R>
50  using common_view_iterator_t =
51  meta::if_c<random_access_and_sized_range<R>, iterator_t<R>,
52  common_iterator_t<iterator_t<R>, sentinel_t<R>>>;
53 
54  template<typename Rng>
55  struct is_common_range : meta::bool_<common_range<Rng>>
56  {};
57  } // namespace detail
59 
60  template<typename Rng, bool = detail::is_common_range<Rng>::value>
61  struct common_view : view_interface<common_view<Rng>, range_cardinality<Rng>::value>
62  {
63  private:
64  CPP_assert(view_<Rng>);
65  CPP_assert(!(common_range<Rng> && view_<Rng>));
66  Rng rng_;
67 
68  sentinel_t<Rng> end_(std::false_type)
69  {
70  return ranges::end(rng_);
71  }
72  iterator_t<Rng> end_(std::true_type)
73  {
74  return ranges::begin(rng_) + ranges::distance(rng_);
75  }
76  template(bool Const = true)(
77  requires Const AND range<meta::const_if_c<Const, Rng>>)
78  sentinel_t<meta::const_if_c<Const, Rng>> end_(std::false_type) const
79  {
80  return ranges::end(rng_);
81  }
82  template(bool Const = true)(
83  requires Const AND range<meta::const_if_c<Const, Rng>>)
84  iterator_t<meta::const_if_c<Const, Rng>> end_(std::true_type) const
85  {
86  return ranges::begin(rng_) + ranges::distance(rng_);
87  }
88 
89  public:
90  common_view() = default;
91  explicit common_view(Rng rng)
92  : rng_(detail::move(rng))
93  {}
94  Rng base() const
95  {
96  return rng_;
97  }
98 
99  detail::common_view_iterator_t<Rng> begin()
100  {
101  return detail::common_view_iterator_t<Rng>{ranges::begin(rng_)};
102  }
103  detail::common_view_iterator_t<Rng> end()
104  {
105  return detail::common_view_iterator_t<Rng>{
106  end_(meta::bool_<detail::random_access_and_sized_range<Rng>>{})};
107  }
108  CPP_auto_member
109  auto CPP_fun(size)()(
110  requires sized_range<Rng>)
111  {
112  return ranges::size(rng_);
113  }
114 
115  template(bool Const = true)(
116  requires range<meta::const_if_c<Const, Rng>>)
117  auto begin() const
118  -> detail::common_view_iterator_t<meta::const_if_c<Const, Rng>>
119  {
120  return detail::common_view_iterator_t<meta::const_if_c<Const, Rng>>{
121  ranges::begin(rng_)};
122  }
123  template(bool Const = true)(
124  requires range<meta::const_if_c<Const, Rng>>)
125  auto end() const
126  -> detail::common_view_iterator_t<meta::const_if_c<Const, Rng>>
127  {
128  return detail::common_view_iterator_t<meta::const_if_c<Const, Rng>>{
129  end_(meta::bool_<detail::random_access_and_sized_range<
130  meta::const_if_c<Const, Rng>>>{})};
131  }
132  CPP_auto_member
133  auto CPP_fun(size)()(const
134  requires sized_range<Rng const>)
135  {
136  return ranges::size(rng_);
137  }
138  };
139 
140  template<typename Rng, bool B>
141  RANGES_INLINE_VAR constexpr bool enable_borrowed_range<common_view<Rng, B>> = //
142  enable_borrowed_range<Rng>;
143 
144 #if RANGES_CXX_DEDUCTION_GUIDES >= RANGES_CXX_DEDUCTION_GUIDES_17
145  template(typename Rng)(
146  requires (!common_range<Rng>)) //
147  common_view(Rng &&)
148  ->common_view<views::all_t<Rng>>;
149 #endif
150 
151  template<typename Rng>
152  struct common_view<Rng, true> : identity_adaptor<Rng>
153  {
154  CPP_assert(common_range<Rng>);
156  };
157 
158  namespace views
159  {
161  {
162  template(typename Rng)(
163  requires viewable_range<Rng> AND common_range<Rng>)
164  all_t<Rng> operator()(Rng && rng) const
165  {
166  return all(static_cast<Rng &&>(rng));
167  }
168 
169  template(typename Rng)(
170  requires viewable_range<Rng> AND (!common_range<Rng>)) //
171  common_view<all_t<Rng>> operator()(Rng && rng) const
172  {
173  return common_view<all_t<Rng>>{all(static_cast<Rng &&>(rng))};
174  }
175  };
176 
177  struct common_fn
178  {
179  template(typename Rng)(
180  requires viewable_range<Rng>)
181  common_view<all_t<Rng>> operator()(Rng && rng) const
182  {
183  return common_view<all_t<Rng>>{all(static_cast<Rng &&>(rng))};
184  }
185  };
186 
190  } // namespace views
192 
194  template<typename Rng>
195  using bounded_view RANGES_DEPRECATED(
196  "The name bounded_view is deprecated. "
197  "Please use common_view instead.") = common_view<Rng>;
199 
200  namespace views
201  {
203  namespace
204  {
205  RANGES_DEPRECATED(
206  "The name views::bounded is deprecated. "
207  "Please use views::common instead.")
208  RANGES_INLINE_VAR constexpr auto & bounded = common;
209  } // namespace
210 
211  template<typename Rng>
212  using bounded_t RANGES_DEPRECATED("The name views::bounded_t is deprecated.") =
213  decltype(common(std::declval<Rng>()));
215  } // namespace views
216 
217  namespace cpp20
218  {
219  namespace views
220  {
223  }
224  template(typename Rng)(
225  requires view_<Rng> && (!common_range<Rng>)) //
226  using common_view = ranges::common_view<Rng>;
227  } // namespace cpp20
228 } // namespace ranges
229 
230 #include <range/v3/detail/epilogue.hpp>
231 
232 #include <range/v3/detail/satisfy_boost_range.hpp>
233 RANGES_SATISFY_BOOST_RANGE(::ranges::common_view)
234 
235 #endif
CPP_concept range
\concept range
Definition: concepts.hpp:69
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
std::integral_constant< bool, B > bool_
An integral constant wrapper for bool.
Definition: meta.hpp:168
meta::size_t< L::size()> size
An integral constant wrapper that is the size of the meta::list L.
Definition: meta.hpp:1696
_t< detail::is_< T, C > > is
is
Definition: meta.hpp:874
template(typename Rng1, typename Rng2, typename Fun)(concept(transformable_ranges_)(Rng1
\concept transformable_ranges_
Definition: common.hpp:62
Definition: all.hpp:96
Definition: interface.hpp:129
Definition: common.hpp:178
Definition: common.hpp:161
Definition: view.hpp:178