2 //===----------------------------------------------------------------------===//
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
8 //===----------------------------------------------------------------------===//
10 #ifndef _LIBCPP___CXX03___RANGES_SINGLE_VIEW_H
11 #define _LIBCPP___CXX03___RANGES_SINGLE_VIEW_H
13 #include <__cxx03/__concepts/constructible.h>
14 #include <__cxx03/__config>
15 #include <__cxx03/__ranges/movable_box.h>
16 #include <__cxx03/__ranges/range_adaptor.h>
17 #include <__cxx03/__ranges/view_interface.h>
18 #include <__cxx03/__type_traits/decay.h>
19 #include <__cxx03/__type_traits/is_object.h>
20 #include <__cxx03/__utility/forward.h>
21 #include <__cxx03/__utility/in_place.h>
22 #include <__cxx03/__utility/move.h>
23 #include <__cxx03/cstddef>
25 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
26 # pragma GCC system_header
30 #include <__cxx03/__undef_macros>
32 _LIBCPP_BEGIN_NAMESPACE_STD
34 #if _LIBCPP_STD_VER >= 20
37 # if _LIBCPP_STD_VER >= 23
38 template <move_constructible _Tp
>
40 template <copy_constructible _Tp
>
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_
;
47 _LIBCPP_HIDE_FROM_ABI
single_view()
48 requires default_initializable
<_Tp
>
51 _LIBCPP_HIDE_FROM_ABI
constexpr explicit single_view(const _Tp
& __t
)
52 # if _LIBCPP_STD_VER >= 23
53 requires copy_constructible
<_Tp
>
55 : __value_(in_place
, __t
) {
58 _LIBCPP_HIDE_FROM_ABI
constexpr explicit single_view(_Tp
&& __t
) : __value_(in_place
, std::move(__t
)) {}
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
)...} {}
65 _LIBCPP_HIDE_FROM_ABI
constexpr _Tp
* begin() noexcept
{ return data(); }
67 _LIBCPP_HIDE_FROM_ABI
constexpr const _Tp
* begin() const noexcept
{ return data(); }
69 _LIBCPP_HIDE_FROM_ABI
constexpr _Tp
* end() noexcept
{ return data() + 1; }
71 _LIBCPP_HIDE_FROM_ABI
constexpr const _Tp
* end() const noexcept
{ return data() + 1; }
73 _LIBCPP_HIDE_FROM_ABI
static constexpr bool empty() noexcept
{ return false; }
75 _LIBCPP_HIDE_FROM_ABI
static constexpr size_t size() noexcept
{ return 1; }
77 _LIBCPP_HIDE_FROM_ABI
constexpr _Tp
* data() noexcept
{ return __value_
.operator->(); }
79 _LIBCPP_HIDE_FROM_ABI
constexpr const _Tp
* data() const noexcept
{ return __value_
.operator->(); }
83 single_view(_Tp
) -> single_view
<_Tp
>;
86 namespace __single_view
{
88 struct __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
));
96 } // namespace __single_view
98 inline namespace __cpo
{
99 inline constexpr auto single
= __single_view::__fn
{};
103 } // namespace ranges
105 #endif // _LIBCPP_STD_VER >= 20
107 _LIBCPP_END_NAMESPACE_STD
111 #endif // _LIBCPP___CXX03___RANGES_SINGLE_VIEW_H