codekingpro/portable-devtools
114k
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-exception7//8//===----------------------------------------------------------------------===//9 10#ifndef _LIBCUDACXX___NODE_HANDLE11#define _LIBCUDACXX___NODE_HANDLE12 13#include <__config>14#include "__assert" // all public C++ headers provide the assertion handler15#include <memory>16#include <optional>17 18#if defined(_CCCL_IMPLICIT_SYSTEM_HEADER_GCC)19# pragma GCC system_header20#elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_CLANG)21# pragma clang system_header22#elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_MSVC)23# pragma system_header24#endif // no system header25 26_LIBCUDACXX_PUSH_MACROS27#include <__undef_macros>28 29_LIBCUDACXX_BEGIN_NAMESPACE_STD30 31#if _LIBCUDACXX_STD_VER > 1432 33// Specialized in __tree & __hash_table for their _NodeType.34template <class _NodeType, class _Alloc>35struct __generic_container_node_destructor;36 37template <class _NodeType, class _Alloc,38 template <class, class> class _MapOrSetSpecifics>39class _LIBCUDACXX_TEMPLATE_VIS __basic_node_handle40 : public _MapOrSetSpecifics<41 _NodeType,42 __basic_node_handle<_NodeType, _Alloc, _MapOrSetSpecifics>>43{44 template <class _Tp, class _Compare, class _Allocator>45 friend class __tree;46 template <class _Tp, class _Hash, class _Equal, class _Allocator>47 friend class __hash_table;48 friend struct _MapOrSetSpecifics<49 _NodeType, __basic_node_handle<_NodeType, _Alloc, _MapOrSetSpecifics>>;50 51 typedef allocator_traits<_Alloc> __alloc_traits;52 typedef typename __rebind_pointer<typename __alloc_traits::void_pointer,53 _NodeType>::type54 __node_pointer_type;55 56public:57 typedef _Alloc allocator_type;58 59private:60 __node_pointer_type __ptr_ = nullptr;61 optional<allocator_type> __alloc_;62 63 _LIBCUDACXX_INLINE_VISIBILITY64 void __release_ptr()65 {66 __ptr_ = nullptr;67 __alloc_ = _CUDA_VSTD::nullopt;68 }69 70 _LIBCUDACXX_INLINE_VISIBILITY71 void __destroy_node_pointer()72 {73 if (__ptr_ != nullptr)74 {75 typedef typename __allocator_traits_rebind<76 allocator_type, _NodeType>::type __node_alloc_type;77 __node_alloc_type __alloc(*__alloc_);78 __generic_container_node_destructor<_NodeType, __node_alloc_type>(79 __alloc, true)(__ptr_);80 __ptr_ = nullptr;81 }82 }83 84 _LIBCUDACXX_INLINE_VISIBILITY85 __basic_node_handle(__node_pointer_type __ptr,86 allocator_type const& __alloc)87 : __ptr_(__ptr), __alloc_(__alloc)88 {89 }90 91public:92 _LIBCUDACXX_INLINE_VISIBILITY93 __basic_node_handle() = default;94 95 _LIBCUDACXX_INLINE_VISIBILITY96 __basic_node_handle(__basic_node_handle&& __other) noexcept97 : __ptr_(__other.__ptr_),98 __alloc_(_CUDA_VSTD::move(__other.__alloc_))99 {100 __other.__ptr_ = nullptr;101 __other.__alloc_ = _CUDA_VSTD::nullopt;102 }103 104 _LIBCUDACXX_INLINE_VISIBILITY105 __basic_node_handle& operator=(__basic_node_handle&& __other)106 {107 _LIBCUDACXX_ASSERT(108 __alloc_ == _CUDA_VSTD::nullopt ||109 __alloc_traits::propagate_on_container_move_assignment::value ||110 __alloc_ == __other.__alloc_,111 "node_type with incompatible allocator passed to "112 "node_type::operator=(node_type&&)");113 114 __destroy_node_pointer();115 __ptr_ = __other.__ptr_;116 117 if (__alloc_traits::propagate_on_container_move_assignment::value ||118 __alloc_ == _CUDA_VSTD::nullopt)119 __alloc_ = _CUDA_VSTD::move(__other.__alloc_);120 121 __other.__ptr_ = nullptr;122 __other.__alloc_ = _CUDA_VSTD::nullopt;123 124 return *this;125 }126 127 _LIBCUDACXX_INLINE_VISIBILITY128 allocator_type get_allocator() const { return *__alloc_; }129 130 _LIBCUDACXX_INLINE_VISIBILITY131 explicit operator bool() const { return __ptr_ != nullptr; }132 133 _LIBCUDACXX_NODISCARD_AFTER_CXX17 _LIBCUDACXX_INLINE_VISIBILITY134 bool empty() const { return __ptr_ == nullptr; }135 136 _LIBCUDACXX_INLINE_VISIBILITY137 void swap(__basic_node_handle& __other) noexcept(138 __alloc_traits::propagate_on_container_swap::value ||139 __alloc_traits::is_always_equal::value)140 {141 using _CUDA_VSTD::swap;142 swap(__ptr_, __other.__ptr_);143 if (__alloc_traits::propagate_on_container_swap::value ||144 __alloc_ == _CUDA_VSTD::nullopt || __other.__alloc_ == _CUDA_VSTD::nullopt)145 swap(__alloc_, __other.__alloc_);146 }147 148 _LIBCUDACXX_INLINE_VISIBILITY149 friend void swap(__basic_node_handle& __a, __basic_node_handle& __b)150 noexcept(noexcept(__a.swap(__b))) { __a.swap(__b); }151 152 _LIBCUDACXX_INLINE_VISIBILITY153 ~__basic_node_handle()154 {155 __destroy_node_pointer();156 }157};158 159template <class _NodeType, class _Derived>160struct __set_node_handle_specifics161{162 typedef typename _NodeType::__node_value_type value_type;163 164 _LIBCUDACXX_INLINE_VISIBILITY165 value_type& value() const166 {167 return static_cast<_Derived const*>(this)->__ptr_->__value_;168 }169};170 171template <class _NodeType, class _Derived>172struct __map_node_handle_specifics173{174 typedef typename _NodeType::__node_value_type::key_type key_type;175 typedef typename _NodeType::__node_value_type::mapped_type mapped_type;176 177 _LIBCUDACXX_INLINE_VISIBILITY178 key_type& key() const179 {180 return static_cast<_Derived const*>(this)->181 __ptr_->__value_.__ref().first;182 }183 184 _LIBCUDACXX_INLINE_VISIBILITY185 mapped_type& mapped() const186 {187 return static_cast<_Derived const*>(this)->188 __ptr_->__value_.__ref().second;189 }190};191 192template <class _NodeType, class _Alloc>193using __set_node_handle =194 __basic_node_handle< _NodeType, _Alloc, __set_node_handle_specifics>;195 196template <class _NodeType, class _Alloc>197using __map_node_handle =198 __basic_node_handle< _NodeType, _Alloc, __map_node_handle_specifics>;199 200template <class _Iterator, class _NodeType>201struct _LIBCUDACXX_TEMPLATE_VIS __insert_return_type202{203 _Iterator position;204 bool inserted;205 _NodeType node;206};207 208#endif // _LIBCUDACXX_STD_VER > 14209 210_LIBCUDACXX_END_NAMESPACE_STD211_LIBCUDACXX_POP_MACROS212 213#endif214 