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/* 14 15template<unspecified> 16class node-handle { 17public: 18 using value_type = see below; // not present for map containers 19 using key_type = see below; // not present for set containers 20 using mapped_type = see below; // not present for set containers 21 using allocator_type = see below; 22 23private: 24 using container_node_type = unspecified; // exposition only 25 using ator_traits = allocator_traits<allocator_type>; // exposition only 26 27 typename ator_traits::template 28 rebind_traits<container_node_type>::pointer ptr_; // exposition only 29 optional<allocator_type> alloc_; // exposition only 30 31public: 32 // [container.node.cons], constructors, copy, and assignment 33 constexpr node-handle() noexcept : ptr_(), alloc_() {} 34 node-handle(node-handle&&) noexcept; 35 node-handle& operator=(node-handle&&); 36 37 // [container.node.dtor], destructor 38 ~node-handle(); 39 40 // [container.node.observers], observers 41 value_type& value() const; // not present for map containers 42 key_type& key() const; // not present for set containers 43 mapped_type& mapped() const; // not present for set containers 44 45 allocator_type get_allocator() const; 46 explicit operator bool() const noexcept; 47 [[nodiscard]] bool empty() const noexcept; // nodiscard since C++20 48 49 // [container.node.modifiers], modifiers 50 void swap(node-handle&) 51 noexcept(ator_traits::propagate_on_container_swap::value || 52 ator_traits::is_always_equal::value); 53 54 friend void swap(node-handle& x, node-handle& y) noexcept(noexcept(x.swap(y))) { 55 x.swap(y); 56 } 57}; 58 59*/ 60 61#include <__config> 62#include <__debug> 63#include <memory> 64#include <optional> 65 66#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) 67#pragma GCC system_header 68#endif 69 70_LIBCPP_BEGIN_NAMESPACE_STD 71 72#if _LIBCPP_STD_VER > 14 73 74// Specialized in __tree & __hash_table for their _NodeType. 75template <class _NodeType, class _Alloc> 76struct __generic_container_node_destructor; 77 78template <class _NodeType, class _Alloc, 79 template <class, class> class _MapOrSetSpecifics> 80class _LIBCPP_TEMPLATE_VIS __basic_node_handle 81 : public _MapOrSetSpecifics< 82 _NodeType, 83 __basic_node_handle<_NodeType, _Alloc, _MapOrSetSpecifics>> 84{ 85 template <class _Tp, class _Compare, class _Allocator> 86 friend class __tree; 87 template <class _Tp, class _Hash, class _Equal, class _Allocator> 88 friend class __hash_table; 89 friend struct _MapOrSetSpecifics< 90 _NodeType, __basic_node_handle<_NodeType, _Alloc, _MapOrSetSpecifics>>; 91 92 typedef allocator_traits<_Alloc> __alloc_traits; 93 typedef typename __rebind_pointer<typename __alloc_traits::void_pointer, 94 _NodeType>::type 95 __node_pointer_type; 96 97public: 98 typedef _Alloc allocator_type; 99 100private: 101 __node_pointer_type __ptr_ = nullptr; 102 optional<allocator_type> __alloc_; 103 104 _LIBCPP_INLINE_VISIBILITY 105 void __release_ptr() 106 { 107 __ptr_ = nullptr; 108 __alloc_ = _VSTD::nullopt; 109 } 110 111 _LIBCPP_INLINE_VISIBILITY 112 void __destroy_node_pointer() 113 { 114 if (__ptr_ != nullptr) 115 { 116 typedef typename __allocator_traits_rebind< 117 allocator_type, _NodeType>::type __node_alloc_type; 118 __node_alloc_type __alloc(*__alloc_); 119 __generic_container_node_destructor<_NodeType, __node_alloc_type>( 120 __alloc, true)(__ptr_); 121 __ptr_ = nullptr; 122 } 123 } 124 125 _LIBCPP_INLINE_VISIBILITY 126 __basic_node_handle(__node_pointer_type __ptr, 127 allocator_type const& __alloc) 128 : __ptr_(__ptr), __alloc_(__alloc) 129 { 130 } 131 132public: 133 _LIBCPP_INLINE_VISIBILITY 134 __basic_node_handle() = default; 135 136 _LIBCPP_INLINE_VISIBILITY 137 __basic_node_handle(__basic_node_handle&& __other) noexcept 138 : __ptr_(__other.__ptr_), 139 __alloc_(_VSTD::move(__other.__alloc_)) 140 { 141 __other.__ptr_ = nullptr; 142 __other.__alloc_ = _VSTD::nullopt; 143 } 144 145 _LIBCPP_INLINE_VISIBILITY 146 __basic_node_handle& operator=(__basic_node_handle&& __other) 147 { 148 _LIBCPP_ASSERT( 149 __alloc_ == _VSTD::nullopt || 150 __alloc_traits::propagate_on_container_move_assignment::value || 151 __alloc_ == __other.__alloc_, 152 "node_type with incompatible allocator passed to " 153 "node_type::operator=(node_type&&)"); 154 155 __destroy_node_pointer(); 156 __ptr_ = __other.__ptr_; 157 158 if (__alloc_traits::propagate_on_container_move_assignment::value || 159 __alloc_ == _VSTD::nullopt) 160 __alloc_ = _VSTD::move(__other.__alloc_); 161 162 __other.__ptr_ = nullptr; 163 __other.__alloc_ = _VSTD::nullopt; 164 165 return *this; 166 } 167 168 _LIBCPP_INLINE_VISIBILITY 169 allocator_type get_allocator() const { return *__alloc_; } 170 171 _LIBCPP_INLINE_VISIBILITY 172 explicit operator bool() const { return __ptr_ != nullptr; } 173 174 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY 175 bool empty() const { return __ptr_ == nullptr; } 176 177 _LIBCPP_INLINE_VISIBILITY 178 void swap(__basic_node_handle& __other) noexcept( 179 __alloc_traits::propagate_on_container_swap::value || 180 __alloc_traits::is_always_equal::value) 181 { 182 using _VSTD::swap; 183 swap(__ptr_, __other.__ptr_); 184 if (__alloc_traits::propagate_on_container_swap::value || 185 __alloc_ == _VSTD::nullopt || __other.__alloc_ == _VSTD::nullopt) 186 swap(__alloc_, __other.__alloc_); 187 } 188 189 _LIBCPP_INLINE_VISIBILITY 190 friend void swap(__basic_node_handle& __a, __basic_node_handle& __b) 191 noexcept(noexcept(__a.swap(__b))) { __a.swap(__b); } 192 193 _LIBCPP_INLINE_VISIBILITY 194 ~__basic_node_handle() 195 { 196 __destroy_node_pointer(); 197 } 198}; 199 200template <class _NodeType, class _Derived> 201struct __set_node_handle_specifics 202{ 203 typedef typename _NodeType::__node_value_type value_type; 204 205 _LIBCPP_INLINE_VISIBILITY 206 value_type& value() const 207 { 208 return static_cast<_Derived const*>(this)->__ptr_->__value_; 209 } 210}; 211 212template <class _NodeType, class _Derived> 213struct __map_node_handle_specifics 214{ 215 typedef typename _NodeType::__node_value_type::key_type key_type; 216 typedef typename _NodeType::__node_value_type::mapped_type mapped_type; 217 218 _LIBCPP_INLINE_VISIBILITY 219 key_type& key() const 220 { 221 return static_cast<_Derived const*>(this)-> 222 __ptr_->__value_.__ref().first; 223 } 224 225 _LIBCPP_INLINE_VISIBILITY 226 mapped_type& mapped() const 227 { 228 return static_cast<_Derived const*>(this)-> 229 __ptr_->__value_.__ref().second; 230 } 231}; 232 233template <class _NodeType, class _Alloc> 234using __set_node_handle = 235 __basic_node_handle< _NodeType, _Alloc, __set_node_handle_specifics>; 236 237template <class _NodeType, class _Alloc> 238using __map_node_handle = 239 __basic_node_handle< _NodeType, _Alloc, __map_node_handle_specifics>; 240 241template <class _Iterator, class _NodeType> 242struct _LIBCPP_TEMPLATE_VIS __insert_return_type 243{ 244 _Iterator position; 245 bool inserted; 246 _NodeType node; 247}; 248 249#endif // _LIBCPP_STD_VER > 14 250 251_LIBCPP_END_NAMESPACE_STD 252 253#endif // _LIBCPP___NODE_HANDLE 254