VCCC  2024.05
VisualCamp Common C++ library
common_view.hpp
Go to the documentation of this file.
1 //
2 // Created by yonggyulee on 2/3/24.
3 //
4 
5 #ifndef VCCC_RANGES_VIEWS_COMMON_VIEW_HPP
6 #define VCCC_RANGES_VIEWS_COMMON_VIEW_HPP
7 
8 #include <type_traits>
9 #include <utility>
10 
14 #include "vccc/__ranges/begin.hpp"
16 #include "vccc/__ranges/end.hpp"
19 #include "vccc/__ranges/size.hpp"
25 
26 namespace vccc {
27 namespace ranges {
28 
31 
36 template<typename V>
37 class common_view : public view_interface<common_view<V>> {
38  public:
39  static_assert(common_range<V>::value == false, "Constraints not satisfied");
40  static_assert(copyable<iterator_t<V>>::value, "Constraints not satisfied");
41 
42  common_view() = default;
43 
44  constexpr explicit common_view(V r)
45  : base_(std::move(r)) {}
46 
48  constexpr V base() const& {
49  return base_;
50  }
51 
52  constexpr V base() && {
53  return std::move(base_);
54  }
55 
56  constexpr auto begin() {
57  return begin_impl<V>(base_, conjunction<random_access_range<V>, sized_range<V>>{});
58  }
59 
61  constexpr auto begin() const {
62  return begin_impl<const V2>(base_, conjunction<random_access_range<const V2>, sized_range<const V2>>{});
63  }
64 
65  constexpr auto end() {
66  return end_impl<V>(base_, conjunction<random_access_range<V>, sized_range<V>>{});
67  }
68 
70  constexpr auto end() const {
71  return end_impl<const V>(base_, conjunction<random_access_range<const V>, sized_range<const V>>{});
72  }
73 
75  constexpr auto size() {
76  return ranges::size(base_);
77  }
78 
80  constexpr auto size() const {
81  return ranges::size(base_);
82  }
83 
84  private:
85  template<typename V2, typename Base>
86  static constexpr auto begin_impl(Base&& base, std::true_type /* random_and_sized */) {
87  return ranges::begin(base);
88  }
89  template<typename V2, typename Base>
90  static constexpr auto begin_impl(Base&& base, std::false_type /* random_and_sized */) {
91  return common_iterator<iterator_t<V2>, sentinel_t<V2>>(ranges::begin(base));
92  }
93 
94  template<typename V2, typename Base>
95  static constexpr auto end_impl(Base&& base, std::true_type /* random_and_sized */) {
97  }
98  template<typename V2, typename Base>
99  static constexpr auto end_impl(Base&& base, std::false_type /* random_and_sized */) {
100  return common_iterator<iterator_t<V2>, sentinel_t<V2>>(ranges::end(base));
101  }
102 
103  V base_{};
104 };
105 
106 #if __cplusplus >= 201703L
107 
108 template<typename R>
109 common_view(R&&) -> common_view<views::all_t<R>>;
110 
111 #endif
112 
115  return common_view<views::all_t<R>>(std::forward<R>(r));
116 }
117 
118 
119 template<typename V>
121 
123 
124 } // namespace ranges
125 } // namespace vccc
126 
127 #endif // VCCC_RANGES_VIEWS_COMMON_VIEW_HPP
Definition: common_iterator.hpp:48
Adapts a given view with different types for iterator/sentinel pair into a view that is also a common...
Definition: common_view.hpp:37
constexpr V base() const &
Definition: common_view.hpp:48
constexpr common_view(V r)
Definition: common_view.hpp:44
constexpr auto size() const
Definition: common_view.hpp:80
constexpr auto size()
Definition: common_view.hpp:75
constexpr auto begin()
Definition: common_view.hpp:56
constexpr V base() &&
Definition: common_view.hpp:52
constexpr auto end()
Definition: common_view.hpp:65
constexpr auto end() const
Definition: common_view.hpp:70
constexpr auto begin() const
Definition: common_view.hpp:61
helper class template for defining a view, using the curiously recurring template pattern
Definition: view_interface.hpp:78
constexpr common_view< views::all_t< R > > make_common_view(R &&r)
Definition: common_view.hpp:114
typename ranges::iterator< T >::type iterator_t
Definition: iterator_t.hpp:32
constexpr VCCC_INLINE_OR_STATIC detail::begin_niebloid begin
returns an iterator to the beginning of a range
Definition: begin.hpp:116
constexpr VCCC_INLINE_OR_STATIC detail::size_niebloid size
returns the size of a container or array
Definition: size.hpp:145
constexpr VCCC_INLINE_OR_STATIC detail::end_niebloid end
returns a sentinel indicating the end of a range
Definition: end.hpp:120
Definition: matrix.hpp:495
Definition: directory.h:12
constexpr VCCC_INLINE_OR_STATIC detail::element_niebloid< 1 > value
Definition: key_value.hpp:35
Definition: conjunction.hpp:22
specifies that an object of a type can be copied, moved, and swapped
Definition: copyable.hpp:59
Definition: common_range.hpp:41
Definition: enable_borrowed_range.hpp:17
specifies a range whose iterator type satisfies random_access_iterator
Definition: random_access_range.hpp:48
specifies that a range knows its size in constant time
Definition: sized_range.hpp:38