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/view_interface.h> 15 #include <__utility/forward.h> 16 #include <__utility/in_place.h> 17 #include <__utility/move.h> 18 #include <concepts> 19 #include <type_traits> 20 21 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) 22 #pragma GCC system_header 23 #endif 24 25 _LIBCPP_BEGIN_NAMESPACE_STD 26 27 #if !defined(_LIBCPP_HAS_NO_CONCEPTS) && !defined(_LIBCPP_HAS_NO_INCOMPLETE_RANGES) 28 29 namespace ranges { 30 template<copy_constructible _Tp> 31 requires is_object_v<_Tp> 32 class single_view : public view_interface<single_view<_Tp>> { 33 __copyable_box<_Tp> __value_; 34 35 public: 36 _LIBCPP_HIDE_FROM_ABI 37 single_view() requires default_initializable<_Tp> = default; 38 39 _LIBCPP_HIDE_FROM_ABI 40 constexpr explicit single_view(const _Tp& __t) : __value_(in_place, __t) {} 41 42 _LIBCPP_HIDE_FROM_ABI 43 constexpr explicit single_view(_Tp&& __t) : __value_(in_place, _VSTD::move(__t)) {} 44 45 template<class... _Args> 46 requires constructible_from<_Tp, _Args...> 47 _LIBCPP_HIDE_FROM_ABI 48 constexpr explicit single_view(in_place_t, _Args&&... __args) 49 : __value_{in_place, _VSTD::forward<_Args>(__args)...} {} 50 51 _LIBCPP_HIDE_FROM_ABI 52 constexpr _Tp* begin() noexcept { return data(); } 53 54 _LIBCPP_HIDE_FROM_ABI 55 constexpr const _Tp* begin() const noexcept { return data(); } 56 57 _LIBCPP_HIDE_FROM_ABI 58 constexpr _Tp* end() noexcept { return data() + 1; } 59 60 _LIBCPP_HIDE_FROM_ABI 61 constexpr const _Tp* end() const noexcept { return data() + 1; } 62 63 _LIBCPP_HIDE_FROM_ABI 64 static constexpr size_t size() noexcept { return 1; } 65 66 _LIBCPP_HIDE_FROM_ABI 67 constexpr _Tp* data() noexcept { return __value_.operator->(); } 68 69 _LIBCPP_HIDE_FROM_ABI 70 constexpr const _Tp* data() const noexcept { return __value_.operator->(); } 71 }; 72 73 template<class _Tp> 74 single_view(_Tp) -> single_view<_Tp>; 75 } // namespace ranges 76 77 #endif // !defined(_LIBCPP_HAS_NO_CONCEPTS) && !defined(_LIBCPP_HAS_NO_INCOMPLETE_RANGES) 78 79 _LIBCPP_END_NAMESPACE_STD 80 81 #endif // _LIBCPP___RANGES_SINGLE_VIEW_H 82