__node_handle 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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_PUSH_MACROS
  57. #include <__undef_macros>
  58. _LIBCPP_BEGIN_NAMESPACE_STD
  59. #if _LIBCPP_STD_VER >= 17
  60. // Specialized in __tree & __hash_table for their _NodeType.
  61. template <class _NodeType, class _Alloc>
  62. struct __generic_container_node_destructor;
  63. template <class _NodeType, class _Alloc, template <class, class> class _MapOrSetSpecifics>
  64. class _LIBCPP_TEMPLATE_VIS __basic_node_handle
  65. : public _MapOrSetSpecifics< _NodeType, __basic_node_handle<_NodeType, _Alloc, _MapOrSetSpecifics>> {
  66. template <class _Tp, class _Compare, class _Allocator>
  67. friend class __tree;
  68. template <class _Tp, class _Hash, class _Equal, class _Allocator>
  69. friend class __hash_table;
  70. friend struct _MapOrSetSpecifics< _NodeType, __basic_node_handle<_NodeType, _Alloc, _MapOrSetSpecifics>>;
  71. typedef allocator_traits<_Alloc> __alloc_traits;
  72. typedef __rebind_pointer_t<typename __alloc_traits::void_pointer, _NodeType> __node_pointer_type;
  73. public:
  74. typedef _Alloc allocator_type;
  75. private:
  76. __node_pointer_type __ptr_ = nullptr;
  77. optional<allocator_type> __alloc_;
  78. _LIBCPP_HIDE_FROM_ABI void __release_ptr() {
  79. __ptr_ = nullptr;
  80. __alloc_ = std::nullopt;
  81. }
  82. _LIBCPP_HIDE_FROM_ABI void __destroy_node_pointer() {
  83. if (__ptr_ != nullptr) {
  84. typedef typename __allocator_traits_rebind< allocator_type, _NodeType>::type __node_alloc_type;
  85. __node_alloc_type __alloc(*__alloc_);
  86. __generic_container_node_destructor<_NodeType, __node_alloc_type>(__alloc, true)(__ptr_);
  87. __ptr_ = nullptr;
  88. }
  89. }
  90. _LIBCPP_HIDE_FROM_ABI __basic_node_handle(__node_pointer_type __ptr, allocator_type const& __alloc)
  91. : __ptr_(__ptr), __alloc_(__alloc) {}
  92. public:
  93. _LIBCPP_HIDE_FROM_ABI __basic_node_handle() = default;
  94. _LIBCPP_HIDE_FROM_ABI __basic_node_handle(__basic_node_handle&& __other) noexcept
  95. : __ptr_(__other.__ptr_), __alloc_(std::move(__other.__alloc_)) {
  96. __other.__ptr_ = nullptr;
  97. __other.__alloc_ = std::nullopt;
  98. }
  99. _LIBCPP_HIDE_FROM_ABI __basic_node_handle& operator=(__basic_node_handle&& __other) {
  100. _LIBCPP_ASSERT_COMPATIBLE_ALLOCATOR(
  101. __alloc_ == std::nullopt || __alloc_traits::propagate_on_container_move_assignment::value ||
  102. __alloc_ == __other.__alloc_,
  103. "node_type with incompatible allocator passed to "
  104. "node_type::operator=(node_type&&)");
  105. __destroy_node_pointer();
  106. __ptr_ = __other.__ptr_;
  107. if (__alloc_traits::propagate_on_container_move_assignment::value || __alloc_ == std::nullopt)
  108. __alloc_ = std::move(__other.__alloc_);
  109. __other.__ptr_ = nullptr;
  110. __other.__alloc_ = std::nullopt;
  111. return *this;
  112. }
  113. _LIBCPP_HIDE_FROM_ABI allocator_type get_allocator() const { return *__alloc_; }
  114. _LIBCPP_HIDE_FROM_ABI explicit operator bool() const { return __ptr_ != nullptr; }
  115. _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_HIDE_FROM_ABI bool empty() const { return __ptr_ == nullptr; }
  116. _LIBCPP_HIDE_FROM_ABI void swap(__basic_node_handle& __other) noexcept(
  117. __alloc_traits::propagate_on_container_swap::value || __alloc_traits::is_always_equal::value) {
  118. using std::swap;
  119. swap(__ptr_, __other.__ptr_);
  120. if (__alloc_traits::propagate_on_container_swap::value || __alloc_ == std::nullopt ||
  121. __other.__alloc_ == std::nullopt)
  122. swap(__alloc_, __other.__alloc_);
  123. }
  124. _LIBCPP_HIDE_FROM_ABI friend void
  125. swap(__basic_node_handle& __a, __basic_node_handle& __b) noexcept(noexcept(__a.swap(__b))) {
  126. __a.swap(__b);
  127. }
  128. _LIBCPP_HIDE_FROM_ABI ~__basic_node_handle() { __destroy_node_pointer(); }
  129. };
  130. template <class _NodeType, class _Derived>
  131. struct __set_node_handle_specifics {
  132. typedef typename _NodeType::__node_value_type value_type;
  133. _LIBCPP_HIDE_FROM_ABI value_type& value() const { return static_cast<_Derived const*>(this)->__ptr_->__get_value(); }
  134. };
  135. template <class _NodeType, class _Derived>
  136. struct __map_node_handle_specifics {
  137. typedef typename _NodeType::__node_value_type::key_type key_type;
  138. typedef typename _NodeType::__node_value_type::mapped_type mapped_type;
  139. _LIBCPP_HIDE_FROM_ABI key_type& key() const {
  140. return static_cast<_Derived const*>(this)->__ptr_->__get_value().__ref().first;
  141. }
  142. _LIBCPP_HIDE_FROM_ABI mapped_type& mapped() const {
  143. return static_cast<_Derived const*>(this)->__ptr_->__get_value().__ref().second;
  144. }
  145. };
  146. template <class _NodeType, class _Alloc>
  147. using __set_node_handle = __basic_node_handle< _NodeType, _Alloc, __set_node_handle_specifics>;
  148. template <class _NodeType, class _Alloc>
  149. using __map_node_handle = __basic_node_handle< _NodeType, _Alloc, __map_node_handle_specifics>;
  150. template <class _Iterator, class _NodeType>
  151. struct _LIBCPP_TEMPLATE_VIS __insert_return_type {
  152. _Iterator position;
  153. bool inserted;
  154. _NodeType node;
  155. };
  156. #endif // _LIBCPP_STD_VER >= 17
  157. _LIBCPP_END_NAMESPACE_STD
  158. _LIBCPP_POP_MACROS
  159. #endif // _LIBCPP___NODE_HANDLE