__node_handle 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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/allocator_traits.h>
  51. #include <__memory/pointer_traits.h>
  52. #include <optional>
  53. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  54. # pragma GCC system_header
  55. #endif
  56. _LIBCPP_BEGIN_NAMESPACE_STD
  57. #if _LIBCPP_STD_VER > 14
  58. // Specialized in __tree & __hash_table for their _NodeType.
  59. template <class _NodeType, class _Alloc>
  60. struct __generic_container_node_destructor;
  61. template <class _NodeType, class _Alloc,
  62. template <class, class> class _MapOrSetSpecifics>
  63. class _LIBCPP_TEMPLATE_VIS __basic_node_handle
  64. : public _MapOrSetSpecifics<
  65. _NodeType,
  66. __basic_node_handle<_NodeType, _Alloc, _MapOrSetSpecifics>>
  67. {
  68. template <class _Tp, class _Compare, class _Allocator>
  69. friend class __tree;
  70. template <class _Tp, class _Hash, class _Equal, class _Allocator>
  71. friend class __hash_table;
  72. friend struct _MapOrSetSpecifics<
  73. _NodeType, __basic_node_handle<_NodeType, _Alloc, _MapOrSetSpecifics>>;
  74. typedef allocator_traits<_Alloc> __alloc_traits;
  75. typedef __rebind_pointer_t<typename __alloc_traits::void_pointer,
  76. _NodeType>
  77. __node_pointer_type;
  78. public:
  79. typedef _Alloc allocator_type;
  80. private:
  81. __node_pointer_type __ptr_ = nullptr;
  82. optional<allocator_type> __alloc_;
  83. _LIBCPP_INLINE_VISIBILITY
  84. void __release_ptr()
  85. {
  86. __ptr_ = nullptr;
  87. __alloc_ = _VSTD::nullopt;
  88. }
  89. _LIBCPP_INLINE_VISIBILITY
  90. void __destroy_node_pointer()
  91. {
  92. if (__ptr_ != nullptr)
  93. {
  94. typedef typename __allocator_traits_rebind<
  95. allocator_type, _NodeType>::type __node_alloc_type;
  96. __node_alloc_type __alloc(*__alloc_);
  97. __generic_container_node_destructor<_NodeType, __node_alloc_type>(
  98. __alloc, true)(__ptr_);
  99. __ptr_ = nullptr;
  100. }
  101. }
  102. _LIBCPP_INLINE_VISIBILITY
  103. __basic_node_handle(__node_pointer_type __ptr,
  104. allocator_type const& __alloc)
  105. : __ptr_(__ptr), __alloc_(__alloc)
  106. {
  107. }
  108. public:
  109. _LIBCPP_INLINE_VISIBILITY
  110. __basic_node_handle() = default;
  111. _LIBCPP_INLINE_VISIBILITY
  112. __basic_node_handle(__basic_node_handle&& __other) noexcept
  113. : __ptr_(__other.__ptr_),
  114. __alloc_(_VSTD::move(__other.__alloc_))
  115. {
  116. __other.__ptr_ = nullptr;
  117. __other.__alloc_ = _VSTD::nullopt;
  118. }
  119. _LIBCPP_INLINE_VISIBILITY
  120. __basic_node_handle& operator=(__basic_node_handle&& __other)
  121. {
  122. _LIBCPP_ASSERT(
  123. __alloc_ == _VSTD::nullopt ||
  124. __alloc_traits::propagate_on_container_move_assignment::value ||
  125. __alloc_ == __other.__alloc_,
  126. "node_type with incompatible allocator passed to "
  127. "node_type::operator=(node_type&&)");
  128. __destroy_node_pointer();
  129. __ptr_ = __other.__ptr_;
  130. if (__alloc_traits::propagate_on_container_move_assignment::value ||
  131. __alloc_ == _VSTD::nullopt)
  132. __alloc_ = _VSTD::move(__other.__alloc_);
  133. __other.__ptr_ = nullptr;
  134. __other.__alloc_ = _VSTD::nullopt;
  135. return *this;
  136. }
  137. _LIBCPP_INLINE_VISIBILITY
  138. allocator_type get_allocator() const { return *__alloc_; }
  139. _LIBCPP_INLINE_VISIBILITY
  140. explicit operator bool() const { return __ptr_ != nullptr; }
  141. _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
  142. bool empty() const { return __ptr_ == nullptr; }
  143. _LIBCPP_INLINE_VISIBILITY
  144. void swap(__basic_node_handle& __other) noexcept(
  145. __alloc_traits::propagate_on_container_swap::value ||
  146. __alloc_traits::is_always_equal::value)
  147. {
  148. using _VSTD::swap;
  149. swap(__ptr_, __other.__ptr_);
  150. if (__alloc_traits::propagate_on_container_swap::value ||
  151. __alloc_ == _VSTD::nullopt || __other.__alloc_ == _VSTD::nullopt)
  152. swap(__alloc_, __other.__alloc_);
  153. }
  154. _LIBCPP_INLINE_VISIBILITY
  155. friend void swap(__basic_node_handle& __a, __basic_node_handle& __b)
  156. noexcept(noexcept(__a.swap(__b))) { __a.swap(__b); }
  157. _LIBCPP_INLINE_VISIBILITY
  158. ~__basic_node_handle()
  159. {
  160. __destroy_node_pointer();
  161. }
  162. };
  163. template <class _NodeType, class _Derived>
  164. struct __set_node_handle_specifics
  165. {
  166. typedef typename _NodeType::__node_value_type value_type;
  167. _LIBCPP_INLINE_VISIBILITY
  168. value_type& value() const
  169. {
  170. return static_cast<_Derived const*>(this)->__ptr_->__value_;
  171. }
  172. };
  173. template <class _NodeType, class _Derived>
  174. struct __map_node_handle_specifics
  175. {
  176. typedef typename _NodeType::__node_value_type::key_type key_type;
  177. typedef typename _NodeType::__node_value_type::mapped_type mapped_type;
  178. _LIBCPP_INLINE_VISIBILITY
  179. key_type& key() const
  180. {
  181. return static_cast<_Derived const*>(this)->
  182. __ptr_->__value_.__ref().first;
  183. }
  184. _LIBCPP_INLINE_VISIBILITY
  185. mapped_type& mapped() const
  186. {
  187. return static_cast<_Derived const*>(this)->
  188. __ptr_->__value_.__ref().second;
  189. }
  190. };
  191. template <class _NodeType, class _Alloc>
  192. using __set_node_handle =
  193. __basic_node_handle< _NodeType, _Alloc, __set_node_handle_specifics>;
  194. template <class _NodeType, class _Alloc>
  195. using __map_node_handle =
  196. __basic_node_handle< _NodeType, _Alloc, __map_node_handle_specifics>;
  197. template <class _Iterator, class _NodeType>
  198. struct _LIBCPP_TEMPLATE_VIS __insert_return_type
  199. {
  200. _Iterator position;
  201. bool inserted;
  202. _NodeType node;
  203. };
  204. #endif // _LIBCPP_STD_VER > 14
  205. _LIBCPP_END_NAMESPACE_STD
  206. #endif // _LIBCPP___NODE_HANDLE