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