node_handle.hpp 15 KB

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