2 //===----------------------------------------------------------------------===//
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
8 //===----------------------------------------------------------------------===//
10 #ifndef _LIBCPP___RANGES_COPYABLE_BOX_H
11 #define _LIBCPP___RANGES_COPYABLE_BOX_H
14 #include <__memory/addressof.h>
15 #include <__memory/construct_at.h>
16 #include <__utility/move.h>
19 #include <type_traits>
21 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
22 #pragma GCC system_header
25 _LIBCPP_BEGIN_NAMESPACE_STD
27 #if !defined(_LIBCPP_HAS_NO_CONCEPTS) && !defined(_LIBCPP_HAS_NO_INCOMPLETE_RANGES)
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.
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.
38 concept __copy_constructible_object = copy_constructible<_Tp> && is_object_v<_Tp>;
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 [[no_unique_address]] optional<_Tp> __val_;
47 template<class ..._Args>
48 requires is_constructible_v<_Tp, _Args...>
50 constexpr explicit __copyable_box(in_place_t, _Args&& ...__args)
51 noexcept(is_nothrow_constructible_v<_Tp, _Args...>)
52 : __val_(in_place, _VSTD::forward<_Args>(__args)...)
56 constexpr __copyable_box() noexcept(is_nothrow_default_constructible_v<_Tp>)
57 requires default_initializable<_Tp>
61 _LIBCPP_HIDE_FROM_ABI __copyable_box(__copyable_box const&) = default;
62 _LIBCPP_HIDE_FROM_ABI __copyable_box(__copyable_box&&) = default;
65 constexpr __copyable_box& operator=(__copyable_box const& __other)
66 noexcept(is_nothrow_copy_constructible_v<_Tp>)
68 if (this != _VSTD::addressof(__other)) {
69 if (__other.__has_value()) __val_.emplace(*__other);
76 __copyable_box& operator=(__copyable_box&&) requires movable<_Tp> = default;
79 constexpr __copyable_box& operator=(__copyable_box&& __other)
80 noexcept(is_nothrow_move_constructible_v<_Tp>)
82 if (this != _VSTD::addressof(__other)) {
83 if (__other.__has_value()) __val_.emplace(_VSTD::move(*__other));
89 _LIBCPP_HIDE_FROM_ABI constexpr _Tp const& operator*() const noexcept { return *__val_; }
90 _LIBCPP_HIDE_FROM_ABI constexpr _Tp& operator*() noexcept { return *__val_; }
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->(); }
95 _LIBCPP_HIDE_FROM_ABI constexpr bool __has_value() const noexcept { return __val_.has_value(); }
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:
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.
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
111 concept __doesnt_need_empty_state_for_copy = copyable<_Tp> || is_nothrow_copy_constructible_v<_Tp>;
114 concept __doesnt_need_empty_state_for_move = movable<_Tp> || is_nothrow_move_constructible_v<_Tp>;
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 [[no_unique_address]] _Tp __val_;
122 template<class ..._Args>
123 requires is_constructible_v<_Tp, _Args...>
124 _LIBCPP_HIDE_FROM_ABI
125 constexpr explicit __copyable_box(in_place_t, _Args&& ...__args)
126 noexcept(is_nothrow_constructible_v<_Tp, _Args...>)
127 : __val_(_VSTD::forward<_Args>(__args)...)
130 _LIBCPP_HIDE_FROM_ABI
131 constexpr __copyable_box() noexcept(is_nothrow_default_constructible_v<_Tp>)
132 requires default_initializable<_Tp>
136 _LIBCPP_HIDE_FROM_ABI __copyable_box(__copyable_box const&) = default;
137 _LIBCPP_HIDE_FROM_ABI __copyable_box(__copyable_box&&) = default;
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;
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 != _VSTD::addressof(__other)) {
148 _VSTD::destroy_at(_VSTD::addressof(__val_));
149 _VSTD::construct_at(_VSTD::addressof(__val_), __other.__val_);
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 != _VSTD::addressof(__other)) {
158 _VSTD::destroy_at(_VSTD::addressof(__val_));
159 _VSTD::construct_at(_VSTD::addressof(__val_), _VSTD::move(__other.__val_));
164 _LIBCPP_HIDE_FROM_ABI constexpr _Tp const& operator*() const noexcept { return __val_; }
165 _LIBCPP_HIDE_FROM_ABI constexpr _Tp& operator*() noexcept { return __val_; }
167 _LIBCPP_HIDE_FROM_ABI constexpr const _Tp *operator->() const noexcept { return _VSTD::addressof(__val_); }
168 _LIBCPP_HIDE_FROM_ABI constexpr _Tp *operator->() noexcept { return _VSTD::addressof(__val_); }
170 _LIBCPP_HIDE_FROM_ABI constexpr bool __has_value() const noexcept { return true; }
172 } // namespace ranges
174 #endif // !defined(_LIBCPP_HAS_NO_CONCEPTS) && !defined(_LIBCPP_HAS_NO_INCOMPLETE_RANGES)
176 _LIBCPP_END_NAMESPACE_STD
178 #endif // _LIBCPP___RANGES_COPYABLE_BOX_H