xref: /freebsd-src/contrib/llvm-project/libcxx/include/__ranges/movable_box.h (revision 647cbc5de815c5651677bf8582797f716ec7b48d)
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 <__type_traits/is_nothrow_copy_constructible.h>
2106c3fb27SDimitry Andric #include <__type_traits/is_nothrow_default_constructible.h>
2206c3fb27SDimitry Andric #include <__utility/move.h>
2306c3fb27SDimitry Andric #include <optional>
2406c3fb27SDimitry Andric 
2506c3fb27SDimitry Andric #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
2606c3fb27SDimitry Andric #  pragma GCC system_header
2706c3fb27SDimitry Andric #endif
2806c3fb27SDimitry Andric 
295f757f3fSDimitry Andric _LIBCPP_PUSH_MACROS
305f757f3fSDimitry Andric #include <__undef_macros>
315f757f3fSDimitry Andric 
3206c3fb27SDimitry Andric _LIBCPP_BEGIN_NAMESPACE_STD
3306c3fb27SDimitry Andric 
3406c3fb27SDimitry Andric #if _LIBCPP_STD_VER >= 20
3506c3fb27SDimitry Andric 
3606c3fb27SDimitry Andric // __movable_box allows turning a type that is move-constructible (but maybe not move-assignable) into
3706c3fb27SDimitry Andric // a type that is both move-constructible and move-assignable. It does that by introducing an empty state
3806c3fb27SDimitry Andric // and basically doing destroy-then-copy-construct in the assignment operator. The empty state is necessary
3906c3fb27SDimitry Andric // to handle the case where the copy construction fails after destroying the object.
4006c3fb27SDimitry Andric //
4106c3fb27SDimitry Andric // In some cases, we can completely avoid the use of an empty state; we provide a specialization of
4206c3fb27SDimitry Andric // __movable_box that does this, see below for the details.
4306c3fb27SDimitry Andric 
4406c3fb27SDimitry Andric // until C++23, `__movable_box` was named `__copyable_box` and required the stored type to be copy-constructible, not
4506c3fb27SDimitry Andric // just move-constructible; we preserve the old behavior in pre-C++23 modes.
4606c3fb27SDimitry Andric template <class _Tp>
4706c3fb27SDimitry Andric concept __movable_box_object =
4806c3fb27SDimitry Andric #  if _LIBCPP_STD_VER >= 23
4906c3fb27SDimitry Andric     move_constructible<_Tp>
5006c3fb27SDimitry Andric #  else
5106c3fb27SDimitry Andric     copy_constructible<_Tp>
5206c3fb27SDimitry Andric #  endif
5306c3fb27SDimitry Andric     && is_object_v<_Tp>;
5406c3fb27SDimitry Andric 
5506c3fb27SDimitry Andric namespace ranges {
5606c3fb27SDimitry Andric // Primary template - uses std::optional and introduces an empty state in case assignment fails.
5706c3fb27SDimitry Andric template <__movable_box_object _Tp>
5806c3fb27SDimitry Andric class __movable_box {
5906c3fb27SDimitry Andric   _LIBCPP_NO_UNIQUE_ADDRESS optional<_Tp> __val_;
6006c3fb27SDimitry Andric 
6106c3fb27SDimitry Andric public:
6206c3fb27SDimitry Andric   template <class... _Args>
6306c3fb27SDimitry Andric     requires is_constructible_v<_Tp, _Args...>
6406c3fb27SDimitry Andric   _LIBCPP_HIDE_FROM_ABI constexpr explicit __movable_box(in_place_t, _Args&&... __args) noexcept(
6506c3fb27SDimitry Andric       is_nothrow_constructible_v<_Tp, _Args...>)
6606c3fb27SDimitry Andric       : __val_(in_place, std::forward<_Args>(__args)...) {}
6706c3fb27SDimitry Andric 
6806c3fb27SDimitry Andric   _LIBCPP_HIDE_FROM_ABI constexpr __movable_box() noexcept(is_nothrow_default_constructible_v<_Tp>)
6906c3fb27SDimitry Andric     requires default_initializable<_Tp>
7006c3fb27SDimitry Andric       : __val_(in_place) {}
7106c3fb27SDimitry Andric 
7206c3fb27SDimitry Andric   _LIBCPP_HIDE_FROM_ABI __movable_box(__movable_box const&) = default;
7306c3fb27SDimitry Andric   _LIBCPP_HIDE_FROM_ABI __movable_box(__movable_box&&)      = default;
7406c3fb27SDimitry Andric 
7506c3fb27SDimitry Andric   _LIBCPP_HIDE_FROM_ABI constexpr __movable_box&
7606c3fb27SDimitry Andric   operator=(__movable_box const& __other) noexcept(is_nothrow_copy_constructible_v<_Tp>)
7706c3fb27SDimitry Andric #  if _LIBCPP_STD_VER >= 23
7806c3fb27SDimitry Andric     requires copy_constructible<_Tp>
7906c3fb27SDimitry Andric #  endif
8006c3fb27SDimitry Andric   {
8106c3fb27SDimitry Andric     if (this != std::addressof(__other)) {
8206c3fb27SDimitry Andric       if (__other.__has_value())
8306c3fb27SDimitry Andric         __val_.emplace(*__other);
8406c3fb27SDimitry Andric       else
8506c3fb27SDimitry Andric         __val_.reset();
8606c3fb27SDimitry Andric     }
8706c3fb27SDimitry Andric     return *this;
8806c3fb27SDimitry Andric   }
8906c3fb27SDimitry Andric 
9006c3fb27SDimitry Andric   _LIBCPP_HIDE_FROM_ABI __movable_box& operator=(__movable_box&&)
9106c3fb27SDimitry Andric     requires movable<_Tp>
9206c3fb27SDimitry Andric   = default;
9306c3fb27SDimitry Andric 
9406c3fb27SDimitry Andric   _LIBCPP_HIDE_FROM_ABI constexpr __movable_box&
9506c3fb27SDimitry Andric   operator=(__movable_box&& __other) noexcept(is_nothrow_move_constructible_v<_Tp>) {
9606c3fb27SDimitry Andric     if (this != std::addressof(__other)) {
9706c3fb27SDimitry Andric       if (__other.__has_value())
9806c3fb27SDimitry Andric         __val_.emplace(std::move(*__other));
9906c3fb27SDimitry Andric       else
10006c3fb27SDimitry Andric         __val_.reset();
10106c3fb27SDimitry Andric     }
10206c3fb27SDimitry Andric     return *this;
10306c3fb27SDimitry Andric   }
10406c3fb27SDimitry Andric 
10506c3fb27SDimitry Andric   _LIBCPP_HIDE_FROM_ABI constexpr _Tp const& operator*() const noexcept { return *__val_; }
10606c3fb27SDimitry Andric   _LIBCPP_HIDE_FROM_ABI constexpr _Tp& operator*() noexcept { return *__val_; }
10706c3fb27SDimitry Andric 
10806c3fb27SDimitry Andric   _LIBCPP_HIDE_FROM_ABI constexpr const _Tp* operator->() const noexcept { return __val_.operator->(); }
10906c3fb27SDimitry Andric   _LIBCPP_HIDE_FROM_ABI constexpr _Tp* operator->() noexcept { return __val_.operator->(); }
11006c3fb27SDimitry Andric 
11106c3fb27SDimitry Andric   _LIBCPP_HIDE_FROM_ABI constexpr bool __has_value() const noexcept { return __val_.has_value(); }
11206c3fb27SDimitry Andric };
11306c3fb27SDimitry Andric 
11406c3fb27SDimitry Andric // This partial specialization implements an optimization for when we know we don't need to store
11506c3fb27SDimitry Andric // an empty state to represent failure to perform an assignment. For copy-assignment, this happens:
11606c3fb27SDimitry Andric //
11706c3fb27SDimitry Andric // 1. If the type is copyable (which includes copy-assignment), we can use the type's own assignment operator
11806c3fb27SDimitry Andric //    directly and avoid using std::optional.
11906c3fb27SDimitry Andric // 2. If the type is not copyable, but it is nothrow-copy-constructible, then we can implement assignment as
12006c3fb27SDimitry Andric //    destroy-and-then-construct and we know it will never fail, so we don't need an empty state.
12106c3fb27SDimitry Andric //
12206c3fb27SDimitry Andric // The exact same reasoning can be applied for move-assignment, with copyable replaced by movable and
12306c3fb27SDimitry Andric // nothrow-copy-constructible replaced by nothrow-move-constructible. This specialization is enabled
12406c3fb27SDimitry Andric // whenever we can apply any of these optimizations for both the copy assignment and the move assignment
12506c3fb27SDimitry Andric // operator.
12606c3fb27SDimitry Andric 
12706c3fb27SDimitry Andric #  if _LIBCPP_STD_VER >= 23
12806c3fb27SDimitry Andric template <class _Tp>
12906c3fb27SDimitry Andric concept __doesnt_need_empty_state =
13006c3fb27SDimitry Andric     (copy_constructible<_Tp>
13106c3fb27SDimitry Andric          // 1. If copy_constructible<T> is true, movable-box<T> should store only a T if either T models
13206c3fb27SDimitry Andric          //    copyable, or is_nothrow_move_constructible_v<T> && is_nothrow_copy_constructible_v<T> is true.
13306c3fb27SDimitry Andric          ? copyable<_Tp> || (is_nothrow_move_constructible_v<_Tp> && is_nothrow_copy_constructible_v<_Tp>)
13406c3fb27SDimitry Andric          // 2. Otherwise, movable-box<T> should store only a T if either T models movable or
13506c3fb27SDimitry Andric          //    is_nothrow_move_constructible_v<T> is true.
13606c3fb27SDimitry Andric          : movable<_Tp> || is_nothrow_move_constructible_v<_Tp>);
137*647cbc5dSDimitry Andric 
138*647cbc5dSDimitry Andric // When _Tp doesn't have an assignment operator, we must implement __movable_box's assignment operator
139*647cbc5dSDimitry Andric // by doing destroy_at followed by construct_at. However, that implementation strategy leads to UB if the nested
140*647cbc5dSDimitry Andric // _Tp is potentially overlapping, as it is doing a non-transparent replacement of the sub-object, which means that
141*647cbc5dSDimitry Andric // we're not considered "nested" inside the movable-box anymore, and since we're not nested within it, [basic.life]/1.5
142*647cbc5dSDimitry Andric // says that we essentially just reused the storage of the movable-box for a completely unrelated object and ended the
143*647cbc5dSDimitry Andric // movable-box's lifetime.
144*647cbc5dSDimitry Andric // https://github.com/llvm/llvm-project/issues/70494#issuecomment-1845646490
145*647cbc5dSDimitry Andric //
146*647cbc5dSDimitry Andric // Hence, when the _Tp doesn't have an assignment operator, we can't risk making it a potentially-overlapping
147*647cbc5dSDimitry Andric // subobject because of the above, and we don't use [[no_unique_address]] in that case.
148*647cbc5dSDimitry Andric template <class _Tp>
149*647cbc5dSDimitry Andric concept __can_use_no_unique_address = (copy_constructible<_Tp> ? copyable<_Tp> : movable<_Tp>);
150*647cbc5dSDimitry Andric 
15106c3fb27SDimitry Andric #  else
15206c3fb27SDimitry Andric 
15306c3fb27SDimitry Andric template <class _Tp>
15406c3fb27SDimitry Andric concept __doesnt_need_empty_state_for_copy = copyable<_Tp> || is_nothrow_copy_constructible_v<_Tp>;
15506c3fb27SDimitry Andric 
15606c3fb27SDimitry Andric template <class _Tp>
15706c3fb27SDimitry Andric concept __doesnt_need_empty_state_for_move = movable<_Tp> || is_nothrow_move_constructible_v<_Tp>;
15806c3fb27SDimitry Andric 
15906c3fb27SDimitry Andric template <class _Tp>
16006c3fb27SDimitry Andric concept __doesnt_need_empty_state = __doesnt_need_empty_state_for_copy<_Tp> && __doesnt_need_empty_state_for_move<_Tp>;
161*647cbc5dSDimitry Andric 
162*647cbc5dSDimitry Andric template <class _Tp>
163*647cbc5dSDimitry Andric concept __can_use_no_unique_address = copyable<_Tp>;
16406c3fb27SDimitry Andric #  endif
16506c3fb27SDimitry Andric 
166*647cbc5dSDimitry Andric template <class _Tp>
167*647cbc5dSDimitry Andric struct __movable_box_holder {
168*647cbc5dSDimitry Andric   _Tp __val_;
169*647cbc5dSDimitry Andric 
170*647cbc5dSDimitry Andric   template <class... _Args>
171*647cbc5dSDimitry Andric   _LIBCPP_HIDE_FROM_ABI constexpr explicit __movable_box_holder(in_place_t, _Args&&... __args)
172*647cbc5dSDimitry Andric       : __val_(std::forward<_Args>(__args)...) {}
173*647cbc5dSDimitry Andric };
174*647cbc5dSDimitry Andric 
175*647cbc5dSDimitry Andric template <class _Tp>
176*647cbc5dSDimitry Andric   requires __can_use_no_unique_address<_Tp>
177*647cbc5dSDimitry Andric struct __movable_box_holder<_Tp> {
178*647cbc5dSDimitry Andric   _LIBCPP_NO_UNIQUE_ADDRESS _Tp __val_;
179*647cbc5dSDimitry Andric 
180*647cbc5dSDimitry Andric   template <class... _Args>
181*647cbc5dSDimitry Andric   _LIBCPP_HIDE_FROM_ABI constexpr explicit __movable_box_holder(in_place_t, _Args&&... __args)
182*647cbc5dSDimitry Andric       : __val_(std::forward<_Args>(__args)...) {}
183*647cbc5dSDimitry Andric };
184*647cbc5dSDimitry Andric 
18506c3fb27SDimitry Andric template <__movable_box_object _Tp>
18606c3fb27SDimitry Andric   requires __doesnt_need_empty_state<_Tp>
18706c3fb27SDimitry Andric class __movable_box<_Tp> {
188*647cbc5dSDimitry Andric   _LIBCPP_NO_UNIQUE_ADDRESS __movable_box_holder<_Tp> __holder_;
18906c3fb27SDimitry Andric 
19006c3fb27SDimitry Andric public:
19106c3fb27SDimitry Andric   template <class... _Args>
19206c3fb27SDimitry Andric     requires is_constructible_v<_Tp, _Args...>
193*647cbc5dSDimitry Andric   _LIBCPP_HIDE_FROM_ABI constexpr explicit __movable_box(in_place_t __inplace, _Args&&... __args) noexcept(
19406c3fb27SDimitry Andric       is_nothrow_constructible_v<_Tp, _Args...>)
195*647cbc5dSDimitry Andric       : __holder_(__inplace, std::forward<_Args>(__args)...) {}
19606c3fb27SDimitry Andric 
19706c3fb27SDimitry Andric   _LIBCPP_HIDE_FROM_ABI constexpr __movable_box() noexcept(is_nothrow_default_constructible_v<_Tp>)
19806c3fb27SDimitry Andric     requires default_initializable<_Tp>
199*647cbc5dSDimitry Andric       : __holder_(in_place_t{}) {}
20006c3fb27SDimitry Andric 
20106c3fb27SDimitry Andric   _LIBCPP_HIDE_FROM_ABI __movable_box(__movable_box const&) = default;
20206c3fb27SDimitry Andric   _LIBCPP_HIDE_FROM_ABI __movable_box(__movable_box&&)      = default;
20306c3fb27SDimitry Andric 
20406c3fb27SDimitry Andric   // Implementation of assignment operators in case we perform optimization (1)
20506c3fb27SDimitry Andric   _LIBCPP_HIDE_FROM_ABI __movable_box& operator=(__movable_box const&)
20606c3fb27SDimitry Andric     requires copyable<_Tp>
20706c3fb27SDimitry Andric   = default;
20806c3fb27SDimitry Andric   _LIBCPP_HIDE_FROM_ABI __movable_box& operator=(__movable_box&&)
20906c3fb27SDimitry Andric     requires movable<_Tp>
21006c3fb27SDimitry Andric   = default;
21106c3fb27SDimitry Andric 
21206c3fb27SDimitry Andric   // Implementation of assignment operators in case we perform optimization (2)
21306c3fb27SDimitry Andric   _LIBCPP_HIDE_FROM_ABI constexpr __movable_box& operator=(__movable_box const& __other) noexcept {
21406c3fb27SDimitry Andric     static_assert(is_nothrow_copy_constructible_v<_Tp>);
215*647cbc5dSDimitry Andric     static_assert(!__can_use_no_unique_address<_Tp>);
21606c3fb27SDimitry Andric     if (this != std::addressof(__other)) {
217*647cbc5dSDimitry Andric       std::destroy_at(std::addressof(__holder_.__val_));
218*647cbc5dSDimitry Andric       std::construct_at(std::addressof(__holder_.__val_), __other.__holder_.__val_);
21906c3fb27SDimitry Andric     }
22006c3fb27SDimitry Andric     return *this;
22106c3fb27SDimitry Andric   }
22206c3fb27SDimitry Andric 
22306c3fb27SDimitry Andric   _LIBCPP_HIDE_FROM_ABI constexpr __movable_box& operator=(__movable_box&& __other) noexcept {
22406c3fb27SDimitry Andric     static_assert(is_nothrow_move_constructible_v<_Tp>);
225*647cbc5dSDimitry Andric     static_assert(!__can_use_no_unique_address<_Tp>);
22606c3fb27SDimitry Andric     if (this != std::addressof(__other)) {
227*647cbc5dSDimitry Andric       std::destroy_at(std::addressof(__holder_.__val_));
228*647cbc5dSDimitry Andric       std::construct_at(std::addressof(__holder_.__val_), std::move(__other.__holder_.__val_));
22906c3fb27SDimitry Andric     }
23006c3fb27SDimitry Andric     return *this;
23106c3fb27SDimitry Andric   }
23206c3fb27SDimitry Andric 
233*647cbc5dSDimitry Andric   _LIBCPP_HIDE_FROM_ABI constexpr _Tp const& operator*() const noexcept { return __holder_.__val_; }
234*647cbc5dSDimitry Andric   _LIBCPP_HIDE_FROM_ABI constexpr _Tp& operator*() noexcept { return __holder_.__val_; }
23506c3fb27SDimitry Andric 
236*647cbc5dSDimitry Andric   _LIBCPP_HIDE_FROM_ABI constexpr const _Tp* operator->() const noexcept { return std::addressof(__holder_.__val_); }
237*647cbc5dSDimitry Andric   _LIBCPP_HIDE_FROM_ABI constexpr _Tp* operator->() noexcept { return std::addressof(__holder_.__val_); }
23806c3fb27SDimitry Andric 
23906c3fb27SDimitry Andric   _LIBCPP_HIDE_FROM_ABI constexpr bool __has_value() const noexcept { return true; }
24006c3fb27SDimitry Andric };
24106c3fb27SDimitry Andric } // namespace ranges
24206c3fb27SDimitry Andric 
24306c3fb27SDimitry Andric #endif // _LIBCPP_STD_VER >= 20
24406c3fb27SDimitry Andric 
24506c3fb27SDimitry Andric _LIBCPP_END_NAMESPACE_STD
24606c3fb27SDimitry Andric 
2475f757f3fSDimitry Andric _LIBCPP_POP_MACROS
2485f757f3fSDimitry Andric 
24906c3fb27SDimitry Andric #endif // _LIBCPP___RANGES_MOVABLE_BOX_H
250