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