xref: /freebsd-src/contrib/llvm-project/libcxx/include/__ranges/movable_box.h (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
106c3fb27SDimitry Andric // -*- C++ -*-
206c3fb27SDimitry Andric //===----------------------------------------------------------------------===//
306c3fb27SDimitry Andric //
406c3fb27SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
506c3fb27SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
606c3fb27SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
706c3fb27SDimitry Andric //
806c3fb27SDimitry Andric //===----------------------------------------------------------------------===//
906c3fb27SDimitry Andric 
1006c3fb27SDimitry Andric #ifndef _LIBCPP___RANGES_MOVABLE_BOX_H
1106c3fb27SDimitry Andric #define _LIBCPP___RANGES_MOVABLE_BOX_H
1206c3fb27SDimitry Andric 
1306c3fb27SDimitry Andric #include <__concepts/constructible.h>
1406c3fb27SDimitry Andric #include <__concepts/copyable.h>
1506c3fb27SDimitry Andric #include <__concepts/movable.h>
1606c3fb27SDimitry Andric #include <__config>
1706c3fb27SDimitry Andric #include <__memory/addressof.h>
1806c3fb27SDimitry Andric #include <__memory/construct_at.h>
1906c3fb27SDimitry Andric #include <__type_traits/is_nothrow_constructible.h>
2006c3fb27SDimitry Andric #include <__utility/move.h>
2106c3fb27SDimitry Andric #include <optional>
2206c3fb27SDimitry Andric 
2306c3fb27SDimitry Andric #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
2406c3fb27SDimitry Andric #  pragma GCC system_header
2506c3fb27SDimitry Andric #endif
2606c3fb27SDimitry Andric 
275f757f3fSDimitry Andric _LIBCPP_PUSH_MACROS
285f757f3fSDimitry Andric #include <__undef_macros>
295f757f3fSDimitry Andric 
3006c3fb27SDimitry Andric _LIBCPP_BEGIN_NAMESPACE_STD
3106c3fb27SDimitry Andric 
3206c3fb27SDimitry Andric #if _LIBCPP_STD_VER >= 20
3306c3fb27SDimitry Andric 
3406c3fb27SDimitry Andric // __movable_box allows turning a type that is move-constructible (but maybe not move-assignable) into
3506c3fb27SDimitry Andric // a type that is both move-constructible and move-assignable. It does that by introducing an empty state
3606c3fb27SDimitry Andric // and basically doing destroy-then-copy-construct in the assignment operator. The empty state is necessary
3706c3fb27SDimitry Andric // to handle the case where the copy construction fails after destroying the object.
3806c3fb27SDimitry Andric //
3906c3fb27SDimitry Andric // In some cases, we can completely avoid the use of an empty state; we provide a specialization of
4006c3fb27SDimitry Andric // __movable_box that does this, see below for the details.
4106c3fb27SDimitry Andric 
4206c3fb27SDimitry Andric // until C++23, `__movable_box` was named `__copyable_box` and required the stored type to be copy-constructible, not
4306c3fb27SDimitry Andric // just move-constructible; we preserve the old behavior in pre-C++23 modes.
4406c3fb27SDimitry Andric template <class _Tp>
4506c3fb27SDimitry Andric concept __movable_box_object =
4606c3fb27SDimitry Andric #  if _LIBCPP_STD_VER >= 23
4706c3fb27SDimitry Andric     move_constructible<_Tp>
4806c3fb27SDimitry Andric #  else
4906c3fb27SDimitry Andric     copy_constructible<_Tp>
5006c3fb27SDimitry Andric #  endif
5106c3fb27SDimitry Andric     && is_object_v<_Tp>;
5206c3fb27SDimitry Andric 
5306c3fb27SDimitry Andric namespace ranges {
5406c3fb27SDimitry Andric // Primary template - uses std::optional and introduces an empty state in case assignment fails.
5506c3fb27SDimitry Andric template <__movable_box_object _Tp>
5606c3fb27SDimitry Andric class __movable_box {
5706c3fb27SDimitry Andric   _LIBCPP_NO_UNIQUE_ADDRESS optional<_Tp> __val_;
5806c3fb27SDimitry Andric 
5906c3fb27SDimitry Andric public:
6006c3fb27SDimitry Andric   template <class... _Args>
6106c3fb27SDimitry Andric     requires is_constructible_v<_Tp, _Args...>
6206c3fb27SDimitry Andric   _LIBCPP_HIDE_FROM_ABI constexpr explicit __movable_box(in_place_t, _Args&&... __args) noexcept(
6306c3fb27SDimitry Andric       is_nothrow_constructible_v<_Tp, _Args...>)
6406c3fb27SDimitry Andric       : __val_(in_place, std::forward<_Args>(__args)...) {}
6506c3fb27SDimitry Andric 
6606c3fb27SDimitry Andric   _LIBCPP_HIDE_FROM_ABI constexpr __movable_box() noexcept(is_nothrow_default_constructible_v<_Tp>)
6706c3fb27SDimitry Andric     requires default_initializable<_Tp>
6806c3fb27SDimitry Andric       : __val_(in_place) {}
6906c3fb27SDimitry Andric 
7006c3fb27SDimitry Andric   _LIBCPP_HIDE_FROM_ABI __movable_box(__movable_box const&) = default;
7106c3fb27SDimitry Andric   _LIBCPP_HIDE_FROM_ABI __movable_box(__movable_box&&)      = default;
7206c3fb27SDimitry Andric 
7306c3fb27SDimitry Andric   _LIBCPP_HIDE_FROM_ABI constexpr __movable_box&
7406c3fb27SDimitry Andric   operator=(__movable_box const& __other) noexcept(is_nothrow_copy_constructible_v<_Tp>)
7506c3fb27SDimitry Andric #  if _LIBCPP_STD_VER >= 23
7606c3fb27SDimitry Andric     requires copy_constructible<_Tp>
7706c3fb27SDimitry Andric #  endif
7806c3fb27SDimitry Andric   {
7906c3fb27SDimitry Andric     if (this != std::addressof(__other)) {
8006c3fb27SDimitry Andric       if (__other.__has_value())
8106c3fb27SDimitry Andric         __val_.emplace(*__other);
8206c3fb27SDimitry Andric       else
8306c3fb27SDimitry Andric         __val_.reset();
8406c3fb27SDimitry Andric     }
8506c3fb27SDimitry Andric     return *this;
8606c3fb27SDimitry Andric   }
8706c3fb27SDimitry Andric 
8806c3fb27SDimitry Andric   _LIBCPP_HIDE_FROM_ABI __movable_box& operator=(__movable_box&&)
8906c3fb27SDimitry Andric     requires movable<_Tp>
9006c3fb27SDimitry Andric   = default;
9106c3fb27SDimitry Andric 
9206c3fb27SDimitry Andric   _LIBCPP_HIDE_FROM_ABI constexpr __movable_box&
9306c3fb27SDimitry Andric   operator=(__movable_box&& __other) noexcept(is_nothrow_move_constructible_v<_Tp>) {
9406c3fb27SDimitry Andric     if (this != std::addressof(__other)) {
9506c3fb27SDimitry Andric       if (__other.__has_value())
9606c3fb27SDimitry Andric         __val_.emplace(std::move(*__other));
9706c3fb27SDimitry Andric       else
9806c3fb27SDimitry Andric         __val_.reset();
9906c3fb27SDimitry Andric     }
10006c3fb27SDimitry Andric     return *this;
10106c3fb27SDimitry Andric   }
10206c3fb27SDimitry Andric 
10306c3fb27SDimitry Andric   _LIBCPP_HIDE_FROM_ABI constexpr _Tp const& operator*() const noexcept { return *__val_; }
10406c3fb27SDimitry Andric   _LIBCPP_HIDE_FROM_ABI constexpr _Tp& operator*() noexcept { return *__val_; }
10506c3fb27SDimitry Andric 
10606c3fb27SDimitry Andric   _LIBCPP_HIDE_FROM_ABI constexpr const _Tp* operator->() const noexcept { return __val_.operator->(); }
10706c3fb27SDimitry Andric   _LIBCPP_HIDE_FROM_ABI constexpr _Tp* operator->() noexcept { return __val_.operator->(); }
10806c3fb27SDimitry Andric 
10906c3fb27SDimitry Andric   _LIBCPP_HIDE_FROM_ABI constexpr bool __has_value() const noexcept { return __val_.has_value(); }
11006c3fb27SDimitry Andric };
11106c3fb27SDimitry Andric 
11206c3fb27SDimitry Andric // This partial specialization implements an optimization for when we know we don't need to store
11306c3fb27SDimitry Andric // an empty state to represent failure to perform an assignment. For copy-assignment, this happens:
11406c3fb27SDimitry Andric //
11506c3fb27SDimitry Andric // 1. If the type is copyable (which includes copy-assignment), we can use the type's own assignment operator
11606c3fb27SDimitry Andric //    directly and avoid using std::optional.
11706c3fb27SDimitry Andric // 2. If the type is not copyable, but it is nothrow-copy-constructible, then we can implement assignment as
11806c3fb27SDimitry Andric //    destroy-and-then-construct and we know it will never fail, so we don't need an empty state.
11906c3fb27SDimitry Andric //
12006c3fb27SDimitry Andric // The exact same reasoning can be applied for move-assignment, with copyable replaced by movable and
12106c3fb27SDimitry Andric // nothrow-copy-constructible replaced by nothrow-move-constructible. This specialization is enabled
12206c3fb27SDimitry Andric // whenever we can apply any of these optimizations for both the copy assignment and the move assignment
12306c3fb27SDimitry Andric // operator.
12406c3fb27SDimitry Andric 
12506c3fb27SDimitry Andric #  if _LIBCPP_STD_VER >= 23
12606c3fb27SDimitry Andric template <class _Tp>
12706c3fb27SDimitry Andric concept __doesnt_need_empty_state =
12806c3fb27SDimitry Andric     (copy_constructible<_Tp>
12906c3fb27SDimitry Andric          // 1. If copy_constructible<T> is true, movable-box<T> should store only a T if either T models
13006c3fb27SDimitry Andric          //    copyable, or is_nothrow_move_constructible_v<T> && is_nothrow_copy_constructible_v<T> is true.
13106c3fb27SDimitry Andric          ? copyable<_Tp> || (is_nothrow_move_constructible_v<_Tp> && is_nothrow_copy_constructible_v<_Tp>)
13206c3fb27SDimitry Andric          // 2. Otherwise, movable-box<T> should store only a T if either T models movable or
13306c3fb27SDimitry Andric          //    is_nothrow_move_constructible_v<T> is true.
13406c3fb27SDimitry Andric          : movable<_Tp> || is_nothrow_move_constructible_v<_Tp>);
135*647cbc5dSDimitry Andric 
136*647cbc5dSDimitry Andric // When _Tp doesn't have an assignment operator, we must implement __movable_box's assignment operator
137*647cbc5dSDimitry Andric // by doing destroy_at followed by construct_at. However, that implementation strategy leads to UB if the nested
138*647cbc5dSDimitry Andric // _Tp is potentially overlapping, as it is doing a non-transparent replacement of the sub-object, which means that
139*647cbc5dSDimitry Andric // we're not considered "nested" inside the movable-box anymore, and since we're not nested within it, [basic.life]/1.5
140*647cbc5dSDimitry Andric // says that we essentially just reused the storage of the movable-box for a completely unrelated object and ended the
141*647cbc5dSDimitry Andric // movable-box's lifetime.
142*647cbc5dSDimitry Andric // https://github.com/llvm/llvm-project/issues/70494#issuecomment-1845646490
143*647cbc5dSDimitry Andric //
144*647cbc5dSDimitry Andric // Hence, when the _Tp doesn't have an assignment operator, we can't risk making it a potentially-overlapping
145*647cbc5dSDimitry Andric // subobject because of the above, and we don't use [[no_unique_address]] in that case.
146*647cbc5dSDimitry Andric template <class _Tp>
147*647cbc5dSDimitry Andric concept __can_use_no_unique_address = (copy_constructible<_Tp> ? copyable<_Tp> : movable<_Tp>);
148*647cbc5dSDimitry Andric 
14906c3fb27SDimitry Andric #  else
15006c3fb27SDimitry Andric 
15106c3fb27SDimitry Andric template <class _Tp>
15206c3fb27SDimitry Andric concept __doesnt_need_empty_state_for_copy = copyable<_Tp> || is_nothrow_copy_constructible_v<_Tp>;
15306c3fb27SDimitry Andric 
15406c3fb27SDimitry Andric template <class _Tp>
15506c3fb27SDimitry Andric concept __doesnt_need_empty_state_for_move = movable<_Tp> || is_nothrow_move_constructible_v<_Tp>;
15606c3fb27SDimitry Andric 
15706c3fb27SDimitry Andric template <class _Tp>
15806c3fb27SDimitry Andric concept __doesnt_need_empty_state = __doesnt_need_empty_state_for_copy<_Tp> && __doesnt_need_empty_state_for_move<_Tp>;
159*647cbc5dSDimitry Andric 
160*647cbc5dSDimitry Andric template <class _Tp>
161*647cbc5dSDimitry Andric concept __can_use_no_unique_address = copyable<_Tp>;
16206c3fb27SDimitry Andric #  endif
16306c3fb27SDimitry Andric 
164*647cbc5dSDimitry Andric template <class _Tp>
165*647cbc5dSDimitry Andric struct __movable_box_holder {
166*647cbc5dSDimitry Andric   _Tp __val_;
167*647cbc5dSDimitry Andric 
168*647cbc5dSDimitry Andric   template <class... _Args>
169*647cbc5dSDimitry Andric   _LIBCPP_HIDE_FROM_ABI constexpr explicit __movable_box_holder(in_place_t, _Args&&... __args)
170*647cbc5dSDimitry Andric       : __val_(std::forward<_Args>(__args)...) {}
171*647cbc5dSDimitry Andric };
172*647cbc5dSDimitry Andric 
173*647cbc5dSDimitry Andric template <class _Tp>
174*647cbc5dSDimitry Andric   requires __can_use_no_unique_address<_Tp>
175*647cbc5dSDimitry Andric struct __movable_box_holder<_Tp> {
176*647cbc5dSDimitry Andric   _LIBCPP_NO_UNIQUE_ADDRESS _Tp __val_;
177*647cbc5dSDimitry Andric 
178*647cbc5dSDimitry Andric   template <class... _Args>
179*647cbc5dSDimitry Andric   _LIBCPP_HIDE_FROM_ABI constexpr explicit __movable_box_holder(in_place_t, _Args&&... __args)
180*647cbc5dSDimitry Andric       : __val_(std::forward<_Args>(__args)...) {}
181*647cbc5dSDimitry Andric };
182*647cbc5dSDimitry Andric 
18306c3fb27SDimitry Andric template <__movable_box_object _Tp>
18406c3fb27SDimitry Andric   requires __doesnt_need_empty_state<_Tp>
18506c3fb27SDimitry Andric class __movable_box<_Tp> {
186*647cbc5dSDimitry Andric   _LIBCPP_NO_UNIQUE_ADDRESS __movable_box_holder<_Tp> __holder_;
18706c3fb27SDimitry Andric 
18806c3fb27SDimitry Andric public:
18906c3fb27SDimitry Andric   template <class... _Args>
19006c3fb27SDimitry Andric     requires is_constructible_v<_Tp, _Args...>
191*647cbc5dSDimitry Andric   _LIBCPP_HIDE_FROM_ABI constexpr explicit __movable_box(in_place_t __inplace, _Args&&... __args) noexcept(
19206c3fb27SDimitry Andric       is_nothrow_constructible_v<_Tp, _Args...>)
193*647cbc5dSDimitry Andric       : __holder_(__inplace, std::forward<_Args>(__args)...) {}
19406c3fb27SDimitry Andric 
19506c3fb27SDimitry Andric   _LIBCPP_HIDE_FROM_ABI constexpr __movable_box() noexcept(is_nothrow_default_constructible_v<_Tp>)
19606c3fb27SDimitry Andric     requires default_initializable<_Tp>
197*647cbc5dSDimitry Andric       : __holder_(in_place_t{}) {}
19806c3fb27SDimitry Andric 
19906c3fb27SDimitry Andric   _LIBCPP_HIDE_FROM_ABI __movable_box(__movable_box const&) = default;
20006c3fb27SDimitry Andric   _LIBCPP_HIDE_FROM_ABI __movable_box(__movable_box&&)      = default;
20106c3fb27SDimitry Andric 
20206c3fb27SDimitry Andric   // Implementation of assignment operators in case we perform optimization (1)
20306c3fb27SDimitry Andric   _LIBCPP_HIDE_FROM_ABI __movable_box& operator=(__movable_box const&)
20406c3fb27SDimitry Andric     requires copyable<_Tp>
20506c3fb27SDimitry Andric   = default;
20606c3fb27SDimitry Andric   _LIBCPP_HIDE_FROM_ABI __movable_box& operator=(__movable_box&&)
20706c3fb27SDimitry Andric     requires movable<_Tp>
20806c3fb27SDimitry Andric   = default;
20906c3fb27SDimitry Andric 
21006c3fb27SDimitry Andric   // Implementation of assignment operators in case we perform optimization (2)
21106c3fb27SDimitry Andric   _LIBCPP_HIDE_FROM_ABI constexpr __movable_box& operator=(__movable_box const& __other) noexcept {
21206c3fb27SDimitry Andric     static_assert(is_nothrow_copy_constructible_v<_Tp>);
213*647cbc5dSDimitry Andric     static_assert(!__can_use_no_unique_address<_Tp>);
21406c3fb27SDimitry Andric     if (this != std::addressof(__other)) {
215*647cbc5dSDimitry Andric       std::destroy_at(std::addressof(__holder_.__val_));
216*647cbc5dSDimitry Andric       std::construct_at(std::addressof(__holder_.__val_), __other.__holder_.__val_);
21706c3fb27SDimitry Andric     }
21806c3fb27SDimitry Andric     return *this;
21906c3fb27SDimitry Andric   }
22006c3fb27SDimitry Andric 
22106c3fb27SDimitry Andric   _LIBCPP_HIDE_FROM_ABI constexpr __movable_box& operator=(__movable_box&& __other) noexcept {
22206c3fb27SDimitry Andric     static_assert(is_nothrow_move_constructible_v<_Tp>);
223*647cbc5dSDimitry Andric     static_assert(!__can_use_no_unique_address<_Tp>);
22406c3fb27SDimitry Andric     if (this != std::addressof(__other)) {
225*647cbc5dSDimitry Andric       std::destroy_at(std::addressof(__holder_.__val_));
226*647cbc5dSDimitry Andric       std::construct_at(std::addressof(__holder_.__val_), std::move(__other.__holder_.__val_));
22706c3fb27SDimitry Andric     }
22806c3fb27SDimitry Andric     return *this;
22906c3fb27SDimitry Andric   }
23006c3fb27SDimitry Andric 
231*647cbc5dSDimitry Andric   _LIBCPP_HIDE_FROM_ABI constexpr _Tp const& operator*() const noexcept { return __holder_.__val_; }
232*647cbc5dSDimitry Andric   _LIBCPP_HIDE_FROM_ABI constexpr _Tp& operator*() noexcept { return __holder_.__val_; }
23306c3fb27SDimitry Andric 
234*647cbc5dSDimitry Andric   _LIBCPP_HIDE_FROM_ABI constexpr const _Tp* operator->() const noexcept { return std::addressof(__holder_.__val_); }
235*647cbc5dSDimitry Andric   _LIBCPP_HIDE_FROM_ABI constexpr _Tp* operator->() noexcept { return std::addressof(__holder_.__val_); }
23606c3fb27SDimitry Andric 
23706c3fb27SDimitry Andric   _LIBCPP_HIDE_FROM_ABI constexpr bool __has_value() const noexcept { return true; }
23806c3fb27SDimitry Andric };
23906c3fb27SDimitry Andric } // namespace ranges
24006c3fb27SDimitry Andric 
24106c3fb27SDimitry Andric #endif // _LIBCPP_STD_VER >= 20
24206c3fb27SDimitry Andric 
24306c3fb27SDimitry Andric _LIBCPP_END_NAMESPACE_STD
24406c3fb27SDimitry Andric 
2455f757f3fSDimitry Andric _LIBCPP_POP_MACROS
2465f757f3fSDimitry Andric 
24706c3fb27SDimitry Andric #endif // _LIBCPP___RANGES_MOVABLE_BOX_H
248