VCCC  2024.05
VisualCamp Common C++ library
single.hpp
Go to the documentation of this file.
1 //
2 // Created by yonggyulee on 2024/01/02.
3 //
4 
5 #ifndef VCCC_RANGES_VIEWS_SINGLE_HPP
6 #define VCCC_RANGES_VIEWS_SINGLE_HPP
7 
8 #include <type_traits>
9 #include <utility>
10 
17 
18 namespace vccc {
19 namespace ranges {
20 
36 
37 template<typename T>
38 class single_view : public ranges::view_interface<single_view<T>> {
39  public:
40  static_assert(copy_constructible<T>::value, "Constraints not satisfied");
41  static_assert(std::is_object<T>::value, "Constraints not satisfied");
42 
43  constexpr single_view() = default;
44 
45  constexpr explicit single_view(const T& t)
46  : value_(t) {}
47 
48  constexpr explicit single_view(T&& t)
49  : value_(std::move(t)) {}
50 
51  template<typename InPlace, typename... Args, std::enable_if_t<conjunction<
52  std::is_same<InPlace, vccc::in_place_t>,
53  constructible_from<T, Args...>
54  >::value, int> = 0>
55  constexpr explicit single_view(InPlace, Args&&... args)
56  : value_{vccc::in_place, std::forward<Args>(args)...} {}
57 
58  constexpr T* begin() noexcept { return data(); }
59  constexpr const T* begin() const noexcept { return data(); }
60 
61  constexpr T* end() noexcept { return data() + 1; }
62  constexpr const T* end() const noexcept { return data() + 1; }
63 
64  static constexpr std::size_t size() noexcept { return 1; }
65 
66  constexpr T* data() noexcept { return value_.operator->(); }
67  constexpr const T* data() const noexcept { return value_.operator->(); }
68 
69  private:
70  movable_box<T> value_;
71 };
72 
73 #if __cplusplus >= 201703L
74 
75 template<typename T>
76 single_view(T) -> single_view<T>;
77 
78 #endif
79 
81 
82 namespace views {
83 namespace detail {
84 
86  template<typename T, std::enable_if_t<conjunction<
88  std::is_object<std::decay_t<T>>,
89  std::is_constructible<std::decay_t<T>, T&&>
90  >::value, int> = 0>
92  operator()(T&& t) const {
93  return single_view<std::decay_t<T>>(std::forward<T>(t));
94  }
95 };
96 
97 } // namespace detail
98 
99 namespace niebloid {
100 
103 
105 
107 
108 } // namespace niebloid
109 using namespace niebloid;
110 
111 } // namespace vccc
112 } // namespace ranges
113 } // namespace views
114 
115 #endif // VCCC_RANGES_VIEWS_SINGLE_HPP
Definition: movable_box.hpp:212
Definition: single.hpp:38
constexpr T * end() noexcept
Definition: single.hpp:61
static constexpr std::size_t size() noexcept
Definition: single.hpp:64
constexpr const T * begin() const noexcept
Definition: single.hpp:59
constexpr single_view(InPlace, Args &&... args)
Definition: single.hpp:55
constexpr T * begin() noexcept
Definition: single.hpp:58
constexpr T * data() noexcept
Definition: single.hpp:66
constexpr single_view()=default
constexpr single_view(T &&t)
Definition: single.hpp:48
constexpr const T * data() const noexcept
Definition: single.hpp:67
constexpr const T * end() const noexcept
Definition: single.hpp:62
constexpr single_view(const T &t)
Definition: single.hpp:45
helper class template for defining a view, using the curiously recurring template pattern
Definition: view_interface.hpp:78
constexpr VCCC_INLINE_OR_STATIC detail::single_niebloid single
Definition: single.hpp:104
constexpr VCCC_INLINE_OR_STATIC in_place_t in_place
Definition: in_place.hpp:47
#define VCCC_INLINE_OR_STATIC
Definition: inline_or_static.hpp:9
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 a variable of the type can be constructed from or bound to a set of argument types
Definition: constructible_from.hpp:31
specifies that an object of a type can be copy constructed and move constructed
Definition: copy_constructible.hpp:55
constexpr single_view< std::decay_t< T > > operator()(T &&t) const
Definition: single.hpp:92