VCCC  2024.05
VisualCamp Common C++ library
ref_view.hpp
Go to the documentation of this file.
1 //
2 // Created by yonggyulee on 2024/01/02.
3 //
4 
5 #ifndef VCCC_RANGES_REF_VIEW_HPP
6 #define VCCC_RANGES_REF_VIEW_HPP
7 
8 #include <memory>
9 #include <type_traits>
10 
16 #include "vccc/__ranges/begin.hpp"
17 #include "vccc/__ranges/data.hpp"
18 #include "vccc/__ranges/empty.hpp"
19 #include "vccc/__ranges/end.hpp"
21 #include "vccc/__ranges/size.hpp"
26 
27 namespace vccc {
28 namespace ranges {
29 
32 
39 template<typename R>
40 class ref_view : public view_interface<ref_view<R>> {
41  public:
42  static_assert(std::is_object<R>::value, "Constraints not satisfied");
43 
44  template<typename T, std::enable_if_t<conjunction<
47  std::is_lvalue_reference<T&&>
48  >::value, int> = 0>
50  : r_(vccc::addressof(static_cast<R&>(std::forward<T>(t)))) {}
51 
52  constexpr R& base() const {
53  return *r_;
54  }
55 
56  constexpr iterator_t<R> begin() const {
57  return ranges::begin(*r_);
58  }
59 
60  constexpr sentinel_t<R> end() const {
61  return ranges::end(*r_);
62  }
63 
64  template<typename T = R, std::enable_if_t<
65  is_invocable<decltype(ranges::empty), T&>::value, int> = 0>
66  constexpr bool empty() const {
67  return ranges::empty(*r_);
68  }
69 
70  template<typename T = R, std::enable_if_t<
71  sized_range<T>::value, int> = 0>
72  constexpr auto size() const {
73  return ranges::size(*r_);
74  }
75 
76  template<typename T = R, std::enable_if_t<
78  constexpr auto data() const {
79  return ranges::data(*r_);
80  }
81 
82  private:
83  R* r_;
84 };
85 
86 #if __cplusplus >= 201703L
87 
88 template<typename R>
89 ref_view(R&) -> ref_view<R>;
90 
91 #endif
92 
93 template<typename T>
94 struct enable_borrowed_range<ref_view<T>> : std::true_type {};
95 
97 
98 } // namespace ranges
99 } // namespace namespace vccc
100 
101 #endif // VCCC_RANGES_REF_VIEW_HPP
#define VCCC_ADDRESSOF_CONSTEXPR
Definition: addressof.hpp:14
a view of the elements of some other range
Definition: ref_view.hpp:40
constexpr auto size() const
Definition: ref_view.hpp:72
constexpr sentinel_t< R > end() const
Definition: ref_view.hpp:60
constexpr bool empty() const
Definition: ref_view.hpp:66
VCCC_ADDRESSOF_CONSTEXPR ref_view(T &&t) noexcept
Definition: ref_view.hpp:49
constexpr iterator_t< R > begin() const
Definition: ref_view.hpp:56
constexpr auto data() const
Definition: ref_view.hpp:78
constexpr R & base() const
Definition: ref_view.hpp:52
helper class template for defining a view, using the curiously recurring template pattern
Definition: view_interface.hpp:78
std::enable_if_t< std::is_object< T >::value, T * > addressof(T &t) noexcept
Definition: addressof.hpp:33
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: directory.h:12
constexpr VCCC_INLINE_OR_STATIC detail::element_niebloid< 1 > value
Definition: key_value.hpp:35
Definition: conjunction.hpp:22
Models std::convertible_to
Definition: convertible_to.hpp:38
Definition: different_from.hpp:18
Determines whether INVOKE(std::declval<Fn>(), std::declval<ArgTypes>()...) is well formed when treate...
Definition: is_invocable.hpp:77
specifies a range whose iterator type satisfies contiguous_iterator
Definition: contiguous_range.hpp:69
Definition: enable_borrowed_range.hpp:17
specifies that a range knows its size in constant time
Definition: sized_range.hpp:38