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