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 #ifndef _LIBCPP___RANGES_SINGLE_VIEW_H 10 #define _LIBCPP___RANGES_SINGLE_VIEW_H 11 12 #include <__config> 13 #include <__ranges/copyable_box.h> 14 #include <__ranges/range_adaptor.h> 15 #include <__ranges/view_interface.h> 16 #include <__utility/forward.h> 17 #include <__utility/in_place.h> 18 #include <__utility/move.h> 19 #include <concepts> 20 #include <type_traits> 21 22 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) 23 # pragma GCC system_header 24 #endif 25 26 _LIBCPP_BEGIN_NAMESPACE_STD 27 28 #if _LIBCPP_STD_VER > 17 && !defined(_LIBCPP_HAS_NO_INCOMPLETE_RANGES) 29 30 namespace ranges { 31 template<copy_constructible _Tp> 32 requires is_object_v<_Tp> 33 class single_view : public view_interface<single_view<_Tp>> { 34 __copyable_box<_Tp> __value_; 35 36 public: 37 _LIBCPP_HIDE_FROM_ABI 38 single_view() requires default_initializable<_Tp> = default; 39 40 _LIBCPP_HIDE_FROM_ABI 41 constexpr explicit single_view(const _Tp& __t) : __value_(in_place, __t) {} 42 43 _LIBCPP_HIDE_FROM_ABI 44 constexpr explicit single_view(_Tp&& __t) : __value_(in_place, std::move(__t)) {} 45 46 template<class... _Args> 47 requires constructible_from<_Tp, _Args...> 48 _LIBCPP_HIDE_FROM_ABI 49 constexpr explicit single_view(in_place_t, _Args&&... __args) 50 : __value_{in_place, std::forward<_Args>(__args)...} {} 51 52 _LIBCPP_HIDE_FROM_ABI 53 constexpr _Tp* begin() noexcept { return data(); } 54 55 _LIBCPP_HIDE_FROM_ABI 56 constexpr const _Tp* begin() const noexcept { return data(); } 57 58 _LIBCPP_HIDE_FROM_ABI 59 constexpr _Tp* end() noexcept { return data() + 1; } 60 61 _LIBCPP_HIDE_FROM_ABI 62 constexpr const _Tp* end() const noexcept { return data() + 1; } 63 64 _LIBCPP_HIDE_FROM_ABI 65 static constexpr size_t size() noexcept { return 1; } 66 67 _LIBCPP_HIDE_FROM_ABI 68 constexpr _Tp* data() noexcept { return __value_.operator->(); } 69 70 _LIBCPP_HIDE_FROM_ABI 71 constexpr const _Tp* data() const noexcept { return __value_.operator->(); } 72 }; 73 74 template<class _Tp> 75 single_view(_Tp) -> single_view<_Tp>; 76 77 namespace views { 78 namespace __single_view { 79 80 struct __fn : __range_adaptor_closure<__fn> { 81 template<class _Range> 82 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI 83 constexpr auto operator()(_Range&& __range) const 84 noexcept(noexcept(single_view<decay_t<_Range&&>>(std::forward<_Range>(__range)))) 85 -> decltype( single_view<decay_t<_Range&&>>(std::forward<_Range>(__range))) 86 { return single_view<decay_t<_Range&&>>(std::forward<_Range>(__range)); } 87 }; 88 } // namespace __single_view 89 90 inline namespace __cpo { 91 inline constexpr auto single = __single_view::__fn{}; 92 } // namespace __cpo 93 94 } // namespace views 95 } // namespace ranges 96 97 #endif // _LIBCPP_STD_VER > 17 && !defined(_LIBCPP_HAS_NO_INCOMPLETE_RANGES) 98 99 _LIBCPP_END_NAMESPACE_STD 100 101 #endif // _LIBCPP___RANGES_SINGLE_VIEW_H 102