1 // -*- C++ -*-
2 //===----------------------------------------------------------------------===//
3 //
4 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5 // See https://llvm.org/LICENSE.txt for license information.
6 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #ifndef _LIBCPP___RANGES_SINGLE_VIEW_H
11 #define _LIBCPP___RANGES_SINGLE_VIEW_H
12 
13 #include <__concepts/constructible.h>
14 #include <__config>
15 #include <__ranges/movable_box.h>
16 #include <__ranges/range_adaptor.h>
17 #include <__ranges/view_interface.h>
18 #include <__type_traits/decay.h>
19 #include <__type_traits/is_object.h>
20 #include <__utility/forward.h>
21 #include <__utility/in_place.h>
22 #include <__utility/move.h>
23 #include <cstddef>
24 
25 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
26 #  pragma GCC system_header
27 #endif
28 
29 _LIBCPP_PUSH_MACROS
30 #include <__undef_macros>
31 
32 _LIBCPP_BEGIN_NAMESPACE_STD
33 
34 #if _LIBCPP_STD_VER >= 20
35 
36 namespace ranges {
37 #  if _LIBCPP_STD_VER >= 23
38 template <move_constructible _Tp>
39 #  else
40 template <copy_constructible _Tp>
41 #  endif
42   requires is_object_v<_Tp>
43 class _LIBCPP_ABI_LLVM18_NO_UNIQUE_ADDRESS single_view : public view_interface<single_view<_Tp>> {
44   _LIBCPP_NO_UNIQUE_ADDRESS __movable_box<_Tp> __value_;
45 
46 public:
47   _LIBCPP_HIDE_FROM_ABI single_view()
48     requires default_initializable<_Tp>
49   = default;
50 
51   _LIBCPP_HIDE_FROM_ABI constexpr explicit single_view(const _Tp& __t)
52 #  if _LIBCPP_STD_VER >= 23
53     requires copy_constructible<_Tp>
54 #  endif
55       : __value_(in_place, __t) {
56   }
57 
58   _LIBCPP_HIDE_FROM_ABI constexpr explicit single_view(_Tp&& __t) : __value_(in_place, std::move(__t)) {}
59 
60   template <class... _Args>
61     requires constructible_from<_Tp, _Args...>
62   _LIBCPP_HIDE_FROM_ABI constexpr explicit single_view(in_place_t, _Args&&... __args)
63       : __value_{in_place, std::forward<_Args>(__args)...} {}
64 
65   _LIBCPP_HIDE_FROM_ABI constexpr _Tp* begin() noexcept { return data(); }
66 
67   _LIBCPP_HIDE_FROM_ABI constexpr const _Tp* begin() const noexcept { return data(); }
68 
69   _LIBCPP_HIDE_FROM_ABI constexpr _Tp* end() noexcept { return data() + 1; }
70 
71   _LIBCPP_HIDE_FROM_ABI constexpr const _Tp* end() const noexcept { return data() + 1; }
72 
73   _LIBCPP_HIDE_FROM_ABI static constexpr size_t size() noexcept { return 1; }
74 
75   _LIBCPP_HIDE_FROM_ABI constexpr _Tp* data() noexcept { return __value_.operator->(); }
76 
77   _LIBCPP_HIDE_FROM_ABI constexpr const _Tp* data() const noexcept { return __value_.operator->(); }
78 };
79 
80 template <class _Tp>
81 single_view(_Tp) -> single_view<_Tp>;
82 
83 namespace views {
84 namespace __single_view {
85 
86 struct __fn : __range_adaptor_closure<__fn> {
87   template <class _Range>
88   [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto operator()(_Range&& __range) const
89       noexcept(noexcept(single_view<decay_t<_Range&&>>(std::forward<_Range>(__range))))
90           -> decltype(single_view<decay_t<_Range&&>>(std::forward<_Range>(__range))) {
91     return single_view<decay_t<_Range&&>>(std::forward<_Range>(__range));
92   }
93 };
94 } // namespace __single_view
95 
96 inline namespace __cpo {
97 inline constexpr auto single = __single_view::__fn{};
98 } // namespace __cpo
99 
100 } // namespace views
101 } // namespace ranges
102 
103 #endif // _LIBCPP_STD_VER >= 20
104 
105 _LIBCPP_END_NAMESPACE_STD
106 
107 _LIBCPP_POP_MACROS
108 
109 #endif // _LIBCPP___RANGES_SINGLE_VIEW_H
110