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. 46template <class _Tp> 47concept __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 55namespace ranges { 56// Primary template - uses std::optional and introduces an empty state in case assignment fails. 57template <__movable_box_object _Tp> 58class __movable_box { 59 _LIBCPP_NO_UNIQUE_ADDRESS optional<_Tp> __val_; 60 61public: 62 template <class... _Args> 63 requires is_constructible_v<_Tp, _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 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& 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& 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 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 128template <class _Tp> 129concept __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 138// When _Tp doesn't have an assignment operator, we must implement __movable_box's assignment operator 139// by doing destroy_at followed by construct_at. However, that implementation strategy leads to UB if the nested 140// _Tp is potentially overlapping, as it is doing a non-transparent replacement of the sub-object, which means that 141// we're not considered "nested" inside the movable-box anymore, and since we're not nested within it, [basic.life]/1.5 142// says that we essentially just reused the storage of the movable-box for a completely unrelated object and ended the 143// movable-box's lifetime. 144// https://github.com/llvm/llvm-project/issues/70494#issuecomment-1845646490 145// 146// Hence, when the _Tp doesn't have an assignment operator, we can't risk making it a potentially-overlapping 147// subobject because of the above, and we don't use [[no_unique_address]] in that case. 148template <class _Tp> 149concept __can_use_no_unique_address = (copy_constructible<_Tp> ? copyable<_Tp> : movable<_Tp>); 150 151# else 152 153template <class _Tp> 154concept __doesnt_need_empty_state_for_copy = copyable<_Tp> || is_nothrow_copy_constructible_v<_Tp>; 155 156template <class _Tp> 157concept __doesnt_need_empty_state_for_move = movable<_Tp> || is_nothrow_move_constructible_v<_Tp>; 158 159template <class _Tp> 160concept __doesnt_need_empty_state = __doesnt_need_empty_state_for_copy<_Tp> && __doesnt_need_empty_state_for_move<_Tp>; 161 162template <class _Tp> 163concept __can_use_no_unique_address = copyable<_Tp>; 164# endif 165 166template <class _Tp> 167struct __movable_box_holder { 168 _Tp __val_; 169 170 template <class... _Args> 171 _LIBCPP_HIDE_FROM_ABI constexpr explicit __movable_box_holder(in_place_t, _Args&&... __args) 172 : __val_(std::forward<_Args>(__args)...) {} 173}; 174 175template <class _Tp> 176 requires __can_use_no_unique_address<_Tp> 177struct __movable_box_holder<_Tp> { 178 _LIBCPP_NO_UNIQUE_ADDRESS _Tp __val_; 179 180 template <class... _Args> 181 _LIBCPP_HIDE_FROM_ABI constexpr explicit __movable_box_holder(in_place_t, _Args&&... __args) 182 : __val_(std::forward<_Args>(__args)...) {} 183}; 184 185template <__movable_box_object _Tp> 186 requires __doesnt_need_empty_state<_Tp> 187class __movable_box<_Tp> { 188 _LIBCPP_NO_UNIQUE_ADDRESS __movable_box_holder<_Tp> __holder_; 189 190public: 191 template <class... _Args> 192 requires is_constructible_v<_Tp, _Args...> 193 _LIBCPP_HIDE_FROM_ABI constexpr explicit __movable_box(in_place_t __inplace, _Args&&... __args) noexcept( 194 is_nothrow_constructible_v<_Tp, _Args...>) 195 : __holder_(__inplace, std::forward<_Args>(__args)...) {} 196 197 _LIBCPP_HIDE_FROM_ABI constexpr __movable_box() noexcept(is_nothrow_default_constructible_v<_Tp>) 198 requires default_initializable<_Tp> 199 : __holder_(in_place_t{}) {} 200 201 _LIBCPP_HIDE_FROM_ABI __movable_box(__movable_box const&) = default; 202 _LIBCPP_HIDE_FROM_ABI __movable_box(__movable_box&&) = default; 203 204 // Implementation of assignment operators in case we perform optimization (1) 205 _LIBCPP_HIDE_FROM_ABI __movable_box& operator=(__movable_box const&) 206 requires copyable<_Tp> 207 = default; 208 _LIBCPP_HIDE_FROM_ABI __movable_box& operator=(__movable_box&&) 209 requires movable<_Tp> 210 = default; 211 212 // Implementation of assignment operators in case we perform optimization (2) 213 _LIBCPP_HIDE_FROM_ABI constexpr __movable_box& operator=(__movable_box const& __other) noexcept { 214 static_assert(is_nothrow_copy_constructible_v<_Tp>); 215 static_assert(!__can_use_no_unique_address<_Tp>); 216 if (this != std::addressof(__other)) { 217 std::destroy_at(std::addressof(__holder_.__val_)); 218 std::construct_at(std::addressof(__holder_.__val_), __other.__holder_.__val_); 219 } 220 return *this; 221 } 222 223 _LIBCPP_HIDE_FROM_ABI constexpr __movable_box& operator=(__movable_box&& __other) noexcept { 224 static_assert(is_nothrow_move_constructible_v<_Tp>); 225 static_assert(!__can_use_no_unique_address<_Tp>); 226 if (this != std::addressof(__other)) { 227 std::destroy_at(std::addressof(__holder_.__val_)); 228 std::construct_at(std::addressof(__holder_.__val_), std::move(__other.__holder_.__val_)); 229 } 230 return *this; 231 } 232 233 _LIBCPP_HIDE_FROM_ABI constexpr _Tp const& operator*() const noexcept { return __holder_.__val_; } 234 _LIBCPP_HIDE_FROM_ABI constexpr _Tp& operator*() noexcept { return __holder_.__val_; } 235 236 _LIBCPP_HIDE_FROM_ABI constexpr const _Tp* operator->() const noexcept { return std::addressof(__holder_.__val_); } 237 _LIBCPP_HIDE_FROM_ABI constexpr _Tp* operator->() noexcept { return std::addressof(__holder_.__val_); } 238 239 _LIBCPP_HIDE_FROM_ABI constexpr bool __has_value() const noexcept { return true; } 240}; 241} // namespace ranges 242 243#endif // _LIBCPP_STD_VER >= 20 244 245_LIBCPP_END_NAMESPACE_STD 246 247_LIBCPP_POP_MACROS 248 249#endif // _LIBCPP___RANGES_MOVABLE_BOX_H 250