copyable_box.h 7.3 KB

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