Debian Codegraph @

3rd_party/range-v3/range/v3/view/single.hpp

raw
1 /// \file
2 // Range v3 library
3 //
4 // Copyright Eric Niebler 2013-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
14 #ifndef RANGES_V3_VIEW_SINGLE_HPP
15 #define RANGES_V3_VIEW_SINGLE_HPP
16
17 #include <type_traits>
18 #include <utility>
19
20 #include <range/v3/range_fwd.hpp>
21
22 #include <range/v3/iterator/concepts.hpp>
23 #include <range/v3/iterator/traits.hpp>
24 #include <range/v3/range/access.hpp>
25 #include <range/v3/range/concepts.hpp>
26 #include <range/v3/range/traits.hpp>
27 #include <range/v3/utility/addressof.hpp>
28 #include <range/v3/utility/optional.hpp>
29 #include <range/v3/utility/semiregular_box.hpp>
30 #include <range/v3/utility/static_const.hpp>
31 #include <range/v3/view/facade.hpp>
32
33 #include <range/v3/detail/prologue.hpp>
34
35 namespace ranges
36 {
37 /// \addtogroup group-views
38 /// @{
39 template<typename T>
40 struct single_view : view_interface<single_view<T>, (cardinality)1>
41 {
42 private:
43 CPP_assert(copy_constructible<T>);
44 static_assert(std::is_object<T>::value,
45 "The template parameter of single_view must be an object type");
46 semiregular_box_t<T> value_;
47 template<typename... Args>
48 constexpr single_view(in_place_t, std::true_type, Args &&... args)
49 : value_{static_cast<Args &&>(args)...}
50 {}
51 template<typename... Args>
52 constexpr single_view(in_place_t, std::false_type, Args &&... args)
53 : value_{in_place, static_cast<Args &&>(args)...}
54 {}
55
56 public:
57 single_view() = default;
58 constexpr explicit single_view(T const & t)
59 : value_(t)
60 {}
61 constexpr explicit single_view(T && t)
62 : value_(std::move(t))
63 {}
64 template(class... Args)(
65 requires constructible_from<T, Args...>)
66 constexpr single_view(in_place_t, Args &&... args)
67 : single_view{in_place,
68 meta::bool_<(bool)semiregular<T>>{},
69 static_cast<Args &&>(args)...}
70 {}
71 constexpr T * begin() noexcept
72 {
73 return data();
74 }
75 constexpr T const * begin() const noexcept
76 {
77 return data();
78 }
79 constexpr T * end() noexcept
80 {
81 return data() + 1;
82 }
83 constexpr T const * end() const noexcept
84 {
85 return data() + 1;
86 }
87 static constexpr std::size_t size() noexcept
88 {
89 return 1u;
90 }
91 constexpr T * data() noexcept
92 {
93 return detail::addressof(static_cast<T &>(value_));
94 }
95 constexpr T const * data() const noexcept
96 {
97 return detail::addressof(static_cast<T const &>(value_));
98 }
99 };
100
101 #if RANGES_CXX_DEDUCTION_GUIDES >= RANGES_CXX_DEDUCTION_GUIDES_17
102 template<class T>
103 explicit single_view(T &&) //
104 -> single_view<detail::decay_t<T>>;
105 #endif
106
107 namespace views
108 {
109 struct single_fn
110 {
111 template(typename Val)(
112 requires copy_constructible<Val>)
113 single_view<Val> operator()(Val value) const
114 {
115 return single_view<Val>{std::move(value)};
116 }
117 };
118
119 /// \relates single_fn
120 /// \ingroup group-views
121 RANGES_INLINE_VARIABLE(single_fn, single)
122 } // namespace views
123
124 namespace cpp20
125 {
126 namespace views
127 {
128 using ranges::views::single;
129 }
130 template(typename T)(
131 requires std::is_object<T>::value) //
132 using single_view = ranges::single_view<T>;
133 } // namespace cpp20
134 /// @}
135 } // namespace ranges
136
137 #include <range/v3/detail/epilogue.hpp>
138 #include <range/v3/detail/satisfy_boost_range.hpp>
139 RANGES_SATISFY_BOOST_RANGE(::ranges::single_view)
140
141 #endif