copyable_box.h 7.2 KB

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