xref: /freebsd/contrib/llvm-project/libcxx/include/__node_handle (revision 59c8e88e72633afbc47a4ace0d2170d00d51f7dc)
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 <__assert>
62#include <__config>
63#include <__memory/allocator_traits.h>
64#include <__memory/pointer_traits.h>
65#include <optional>
66
67#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
68#  pragma GCC system_header
69#endif
70
71_LIBCPP_PUSH_MACROS
72#include <__undef_macros>
73
74_LIBCPP_BEGIN_NAMESPACE_STD
75
76#if _LIBCPP_STD_VER >= 17
77
78// Specialized in __tree & __hash_table for their _NodeType.
79template <class _NodeType, class _Alloc>
80struct __generic_container_node_destructor;
81
82template <class _NodeType, class _Alloc,
83          template <class, class> class _MapOrSetSpecifics>
84class _LIBCPP_TEMPLATE_VIS __basic_node_handle
85    : public _MapOrSetSpecifics<
86          _NodeType,
87          __basic_node_handle<_NodeType, _Alloc, _MapOrSetSpecifics>>
88{
89    template <class _Tp, class _Compare, class _Allocator>
90        friend class __tree;
91    template <class _Tp, class _Hash, class _Equal, class _Allocator>
92        friend class __hash_table;
93    friend struct _MapOrSetSpecifics<
94        _NodeType, __basic_node_handle<_NodeType, _Alloc, _MapOrSetSpecifics>>;
95
96    typedef allocator_traits<_Alloc> __alloc_traits;
97    typedef __rebind_pointer_t<typename __alloc_traits::void_pointer,
98                                      _NodeType>
99        __node_pointer_type;
100
101public:
102    typedef _Alloc allocator_type;
103
104private:
105    __node_pointer_type __ptr_ = nullptr;
106    optional<allocator_type> __alloc_;
107
108    _LIBCPP_INLINE_VISIBILITY
109    void __release_ptr()
110    {
111        __ptr_ = nullptr;
112        __alloc_ = _VSTD::nullopt;
113    }
114
115    _LIBCPP_INLINE_VISIBILITY
116    void __destroy_node_pointer()
117    {
118        if (__ptr_ != nullptr)
119        {
120            typedef typename __allocator_traits_rebind<
121                allocator_type, _NodeType>::type __node_alloc_type;
122            __node_alloc_type __alloc(*__alloc_);
123            __generic_container_node_destructor<_NodeType, __node_alloc_type>(
124                __alloc, true)(__ptr_);
125            __ptr_ = nullptr;
126        }
127    }
128
129    _LIBCPP_INLINE_VISIBILITY
130    __basic_node_handle(__node_pointer_type __ptr,
131                        allocator_type const& __alloc)
132            : __ptr_(__ptr), __alloc_(__alloc)
133    {
134    }
135
136public:
137    _LIBCPP_INLINE_VISIBILITY
138    __basic_node_handle() = default;
139
140    _LIBCPP_INLINE_VISIBILITY
141    __basic_node_handle(__basic_node_handle&& __other) noexcept
142            : __ptr_(__other.__ptr_),
143              __alloc_(_VSTD::move(__other.__alloc_))
144    {
145        __other.__ptr_ = nullptr;
146        __other.__alloc_ = _VSTD::nullopt;
147    }
148
149    _LIBCPP_INLINE_VISIBILITY
150    __basic_node_handle& operator=(__basic_node_handle&& __other)
151    {
152        _LIBCPP_ASSERT_COMPATIBLE_ALLOCATOR(
153            __alloc_ == _VSTD::nullopt ||
154            __alloc_traits::propagate_on_container_move_assignment::value ||
155            __alloc_ == __other.__alloc_,
156            "node_type with incompatible allocator passed to "
157            "node_type::operator=(node_type&&)");
158
159        __destroy_node_pointer();
160        __ptr_ = __other.__ptr_;
161
162        if (__alloc_traits::propagate_on_container_move_assignment::value ||
163            __alloc_ == _VSTD::nullopt)
164            __alloc_ = _VSTD::move(__other.__alloc_);
165
166        __other.__ptr_ = nullptr;
167        __other.__alloc_ = _VSTD::nullopt;
168
169        return *this;
170    }
171
172    _LIBCPP_INLINE_VISIBILITY
173    allocator_type get_allocator() const { return *__alloc_; }
174
175    _LIBCPP_INLINE_VISIBILITY
176    explicit operator bool() const { return __ptr_ != nullptr; }
177
178    _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
179    bool empty() const { return __ptr_ == nullptr; }
180
181    _LIBCPP_INLINE_VISIBILITY
182    void swap(__basic_node_handle& __other) noexcept(
183        __alloc_traits::propagate_on_container_swap::value ||
184        __alloc_traits::is_always_equal::value)
185    {
186        using _VSTD::swap;
187        swap(__ptr_, __other.__ptr_);
188        if (__alloc_traits::propagate_on_container_swap::value ||
189            __alloc_ == _VSTD::nullopt || __other.__alloc_ == _VSTD::nullopt)
190            swap(__alloc_, __other.__alloc_);
191    }
192
193    _LIBCPP_INLINE_VISIBILITY
194    friend void swap(__basic_node_handle& __a, __basic_node_handle& __b)
195        noexcept(noexcept(__a.swap(__b))) { __a.swap(__b); }
196
197    _LIBCPP_INLINE_VISIBILITY
198    ~__basic_node_handle()
199    {
200        __destroy_node_pointer();
201    }
202};
203
204template <class _NodeType, class _Derived>
205struct __set_node_handle_specifics
206{
207    typedef typename _NodeType::__node_value_type value_type;
208
209    _LIBCPP_INLINE_VISIBILITY
210    value_type& value() const
211    {
212        return static_cast<_Derived const*>(this)->__ptr_->__value_;
213    }
214};
215
216template <class _NodeType, class _Derived>
217struct __map_node_handle_specifics
218{
219    typedef typename _NodeType::__node_value_type::key_type key_type;
220    typedef typename _NodeType::__node_value_type::mapped_type mapped_type;
221
222    _LIBCPP_INLINE_VISIBILITY
223    key_type& key() const
224    {
225        return static_cast<_Derived const*>(this)->
226            __ptr_->__value_.__ref().first;
227    }
228
229    _LIBCPP_INLINE_VISIBILITY
230    mapped_type& mapped() const
231    {
232        return static_cast<_Derived const*>(this)->
233            __ptr_->__value_.__ref().second;
234    }
235};
236
237template <class _NodeType, class _Alloc>
238using __set_node_handle =
239    __basic_node_handle< _NodeType, _Alloc, __set_node_handle_specifics>;
240
241template <class _NodeType, class _Alloc>
242using __map_node_handle =
243    __basic_node_handle< _NodeType, _Alloc, __map_node_handle_specifics>;
244
245template <class _Iterator, class _NodeType>
246struct _LIBCPP_TEMPLATE_VIS __insert_return_type
247{
248    _Iterator position;
249    bool inserted;
250    _NodeType node;
251};
252
253#endif // _LIBCPP_STD_VER >= 17
254
255_LIBCPP_END_NAMESPACE_STD
256
257_LIBCPP_POP_MACROS
258
259#endif  // _LIBCPP___NODE_HANDLE
260