node_handle.hpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. //////////////////////////////////////////////////////////////////////////////
  2. //
  3. // (C) Copyright Ion Gaztanaga 2016-2016. Distributed under the Boost
  4. // Software License, Version 1.0. (See accompanying file
  5. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. //
  7. // See http://www.boost.org/libs/container for documentation.
  8. //
  9. //////////////////////////////////////////////////////////////////////////////
  10. #ifndef BOOST_CONTAINER_NODE_HANDLE_HPP
  11. #define BOOST_CONTAINER_NODE_HANDLE_HPP
  12. #ifndef BOOST_CONFIG_HPP
  13. # include <boost/config.hpp>
  14. #endif
  15. #if defined(BOOST_HAS_PRAGMA_ONCE)
  16. # pragma once
  17. #endif
  18. #include <boost/container/detail/config_begin.hpp>
  19. #include <boost/container/detail/workaround.hpp>
  20. #include <boost/container/detail/placement_new.hpp>
  21. #include <boost/move/detail/to_raw_pointer.hpp>
  22. #include <boost/container/allocator_traits.hpp>
  23. #include <boost/container/detail/mpl.hpp>
  24. #include <boost/move/utility_core.hpp>
  25. #include <boost/move/adl_move_swap.hpp>
  26. #include <boost/container/detail/mpl.hpp>
  27. #include <boost/assert.hpp>
  28. //GCC 12 is confused about maybe uninitialized allocators
  29. #if defined(BOOST_GCC) && (BOOST_GCC >= 120000) && (BOOST_GCC < 130000)
  30. #pragma GCC diagnostic push
  31. #pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
  32. #endif
  33. //!\file
  34. namespace boost {
  35. namespace container {
  36. ///@cond
  37. template<class Value, class KeyMapped>
  38. struct node_handle_keymapped_traits
  39. {
  40. typedef typename KeyMapped::key_type key_type;
  41. typedef typename KeyMapped::mapped_type mapped_type;
  42. };
  43. template<class Value>
  44. struct node_handle_keymapped_traits<Value, void>
  45. {
  46. typedef Value key_type;
  47. typedef Value mapped_type;
  48. };
  49. class node_handle_friend
  50. {
  51. public:
  52. template<class NH>
  53. inline static void destroy_alloc(NH &nh) BOOST_NOEXCEPT
  54. { nh.destroy_alloc(); }
  55. template<class NH>
  56. inline static typename NH::node_pointer &get_node_pointer(NH &nh) BOOST_NOEXCEPT
  57. { return nh.get_node_pointer(); }
  58. };
  59. ///@endcond
  60. //! A node_handle is an object that accepts ownership of a single element from an associative container.
  61. //! It may be used to transfer that ownership to another container with compatible nodes. Containers
  62. //! with compatible nodes have the same node handle type. Elements may be transferred in either direction
  63. //! between container types in the same row:.
  64. //!
  65. //! Container types with compatible nodes
  66. //!
  67. //! map<K, T, C1, A> <-> map<K, T, C2, A>
  68. //!
  69. //! map<K, T, C1, A> <-> multimap<K, T, C2, A>
  70. //!
  71. //! set<K, C1, A> <-> set<K, C2, A>
  72. //!
  73. //! set<K, C1, A> <-> multiset<K, C2, A>
  74. //!
  75. //! If a node handle is not empty, then it contains an allocator that is equal to the allocator of the container
  76. //! when the element was extracted. If a node handle is empty, it contains no allocator.
  77. template <class NodeAllocator, class KeyMapped = void>
  78. class node_handle
  79. {
  80. typedef NodeAllocator nallocator_type;
  81. typedef allocator_traits<NodeAllocator> nator_traits;
  82. typedef typename nator_traits::value_type priv_node_t;
  83. typedef typename priv_node_t::value_type priv_value_t;
  84. typedef node_handle_keymapped_traits<priv_value_t, KeyMapped> keymapped_t;
  85. public:
  86. typedef priv_value_t value_type;
  87. typedef typename keymapped_t::key_type key_type;
  88. typedef typename keymapped_t::mapped_type mapped_type;
  89. typedef typename nator_traits::template portable_rebind_alloc
  90. <value_type>::type allocator_type;
  91. typedef priv_node_t container_node_type;
  92. friend class node_handle_friend;
  93. ///@cond
  94. private:
  95. BOOST_MOVABLE_BUT_NOT_COPYABLE(node_handle)
  96. typedef typename nator_traits::pointer node_pointer;
  97. typedef typename dtl::aligned_storage
  98. < sizeof(nallocator_type)
  99. , dtl::alignment_of<nallocator_type>::value
  100. >::type nalloc_storage_t;
  101. node_pointer m_ptr;
  102. nalloc_storage_t m_nalloc_storage;
  103. void move_construct_alloc(nallocator_type &al)
  104. { ::new((void*)m_nalloc_storage.data, boost_container_new_t()) nallocator_type(::boost::move(al)); }
  105. void destroy_deallocate_node()
  106. {
  107. boost::movelib::to_raw_pointer(m_ptr)->destructor(this->node_alloc());
  108. nator_traits::deallocate(this->node_alloc(), m_ptr, 1u);
  109. }
  110. template<class OtherNodeHandle>
  111. void move_construct_end(OtherNodeHandle &nh)
  112. {
  113. if(m_ptr){
  114. ::new ((void*)m_nalloc_storage.data, boost_container_new_t()) nallocator_type(::boost::move(nh.node_alloc()));
  115. node_handle_friend::destroy_alloc(nh);
  116. node_handle_friend::get_node_pointer(nh) = node_pointer();
  117. }
  118. BOOST_ASSERT(nh.empty());
  119. }
  120. void destroy_alloc() BOOST_NOEXCEPT
  121. { static_cast<nallocator_type*>((void*)m_nalloc_storage.data)->~nallocator_type(); }
  122. node_pointer &get_node_pointer() BOOST_NOEXCEPT
  123. { return m_ptr; }
  124. ///@endcond
  125. public:
  126. //! <b>Effects</b>: Initializes m_ptr to nullptr.
  127. //!
  128. //! <b>Postcondition</b>: this->empty()
  129. BOOST_CXX14_CONSTEXPR node_handle() BOOST_NOEXCEPT
  130. : m_ptr()
  131. { }
  132. //! <b>Effects</b>: Constructs a node_handle object initializing internal pointer with p.
  133. //! If p != nullptr copy constructs internal allocator from al.
  134. node_handle(node_pointer p, const nallocator_type &al) BOOST_NOEXCEPT
  135. : m_ptr(p)
  136. {
  137. if(m_ptr){
  138. ::new ((void*)m_nalloc_storage.data, boost_container_new_t()) nallocator_type(al);
  139. }
  140. }
  141. //! <b>Effects</b>: Constructs a node_handle object initializing internal pointer with a related nh's internal pointer
  142. //! and assigns nullptr to the later. If nh's internal pointer was not nullptr, move constructs internal
  143. //! allocator with nh's internal allocator and destroy nh's internal allocator.
  144. //!
  145. //! <b>Postcondition</b>: nh.empty()
  146. //!
  147. //! <b>Note</b>: Two node_handle's are related if only one of KeyMapped template parameter
  148. //! of a node handle is void.
  149. template<class KeyMapped2>
  150. node_handle( BOOST_RV_REF_BEG node_handle<NodeAllocator, KeyMapped2> BOOST_RV_REF_END nh
  151. , typename dtl::enable_if_c
  152. < ((unsigned)dtl::is_same<KeyMapped, void>::value +
  153. (unsigned)dtl::is_same<KeyMapped2, void>::value) == 1u
  154. >::type* = 0) BOOST_NOEXCEPT
  155. : m_ptr(nh.get())
  156. { this->move_construct_end(nh); }
  157. //! <b>Effects</b>: Constructs a node_handle object initializing internal pointer with nh's internal pointer
  158. //! and assigns nullptr to the later. If nh's internal pointer was not nullptr, move constructs internal
  159. //! allocator with nh's internal allocator and destroy nh's internal allocator.
  160. //!
  161. //! <b>Postcondition</b>: nh.empty()
  162. node_handle (BOOST_RV_REF(node_handle) nh) BOOST_NOEXCEPT
  163. : m_ptr(nh.m_ptr)
  164. { this->move_construct_end(nh); }
  165. //! <b>Effects</b>: If !this->empty(), destroys the value_type subobject in the container_node_type object
  166. //! pointed to by c by calling allocator_traits<impl_defined>::destroy, then deallocates m_ptr by calling
  167. //! nator_traits::rebind_traits<container_node_type>::deallocate.
  168. ~node_handle() BOOST_NOEXCEPT
  169. {
  170. if(!this->empty()){
  171. this->destroy_deallocate_node();
  172. this->destroy_alloc();
  173. }
  174. }
  175. //! <b>Requires</b>: Either this->empty(), or nator_traits::propagate_on_container_move_assignment is true, or
  176. //! node_alloc() == nh.node_alloc().
  177. //!
  178. //! <b>Effects</b>: If m_ptr != nullptr, destroys the value_type subobject in the container_node_type object
  179. //! pointed to by m_ptr by calling nator_traits::destroy, then deallocates m_ptr by calling
  180. //! nator_traits::deallocate. Assigns nh.m_ptr to m_ptr. If this->empty()
  181. //! or nator_traits::propagate_on_container_move_assignment is true, move assigns nh.node_alloc() to
  182. //! node_alloc(). Assigns nullptr to nh.m_ptr and assigns nullopt to nh.node_alloc().
  183. //!
  184. //! <b>Returns</b>: *this.
  185. //!
  186. //! <b>Throws</b>: Nothing.
  187. node_handle & operator=(BOOST_RV_REF(node_handle) nh) BOOST_NOEXCEPT
  188. {
  189. BOOST_ASSERT(this->empty() || nator_traits::propagate_on_container_move_assignment::value
  190. || nator_traits::equal(node_alloc(), nh.node_alloc()));
  191. bool const was_this_non_null = !this->empty();
  192. bool const was_nh_non_null = !nh.empty();
  193. if(was_nh_non_null){
  194. if(was_this_non_null){
  195. this->destroy_deallocate_node();
  196. if(nator_traits::propagate_on_container_move_assignment::value){
  197. this->node_alloc() = ::boost::move(nh.node_alloc());
  198. }
  199. }
  200. else{
  201. this->move_construct_alloc(nh.node_alloc());
  202. }
  203. m_ptr = nh.m_ptr;
  204. nh.m_ptr = node_pointer();
  205. nh.destroy_alloc();
  206. }
  207. else if(was_this_non_null){
  208. this->destroy_deallocate_node();
  209. this->destroy_alloc();
  210. m_ptr = node_pointer();
  211. }
  212. return *this;
  213. }
  214. //! <b>Requires</b>: empty() == false.
  215. //!
  216. //! <b>Returns</b>: A reference to the value_type subobject in the container_node_type object pointed to by m_ptr
  217. //!
  218. //! <b>Throws</b>: Nothing.
  219. value_type& value() const BOOST_NOEXCEPT
  220. {
  221. BOOST_CONTAINER_STATIC_ASSERT((dtl::is_same<KeyMapped, void>::value));
  222. BOOST_ASSERT(!empty());
  223. return m_ptr->get_data();
  224. }
  225. //! <b>Requires</b>: empty() == false.
  226. //!
  227. //! <b>Returns</b>: A non-const reference to the key_type member of the value_type subobject in the
  228. //! container_node_type object pointed to by m_ptr.
  229. //!
  230. //! <b>Throws</b>: Nothing.
  231. //!
  232. //! <b>Requires</b>: Modifying the key through the returned reference is permitted.
  233. key_type& key() const BOOST_NOEXCEPT
  234. {
  235. BOOST_CONTAINER_STATIC_ASSERT((!dtl::is_same<KeyMapped, void>::value));
  236. BOOST_ASSERT(!empty());
  237. return const_cast<key_type &>(KeyMapped().key_of_value(m_ptr->get_data()));
  238. }
  239. //! <b>Requires</b>: empty() == false.
  240. //!
  241. //! <b>Returns</b>: A reference to the mapped_type member of the value_type subobject
  242. //! in the container_node_type object pointed to by m_ptr
  243. //!
  244. //! <b>Throws</b>: Nothing.
  245. mapped_type& mapped() const BOOST_NOEXCEPT
  246. {
  247. BOOST_CONTAINER_STATIC_ASSERT((!dtl::is_same<KeyMapped, void>::value));
  248. BOOST_ASSERT(!empty());
  249. return KeyMapped().mapped_of_value(m_ptr->get_data());
  250. }
  251. //! <b>Requires</b>: empty() == false.
  252. //!
  253. //! <b>Returns</b>: A copy of the internally hold allocator.
  254. //!
  255. //! <b>Throws</b>: Nothing.
  256. allocator_type get_allocator() const
  257. {
  258. BOOST_ASSERT(!empty());
  259. return this->node_alloc();
  260. }
  261. //! <b>Returns</b>: m_ptr != nullptr.
  262. //!
  263. #ifdef BOOST_CONTAINER_DOXYGEN_INVOKED
  264. inline explicit operator bool
  265. #else
  266. private: struct bool_conversion {int for_bool; int for_arg(); }; typedef int bool_conversion::* explicit_bool_arg;
  267. public: inline operator explicit_bool_arg
  268. #endif
  269. ()const BOOST_NOEXCEPT
  270. { return m_ptr ? &bool_conversion::for_bool : explicit_bool_arg(0); }
  271. //! <b>Returns</b>: m_ptr == nullptr.
  272. //!
  273. bool empty() const BOOST_NOEXCEPT
  274. {
  275. return !this->m_ptr;
  276. }
  277. //! <b>Requires</b>: this->empty(), or nh.empty(), or nator_traits::propagate_on_container_swap is true, or
  278. //! node_alloc() == nh.node_alloc().
  279. //!
  280. //! <b>Effects</b>: Calls swap(m_ptr, nh.m_ptr). If this->empty(), or nh.empty(), or nator_traits::propagate_on_-
  281. //! container_swap is true calls swap(node_alloc(), nh.node_alloc()).
  282. void swap(node_handle &nh)
  283. BOOST_NOEXCEPT_IF(nator_traits::propagate_on_container_swap::value || nator_traits::is_always_equal::value)
  284. {
  285. BOOST_ASSERT(this->empty() || nh.empty() || nator_traits::propagate_on_container_swap::value
  286. || nator_traits::equal(node_alloc(), nh.node_alloc()));
  287. bool const was_this_non_null = !this->empty();
  288. bool const was_nh_non_null = !nh.empty();
  289. if(was_nh_non_null){
  290. if(was_this_non_null){
  291. if(nator_traits::propagate_on_container_swap::value){
  292. ::boost::adl_move_swap(this->node_alloc(), nh.node_alloc());
  293. }
  294. }
  295. else{
  296. this->move_construct_alloc(nh.node_alloc());
  297. nh.destroy_alloc();
  298. }
  299. }
  300. else if(was_this_non_null){
  301. nh.move_construct_alloc(this->node_alloc());
  302. this->destroy_alloc();
  303. }
  304. ::boost::adl_move_swap(m_ptr, nh.m_ptr);
  305. }
  306. //! <b>Effects</b>: If this->empty() returns nullptr, otherwise returns m_ptr
  307. //! resets m_ptr to nullptr and destroys the internal allocator.
  308. //!
  309. //! <b>Postcondition</b>: this->empty()
  310. //!
  311. //! <b>Note</b>: Non-standard extensions
  312. node_pointer release() BOOST_NOEXCEPT
  313. {
  314. node_pointer p(m_ptr);
  315. m_ptr = node_pointer();
  316. if(p)
  317. this->destroy_alloc();
  318. return p;
  319. }
  320. //! <b>Effects</b>: Returns m_ptr.
  321. //!
  322. //! <b>Note</b>: Non-standard extensions
  323. node_pointer get() const BOOST_NOEXCEPT
  324. {
  325. return m_ptr;
  326. }
  327. //! <b>Effects</b>: Returns a reference to the internal node allocator.
  328. //!
  329. //! <b>Note</b>: Non-standard extensions
  330. nallocator_type &node_alloc() BOOST_NOEXCEPT
  331. {
  332. BOOST_ASSERT(!empty());
  333. return *static_cast<nallocator_type*>((void*)m_nalloc_storage.data);
  334. }
  335. //! <b>Effects</b>: Returns a reference to the internal node allocator.
  336. //!
  337. //! <b>Note</b>: Non-standard extensions
  338. const nallocator_type &node_alloc() const BOOST_NOEXCEPT
  339. {
  340. BOOST_ASSERT(!empty());
  341. return *static_cast<const nallocator_type*>((const void*)m_nalloc_storage.data);
  342. }
  343. //! <b>Effects</b>: x.swap(y).
  344. //!
  345. friend void swap(node_handle & x, node_handle & y) BOOST_NOEXCEPT_IF(BOOST_NOEXCEPT(x.swap(y)))
  346. { x.swap(y); }
  347. };
  348. //! A class template used to describe the results of inserting a
  349. //! Container::node_type in a Container with unique keys.
  350. //! Includes at least the following non-static public data members:
  351. //!
  352. //! <ul><li>bool inserted</li>;
  353. //! <li>Iterator position</li>;
  354. //! <li>NodeType node</li></ul>
  355. //!
  356. //! This type is MoveConstructible, MoveAssignable, DefaultConstructible,
  357. //! Destructible, and lvalues of that type are swappable
  358. template<class Iterator, class NodeType>
  359. struct insert_return_type_base
  360. {
  361. private:
  362. BOOST_MOVABLE_BUT_NOT_COPYABLE(insert_return_type_base)
  363. public:
  364. insert_return_type_base()
  365. : inserted(false), position(), node()
  366. {}
  367. insert_return_type_base(BOOST_RV_REF(insert_return_type_base) other)
  368. : inserted(other.inserted), position(other.position), node(boost::move(other.node))
  369. {}
  370. template<class RelatedIt, class RelatedNode>
  371. insert_return_type_base(bool insert, RelatedIt it, BOOST_RV_REF(RelatedNode) n)
  372. : inserted(insert), position(it), node(boost::move(n))
  373. {}
  374. insert_return_type_base & operator=(BOOST_RV_REF(insert_return_type_base) other)
  375. {
  376. inserted = other.inserted;
  377. position = other.position;
  378. node = boost::move(other.node);
  379. return *this;
  380. }
  381. bool inserted;
  382. Iterator position;
  383. NodeType node;
  384. };
  385. } //namespace container {
  386. } //namespace boost {
  387. #if defined(BOOST_GCC) && (BOOST_GCC >= 120000) && (BOOST_GCC < 130000)
  388. #pragma GCC diagnostic pop
  389. #endif
  390. #include <boost/container/detail/config_end.hpp>
  391. #endif //BOOST_CONTAINER_NODE_HANDLE_HPP