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