node_handle.hpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. /* Copyright 2003-2022 Joaquin M Lopez Munoz.
  2. * Distributed under the Boost Software License, Version 1.0.
  3. * (See accompanying file LICENSE_1_0.txt or copy at
  4. * http://www.boost.org/LICENSE_1_0.txt)
  5. *
  6. * See http://www.boost.org/libs/multi_index for library home page.
  7. */
  8. #ifndef BOOST_MULTI_INDEX_DETAIL_NODE_HANDLE_HPP
  9. #define BOOST_MULTI_INDEX_DETAIL_NODE_HANDLE_HPP
  10. #if defined(_MSC_VER)
  11. #pragma once
  12. #endif
  13. #include <boost/config.hpp> /* keep it first to prevent nasty warns in MSVC */
  14. #include <algorithm>
  15. #include <boost/core/addressof.hpp>
  16. #include <boost/detail/workaround.hpp>
  17. #include <boost/move/core.hpp>
  18. #include <boost/move/utility_core.hpp>
  19. #include <boost/multi_index_container_fwd.hpp>
  20. #include <boost/multi_index/detail/allocator_traits.hpp>
  21. #include <boost/type_traits/aligned_storage.hpp>
  22. #include <boost/type_traits/alignment_of.hpp>
  23. #include <new>
  24. #if !defined(BOOST_NO_SFINAE)
  25. #include <boost/core/enable_if.hpp>
  26. #include <boost/type_traits/is_const.hpp>
  27. #include <boost/type_traits/is_same.hpp>
  28. #endif
  29. namespace boost{
  30. namespace multi_index{
  31. namespace detail{
  32. /* Node handle template class following [container.node] specs.
  33. */
  34. #include <boost/multi_index/detail/define_if_constexpr_macro.hpp>
  35. template<typename Node,typename Allocator>
  36. class node_handle
  37. {
  38. public:
  39. typedef typename Node::value_type value_type;
  40. typedef Allocator allocator_type;
  41. private:
  42. typedef allocator_traits<allocator_type> alloc_traits;
  43. public:
  44. node_handle()BOOST_NOEXCEPT:node(0){}
  45. node_handle(BOOST_RV_REF(node_handle) x)BOOST_NOEXCEPT:node(x.node)
  46. {
  47. if(!x.empty()){
  48. move_construct_allocator(boost::move(x));
  49. x.destroy_allocator();
  50. x.node=0;
  51. }
  52. }
  53. ~node_handle()
  54. {
  55. if(!empty()){
  56. delete_node();
  57. destroy_allocator();
  58. }
  59. }
  60. node_handle& operator=(BOOST_RV_REF(node_handle) x)
  61. {
  62. if(this!=&x){
  63. if(!empty()){
  64. delete_node();
  65. if(!x.empty()){
  66. BOOST_MULTI_INDEX_IF_CONSTEXPR(
  67. alloc_traits::propagate_on_container_move_assignment::value){
  68. move_assign_allocator(boost::move(x));
  69. }
  70. x.destroy_allocator();
  71. }
  72. else{
  73. destroy_allocator();
  74. }
  75. }
  76. else if(!x.empty()){
  77. move_construct_allocator(boost::move(x));
  78. x.destroy_allocator();
  79. }
  80. node=x.node;
  81. x.node=0;
  82. }
  83. return *this;
  84. }
  85. value_type& value()const{return node->value();}
  86. allocator_type get_allocator()const{return *allocator_ptr();}
  87. #if !defined(BOOST_NO_CXX11_EXPLICIT_CONVERSION_OPERATORS)
  88. explicit
  89. #endif
  90. operator bool()const BOOST_NOEXCEPT{return (node!=0);}
  91. #if BOOST_WORKAROUND(BOOST_GCC_VERSION,>=70000)&&__cplusplus<201103L
  92. /* https://github.com/boostorg/config/issues/336 */
  93. #else
  94. BOOST_ATTRIBUTE_NODISCARD
  95. #endif
  96. bool empty()const BOOST_NOEXCEPT{return (node==0);}
  97. void swap(node_handle& x)
  98. BOOST_NOEXCEPT_IF(
  99. alloc_traits::propagate_on_container_swap::value||
  100. alloc_traits::is_always_equal::value)
  101. {
  102. if(!empty()){
  103. if(!x.empty()){
  104. BOOST_MULTI_INDEX_IF_CONSTEXPR(
  105. alloc_traits::propagate_on_container_swap::value){
  106. using std::swap;
  107. swap(*allocator_ptr(),*x.allocator_ptr());
  108. }
  109. }
  110. else{
  111. x.move_construct_allocator(boost::move(*this));
  112. destroy_allocator();
  113. }
  114. }
  115. else if(!x.empty()){
  116. move_construct_allocator(boost::move(x));
  117. x.destroy_allocator();
  118. }
  119. std::swap(node,x.node);
  120. }
  121. friend void swap(node_handle& x,node_handle& y)
  122. BOOST_NOEXCEPT_IF(noexcept(x.swap(y)))
  123. {
  124. x.swap(y);
  125. }
  126. private:
  127. BOOST_MOVABLE_BUT_NOT_COPYABLE(node_handle)
  128. template <typename,typename,typename>
  129. friend class boost::multi_index::multi_index_container;
  130. node_handle(Node* node_,const allocator_type& al):node(node_)
  131. {
  132. ::new (static_cast<void*>(allocator_ptr())) allocator_type(al);
  133. }
  134. void release_node()
  135. {
  136. if(!empty()){
  137. node=0;
  138. destroy_allocator();
  139. }
  140. }
  141. #include <boost/multi_index/detail/ignore_wstrict_aliasing.hpp>
  142. const allocator_type* allocator_ptr()const
  143. {
  144. return reinterpret_cast<const allocator_type*>(&space);
  145. }
  146. allocator_type* allocator_ptr()
  147. {
  148. return reinterpret_cast<allocator_type*>(&space);
  149. }
  150. #include <boost/multi_index/detail/restore_wstrict_aliasing.hpp>
  151. void move_construct_allocator(BOOST_RV_REF(node_handle) x)
  152. {
  153. ::new (static_cast<void*>(allocator_ptr()))
  154. allocator_type(boost::move(*x.allocator_ptr()));
  155. }
  156. void move_assign_allocator(BOOST_RV_REF(node_handle) x)
  157. {
  158. *allocator_ptr()=boost::move(*x.allocator_ptr());
  159. }
  160. void destroy_allocator(){allocator_ptr()->~allocator_type();}
  161. void delete_node()
  162. {
  163. typedef typename rebind_alloc_for<
  164. allocator_type,Node
  165. >::type node_allocator;
  166. typedef detail::allocator_traits<node_allocator> node_alloc_traits;
  167. typedef typename node_alloc_traits::pointer node_pointer;
  168. alloc_traits::destroy(*allocator_ptr(),boost::addressof(node->value()));
  169. node_allocator nal(*allocator_ptr());
  170. node_alloc_traits::deallocate(nal,static_cast<node_pointer>(node),1);
  171. }
  172. Node* node;
  173. typename aligned_storage<
  174. sizeof(allocator_type),
  175. alignment_of<allocator_type>::value
  176. >::type space;
  177. };
  178. #include <boost/multi_index/detail/undef_if_constexpr_macro.hpp>
  179. /* node handle insert return type template class following
  180. * [container.insert.return] specs.
  181. */
  182. template<typename Iterator,typename NodeHandle>
  183. struct insert_return_type
  184. {
  185. insert_return_type(
  186. Iterator position_,bool inserted_,BOOST_RV_REF(NodeHandle) node_):
  187. position(position_),inserted(inserted_),node(boost::move(node_)){}
  188. insert_return_type(BOOST_RV_REF(insert_return_type) x):
  189. position(x.position),inserted(x.inserted),node(boost::move(x.node)){}
  190. insert_return_type& operator=(BOOST_RV_REF(insert_return_type) x)
  191. {
  192. position=x.position;
  193. inserted=x.inserted;
  194. node=boost::move(x.node);
  195. return *this;
  196. }
  197. Iterator position;
  198. bool inserted;
  199. NodeHandle node;
  200. private:
  201. BOOST_MOVABLE_BUT_NOT_COPYABLE(insert_return_type)
  202. };
  203. /* utility for SFINAEing merge and related operations */
  204. #if !defined(BOOST_NO_SFINAE)
  205. #define BOOST_MULTI_INDEX_ENABLE_IF_MERGEABLE(Dst,Src,T) \
  206. typename enable_if_c< \
  207. !is_const< Dst >::value&&!is_const< Src >::value&& \
  208. is_same<typename Dst::node_type,typename Src::node_type>::value, \
  209. T \
  210. >::type
  211. #else
  212. #define BOOST_MULTI_INDEX_ENABLE_IF_MERGEABLE(Dst,Src,T) T
  213. #endif
  214. } /* namespace multi_index::detail */
  215. } /* namespace multi_index */
  216. } /* namespace boost */
  217. #endif