| 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___NODE_HANDLE |
| 11 | #define _LIBCPP___NODE_HANDLE |
| 12 | |
| 13 | #include <__config> |
| 14 | #include <memory> |
| 15 | #include <optional> |
| 16 | |
| 17 | #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) |
| 18 | #pragma GCC system_header |
| 19 | #endif |
| 20 | |
| 21 | _LIBCPP_PUSH_MACROS |
| 22 | #include <__undef_macros> |
| 23 | |
| 24 | _LIBCPP_BEGIN_NAMESPACE_STD |
| 25 | |
| 26 | #if _LIBCPP_STD_VER > 14 |
| 27 | |
| 28 | // Specialized in __tree & __hash_table for their _NodeType. |
| 29 | template <class _NodeType, class _Alloc> |
| 30 | struct __generic_container_node_destructor; |
| 31 | |
| 32 | template <class _NodeType, class _Alloc, |
| 33 | template <class, class> class _MapOrSetSpecifics> |
| 34 | class _LIBCPP_TEMPLATE_VIS __basic_node_handle |
| 35 | : public _MapOrSetSpecifics< |
| 36 | _NodeType, |
| 37 | __basic_node_handle<_NodeType, _Alloc, _MapOrSetSpecifics>> |
| 38 | { |
| 39 | template <class _Tp, class _Compare, class _Allocator> |
| 40 | friend class __tree; |
| 41 | template <class _Tp, class _Hash, class _Equal, class _Allocator> |
| 42 | friend class __hash_table; |
| 43 | friend struct _MapOrSetSpecifics< |
| 44 | _NodeType, __basic_node_handle<_NodeType, _Alloc, _MapOrSetSpecifics>>; |
| 45 | |
| 46 | typedef allocator_traits<_Alloc> __alloc_traits; |
| 47 | typedef typename __rebind_pointer<typename __alloc_traits::void_pointer, |
| 48 | _NodeType>::type |
| 49 | __node_pointer_type; |
| 50 | |
| 51 | public: |
| 52 | typedef _Alloc allocator_type; |
| 53 | |
| 54 | private: |
| 55 | __node_pointer_type __ptr_ = nullptr; |
| 56 | optional<allocator_type> __alloc_; |
| 57 | |
| 58 | _LIBCPP_INLINE_VISIBILITY |
| 59 | void __release_ptr() |
| 60 | { |
| 61 | __ptr_ = nullptr; |
| 62 | __alloc_ = _VSTD::nullopt; |
| 63 | } |
| 64 | |
| 65 | _LIBCPP_INLINE_VISIBILITY |
| 66 | void __destroy_node_pointer() |
| 67 | { |
| 68 | if (__ptr_ != nullptr) |
| 69 | { |
| 70 | typedef typename __allocator_traits_rebind< |
| 71 | allocator_type, _NodeType>::type __node_alloc_type; |
| 72 | __node_alloc_type __alloc(*__alloc_); |
| 73 | __generic_container_node_destructor<_NodeType, __node_alloc_type>( |
| 74 | __alloc, true)(__ptr_); |
| 75 | __ptr_ = nullptr; |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | _LIBCPP_INLINE_VISIBILITY |
| 80 | __basic_node_handle(__node_pointer_type __ptr, |
| 81 | allocator_type const& __alloc) |
| 82 | : __ptr_(__ptr), __alloc_(__alloc) |
| 83 | { |
| 84 | } |
| 85 | |
| 86 | public: |
| 87 | _LIBCPP_INLINE_VISIBILITY |
| 88 | __basic_node_handle() = default; |
| 89 | |
| 90 | _LIBCPP_INLINE_VISIBILITY |
| 91 | __basic_node_handle(__basic_node_handle&& __other) noexcept |
| 92 | : __ptr_(__other.__ptr_), |
| 93 | __alloc_(_VSTD::move(__other.__alloc_)) |
| 94 | { |
| 95 | __other.__ptr_ = nullptr; |
| 96 | __other.__alloc_ = _VSTD::nullopt; |
| 97 | } |
| 98 | |
| 99 | _LIBCPP_INLINE_VISIBILITY |
| 100 | __basic_node_handle& operator=(__basic_node_handle&& __other) |
| 101 | { |
| 102 | _LIBCPP_ASSERT( |
| 103 | __alloc_ == _VSTD::nullopt || |
| 104 | __alloc_traits::propagate_on_container_move_assignment::value || |
| 105 | __alloc_ == __other.__alloc_, |
| 106 | "node_type with incompatible allocator passed to " |
| 107 | "node_type::operator=(node_type&&)" ); |
| 108 | |
| 109 | __destroy_node_pointer(); |
| 110 | __ptr_ = __other.__ptr_; |
| 111 | |
| 112 | if (__alloc_traits::propagate_on_container_move_assignment::value || |
| 113 | __alloc_ == _VSTD::nullopt) |
| 114 | __alloc_ = _VSTD::move(__other.__alloc_); |
| 115 | |
| 116 | __other.__ptr_ = nullptr; |
| 117 | __other.__alloc_ = _VSTD::nullopt; |
| 118 | |
| 119 | return *this; |
| 120 | } |
| 121 | |
| 122 | _LIBCPP_INLINE_VISIBILITY |
| 123 | allocator_type get_allocator() const { return *__alloc_; } |
| 124 | |
| 125 | _LIBCPP_INLINE_VISIBILITY |
| 126 | explicit operator bool() const { return __ptr_ != nullptr; } |
| 127 | |
| 128 | _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY |
| 129 | bool empty() const { return __ptr_ == nullptr; } |
| 130 | |
| 131 | _LIBCPP_INLINE_VISIBILITY |
| 132 | void swap(__basic_node_handle& __other) noexcept( |
| 133 | __alloc_traits::propagate_on_container_swap::value || |
| 134 | __alloc_traits::is_always_equal::value) |
| 135 | { |
| 136 | using _VSTD::swap; |
| 137 | swap(__ptr_, __other.__ptr_); |
| 138 | if (__alloc_traits::propagate_on_container_swap::value || |
| 139 | __alloc_ == _VSTD::nullopt || __other.__alloc_ == _VSTD::nullopt) |
| 140 | swap(__alloc_, __other.__alloc_); |
| 141 | } |
| 142 | |
| 143 | _LIBCPP_INLINE_VISIBILITY |
| 144 | friend void swap(__basic_node_handle& __a, __basic_node_handle& __b) |
| 145 | noexcept(noexcept(__a.swap(__b))) { __a.swap(__b); } |
| 146 | |
| 147 | _LIBCPP_INLINE_VISIBILITY |
| 148 | ~__basic_node_handle() |
| 149 | { |
| 150 | __destroy_node_pointer(); |
| 151 | } |
| 152 | }; |
| 153 | |
| 154 | template <class _NodeType, class _Derived> |
| 155 | struct __set_node_handle_specifics |
| 156 | { |
| 157 | typedef typename _NodeType::__node_value_type value_type; |
| 158 | |
| 159 | _LIBCPP_INLINE_VISIBILITY |
| 160 | value_type& value() const |
| 161 | { |
| 162 | return static_cast<_Derived const*>(this)->__ptr_->__value_; |
| 163 | } |
| 164 | }; |
| 165 | |
| 166 | template <class _NodeType, class _Derived> |
| 167 | struct __map_node_handle_specifics |
| 168 | { |
| 169 | typedef typename _NodeType::__node_value_type::key_type key_type; |
| 170 | typedef typename _NodeType::__node_value_type::mapped_type mapped_type; |
| 171 | |
| 172 | _LIBCPP_INLINE_VISIBILITY |
| 173 | key_type& key() const |
| 174 | { |
| 175 | return static_cast<_Derived const*>(this)-> |
| 176 | __ptr_->__value_.__ref().first; |
| 177 | } |
| 178 | |
| 179 | _LIBCPP_INLINE_VISIBILITY |
| 180 | mapped_type& mapped() const |
| 181 | { |
| 182 | return static_cast<_Derived const*>(this)-> |
| 183 | __ptr_->__value_.__ref().second; |
| 184 | } |
| 185 | }; |
| 186 | |
| 187 | template <class _NodeType, class _Alloc> |
| 188 | using __set_node_handle = |
| 189 | __basic_node_handle< _NodeType, _Alloc, __set_node_handle_specifics>; |
| 190 | |
| 191 | template <class _NodeType, class _Alloc> |
| 192 | using __map_node_handle = |
| 193 | __basic_node_handle< _NodeType, _Alloc, __map_node_handle_specifics>; |
| 194 | |
| 195 | template <class _Iterator, class _NodeType> |
| 196 | struct _LIBCPP_TEMPLATE_VIS __insert_return_type |
| 197 | { |
| 198 | _Iterator position; |
| 199 | bool inserted; |
| 200 | _NodeType node; |
| 201 | }; |
| 202 | |
| 203 | #endif // _LIBCPP_STD_VER > 14 |
| 204 | |
| 205 | _LIBCPP_END_NAMESPACE_STD |
| 206 | _LIBCPP_POP_MACROS |
| 207 | |
| 208 | #endif |
| 209 | |