Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-03 08:14:02

0001 // -*- C++ -*-
0002 //===----------------------------------------------------------------------===//
0003 //
0004 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0005 // See https://llvm.org/LICENSE.txt for license information.
0006 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0007 //
0008 //===----------------------------------------------------------------------===//
0009 
0010 #ifndef _LIBCPP___RANGES_MOVABLE_BOX_H
0011 #define _LIBCPP___RANGES_MOVABLE_BOX_H
0012 
0013 #include <__concepts/constructible.h>
0014 #include <__concepts/copyable.h>
0015 #include <__concepts/movable.h>
0016 #include <__config>
0017 #include <__memory/addressof.h>
0018 #include <__memory/construct_at.h>
0019 #include <__type_traits/is_nothrow_constructible.h>
0020 #include <__utility/move.h>
0021 #include <optional>
0022 
0023 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
0024 #  pragma GCC system_header
0025 #endif
0026 
0027 _LIBCPP_PUSH_MACROS
0028 #include <__undef_macros>
0029 
0030 _LIBCPP_BEGIN_NAMESPACE_STD
0031 
0032 #if _LIBCPP_STD_VER >= 20
0033 
0034 // __movable_box allows turning a type that is move-constructible (but maybe not move-assignable) into
0035 // a type that is both move-constructible and move-assignable. It does that by introducing an empty state
0036 // and basically doing destroy-then-copy-construct in the assignment operator. The empty state is necessary
0037 // to handle the case where the copy construction fails after destroying the object.
0038 //
0039 // In some cases, we can completely avoid the use of an empty state; we provide a specialization of
0040 // __movable_box that does this, see below for the details.
0041 
0042 // until C++23, `__movable_box` was named `__copyable_box` and required the stored type to be copy-constructible, not
0043 // just move-constructible; we preserve the old behavior in pre-C++23 modes.
0044 template <class _Tp>
0045 concept __movable_box_object =
0046 #  if _LIBCPP_STD_VER >= 23
0047     move_constructible<_Tp>
0048 #  else
0049     copy_constructible<_Tp>
0050 #  endif
0051     && is_object_v<_Tp>;
0052 
0053 namespace ranges {
0054 // Primary template - uses std::optional and introduces an empty state in case assignment fails.
0055 template <__movable_box_object _Tp>
0056 class __movable_box {
0057   _LIBCPP_NO_UNIQUE_ADDRESS optional<_Tp> __val_;
0058 
0059 public:
0060   template <class... _Args>
0061     requires is_constructible_v<_Tp, _Args...>
0062   _LIBCPP_HIDE_FROM_ABI constexpr explicit __movable_box(in_place_t, _Args&&... __args) noexcept(
0063       is_nothrow_constructible_v<_Tp, _Args...>)
0064       : __val_(in_place, std::forward<_Args>(__args)...) {}
0065 
0066   _LIBCPP_HIDE_FROM_ABI constexpr __movable_box() noexcept(is_nothrow_default_constructible_v<_Tp>)
0067     requires default_initializable<_Tp>
0068       : __val_(in_place) {}
0069 
0070   _LIBCPP_HIDE_FROM_ABI __movable_box(__movable_box const&) = default;
0071   _LIBCPP_HIDE_FROM_ABI __movable_box(__movable_box&&)      = default;
0072 
0073   _LIBCPP_HIDE_FROM_ABI constexpr __movable_box&
0074   operator=(__movable_box const& __other) noexcept(is_nothrow_copy_constructible_v<_Tp>)
0075 #  if _LIBCPP_STD_VER >= 23
0076     requires copy_constructible<_Tp>
0077 #  endif
0078   {
0079     if (this != std::addressof(__other)) {
0080       if (__other.__has_value())
0081         __val_.emplace(*__other);
0082       else
0083         __val_.reset();
0084     }
0085     return *this;
0086   }
0087 
0088   _LIBCPP_HIDE_FROM_ABI __movable_box& operator=(__movable_box&&)
0089     requires movable<_Tp>
0090   = default;
0091 
0092   _LIBCPP_HIDE_FROM_ABI constexpr __movable_box&
0093   operator=(__movable_box&& __other) noexcept(is_nothrow_move_constructible_v<_Tp>) {
0094     if (this != std::addressof(__other)) {
0095       if (__other.__has_value())
0096         __val_.emplace(std::move(*__other));
0097       else
0098         __val_.reset();
0099     }
0100     return *this;
0101   }
0102 
0103   _LIBCPP_HIDE_FROM_ABI constexpr _Tp const& operator*() const noexcept { return *__val_; }
0104   _LIBCPP_HIDE_FROM_ABI constexpr _Tp& operator*() noexcept { return *__val_; }
0105 
0106   _LIBCPP_HIDE_FROM_ABI constexpr const _Tp* operator->() const noexcept { return __val_.operator->(); }
0107   _LIBCPP_HIDE_FROM_ABI constexpr _Tp* operator->() noexcept { return __val_.operator->(); }
0108 
0109   _LIBCPP_HIDE_FROM_ABI constexpr bool __has_value() const noexcept { return __val_.has_value(); }
0110 };
0111 
0112 // This partial specialization implements an optimization for when we know we don't need to store
0113 // an empty state to represent failure to perform an assignment. For copy-assignment, this happens:
0114 //
0115 // 1. If the type is copyable (which includes copy-assignment), we can use the type's own assignment operator
0116 //    directly and avoid using std::optional.
0117 // 2. If the type is not copyable, but it is nothrow-copy-constructible, then we can implement assignment as
0118 //    destroy-and-then-construct and we know it will never fail, so we don't need an empty state.
0119 //
0120 // The exact same reasoning can be applied for move-assignment, with copyable replaced by movable and
0121 // nothrow-copy-constructible replaced by nothrow-move-constructible. This specialization is enabled
0122 // whenever we can apply any of these optimizations for both the copy assignment and the move assignment
0123 // operator.
0124 
0125 #  if _LIBCPP_STD_VER >= 23
0126 template <class _Tp>
0127 concept __doesnt_need_empty_state =
0128     (copy_constructible<_Tp>
0129          // 1. If copy_constructible<T> is true, movable-box<T> should store only a T if either T models
0130          //    copyable, or is_nothrow_move_constructible_v<T> && is_nothrow_copy_constructible_v<T> is true.
0131          ? copyable<_Tp> || (is_nothrow_move_constructible_v<_Tp> && is_nothrow_copy_constructible_v<_Tp>)
0132          // 2. Otherwise, movable-box<T> should store only a T if either T models movable or
0133          //    is_nothrow_move_constructible_v<T> is true.
0134          : movable<_Tp> || is_nothrow_move_constructible_v<_Tp>);
0135 
0136 // When _Tp doesn't have an assignment operator, we must implement __movable_box's assignment operator
0137 // by doing destroy_at followed by construct_at. However, that implementation strategy leads to UB if the nested
0138 // _Tp is potentially overlapping, as it is doing a non-transparent replacement of the sub-object, which means that
0139 // we're not considered "nested" inside the movable-box anymore, and since we're not nested within it, [basic.life]/1.5
0140 // says that we essentially just reused the storage of the movable-box for a completely unrelated object and ended the
0141 // movable-box's lifetime.
0142 // https://github.com/llvm/llvm-project/issues/70494#issuecomment-1845646490
0143 //
0144 // Hence, when the _Tp doesn't have an assignment operator, we can't risk making it a potentially-overlapping
0145 // subobject because of the above, and we don't use [[no_unique_address]] in that case.
0146 template <class _Tp>
0147 concept __can_use_no_unique_address = (copy_constructible<_Tp> ? copyable<_Tp> : movable<_Tp>);
0148 
0149 #  else
0150 
0151 template <class _Tp>
0152 concept __doesnt_need_empty_state_for_copy = copyable<_Tp> || is_nothrow_copy_constructible_v<_Tp>;
0153 
0154 template <class _Tp>
0155 concept __doesnt_need_empty_state_for_move = movable<_Tp> || is_nothrow_move_constructible_v<_Tp>;
0156 
0157 template <class _Tp>
0158 concept __doesnt_need_empty_state = __doesnt_need_empty_state_for_copy<_Tp> && __doesnt_need_empty_state_for_move<_Tp>;
0159 
0160 template <class _Tp>
0161 concept __can_use_no_unique_address = copyable<_Tp>;
0162 #  endif
0163 
0164 template <class _Tp>
0165 struct __movable_box_holder {
0166   _Tp __val_;
0167 
0168   template <class... _Args>
0169   _LIBCPP_HIDE_FROM_ABI constexpr explicit __movable_box_holder(in_place_t, _Args&&... __args)
0170       : __val_(std::forward<_Args>(__args)...) {}
0171 };
0172 
0173 template <class _Tp>
0174   requires __can_use_no_unique_address<_Tp>
0175 struct __movable_box_holder<_Tp> {
0176   _LIBCPP_NO_UNIQUE_ADDRESS _Tp __val_;
0177 
0178   template <class... _Args>
0179   _LIBCPP_HIDE_FROM_ABI constexpr explicit __movable_box_holder(in_place_t, _Args&&... __args)
0180       : __val_(std::forward<_Args>(__args)...) {}
0181 };
0182 
0183 template <__movable_box_object _Tp>
0184   requires __doesnt_need_empty_state<_Tp>
0185 class __movable_box<_Tp> {
0186   _LIBCPP_NO_UNIQUE_ADDRESS __movable_box_holder<_Tp> __holder_;
0187 
0188 public:
0189   template <class... _Args>
0190     requires is_constructible_v<_Tp, _Args...>
0191   _LIBCPP_HIDE_FROM_ABI constexpr explicit __movable_box(in_place_t __inplace, _Args&&... __args) noexcept(
0192       is_nothrow_constructible_v<_Tp, _Args...>)
0193       : __holder_(__inplace, std::forward<_Args>(__args)...) {}
0194 
0195   _LIBCPP_HIDE_FROM_ABI constexpr __movable_box() noexcept(is_nothrow_default_constructible_v<_Tp>)
0196     requires default_initializable<_Tp>
0197       : __holder_(in_place_t{}) {}
0198 
0199   _LIBCPP_HIDE_FROM_ABI __movable_box(__movable_box const&) = default;
0200   _LIBCPP_HIDE_FROM_ABI __movable_box(__movable_box&&)      = default;
0201 
0202   // Implementation of assignment operators in case we perform optimization (1)
0203   _LIBCPP_HIDE_FROM_ABI __movable_box& operator=(__movable_box const&)
0204     requires copyable<_Tp>
0205   = default;
0206   _LIBCPP_HIDE_FROM_ABI __movable_box& operator=(__movable_box&&)
0207     requires movable<_Tp>
0208   = default;
0209 
0210   // Implementation of assignment operators in case we perform optimization (2)
0211   _LIBCPP_HIDE_FROM_ABI constexpr __movable_box& operator=(__movable_box const& __other) noexcept {
0212     static_assert(is_nothrow_copy_constructible_v<_Tp>);
0213     static_assert(!__can_use_no_unique_address<_Tp>);
0214     if (this != std::addressof(__other)) {
0215       std::destroy_at(std::addressof(__holder_.__val_));
0216       std::construct_at(std::addressof(__holder_.__val_), __other.__holder_.__val_);
0217     }
0218     return *this;
0219   }
0220 
0221   _LIBCPP_HIDE_FROM_ABI constexpr __movable_box& operator=(__movable_box&& __other) noexcept {
0222     static_assert(is_nothrow_move_constructible_v<_Tp>);
0223     static_assert(!__can_use_no_unique_address<_Tp>);
0224     if (this != std::addressof(__other)) {
0225       std::destroy_at(std::addressof(__holder_.__val_));
0226       std::construct_at(std::addressof(__holder_.__val_), std::move(__other.__holder_.__val_));
0227     }
0228     return *this;
0229   }
0230 
0231   _LIBCPP_HIDE_FROM_ABI constexpr _Tp const& operator*() const noexcept { return __holder_.__val_; }
0232   _LIBCPP_HIDE_FROM_ABI constexpr _Tp& operator*() noexcept { return __holder_.__val_; }
0233 
0234   _LIBCPP_HIDE_FROM_ABI constexpr const _Tp* operator->() const noexcept { return std::addressof(__holder_.__val_); }
0235   _LIBCPP_HIDE_FROM_ABI constexpr _Tp* operator->() noexcept { return std::addressof(__holder_.__val_); }
0236 
0237   _LIBCPP_HIDE_FROM_ABI constexpr bool __has_value() const noexcept { return true; }
0238 };
0239 } // namespace ranges
0240 
0241 #endif // _LIBCPP_STD_VER >= 20
0242 
0243 _LIBCPP_END_NAMESPACE_STD
0244 
0245 _LIBCPP_POP_MACROS
0246 
0247 #endif // _LIBCPP___RANGES_MOVABLE_BOX_H