VCCC  2024.05
VisualCamp Common C++ library
owning_view.hpp
Go to the documentation of this file.
1 //
2 // Created by yonggyulee on 2024/01/02.
3 //
4 
5 #ifndef VCCC_RANGES_OWINING_VIEW_HPP
6 #define VCCC_RANGES_OWINING_VIEW_HPP
7 
8 #include <utility>
9 
11 #include "vccc/__ranges/begin.hpp"
13 #include "vccc/__ranges/data.hpp"
14 #include "vccc/__ranges/empty.hpp"
16 #include "vccc/__ranges/end.hpp"
18 #include "vccc/__ranges/range.hpp"
19 #include "vccc/__ranges/size.hpp"
24 
25 namespace vccc {
26 namespace ranges {
27 
30 
37 template<typename R>
38 class owning_view : public view_interface<owning_view<R>> {
39  public:
40  static_assert(range<R>::value, "Constraints not satisfied");
41  static_assert(movable<R>::value, "Constraints not satisfied");
42  static_assert(is_initializer_list<R>::value == false, "Constraints not satisfied");
43 
44  constexpr owning_view() = default;
45  constexpr owning_view(owning_view&&) = default;
46  constexpr owning_view(const owning_view&) = delete;
48  : r_(std::move(t)) {}
49 
50 
52  owning_view& operator=(const owning_view&) = delete;
53 
54 
55  constexpr R& base() & noexcept { return r_; }
56  constexpr const R& base() const & noexcept { return r_; }
57  constexpr R&& base() && noexcept { return std::move(r_); }
58  constexpr const R&& base() const && noexcept { return std::move(r_); }
59 
60 
61  template<typename T = R>
62  constexpr iterator_t<T> begin() {
63  return ranges::begin(r_);
64  }
66  constexpr auto begin() const {
67  return ranges::begin(r_);
68  }
69 
70 
71  template<typename T = R>
72  constexpr sentinel_t<T> end() {
73  return ranges::end(r_);
74  }
76  constexpr auto end() const {
77  return ranges::end(r_);
78  }
79 
80 
81  template<typename T = R, std::enable_if_t<is_invocable<decltype(ranges::end), T&>::value, int> = 0>
82  constexpr bool empty() {
83  return ranges::empty(r_);
84  }
85 
86  template<typename T = R, std::enable_if_t<is_invocable<decltype(ranges::end), const T&>::value, int> = 0>
87  constexpr bool empty() const {
88  return ranges::empty(r_);
89  }
90 
91 
93  constexpr auto size() {
94  return ranges::size(r_);
95  }
96 
98  constexpr auto size() const {
99  return ranges::size(r_);
100  }
101 
102 
104  constexpr auto data() {
105  return ranges::data(r_);
106  }
107 
109  constexpr auto data() const {
110  return ranges::data(r_);
111  }
112 
113  private:
114  R r_;
115 };
116 
117 #if __cplusplus >= 201703L
118 
119 template<typename R>
120 owning_view(R&&) -> owning_view<R>;
121 
122 #endif
123 
124 template<typename T>
126 
128 
129 } // namespace ranges
130 } // namespace namespace vccc
131 
132 #endif // VCCC_RANGES_OWINING_VIEW_HPP
a view with unique ownership of some range
Definition: owning_view.hpp:38
constexpr const R & base() const &noexcept
Definition: owning_view.hpp:56
constexpr sentinel_t< T > end()
Definition: owning_view.hpp:72
constexpr auto size() const
Definition: owning_view.hpp:98
constexpr auto size()
Definition: owning_view.hpp:93
constexpr bool empty() const
Definition: owning_view.hpp:87
constexpr owning_view(const owning_view &)=delete
constexpr R & base() &noexcept
Definition: owning_view.hpp:55
owning_view & operator=(owning_view &&)=default
constexpr auto end() const
Definition: owning_view.hpp:76
constexpr auto data() const
Definition: owning_view.hpp:109
constexpr owning_view(R &&t) noexcept(std::is_nothrow_move_constructible< R >::value)
Definition: owning_view.hpp:47
constexpr iterator_t< T > begin()
Definition: owning_view.hpp:62
constexpr R && base() &&noexcept
Definition: owning_view.hpp:57
constexpr owning_view(owning_view &&)=default
constexpr auto begin() const
Definition: owning_view.hpp:66
constexpr auto data()
Definition: owning_view.hpp:104
constexpr owning_view()=default
constexpr bool empty()
Definition: owning_view.hpp:82
constexpr const R && base() const &&noexcept
Definition: owning_view.hpp:58
owning_view & operator=(const owning_view &)=delete
helper class template for defining a view, using the curiously recurring template pattern
Definition: view_interface.hpp:78
constexpr VCCC_INLINE_OR_STATIC detail::empty_niebloid empty
checks whether a range is empty
Definition: empty.hpp:116
constexpr VCCC_INLINE_OR_STATIC detail::data_niebloid data
obtains a pointer to the beginning of a contiguous range
Definition: data.hpp:103
typename sentinel< R >::type sentinel_t
Definition: sentinel_t.hpp:29
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: is_initializer_list.hpp:20
specifies that an object of a type can be moved and swapped
Definition: movable.hpp:58
Definition: enable_borrowed_range.hpp:17
specifies that a type is a range, that is, it provides a begin iterator and an end sentinel
Definition: range.hpp:53