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_COPYABLE_BOX_H 11 #define _LIBCPP___RANGES_COPYABLE_BOX_H 12 13 #include <__concepts/constructible.h> 14 #include <__concepts/copyable.h> 15 #include <__concepts/movable.h> 16 #include <__config> 17 #include <__memory/addressof.h> 18 #include <__memory/construct_at.h> 19 #include <__utility/move.h> 20 #include <optional> 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 >= 20 29 30 // __copyable_box allows turning a type that is copy-constructible (but maybe not copy-assignable) into 31 // a type that is both copy-constructible and copy-assignable. It does that by introducing an empty state 32 // and basically doing destroy-then-copy-construct in the assignment operator. The empty state is necessary 33 // to handle the case where the copy construction fails after destroying the object. 34 // 35 // In some cases, we can completely avoid the use of an empty state; we provide a specialization of 36 // __copyable_box that does this, see below for the details. 37 38 template<class _Tp> 39 concept __copy_constructible_object = copy_constructible<_Tp> && is_object_v<_Tp>; 40 41 namespace ranges { 42 // Primary template - uses std::optional and introduces an empty state in case assignment fails. 43 template<__copy_constructible_object _Tp> 44 class __copyable_box { 45 _LIBCPP_NO_UNIQUE_ADDRESS optional<_Tp> __val_; 46 47 public: 48 template<class ..._Args> 49 requires is_constructible_v<_Tp, _Args...> 50 _LIBCPP_HIDE_FROM_ABI __copyable_box(in_place_t,_Args &&...__args)51 constexpr explicit __copyable_box(in_place_t, _Args&& ...__args) 52 noexcept(is_nothrow_constructible_v<_Tp, _Args...>) 53 : __val_(in_place, std::forward<_Args>(__args)...) 54 { } 55 56 _LIBCPP_HIDE_FROM_ABI __copyable_box()57 constexpr __copyable_box() noexcept(is_nothrow_default_constructible_v<_Tp>) 58 requires default_initializable<_Tp> 59 : __val_(in_place) 60 { } 61 62 _LIBCPP_HIDE_FROM_ABI __copyable_box(__copyable_box const&) = default; 63 _LIBCPP_HIDE_FROM_ABI __copyable_box(__copyable_box&&) = default; 64 65 _LIBCPP_HIDE_FROM_ABI 66 constexpr __copyable_box& operator=(__copyable_box const& __other) noexcept(is_nothrow_copy_constructible_v<_Tp>)67 noexcept(is_nothrow_copy_constructible_v<_Tp>) 68 { 69 if (this != std::addressof(__other)) { 70 if (__other.__has_value()) __val_.emplace(*__other); 71 else __val_.reset(); 72 } 73 return *this; 74 } 75 76 _LIBCPP_HIDE_FROM_ABI 77 __copyable_box& operator=(__copyable_box&&) requires movable<_Tp> = default; 78 79 _LIBCPP_HIDE_FROM_ABI 80 constexpr __copyable_box& operator=(__copyable_box&& __other) noexcept(is_nothrow_move_constructible_v<_Tp>)81 noexcept(is_nothrow_move_constructible_v<_Tp>) 82 { 83 if (this != std::addressof(__other)) { 84 if (__other.__has_value()) __val_.emplace(std::move(*__other)); 85 else __val_.reset(); 86 } 87 return *this; 88 } 89 90 _LIBCPP_HIDE_FROM_ABI constexpr _Tp const& operator*() const noexcept { return *__val_; } 91 _LIBCPP_HIDE_FROM_ABI constexpr _Tp& operator*() noexcept { return *__val_; } 92 93 _LIBCPP_HIDE_FROM_ABI constexpr const _Tp *operator->() const noexcept { return __val_.operator->(); } 94 _LIBCPP_HIDE_FROM_ABI constexpr _Tp *operator->() noexcept { return __val_.operator->(); } 95 __has_value()96 _LIBCPP_HIDE_FROM_ABI constexpr bool __has_value() const noexcept { return __val_.has_value(); } 97 }; 98 99 // This partial specialization implements an optimization for when we know we don't need to store 100 // an empty state to represent failure to perform an assignment. For copy-assignment, this happens: 101 // 102 // 1. If the type is copyable (which includes copy-assignment), we can use the type's own assignment operator 103 // directly and avoid using std::optional. 104 // 2. If the type is not copyable, but it is nothrow-copy-constructible, then we can implement assignment as 105 // destroy-and-then-construct and we know it will never fail, so we don't need an empty state. 106 // 107 // The exact same reasoning can be applied for move-assignment, with copyable replaced by movable and 108 // nothrow-copy-constructible replaced by nothrow-move-constructible. This specialization is enabled 109 // whenever we can apply any of these optimizations for both the copy assignment and the move assignment 110 // operator. 111 template<class _Tp> 112 concept __doesnt_need_empty_state_for_copy = copyable<_Tp> || is_nothrow_copy_constructible_v<_Tp>; 113 114 template<class _Tp> 115 concept __doesnt_need_empty_state_for_move = movable<_Tp> || is_nothrow_move_constructible_v<_Tp>; 116 117 template<__copy_constructible_object _Tp> 118 requires __doesnt_need_empty_state_for_copy<_Tp> && __doesnt_need_empty_state_for_move<_Tp> 119 class __copyable_box<_Tp> { 120 _LIBCPP_NO_UNIQUE_ADDRESS _Tp __val_; 121 122 public: 123 template<class ..._Args> 124 requires is_constructible_v<_Tp, _Args...> 125 _LIBCPP_HIDE_FROM_ABI __copyable_box(in_place_t,_Args &&...__args)126 constexpr explicit __copyable_box(in_place_t, _Args&& ...__args) 127 noexcept(is_nothrow_constructible_v<_Tp, _Args...>) 128 : __val_(std::forward<_Args>(__args)...) 129 { } 130 131 _LIBCPP_HIDE_FROM_ABI __copyable_box()132 constexpr __copyable_box() noexcept(is_nothrow_default_constructible_v<_Tp>) 133 requires default_initializable<_Tp> 134 : __val_() 135 { } 136 137 _LIBCPP_HIDE_FROM_ABI __copyable_box(__copyable_box const&) = default; 138 _LIBCPP_HIDE_FROM_ABI __copyable_box(__copyable_box&&) = default; 139 140 // Implementation of assignment operators in case we perform optimization (1) 141 _LIBCPP_HIDE_FROM_ABI __copyable_box& operator=(__copyable_box const&) requires copyable<_Tp> = default; 142 _LIBCPP_HIDE_FROM_ABI __copyable_box& operator=(__copyable_box&&) requires movable<_Tp> = default; 143 144 // Implementation of assignment operators in case we perform optimization (2) 145 _LIBCPP_HIDE_FROM_ABI 146 constexpr __copyable_box& operator=(__copyable_box const& __other) noexcept { 147 static_assert(is_nothrow_copy_constructible_v<_Tp>); 148 if (this != std::addressof(__other)) { 149 std::destroy_at(std::addressof(__val_)); 150 std::construct_at(std::addressof(__val_), __other.__val_); 151 } 152 return *this; 153 } 154 155 _LIBCPP_HIDE_FROM_ABI 156 constexpr __copyable_box& operator=(__copyable_box&& __other) noexcept { 157 static_assert(is_nothrow_move_constructible_v<_Tp>); 158 if (this != std::addressof(__other)) { 159 std::destroy_at(std::addressof(__val_)); 160 std::construct_at(std::addressof(__val_), std::move(__other.__val_)); 161 } 162 return *this; 163 } 164 165 _LIBCPP_HIDE_FROM_ABI constexpr _Tp const& operator*() const noexcept { return __val_; } 166 _LIBCPP_HIDE_FROM_ABI constexpr _Tp& operator*() noexcept { return __val_; } 167 168 _LIBCPP_HIDE_FROM_ABI constexpr const _Tp *operator->() const noexcept { return std::addressof(__val_); } 169 _LIBCPP_HIDE_FROM_ABI constexpr _Tp *operator->() noexcept { return std::addressof(__val_); } 170 __has_value()171 _LIBCPP_HIDE_FROM_ABI constexpr bool __has_value() const noexcept { return true; } 172 }; 173 } // namespace ranges 174 175 #endif // _LIBCPP_STD_VER >= 20 176 177 _LIBCPP_END_NAMESPACE_STD 178 179 #endif // _LIBCPP___RANGES_COPYABLE_BOX_H 180