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_MOVABLE_BOX_H 11 #define _LIBCPP___RANGES_MOVABLE_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 <__type_traits/is_nothrow_constructible.h> 20 #include <__type_traits/is_nothrow_copy_constructible.h> 21 #include <__type_traits/is_nothrow_default_constructible.h> 22 #include <__utility/move.h> 23 #include <optional> 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 36 // __movable_box allows turning a type that is move-constructible (but maybe not move-assignable) into 37 // a type that is both move-constructible and move-assignable. It does that by introducing an empty state 38 // and basically doing destroy-then-copy-construct in the assignment operator. The empty state is necessary 39 // to handle the case where the copy construction fails after destroying the object. 40 // 41 // In some cases, we can completely avoid the use of an empty state; we provide a specialization of 42 // __movable_box that does this, see below for the details. 43 44 // until C++23, `__movable_box` was named `__copyable_box` and required the stored type to be copy-constructible, not 45 // just move-constructible; we preserve the old behavior in pre-C++23 modes. 46 template <class _Tp> 47 concept __movable_box_object = 48 # if _LIBCPP_STD_VER >= 23 49 move_constructible<_Tp> 50 # else 51 copy_constructible<_Tp> 52 # endif 53 && is_object_v<_Tp>; 54 55 namespace ranges { 56 // Primary template - uses std::optional and introduces an empty state in case assignment fails. 57 template <__movable_box_object _Tp> 58 class __movable_box { 59 _LIBCPP_NO_UNIQUE_ADDRESS optional<_Tp> __val_; 60 61 public: 62 template <class... _Args> 63 requires is_constructible_v<_Tp, _Args...> __movable_box(in_place_t,_Args &&...__args)64 _LIBCPP_HIDE_FROM_ABI constexpr explicit __movable_box(in_place_t, _Args&&... __args) noexcept( 65 is_nothrow_constructible_v<_Tp, _Args...>) 66 : __val_(in_place, std::forward<_Args>(__args)...) {} 67 __movable_box()68 _LIBCPP_HIDE_FROM_ABI constexpr __movable_box() noexcept(is_nothrow_default_constructible_v<_Tp>) 69 requires default_initializable<_Tp> 70 : __val_(in_place) {} 71 72 _LIBCPP_HIDE_FROM_ABI __movable_box(__movable_box const&) = default; 73 _LIBCPP_HIDE_FROM_ABI __movable_box(__movable_box&&) = default; 74 75 _LIBCPP_HIDE_FROM_ABI constexpr __movable_box& noexcept(is_nothrow_copy_constructible_v<_Tp>)76 operator=(__movable_box const& __other) noexcept(is_nothrow_copy_constructible_v<_Tp>) 77 # if _LIBCPP_STD_VER >= 23 78 requires copy_constructible<_Tp> 79 # endif 80 { 81 if (this != std::addressof(__other)) { 82 if (__other.__has_value()) 83 __val_.emplace(*__other); 84 else 85 __val_.reset(); 86 } 87 return *this; 88 } 89 90 _LIBCPP_HIDE_FROM_ABI __movable_box& operator=(__movable_box&&) 91 requires movable<_Tp> 92 = default; 93 94 _LIBCPP_HIDE_FROM_ABI constexpr __movable_box& noexcept(is_nothrow_move_constructible_v<_Tp>)95 operator=(__movable_box&& __other) noexcept(is_nothrow_move_constructible_v<_Tp>) { 96 if (this != std::addressof(__other)) { 97 if (__other.__has_value()) 98 __val_.emplace(std::move(*__other)); 99 else 100 __val_.reset(); 101 } 102 return *this; 103 } 104 105 _LIBCPP_HIDE_FROM_ABI constexpr _Tp const& operator*() const noexcept { return *__val_; } 106 _LIBCPP_HIDE_FROM_ABI constexpr _Tp& operator*() noexcept { return *__val_; } 107 108 _LIBCPP_HIDE_FROM_ABI constexpr const _Tp* operator->() const noexcept { return __val_.operator->(); } 109 _LIBCPP_HIDE_FROM_ABI constexpr _Tp* operator->() noexcept { return __val_.operator->(); } 110 __has_value()111 _LIBCPP_HIDE_FROM_ABI constexpr bool __has_value() const noexcept { return __val_.has_value(); } 112 }; 113 114 // This partial specialization implements an optimization for when we know we don't need to store 115 // an empty state to represent failure to perform an assignment. For copy-assignment, this happens: 116 // 117 // 1. If the type is copyable (which includes copy-assignment), we can use the type's own assignment operator 118 // directly and avoid using std::optional. 119 // 2. If the type is not copyable, but it is nothrow-copy-constructible, then we can implement assignment as 120 // destroy-and-then-construct and we know it will never fail, so we don't need an empty state. 121 // 122 // The exact same reasoning can be applied for move-assignment, with copyable replaced by movable and 123 // nothrow-copy-constructible replaced by nothrow-move-constructible. This specialization is enabled 124 // whenever we can apply any of these optimizations for both the copy assignment and the move assignment 125 // operator. 126 127 # if _LIBCPP_STD_VER >= 23 128 template <class _Tp> 129 concept __doesnt_need_empty_state = 130 (copy_constructible<_Tp> 131 // 1. If copy_constructible<T> is true, movable-box<T> should store only a T if either T models 132 // copyable, or is_nothrow_move_constructible_v<T> && is_nothrow_copy_constructible_v<T> is true. 133 ? copyable<_Tp> || (is_nothrow_move_constructible_v<_Tp> && is_nothrow_copy_constructible_v<_Tp>) 134 // 2. Otherwise, movable-box<T> should store only a T if either T models movable or 135 // is_nothrow_move_constructible_v<T> is true. 136 : movable<_Tp> || is_nothrow_move_constructible_v<_Tp>); 137 # else 138 139 template <class _Tp> 140 concept __doesnt_need_empty_state_for_copy = copyable<_Tp> || is_nothrow_copy_constructible_v<_Tp>; 141 142 template <class _Tp> 143 concept __doesnt_need_empty_state_for_move = movable<_Tp> || is_nothrow_move_constructible_v<_Tp>; 144 145 template <class _Tp> 146 concept __doesnt_need_empty_state = __doesnt_need_empty_state_for_copy<_Tp> && __doesnt_need_empty_state_for_move<_Tp>; 147 # endif 148 149 template <__movable_box_object _Tp> 150 requires __doesnt_need_empty_state<_Tp> 151 class __movable_box<_Tp> { 152 _LIBCPP_NO_UNIQUE_ADDRESS _Tp __val_; 153 154 public: 155 template <class... _Args> 156 requires is_constructible_v<_Tp, _Args...> __movable_box(in_place_t,_Args &&...__args)157 _LIBCPP_HIDE_FROM_ABI constexpr explicit __movable_box(in_place_t, _Args&&... __args) noexcept( 158 is_nothrow_constructible_v<_Tp, _Args...>) 159 : __val_(std::forward<_Args>(__args)...) {} 160 __movable_box()161 _LIBCPP_HIDE_FROM_ABI constexpr __movable_box() noexcept(is_nothrow_default_constructible_v<_Tp>) 162 requires default_initializable<_Tp> 163 : __val_() {} 164 165 _LIBCPP_HIDE_FROM_ABI __movable_box(__movable_box const&) = default; 166 _LIBCPP_HIDE_FROM_ABI __movable_box(__movable_box&&) = default; 167 168 // Implementation of assignment operators in case we perform optimization (1) 169 _LIBCPP_HIDE_FROM_ABI __movable_box& operator=(__movable_box const&) 170 requires copyable<_Tp> 171 = default; 172 _LIBCPP_HIDE_FROM_ABI __movable_box& operator=(__movable_box&&) 173 requires movable<_Tp> 174 = default; 175 176 // Implementation of assignment operators in case we perform optimization (2) 177 _LIBCPP_HIDE_FROM_ABI constexpr __movable_box& operator=(__movable_box const& __other) noexcept { 178 static_assert(is_nothrow_copy_constructible_v<_Tp>); 179 if (this != std::addressof(__other)) { 180 std::destroy_at(std::addressof(__val_)); 181 std::construct_at(std::addressof(__val_), __other.__val_); 182 } 183 return *this; 184 } 185 186 _LIBCPP_HIDE_FROM_ABI constexpr __movable_box& operator=(__movable_box&& __other) noexcept { 187 static_assert(is_nothrow_move_constructible_v<_Tp>); 188 if (this != std::addressof(__other)) { 189 std::destroy_at(std::addressof(__val_)); 190 std::construct_at(std::addressof(__val_), std::move(__other.__val_)); 191 } 192 return *this; 193 } 194 195 _LIBCPP_HIDE_FROM_ABI constexpr _Tp const& operator*() const noexcept { return __val_; } 196 _LIBCPP_HIDE_FROM_ABI constexpr _Tp& operator*() noexcept { return __val_; } 197 198 _LIBCPP_HIDE_FROM_ABI constexpr const _Tp* operator->() const noexcept { return std::addressof(__val_); } 199 _LIBCPP_HIDE_FROM_ABI constexpr _Tp* operator->() noexcept { return std::addressof(__val_); } 200 __has_value()201 _LIBCPP_HIDE_FROM_ABI constexpr bool __has_value() const noexcept { return true; } 202 }; 203 } // namespace ranges 204 205 #endif // _LIBCPP_STD_VER >= 20 206 207 _LIBCPP_END_NAMESPACE_STD 208 209 _LIBCPP_POP_MACROS 210 211 #endif // _LIBCPP___RANGES_MOVABLE_BOX_H 212