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
36namespace ranges {
37# if _LIBCPP_STD_VER >= 23
38template <move_constructible _Tp>
39# else
40template <copy_constructible _Tp>
41# endif
42 requires is_object_v<_Tp>
43class _LIBCPP_ABI_LLVM18_NO_UNIQUE_ADDRESS single_view : public view_interface<single_view<_Tp>> {
44 _LIBCPP_NO_UNIQUE_ADDRESS __movable_box<_Tp> __value_;
45
46public:
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 bool empty() noexcept { return false; }
74
75 _LIBCPP_HIDE_FROM_ABI static constexpr size_t size() noexcept { return 1; }
76
77 _LIBCPP_HIDE_FROM_ABI constexpr _Tp* data() noexcept { return __value_.operator->(); }
78
79 _LIBCPP_HIDE_FROM_ABI constexpr const _Tp* data() const noexcept { return __value_.operator->(); }
80};
81
82template <class _Tp>
83single_view(_Tp) -> single_view<_Tp>;
84
85namespace views {
86namespace __single_view {
87
88struct __fn : __range_adaptor_closure<__fn> {
89 template <class _Range>
90 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto operator()(_Range&& __range) const
91 noexcept(noexcept(single_view<decay_t<_Range&&>>(std::forward<_Range>(__range))))
92 -> decltype(single_view<decay_t<_Range&&>>(std::forward<_Range>(__range))) {
93 return single_view<decay_t<_Range&&>>(std::forward<_Range>(__range));
94 }
95};
96} // namespace __single_view
97
98inline namespace __cpo {
99inline constexpr auto single = __single_view::__fn{};
100} // namespace __cpo
101
102} // namespace views
103} // namespace ranges
104
105#endif // _LIBCPP_STD_VER >= 20
106
107_LIBCPP_END_NAMESPACE_STD
108
109_LIBCPP_POP_MACROS
110
111#endif // _LIBCPP___RANGES_SINGLE_VIEW_H
112