__node_handle 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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. #ifndef _LIBCPP___NODE_HANDLE
  10. #define _LIBCPP___NODE_HANDLE
  11. /*
  12. template<unspecified>
  13. class node-handle {
  14. public:
  15. using value_type = see below; // not present for map containers
  16. using key_type = see below; // not present for set containers
  17. using mapped_type = see below; // not present for set containers
  18. using allocator_type = see below;
  19. private:
  20. using container_node_type = unspecified; // exposition only
  21. using ator_traits = allocator_traits<allocator_type>; // exposition only
  22. typename ator_traits::template
  23. rebind_traits<container_node_type>::pointer ptr_; // exposition only
  24. optional<allocator_type> alloc_; // exposition only
  25. public:
  26. // [container.node.cons], constructors, copy, and assignment
  27. constexpr node-handle() noexcept : ptr_(), alloc_() {}
  28. node-handle(node-handle&&) noexcept;
  29. node-handle& operator=(node-handle&&);
  30. // [container.node.dtor], destructor
  31. ~node-handle();
  32. // [container.node.observers], observers
  33. value_type& value() const; // not present for map containers
  34. key_type& key() const; // not present for set containers
  35. mapped_type& mapped() const; // not present for set containers
  36. allocator_type get_allocator() const;
  37. explicit operator bool() const noexcept;
  38. [[nodiscard]] bool empty() const noexcept; // nodiscard since C++20
  39. // [container.node.modifiers], modifiers
  40. void swap(node-handle&)
  41. noexcept(ator_traits::propagate_on_container_swap::value ||
  42. ator_traits::is_always_equal::value);
  43. friend void swap(node-handle& x, node-handle& y) noexcept(noexcept(x.swap(y))) {
  44. x.swap(y);
  45. }
  46. };
  47. */
  48. #include <__assert>
  49. #include <__config>
  50. #include <memory>
  51. #include <optional>
  52. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  53. # pragma GCC system_header
  54. #endif
  55. _LIBCPP_BEGIN_NAMESPACE_STD
  56. #if _LIBCPP_STD_VER > 14
  57. // Specialized in __tree & __hash_table for their _NodeType.
  58. template <class _NodeType, class _Alloc>
  59. struct __generic_container_node_destructor;
  60. template <class _NodeType, class _Alloc,
  61. template <class, class> class _MapOrSetSpecifics>
  62. class _LIBCPP_TEMPLATE_VIS __basic_node_handle
  63. : public _MapOrSetSpecifics<
  64. _NodeType,
  65. __basic_node_handle<_NodeType, _Alloc, _MapOrSetSpecifics>>
  66. {
  67. template <class _Tp, class _Compare, class _Allocator>
  68. friend class __tree;
  69. template <class _Tp, class _Hash, class _Equal, class _Allocator>
  70. friend class __hash_table;
  71. friend struct _MapOrSetSpecifics<
  72. _NodeType, __basic_node_handle<_NodeType, _Alloc, _MapOrSetSpecifics>>;
  73. typedef allocator_traits<_Alloc> __alloc_traits;
  74. typedef typename __rebind_pointer<typename __alloc_traits::void_pointer,
  75. _NodeType>::type
  76. __node_pointer_type;
  77. public:
  78. typedef _Alloc allocator_type;
  79. private:
  80. __node_pointer_type __ptr_ = nullptr;
  81. optional<allocator_type> __alloc_;
  82. _LIBCPP_INLINE_VISIBILITY
  83. void __release_ptr()
  84. {
  85. __ptr_ = nullptr;
  86. __alloc_ = _VSTD::nullopt;
  87. }
  88. _LIBCPP_INLINE_VISIBILITY
  89. void __destroy_node_pointer()
  90. {
  91. if (__ptr_ != nullptr)
  92. {
  93. typedef typename __allocator_traits_rebind<
  94. allocator_type, _NodeType>::type __node_alloc_type;
  95. __node_alloc_type __alloc(*__alloc_);
  96. __generic_container_node_destructor<_NodeType, __node_alloc_type>(
  97. __alloc, true)(__ptr_);
  98. __ptr_ = nullptr;
  99. }
  100. }
  101. _LIBCPP_INLINE_VISIBILITY
  102. __basic_node_handle(__node_pointer_type __ptr,
  103. allocator_type const& __alloc)
  104. : __ptr_(__ptr), __alloc_(__alloc)
  105. {
  106. }
  107. public:
  108. _LIBCPP_INLINE_VISIBILITY
  109. __basic_node_handle() = default;
  110. _LIBCPP_INLINE_VISIBILITY
  111. __basic_node_handle(__basic_node_handle&& __other) noexcept
  112. : __ptr_(__other.__ptr_),
  113. __alloc_(_VSTD::move(__other.__alloc_))
  114. {
  115. __other.__ptr_ = nullptr;
  116. __other.__alloc_ = _VSTD::nullopt;
  117. }
  118. _LIBCPP_INLINE_VISIBILITY
  119. __basic_node_handle& operator=(__basic_node_handle&& __other)
  120. {
  121. _LIBCPP_ASSERT(
  122. __alloc_ == _VSTD::nullopt ||
  123. __alloc_traits::propagate_on_container_move_assignment::value ||
  124. __alloc_ == __other.__alloc_,
  125. "node_type with incompatible allocator passed to "
  126. "node_type::operator=(node_type&&)");
  127. __destroy_node_pointer();
  128. __ptr_ = __other.__ptr_;
  129. if (__alloc_traits::propagate_on_container_move_assignment::value ||
  130. __alloc_ == _VSTD::nullopt)
  131. __alloc_ = _VSTD::move(__other.__alloc_);
  132. __other.__ptr_ = nullptr;
  133. __other.__alloc_ = _VSTD::nullopt;
  134. return *this;
  135. }
  136. _LIBCPP_INLINE_VISIBILITY
  137. allocator_type get_allocator() const { return *__alloc_; }
  138. _LIBCPP_INLINE_VISIBILITY
  139. explicit operator bool() const { return __ptr_ != nullptr; }
  140. _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
  141. bool empty() const { return __ptr_ == nullptr; }
  142. _LIBCPP_INLINE_VISIBILITY
  143. void swap(__basic_node_handle& __other) noexcept(
  144. __alloc_traits::propagate_on_container_swap::value ||
  145. __alloc_traits::is_always_equal::value)
  146. {
  147. using _VSTD::swap;
  148. swap(__ptr_, __other.__ptr_);
  149. if (__alloc_traits::propagate_on_container_swap::value ||
  150. __alloc_ == _VSTD::nullopt || __other.__alloc_ == _VSTD::nullopt)
  151. swap(__alloc_, __other.__alloc_);
  152. }
  153. _LIBCPP_INLINE_VISIBILITY
  154. friend void swap(__basic_node_handle& __a, __basic_node_handle& __b)
  155. noexcept(noexcept(__a.swap(__b))) { __a.swap(__b); }
  156. _LIBCPP_INLINE_VISIBILITY
  157. ~__basic_node_handle()
  158. {
  159. __destroy_node_pointer();
  160. }
  161. };
  162. template <class _NodeType, class _Derived>
  163. struct __set_node_handle_specifics
  164. {
  165. typedef typename _NodeType::__node_value_type value_type;
  166. _LIBCPP_INLINE_VISIBILITY
  167. value_type& value() const
  168. {
  169. return static_cast<_Derived const*>(this)->__ptr_->__value_;
  170. }
  171. };
  172. template <class _NodeType, class _Derived>
  173. struct __map_node_handle_specifics
  174. {
  175. typedef typename _NodeType::__node_value_type::key_type key_type;
  176. typedef typename _NodeType::__node_value_type::mapped_type mapped_type;
  177. _LIBCPP_INLINE_VISIBILITY
  178. key_type& key() const
  179. {
  180. return static_cast<_Derived const*>(this)->
  181. __ptr_->__value_.__ref().first;
  182. }
  183. _LIBCPP_INLINE_VISIBILITY
  184. mapped_type& mapped() const
  185. {
  186. return static_cast<_Derived const*>(this)->
  187. __ptr_->__value_.__ref().second;
  188. }
  189. };
  190. template <class _NodeType, class _Alloc>
  191. using __set_node_handle =
  192. __basic_node_handle< _NodeType, _Alloc, __set_node_handle_specifics>;
  193. template <class _NodeType, class _Alloc>
  194. using __map_node_handle =
  195. __basic_node_handle< _NodeType, _Alloc, __map_node_handle_specifics>;
  196. template <class _Iterator, class _NodeType>
  197. struct _LIBCPP_TEMPLATE_VIS __insert_return_type
  198. {
  199. _Iterator position;
  200. bool inserted;
  201. _NodeType node;
  202. };
  203. #endif // _LIBCPP_STD_VER > 14
  204. _LIBCPP_END_NAMESPACE_STD
  205. #endif // _LIBCPP___NODE_HANDLE