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 constexpr node-handle(node-handle&&) noexcept; // constexpr since C++26
35 constexpr node-handle& operator=(node-handle&&); // constexpr since C++26
36
37 // [container.node.dtor], destructor
38 constexpr ~node-handle(); // constexpr since C++26
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 constexpr mapped_type& mapped() const; // not present for set containers, constexpr since C++26
44
45 constexpr allocator_type get_allocator() const; // constexpr since C++26
46 constexpr explicit operator bool() const noexcept; // constexpr since C++26
47 [[nodiscard]] constexpr bool empty() const noexcept; // nodiscard since C++20, constexpr since C++26
48
49 // [container.node.modifiers], modifiers
50 constexpr void swap(node-handle&)
51 noexcept(ator_traits::propagate_on_container_swap::value ||
52 ator_traits::is_always_equal::value); // constexpr since C++26
53
54 constexpr friend void swap(node-handle& x, node-handle& y) noexcept(noexcept(x.swap(y))) {
55 x.swap(y);
56 } // constexpr since C++26
57};
58
59*/
60
61#include <__assert>
62#include <__config>
63#include <__memory/allocator_traits.h>
64#include <__memory/pointer_traits.h>
65#include <__type_traits/is_specialization.h>
66#include <__utility/exchange.h>
67#include <optional>
68
69#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
70# pragma GCC system_header
71#endif
72
73_LIBCPP_PUSH_MACROS
74#include <__undef_macros>
75
76#if _LIBCPP_STD_VER >= 17
77
78_LIBCPP_BEGIN_NAMESPACE_STD
79
80// Specialized in __tree & __hash_table for their _NodeType.
81template <class _NodeType, class _Alloc>
82struct __generic_container_node_destructor;
83
84template <class _NodeType, class _Alloc, template <class, class> class _MapOrSetSpecifics>
85class __basic_node_handle
86 : public _MapOrSetSpecifics< _NodeType, __basic_node_handle<_NodeType, _Alloc, _MapOrSetSpecifics>> {
87 template <class _Tp, class _Compare, class _Allocator>
88 friend class __tree;
89 template <class _Tp, class _Hash, class _Equal, class _Allocator>
90 friend class __hash_table;
91 friend struct _MapOrSetSpecifics< _NodeType, __basic_node_handle<_NodeType, _Alloc, _MapOrSetSpecifics>>;
92
93 typedef allocator_traits<_Alloc> __alloc_traits;
94 typedef __rebind_pointer_t<typename __alloc_traits::void_pointer, _NodeType> __node_pointer_type;
95
96public:
97 typedef _Alloc allocator_type;
98
99private:
100 __node_pointer_type __ptr_ = nullptr;
101 optional<allocator_type> __alloc_;
102
103 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 void __release_ptr() {
104 __ptr_ = nullptr;
105 __alloc_ = std::nullopt;
106 }
107
108 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 void __destroy_node_pointer() {
109 if (__ptr_ != nullptr) {
110 typedef typename __allocator_traits_rebind< allocator_type, _NodeType>::type __node_alloc_type;
111 __node_alloc_type __alloc(*__alloc_);
112 __generic_container_node_destructor<_NodeType, __node_alloc_type>(__alloc, true)(__ptr_);
113 __ptr_ = nullptr;
114 }
115 }
116
117 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26
118 __basic_node_handle(__node_pointer_type __ptr, allocator_type const& __alloc)
119 : __ptr_(__ptr), __alloc_(__alloc) {}
120
121public:
122 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 __basic_node_handle() = default;
123
124 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 __basic_node_handle(__basic_node_handle&& __other) noexcept
125 : __ptr_(std::exchange(__other.__ptr_, nullptr)), __alloc_(std::exchange(__other.__alloc_, std::nullopt)) {}
126
127 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 __basic_node_handle& operator=(__basic_node_handle&& __other) {
128 _LIBCPP_ASSERT_COMPATIBLE_ALLOCATOR(
129 __alloc_ == std::nullopt || __alloc_traits::propagate_on_container_move_assignment::value ||
130 __alloc_ == __other.__alloc_,
131 "node_type with incompatible allocator passed to "
132 "node_type::operator=(node_type&&)");
133
134 __destroy_node_pointer();
135 __ptr_ = std::exchange(__other.__ptr_, nullptr);
136
137 if (__alloc_traits::propagate_on_container_move_assignment::value || __alloc_ == std::nullopt)
138 __alloc_ = std::move(__other.__alloc_);
139
140 __other.__alloc_ = std::nullopt;
141
142 return *this;
143 }
144
145 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 allocator_type get_allocator() const { return *__alloc_; }
146
147 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 explicit operator bool() const { return __ptr_ != nullptr; }
148
149 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 bool empty() const { return __ptr_ == nullptr; }
150
151 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 void swap(__basic_node_handle& __other) noexcept(
152 __alloc_traits::propagate_on_container_swap::value || __alloc_traits::is_always_equal::value) {
153 using std::swap;
154 swap(__ptr_, __other.__ptr_);
155 if (__alloc_traits::propagate_on_container_swap::value || __alloc_ == std::nullopt ||
156 __other.__alloc_ == std::nullopt)
157 swap(__alloc_, __other.__alloc_);
158 }
159
160 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 friend void
161 swap(__basic_node_handle& __a, __basic_node_handle& __b) noexcept(noexcept(__a.swap(other&: __b))) {
162 __a.swap(other&: __b);
163 }
164
165 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 ~__basic_node_handle() { __destroy_node_pointer(); }
166};
167
168template <class _NodeType, class _Derived>
169struct __set_node_handle_specifics {
170 typedef typename _NodeType::__node_value_type value_type;
171
172 _LIBCPP_HIDE_FROM_ABI value_type& value() const { return static_cast<_Derived const*>(this)->__ptr_->__get_value(); }
173};
174
175template <class _NodeType, class _Derived>
176struct __map_node_handle_specifics {
177 using key_type = __remove_const_t<typename _NodeType::__node_value_type::first_type>;
178 using mapped_type = typename _NodeType::__node_value_type::second_type;
179
180 // This method is not constexpr as per the standard.
181 _LIBCPP_HIDE_FROM_ABI key_type& key() const {
182 return const_cast<key_type&>(static_cast<_Derived const*>(this)->__ptr_->__get_value().first);
183 }
184
185 _LIBCPP_HIDE_FROM_ABI mapped_type& mapped() const {
186 return static_cast<_Derived const*>(this)->__ptr_->__get_value().second;
187 }
188};
189
190template <class _NodeType, class _Alloc>
191using __set_node_handle _LIBCPP_NODEBUG = __basic_node_handle< _NodeType, _Alloc, __set_node_handle_specifics>;
192
193template <class _NodeType, class _Alloc>
194using __map_node_handle _LIBCPP_NODEBUG = __basic_node_handle< _NodeType, _Alloc, __map_node_handle_specifics>;
195
196template <class _Iterator, class _NodeType>
197struct __insert_return_type {
198 _Iterator position;
199 bool inserted;
200 _NodeType node;
201};
202
203_LIBCPP_END_NAMESPACE_STD
204
205#endif // _LIBCPP_STD_VER >= 17
206
207_LIBCPP_POP_MACROS
208
209#endif // _LIBCPP___NODE_HANDLE
210