2 //===----------------------------------------------------------------------===//
4 // The LLVM Compiler Infrastructure
6 // This file is dual licensed under the MIT and the University of Illinois Open
7 // Source Licenses. See LICENSE.TXT for details.
9 //===----------------------------------------------------------------------===//
11 #ifndef _LIBCPP___NODE_HANDLE
12 #define _LIBCPP___NODE_HANDLE
18 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
19 #pragma GCC system_header
23 #include <__undef_macros>
25 _LIBCPP_BEGIN_NAMESPACE_STD
27 #if _LIBCPP_STD_VER > 14
29 // FIXME: Uncomment this when we support the 'merge' functionality.
30 // #define __cpp_lib_node_extract 201606L
32 // Specialized in __tree & __hash_table for their _NodeType.
33 template <class _NodeType, class _Alloc>
34 struct __generic_container_node_destructor;
36 template <class _NodeType, class _Alloc,
37 template <class, class> class _MapOrSetSpecifics>
38 class _LIBCPP_TEMPLATE_VIS __basic_node_handle
39 : public _MapOrSetSpecifics<
41 __basic_node_handle<_NodeType, _Alloc, _MapOrSetSpecifics>>
43 template <class _Tp, class _Compare, class _Allocator>
45 template <class _Tp, class _Hash, class _Equal, class _Allocator>
46 friend class __hash_table;
47 friend struct _MapOrSetSpecifics<
48 _NodeType, __basic_node_handle<_NodeType, _Alloc, _MapOrSetSpecifics>>;
50 typedef allocator_traits<_Alloc> __alloc_traits;
51 typedef typename __rebind_pointer<typename __alloc_traits::void_pointer,
56 typedef _Alloc allocator_type;
59 __node_pointer_type __ptr_ = nullptr;
60 optional<allocator_type> __alloc_;
62 _LIBCPP_INLINE_VISIBILITY
66 __alloc_ = _VSTD::nullopt;
69 _LIBCPP_INLINE_VISIBILITY
70 void __destroy_node_pointer()
72 if (__ptr_ != nullptr)
74 typedef typename __allocator_traits_rebind<
75 allocator_type, _NodeType>::type __node_alloc_type;
76 __node_alloc_type __alloc(*__alloc_);
77 __generic_container_node_destructor<_NodeType, __node_alloc_type>(
78 __alloc, true)(__ptr_);
83 _LIBCPP_INLINE_VISIBILITY
84 __basic_node_handle(__node_pointer_type __ptr,
85 allocator_type const& __alloc)
86 : __ptr_(__ptr), __alloc_(__alloc)
91 _LIBCPP_INLINE_VISIBILITY
92 __basic_node_handle() = default;
94 _LIBCPP_INLINE_VISIBILITY
95 __basic_node_handle(__basic_node_handle&& __other) noexcept
96 : __ptr_(__other.__ptr_),
97 __alloc_(_VSTD::move(__other.__alloc_))
99 __other.__ptr_ = nullptr;
100 __other.__alloc_ = _VSTD::nullopt;
103 _LIBCPP_INLINE_VISIBILITY
104 __basic_node_handle& operator=(__basic_node_handle&& __other)
107 __alloc_ == _VSTD::nullopt ||
108 __alloc_traits::propagate_on_container_move_assignment::value ||
109 __alloc_ == __other.__alloc_,
110 "node_type with incompatible allocator passed to "
111 "node_type::operator=(node_type&&)");
113 __destroy_node_pointer();
114 __ptr_ = __other.__ptr_;
116 if (__alloc_traits::propagate_on_container_move_assignment::value ||
117 __alloc_ == _VSTD::nullopt)
118 __alloc_ = _VSTD::move(__other.__alloc_);
120 __other.__ptr_ = nullptr;
121 __other.__alloc_ = _VSTD::nullopt;
126 _LIBCPP_INLINE_VISIBILITY
127 allocator_type get_allocator() const { return *__alloc_; }
129 _LIBCPP_INLINE_VISIBILITY
130 explicit operator bool() const { return __ptr_ != nullptr; }
132 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
133 bool empty() const { return __ptr_ == nullptr; }
135 _LIBCPP_INLINE_VISIBILITY
136 void swap(__basic_node_handle& __other) noexcept(
137 __alloc_traits::propagate_on_container_swap::value ||
138 __alloc_traits::is_always_equal::value)
141 swap(__ptr_, __other.__ptr_);
142 if (__alloc_traits::propagate_on_container_swap::value ||
143 __alloc_ == _VSTD::nullopt || __other.__alloc_ == _VSTD::nullopt)
144 swap(__alloc_, __other.__alloc_);
147 _LIBCPP_INLINE_VISIBILITY
148 friend void swap(__basic_node_handle& __a, __basic_node_handle& __b)
149 noexcept(noexcept(__a.swap(__b))) { __a.swap(__b); }
151 _LIBCPP_INLINE_VISIBILITY
152 ~__basic_node_handle()
154 __destroy_node_pointer();
158 template <class _NodeType, class _Derived>
159 struct __set_node_handle_specifics
161 typedef typename _NodeType::__node_value_type value_type;
163 _LIBCPP_INLINE_VISIBILITY
164 value_type& value() const
166 return static_cast<_Derived const*>(this)->__ptr_->__value_;
170 template <class _NodeType, class _Derived>
171 struct __map_node_handle_specifics
173 typedef typename _NodeType::__node_value_type::key_type key_type;
174 typedef typename _NodeType::__node_value_type::mapped_type mapped_type;
176 _LIBCPP_INLINE_VISIBILITY
177 key_type& key() const
179 return static_cast<_Derived const*>(this)->
180 __ptr_->__value_.__ref().first;
183 _LIBCPP_INLINE_VISIBILITY
184 mapped_type& mapped() const
186 return static_cast<_Derived const*>(this)->
187 __ptr_->__value_.__ref().second;
191 template <class _NodeType, class _Alloc>
192 using __set_node_handle =
193 __basic_node_handle< _NodeType, _Alloc, __set_node_handle_specifics>;
195 template <class _NodeType, class _Alloc>
196 using __map_node_handle =
197 __basic_node_handle< _NodeType, _Alloc, __map_node_handle_specifics>;
199 template <class _Iterator, class _NodeType>
201 struct __insert_return_type
208 #endif // _LIBCPP_STD_VER > 14
210 _LIBCPP_END_NAMESPACE_STD