movable_box.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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. #ifndef _LIBCPP___RANGES_MOVABLE_BOX_H
  10. #define _LIBCPP___RANGES_MOVABLE_BOX_H
  11. #include <__concepts/constructible.h>
  12. #include <__concepts/copyable.h>
  13. #include <__concepts/movable.h>
  14. #include <__config>
  15. #include <__memory/addressof.h>
  16. #include <__memory/construct_at.h>
  17. #include <__type_traits/is_nothrow_constructible.h>
  18. #include <__type_traits/is_nothrow_copy_constructible.h>
  19. #include <__type_traits/is_nothrow_default_constructible.h>
  20. #include <__utility/move.h>
  21. #include <optional>
  22. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  23. # pragma GCC system_header
  24. #endif
  25. _LIBCPP_PUSH_MACROS
  26. #include <__undef_macros>
  27. _LIBCPP_BEGIN_NAMESPACE_STD
  28. #if _LIBCPP_STD_VER >= 20
  29. // __movable_box allows turning a type that is move-constructible (but maybe not move-assignable) into
  30. // a type that is both move-constructible and move-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. // __movable_box that does this, see below for the details.
  36. // until C++23, `__movable_box` was named `__copyable_box` and required the stored type to be copy-constructible, not
  37. // just move-constructible; we preserve the old behavior in pre-C++23 modes.
  38. template <class _Tp>
  39. concept __movable_box_object =
  40. # if _LIBCPP_STD_VER >= 23
  41. move_constructible<_Tp>
  42. # else
  43. copy_constructible<_Tp>
  44. # endif
  45. && is_object_v<_Tp>;
  46. namespace ranges {
  47. // Primary template - uses std::optional and introduces an empty state in case assignment fails.
  48. template <__movable_box_object _Tp>
  49. class __movable_box {
  50. _LIBCPP_NO_UNIQUE_ADDRESS optional<_Tp> __val_;
  51. public:
  52. template <class... _Args>
  53. requires is_constructible_v<_Tp, _Args...>
  54. _LIBCPP_HIDE_FROM_ABI constexpr explicit __movable_box(in_place_t, _Args&&... __args) noexcept(
  55. is_nothrow_constructible_v<_Tp, _Args...>)
  56. : __val_(in_place, std::forward<_Args>(__args)...) {}
  57. _LIBCPP_HIDE_FROM_ABI constexpr __movable_box() noexcept(is_nothrow_default_constructible_v<_Tp>)
  58. requires default_initializable<_Tp>
  59. : __val_(in_place) {}
  60. _LIBCPP_HIDE_FROM_ABI __movable_box(__movable_box const&) = default;
  61. _LIBCPP_HIDE_FROM_ABI __movable_box(__movable_box&&) = default;
  62. _LIBCPP_HIDE_FROM_ABI constexpr __movable_box&
  63. operator=(__movable_box const& __other) noexcept(is_nothrow_copy_constructible_v<_Tp>)
  64. # if _LIBCPP_STD_VER >= 23
  65. requires copy_constructible<_Tp>
  66. # endif
  67. {
  68. if (this != std::addressof(__other)) {
  69. if (__other.__has_value())
  70. __val_.emplace(*__other);
  71. else
  72. __val_.reset();
  73. }
  74. return *this;
  75. }
  76. _LIBCPP_HIDE_FROM_ABI __movable_box& operator=(__movable_box&&)
  77. requires movable<_Tp>
  78. = default;
  79. _LIBCPP_HIDE_FROM_ABI constexpr __movable_box&
  80. operator=(__movable_box&& __other) noexcept(is_nothrow_move_constructible_v<_Tp>) {
  81. if (this != std::addressof(__other)) {
  82. if (__other.__has_value())
  83. __val_.emplace(std::move(*__other));
  84. else
  85. __val_.reset();
  86. }
  87. return *this;
  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. _LIBCPP_HIDE_FROM_ABI constexpr const _Tp* operator->() const noexcept { return __val_.operator->(); }
  92. _LIBCPP_HIDE_FROM_ABI constexpr _Tp* operator->() noexcept { return __val_.operator->(); }
  93. _LIBCPP_HIDE_FROM_ABI constexpr bool __has_value() const noexcept { return __val_.has_value(); }
  94. };
  95. // This partial specialization implements an optimization for when we know we don't need to store
  96. // an empty state to represent failure to perform an assignment. For copy-assignment, this happens:
  97. //
  98. // 1. If the type is copyable (which includes copy-assignment), we can use the type's own assignment operator
  99. // directly and avoid using std::optional.
  100. // 2. If the type is not copyable, but it is nothrow-copy-constructible, then we can implement assignment as
  101. // destroy-and-then-construct and we know it will never fail, so we don't need an empty state.
  102. //
  103. // The exact same reasoning can be applied for move-assignment, with copyable replaced by movable and
  104. // nothrow-copy-constructible replaced by nothrow-move-constructible. This specialization is enabled
  105. // whenever we can apply any of these optimizations for both the copy assignment and the move assignment
  106. // operator.
  107. # if _LIBCPP_STD_VER >= 23
  108. template <class _Tp>
  109. concept __doesnt_need_empty_state =
  110. (copy_constructible<_Tp>
  111. // 1. If copy_constructible<T> is true, movable-box<T> should store only a T if either T models
  112. // copyable, or is_nothrow_move_constructible_v<T> && is_nothrow_copy_constructible_v<T> is true.
  113. ? copyable<_Tp> || (is_nothrow_move_constructible_v<_Tp> && is_nothrow_copy_constructible_v<_Tp>)
  114. // 2. Otherwise, movable-box<T> should store only a T if either T models movable or
  115. // is_nothrow_move_constructible_v<T> is true.
  116. : movable<_Tp> || is_nothrow_move_constructible_v<_Tp>);
  117. // When _Tp doesn't have an assignment operator, we must implement __movable_box's assignment operator
  118. // by doing destroy_at followed by construct_at. However, that implementation strategy leads to UB if the nested
  119. // _Tp is potentially overlapping, as it is doing a non-transparent replacement of the sub-object, which means that
  120. // we're not considered "nested" inside the movable-box anymore, and since we're not nested within it, [basic.life]/1.5
  121. // says that we essentially just reused the storage of the movable-box for a completely unrelated object and ended the
  122. // movable-box's lifetime.
  123. // https://github.com/llvm/llvm-project/issues/70494#issuecomment-1845646490
  124. //
  125. // Hence, when the _Tp doesn't have an assignment operator, we can't risk making it a potentially-overlapping
  126. // subobject because of the above, and we don't use [[no_unique_address]] in that case.
  127. template <class _Tp>
  128. concept __can_use_no_unique_address = (copy_constructible<_Tp> ? copyable<_Tp> : movable<_Tp>);
  129. # else
  130. template <class _Tp>
  131. concept __doesnt_need_empty_state_for_copy = copyable<_Tp> || is_nothrow_copy_constructible_v<_Tp>;
  132. template <class _Tp>
  133. concept __doesnt_need_empty_state_for_move = movable<_Tp> || is_nothrow_move_constructible_v<_Tp>;
  134. template <class _Tp>
  135. concept __doesnt_need_empty_state = __doesnt_need_empty_state_for_copy<_Tp> && __doesnt_need_empty_state_for_move<_Tp>;
  136. template <class _Tp>
  137. concept __can_use_no_unique_address = copyable<_Tp>;
  138. # endif
  139. template <class _Tp>
  140. struct __movable_box_holder {
  141. _Tp __val_;
  142. template <class... _Args>
  143. _LIBCPP_HIDE_FROM_ABI constexpr explicit __movable_box_holder(in_place_t, _Args&&... __args)
  144. : __val_(std::forward<_Args>(__args)...) {}
  145. };
  146. template <class _Tp>
  147. requires __can_use_no_unique_address<_Tp>
  148. struct __movable_box_holder<_Tp> {
  149. _LIBCPP_NO_UNIQUE_ADDRESS _Tp __val_;
  150. template <class... _Args>
  151. _LIBCPP_HIDE_FROM_ABI constexpr explicit __movable_box_holder(in_place_t, _Args&&... __args)
  152. : __val_(std::forward<_Args>(__args)...) {}
  153. };
  154. template <__movable_box_object _Tp>
  155. requires __doesnt_need_empty_state<_Tp>
  156. class __movable_box<_Tp> {
  157. _LIBCPP_NO_UNIQUE_ADDRESS __movable_box_holder<_Tp> __holder_;
  158. public:
  159. template <class... _Args>
  160. requires is_constructible_v<_Tp, _Args...>
  161. _LIBCPP_HIDE_FROM_ABI constexpr explicit __movable_box(in_place_t __inplace, _Args&&... __args) noexcept(
  162. is_nothrow_constructible_v<_Tp, _Args...>)
  163. : __holder_(__inplace, std::forward<_Args>(__args)...) {}
  164. _LIBCPP_HIDE_FROM_ABI constexpr __movable_box() noexcept(is_nothrow_default_constructible_v<_Tp>)
  165. requires default_initializable<_Tp>
  166. : __holder_(in_place_t{}) {}
  167. _LIBCPP_HIDE_FROM_ABI __movable_box(__movable_box const&) = default;
  168. _LIBCPP_HIDE_FROM_ABI __movable_box(__movable_box&&) = default;
  169. // Implementation of assignment operators in case we perform optimization (1)
  170. _LIBCPP_HIDE_FROM_ABI __movable_box& operator=(__movable_box const&)
  171. requires copyable<_Tp>
  172. = default;
  173. _LIBCPP_HIDE_FROM_ABI __movable_box& operator=(__movable_box&&)
  174. requires movable<_Tp>
  175. = default;
  176. // Implementation of assignment operators in case we perform optimization (2)
  177. _LIBCPP_HIDE_FROM_ABI constexpr __movable_box& operator=(__movable_box const& __other) noexcept {
  178. static_assert(is_nothrow_copy_constructible_v<_Tp>);
  179. static_assert(!__can_use_no_unique_address<_Tp>);
  180. if (this != std::addressof(__other)) {
  181. std::destroy_at(std::addressof(__holder_.__val_));
  182. std::construct_at(std::addressof(__holder_.__val_), __other.__holder_.__val_);
  183. }
  184. return *this;
  185. }
  186. _LIBCPP_HIDE_FROM_ABI constexpr __movable_box& operator=(__movable_box&& __other) noexcept {
  187. static_assert(is_nothrow_move_constructible_v<_Tp>);
  188. static_assert(!__can_use_no_unique_address<_Tp>);
  189. if (this != std::addressof(__other)) {
  190. std::destroy_at(std::addressof(__holder_.__val_));
  191. std::construct_at(std::addressof(__holder_.__val_), std::move(__other.__holder_.__val_));
  192. }
  193. return *this;
  194. }
  195. _LIBCPP_HIDE_FROM_ABI constexpr _Tp const& operator*() const noexcept { return __holder_.__val_; }
  196. _LIBCPP_HIDE_FROM_ABI constexpr _Tp& operator*() noexcept { return __holder_.__val_; }
  197. _LIBCPP_HIDE_FROM_ABI constexpr const _Tp* operator->() const noexcept { return std::addressof(__holder_.__val_); }
  198. _LIBCPP_HIDE_FROM_ABI constexpr _Tp* operator->() noexcept { return std::addressof(__holder_.__val_); }
  199. _LIBCPP_HIDE_FROM_ABI constexpr bool __has_value() const noexcept { return true; }
  200. };
  201. } // namespace ranges
  202. #endif // _LIBCPP_STD_VER >= 20
  203. _LIBCPP_END_NAMESPACE_STD
  204. _LIBCPP_POP_MACROS
  205. #endif // _LIBCPP___RANGES_MOVABLE_BOX_H