node_alloc_holder.hpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619
  1. //////////////////////////////////////////////////////////////////////////////
  2. //
  3. // (C) Copyright Ion Gaztanaga 2005-2013. 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_DETAIL_NODE_ALLOC_HPP_
  11. #define BOOST_CONTAINER_DETAIL_NODE_ALLOC_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. // container
  21. #include <boost/container/allocator_traits.hpp>
  22. // container/detail
  23. #include <boost/container/detail/addressof.hpp>
  24. #include <boost/container/detail/alloc_helpers.hpp>
  25. #include <boost/container/detail/allocator_version_traits.hpp>
  26. #include <boost/container/detail/construct_in_place.hpp>
  27. #include <boost/container/detail/destroyers.hpp>
  28. #include <boost/move/detail/iterator_to_raw_pointer.hpp>
  29. #include <boost/move/detail/launder.hpp>
  30. #include <boost/container/detail/mpl.hpp>
  31. #include <boost/container/detail/placement_new.hpp>
  32. #include <boost/move/detail/to_raw_pointer.hpp>
  33. #include <boost/container/detail/type_traits.hpp>
  34. #include <boost/container/detail/version_type.hpp>
  35. #include <boost/container/detail/is_pair.hpp>
  36. #include <boost/container/detail/pair.hpp>
  37. // intrusive
  38. #include <boost/intrusive/detail/mpl.hpp>
  39. #include <boost/intrusive/options.hpp>
  40. // move
  41. #include <boost/move/utility_core.hpp>
  42. #if defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
  43. #include <boost/move/detail/fwd_macros.hpp>
  44. #endif
  45. namespace boost {
  46. namespace container {
  47. //This trait is used to type-pun std::pair because in C++03
  48. //compilers std::pair is useless for C++11 features
  49. template<class T, bool>
  50. struct node_internal_data_type
  51. {
  52. typedef T type;
  53. };
  54. template<class T>
  55. struct node_internal_data_type< T, true>
  56. {
  57. typedef dtl::pair< typename dtl::remove_const<typename T::first_type>::type
  58. , typename T::second_type>
  59. type;
  60. };
  61. template <class T, class HookDefiner, bool PairBased = false>
  62. struct base_node
  63. : public HookDefiner::type
  64. {
  65. public:
  66. typedef T value_type;
  67. typedef typename node_internal_data_type<T, PairBased && dtl::is_pair<T>::value>::type internal_type;
  68. typedef typename HookDefiner::type hook_type;
  69. typedef typename dtl::aligned_storage<sizeof(T), dtl::alignment_of<T>::value>::type storage_t;
  70. storage_t m_storage;
  71. #if defined(BOOST_GCC) && (BOOST_GCC >= 40600) && (BOOST_GCC < 80000)
  72. #pragma GCC diagnostic push
  73. #pragma GCC diagnostic ignored "-Wstrict-aliasing"
  74. #define BOOST_CONTAINER_DISABLE_ALIASING_WARNING
  75. # endif
  76. public:
  77. #if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
  78. template<class Alloc, class ...Args>
  79. explicit base_node(Alloc &a, Args &&...args)
  80. : hook_type()
  81. {
  82. ::boost::container::allocator_traits<Alloc>::construct
  83. (a, &this->get_real_data(), ::boost::forward<Args>(args)...);
  84. }
  85. #else //defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
  86. #define BOOST_CONTAINER_BASE_NODE_CONSTRUCT_IMPL(N) \
  87. template< class Alloc BOOST_MOVE_I##N BOOST_MOVE_CLASS##N > \
  88. explicit base_node(Alloc &a BOOST_MOVE_I##N BOOST_MOVE_UREF##N)\
  89. : hook_type()\
  90. {\
  91. ::boost::container::allocator_traits<Alloc>::construct\
  92. (a, &this->get_real_data() BOOST_MOVE_I##N BOOST_MOVE_FWD##N);\
  93. }\
  94. //
  95. BOOST_MOVE_ITERATE_0TO9(BOOST_CONTAINER_BASE_NODE_CONSTRUCT_IMPL)
  96. #undef BOOST_CONTAINER_BASE_NODE_CONSTRUCT_IMPL
  97. #endif // !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
  98. template<class Alloc, class It>
  99. explicit base_node(iterator_arg_t, Alloc &a, It it)
  100. : hook_type()
  101. {
  102. ::boost::container::construct_in_place(a, &this->get_real_data(), it);
  103. }
  104. inline T &get_data()
  105. { return *move_detail::force_ptr<T*>(&this->m_storage); }
  106. inline const T &get_data() const
  107. { return *move_detail::launder_cast<const T*>(&this->m_storage); }
  108. inline internal_type &get_real_data()
  109. { return *move_detail::launder_cast<internal_type*>(&this->m_storage); }
  110. inline const internal_type &get_real_data() const
  111. { return *move_detail::launder_cast<const internal_type*>(&this->m_storage); }
  112. #if defined(BOOST_CONTAINER_DISABLE_ALIASING_WARNING)
  113. #pragma GCC diagnostic pop
  114. #undef BOOST_CONTAINER_DISABLE_ALIASING_WARNING
  115. # endif
  116. template<class Alloc>
  117. inline void destructor(Alloc &a) BOOST_NOEXCEPT
  118. {
  119. allocator_traits<Alloc>::destroy
  120. (a, &this->get_real_data());
  121. this->~base_node();
  122. }
  123. template<class Pair>
  124. inline
  125. typename dtl::enable_if< dtl::is_pair<Pair>, void >::type
  126. do_assign(const Pair &p)
  127. {
  128. typedef typename Pair::first_type first_type;
  129. const_cast<typename dtl::remove_const<first_type>::type &>(this->get_real_data().first) = p.first;
  130. this->get_real_data().second = p.second;
  131. }
  132. template<class V>
  133. inline
  134. typename dtl::disable_if< dtl::is_pair<V>, void >::type
  135. do_assign(const V &v)
  136. { this->get_real_data() = v; }
  137. template<class Pair>
  138. inline
  139. typename dtl::enable_if< dtl::is_pair<Pair>, void >::type
  140. do_move_assign(Pair &p)
  141. {
  142. typedef typename Pair::first_type first_type;
  143. const_cast<first_type&>(this->get_real_data().first) = ::boost::move(p.first);
  144. this->get_real_data().second = ::boost::move(p.second);
  145. }
  146. template<class V>
  147. inline
  148. typename dtl::disable_if< dtl::is_pair<V>, void >::type
  149. do_move_assign(V &v)
  150. { this->get_real_data() = ::boost::move(v); }
  151. private:
  152. base_node();
  153. inline ~base_node()
  154. { }
  155. };
  156. namespace dtl {
  157. BOOST_INTRUSIVE_INSTANTIATE_DEFAULT_TYPE_TMPLT(key_compare)
  158. BOOST_INTRUSIVE_INSTANTIATE_DEFAULT_TYPE_TMPLT(key_equal)
  159. BOOST_INTRUSIVE_INSTANTIATE_DEFAULT_TYPE_TMPLT(hasher)
  160. BOOST_INTRUSIVE_INSTANTIATE_DEFAULT_TYPE_TMPLT(predicate_type)
  161. BOOST_INTRUSIVE_INSTANTIATE_DEFAULT_TYPE_TMPLT(bucket_traits)
  162. BOOST_INTRUSIVE_INSTANTIATE_DEFAULT_TYPE_TMPLT(bucket_type)
  163. template<class Allocator, class ICont>
  164. struct node_alloc_holder
  165. : public allocator_traits<Allocator>::template
  166. portable_rebind_alloc<typename ICont::value_type>::type //NodeAlloc
  167. {
  168. //If the intrusive container is an associative container, obtain the predicate, which will
  169. //be of type node_compare<>. If not an associative container val_compare will be a "nat" type.
  170. typedef BOOST_INTRUSIVE_OBTAIN_TYPE_WITH_DEFAULT
  171. ( boost::container::dtl::
  172. , ICont, key_compare, dtl::nat) intrusive_key_compare;
  173. //If the intrusive container is a hash container, obtain the predicate, which will
  174. //be of type node_compare<>. If not an associative container val_equal will be a "nat" type.
  175. typedef BOOST_INTRUSIVE_OBTAIN_TYPE_WITH_DEFAULT
  176. (boost::container::dtl::
  177. , ICont, key_equal, dtl::nat2) intrusive_val_equal;
  178. typedef BOOST_INTRUSIVE_OBTAIN_TYPE_WITH_DEFAULT
  179. (boost::container::dtl::
  180. , ICont, hasher, dtl::nat3) intrusive_val_hasher;
  181. typedef BOOST_INTRUSIVE_OBTAIN_TYPE_WITH_DEFAULT
  182. (boost::container::dtl::
  183. , ICont, bucket_traits, dtl::natN<0>) intrusive_bucket_traits;
  184. typedef BOOST_INTRUSIVE_OBTAIN_TYPE_WITH_DEFAULT
  185. (boost::container::dtl::
  186. , ICont, bucket_type, dtl::natN<1>) intrusive_bucket_type;
  187. //In that case obtain the value predicate from the node predicate via predicate_type
  188. //if intrusive_key_compare is node_compare<>, nat otherwise
  189. typedef BOOST_INTRUSIVE_OBTAIN_TYPE_WITH_DEFAULT
  190. (boost::container::dtl::
  191. , intrusive_val_equal
  192. , predicate_type, dtl::nat2) val_equal;
  193. typedef BOOST_INTRUSIVE_OBTAIN_TYPE_WITH_DEFAULT
  194. (boost::container::dtl::
  195. , intrusive_val_hasher
  196. , predicate_type, dtl::nat3) val_hasher;
  197. typedef allocator_traits<Allocator> allocator_traits_type;
  198. typedef typename allocator_traits_type::value_type val_type;
  199. typedef ICont intrusive_container;
  200. typedef typename ICont::value_type Node;
  201. typedef typename allocator_traits_type::template
  202. portable_rebind_alloc<Node>::type NodeAlloc;
  203. typedef allocator_traits<NodeAlloc> node_allocator_traits_type;
  204. typedef dtl::allocator_version_traits<NodeAlloc> node_allocator_version_traits_type;
  205. typedef Allocator ValAlloc;
  206. typedef typename node_allocator_traits_type::pointer NodePtr;
  207. typedef dtl::scoped_deallocator<NodeAlloc> Deallocator;
  208. typedef typename node_allocator_traits_type::size_type size_type;
  209. typedef typename node_allocator_traits_type::difference_type difference_type;
  210. typedef dtl::integral_constant<unsigned,
  211. boost::container::dtl::
  212. version<NodeAlloc>::value> alloc_version;
  213. typedef typename ICont::iterator icont_iterator;
  214. typedef typename ICont::const_iterator icont_citerator;
  215. typedef allocator_node_destroyer<NodeAlloc> Destroyer;
  216. typedef allocator_traits<NodeAlloc> NodeAllocTraits;
  217. typedef allocator_version_traits<NodeAlloc> AllocVersionTraits;
  218. private:
  219. BOOST_COPYABLE_AND_MOVABLE(node_alloc_holder)
  220. public:
  221. //Constructors for sequence containers
  222. inline node_alloc_holder()
  223. {}
  224. explicit node_alloc_holder(const intrusive_bucket_traits& bt)
  225. : m_icont(bt)
  226. {}
  227. explicit node_alloc_holder(const ValAlloc &a)
  228. : NodeAlloc(a)
  229. {}
  230. node_alloc_holder(const intrusive_bucket_traits& bt, const ValAlloc& a)
  231. : NodeAlloc(a)
  232. , m_icont(bt)
  233. {}
  234. //Constructors for associative containers
  235. node_alloc_holder(const intrusive_key_compare &c, const ValAlloc &a)
  236. : NodeAlloc(a), m_icont(c)
  237. {}
  238. node_alloc_holder(const intrusive_bucket_traits & bt, const val_hasher &hf, const val_equal &eql, const ValAlloc &a)
  239. : NodeAlloc(a)
  240. , m_icont(bt
  241. , typename ICont::hasher(hf)
  242. , typename ICont::key_equal(eql))
  243. {}
  244. node_alloc_holder(const intrusive_bucket_traits& bt, const val_hasher &hf, const ValAlloc &a)
  245. : NodeAlloc(a)
  246. , m_icont(bt
  247. , typename ICont::hasher(hf)
  248. , typename ICont::key_equal())
  249. {}
  250. node_alloc_holder(const intrusive_bucket_traits& bt, const val_hasher &hf)
  251. : m_icont(bt
  252. , typename ICont::hasher(hf)
  253. , typename ICont::key_equal())
  254. {}
  255. explicit node_alloc_holder(const node_alloc_holder &x)
  256. : NodeAlloc(NodeAllocTraits::select_on_container_copy_construction(x.node_alloc()))
  257. {}
  258. node_alloc_holder(const node_alloc_holder &x, const intrusive_key_compare &c)
  259. : NodeAlloc(NodeAllocTraits::select_on_container_copy_construction(x.node_alloc()))
  260. , m_icont(c)
  261. {}
  262. node_alloc_holder(const node_alloc_holder &x, const intrusive_bucket_traits& bt, const val_hasher &hf, const val_equal &eql)
  263. : NodeAlloc(NodeAllocTraits::select_on_container_copy_construction(x.node_alloc()))
  264. , m_icont( bt
  265. , typename ICont::hasher(hf)
  266. , typename ICont::key_equal(eql))
  267. {}
  268. node_alloc_holder(const val_hasher &hf, const intrusive_bucket_traits& bt, const val_equal &eql)
  269. : m_icont(bt
  270. , typename ICont::hasher(hf)
  271. , typename ICont::key_equal(eql))
  272. {}
  273. explicit node_alloc_holder(BOOST_RV_REF(node_alloc_holder) x)
  274. : NodeAlloc(boost::move(BOOST_MOVE_TO_LV(x).node_alloc()))
  275. { this->icont().swap(x.icont()); }
  276. explicit node_alloc_holder(const intrusive_key_compare &c)
  277. : m_icont(c)
  278. {}
  279. //helpers for move assignments
  280. explicit node_alloc_holder(BOOST_RV_REF(node_alloc_holder) x, const intrusive_key_compare &c)
  281. : NodeAlloc(boost::move(BOOST_MOVE_TO_LV(x).node_alloc())), m_icont(c)
  282. { this->icont().swap(x.icont()); }
  283. explicit node_alloc_holder(BOOST_RV_REF(node_alloc_holder) x, const intrusive_bucket_traits& bt, const val_hasher &hf, const val_equal &eql)
  284. : NodeAlloc(boost::move(BOOST_MOVE_TO_LV(x).node_alloc()))
  285. , m_icont( bt
  286. , typename ICont::hasher(hf)
  287. , typename ICont::key_equal(eql))
  288. { this->icont().swap(BOOST_MOVE_TO_LV(x).icont()); }
  289. inline void copy_assign_alloc(const node_alloc_holder &x)
  290. {
  291. dtl::bool_<allocator_traits_type::propagate_on_container_copy_assignment::value> flag;
  292. dtl::assign_alloc( static_cast<NodeAlloc &>(*this)
  293. , static_cast<const NodeAlloc &>(x), flag);
  294. }
  295. inline void move_assign_alloc( node_alloc_holder &x)
  296. {
  297. dtl::bool_<allocator_traits_type::propagate_on_container_move_assignment::value> flag;
  298. dtl::move_alloc( static_cast<NodeAlloc &>(*this)
  299. , static_cast<NodeAlloc &>(x), flag);
  300. }
  301. inline ~node_alloc_holder()
  302. { this->clear(alloc_version()); }
  303. inline size_type max_size() const
  304. { return allocator_traits_type::max_size(this->node_alloc()); }
  305. inline NodePtr allocate_one()
  306. { return AllocVersionTraits::allocate_one(this->node_alloc()); }
  307. inline void deallocate_one(const NodePtr &p)
  308. { AllocVersionTraits::deallocate_one(this->node_alloc(), p); }
  309. #if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
  310. template<class ...Args>
  311. NodePtr create_node(Args &&...args)
  312. {
  313. NodePtr p = this->allocate_one();
  314. NodeAlloc &nalloc = this->node_alloc();
  315. Deallocator node_deallocator(p, nalloc);
  316. ::new(boost::movelib::iterator_to_raw_pointer(p), boost_container_new_t())
  317. Node(nalloc, boost::forward<Args>(args)...);
  318. node_deallocator.release();
  319. return (p);
  320. }
  321. #else //defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
  322. #define BOOST_CONTAINER_NODE_ALLOC_HOLDER_CONSTRUCT_IMPL(N) \
  323. BOOST_MOVE_TMPL_LT##N BOOST_MOVE_CLASS##N BOOST_MOVE_GT##N \
  324. NodePtr create_node(BOOST_MOVE_UREF##N)\
  325. {\
  326. NodePtr p = this->allocate_one();\
  327. NodeAlloc &nalloc = this->node_alloc();\
  328. Deallocator node_deallocator(p, nalloc);\
  329. ::new(boost::movelib::iterator_to_raw_pointer(p), boost_container_new_t())\
  330. Node(nalloc BOOST_MOVE_I##N BOOST_MOVE_FWD##N);\
  331. node_deallocator.release();\
  332. return (p);\
  333. }\
  334. //
  335. BOOST_MOVE_ITERATE_0TO9(BOOST_CONTAINER_NODE_ALLOC_HOLDER_CONSTRUCT_IMPL)
  336. #undef BOOST_CONTAINER_NODE_ALLOC_HOLDER_CONSTRUCT_IMPL
  337. #endif // !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
  338. template<class It>
  339. NodePtr create_node_from_it(const It &it)
  340. {
  341. NodePtr p = this->allocate_one();
  342. NodeAlloc &nalloc = this->node_alloc();
  343. Deallocator node_deallocator(p, nalloc);
  344. ::new(boost::movelib::iterator_to_raw_pointer(p), boost_container_new_t())
  345. Node(iterator_arg_t(), nalloc, it);
  346. node_deallocator.release();
  347. return (p);
  348. }
  349. template<class KeyConvertible>
  350. NodePtr create_node_from_key(BOOST_FWD_REF(KeyConvertible) key)
  351. {
  352. NodePtr p = this->allocate_one();
  353. BOOST_CONTAINER_TRY{
  354. ::new(boost::movelib::iterator_to_raw_pointer(p), boost_container_new_t()) Node;
  355. NodeAlloc &na = this->node_alloc();
  356. node_allocator_traits_type::construct
  357. (na, dtl::addressof(p->get_real_data().first), boost::forward<KeyConvertible>(key));
  358. BOOST_CONTAINER_TRY{
  359. node_allocator_traits_type::construct(na, dtl::addressof(p->get_real_data().second));
  360. }
  361. BOOST_CONTAINER_CATCH(...){
  362. node_allocator_traits_type::destroy(na, dtl::addressof(p->get_real_data().first));
  363. BOOST_CONTAINER_RETHROW;
  364. }
  365. BOOST_CONTAINER_CATCH_END
  366. }
  367. BOOST_CONTAINER_CATCH(...) {
  368. p->destroy_header();
  369. this->node_alloc().deallocate(p, 1);
  370. BOOST_CONTAINER_RETHROW
  371. }
  372. BOOST_CONTAINER_CATCH_END
  373. return (p);
  374. }
  375. void destroy_node(const NodePtr &nodep)
  376. {
  377. boost::movelib::to_raw_pointer(nodep)->destructor(this->node_alloc());
  378. this->deallocate_one(nodep);
  379. }
  380. void swap(node_alloc_holder &x)
  381. {
  382. this->icont().swap(x.icont());
  383. dtl::bool_<allocator_traits_type::propagate_on_container_swap::value> flag;
  384. dtl::swap_alloc(this->node_alloc(), x.node_alloc(), flag);
  385. }
  386. template<class FwdIterator, class Inserter>
  387. void allocate_many_and_construct
  388. (FwdIterator beg, size_type n, Inserter inserter)
  389. {
  390. if(n){
  391. typedef typename node_allocator_version_traits_type::multiallocation_chain multiallocation_chain_t;
  392. //Try to allocate memory in a single block
  393. typedef typename multiallocation_chain_t::iterator multialloc_iterator_t;
  394. multiallocation_chain_t chain;
  395. NodeAlloc &nalloc = this->node_alloc();
  396. node_allocator_version_traits_type::allocate_individual(nalloc, n, chain);
  397. multialloc_iterator_t itbeg = chain.begin();
  398. multialloc_iterator_t itlast = chain.last();
  399. chain.clear();
  400. Node *p = 0;
  401. BOOST_CONTAINER_TRY{
  402. Deallocator node_deallocator(NodePtr(), nalloc);
  403. dtl::scoped_node_destructor<NodeAlloc> sdestructor(nalloc, 0);
  404. while(n){
  405. --n;
  406. p = boost::movelib::iterator_to_raw_pointer(itbeg);
  407. ++itbeg; //Increment iterator before overwriting pointed memory
  408. //This does not throw
  409. ::new(boost::movelib::iterator_to_raw_pointer(p), boost_container_new_t())
  410. Node(iterator_arg_t(), nalloc, beg);
  411. sdestructor.set(p);
  412. ++beg;
  413. //This can throw in some containers (predicate might throw).
  414. //(sdestructor will destruct the node and node_deallocator will deallocate it in case of exception)
  415. inserter(*p);
  416. sdestructor.release();
  417. }
  418. sdestructor.release();
  419. node_deallocator.release();
  420. }
  421. BOOST_CONTAINER_CATCH(...){
  422. chain.incorporate_after(chain.last(), &*itbeg, &*itlast, n);
  423. node_allocator_version_traits_type::deallocate_individual(this->node_alloc(), chain);
  424. BOOST_CONTAINER_RETHROW
  425. }
  426. BOOST_CONTAINER_CATCH_END
  427. }
  428. }
  429. inline void clear(version_1)
  430. { this->icont().clear_and_dispose(Destroyer(this->node_alloc())); }
  431. void clear(version_2)
  432. {
  433. typename NodeAlloc::multiallocation_chain chain;
  434. allocator_node_destroyer_and_chain_builder<NodeAlloc> builder(this->node_alloc(), chain);
  435. this->icont().clear_and_dispose(builder);
  436. //BOOST_CONTAINER_STATIC_ASSERT((::boost::has_move_emulation_enabled<typename NodeAlloc::multiallocation_chain>::value == true));
  437. if(!chain.empty())
  438. this->node_alloc().deallocate_individual(chain);
  439. }
  440. icont_iterator erase_range(const icont_iterator &first, const icont_iterator &last, version_1)
  441. { return this->icont().erase_and_dispose(first, last, Destroyer(this->node_alloc())); }
  442. icont_iterator erase_range(const icont_iterator &first, const icont_iterator &last, version_2)
  443. {
  444. NodeAlloc & nalloc = this->node_alloc();
  445. typename NodeAlloc::multiallocation_chain chain;
  446. allocator_node_destroyer_and_chain_builder<NodeAlloc> chain_builder(nalloc, chain);
  447. icont_iterator ret_it = this->icont().erase_and_dispose(first, last, chain_builder);
  448. nalloc.deallocate_individual(chain);
  449. return ret_it;
  450. }
  451. template<class Key>
  452. inline size_type erase_key(const Key& k, version_1)
  453. { return this->icont().erase_and_dispose(k, Destroyer(this->node_alloc())); }
  454. template<class Key>
  455. inline size_type erase_key(const Key& k, version_2)
  456. {
  457. allocator_multialloc_chain_node_deallocator<NodeAlloc> chain_holder(this->node_alloc());
  458. return this->icont().erase_and_dispose(k, chain_holder.get_chain_builder());
  459. }
  460. template<class Key, class KeyCompare>
  461. inline size_type erase_key(const Key& k, KeyCompare cmp, version_1)
  462. {
  463. return this->icont().erase_and_dispose(k, cmp, Destroyer(this->node_alloc()));
  464. }
  465. template<class Key, class KeyCompare>
  466. inline size_type erase_key(const Key& k, KeyCompare cmp, version_2)
  467. {
  468. allocator_multialloc_chain_node_deallocator<NodeAlloc> chain_holder(this->node_alloc());
  469. return this->icont().erase_and_dispose(k, cmp, chain_holder.get_chain_builder());
  470. }
  471. protected:
  472. struct cloner
  473. {
  474. inline explicit cloner(node_alloc_holder &holder)
  475. : m_holder(holder)
  476. {}
  477. inline NodePtr operator()(const Node &other) const
  478. { return m_holder.create_node(other.get_real_data()); }
  479. node_alloc_holder &m_holder;
  480. };
  481. struct move_cloner
  482. {
  483. inline move_cloner(node_alloc_holder &holder)
  484. : m_holder(holder)
  485. {}
  486. inline NodePtr operator()(Node &other)
  487. { //Use get_real_data() instead of get_real_data to allow moving const key in [multi]map
  488. return m_holder.create_node(::boost::move(other.get_real_data()));
  489. }
  490. node_alloc_holder &m_holder;
  491. };
  492. inline ICont &non_const_icont() const
  493. { return const_cast<ICont&>(this->m_icont); }
  494. inline NodeAlloc &node_alloc()
  495. { return static_cast<NodeAlloc &>(*this); }
  496. inline const NodeAlloc &node_alloc() const
  497. { return static_cast<const NodeAlloc &>(*this); }
  498. public:
  499. inline ICont &icont()
  500. { return this->m_icont; }
  501. inline const ICont &icont() const
  502. { return this->m_icont; }
  503. protected:
  504. //The intrusive container
  505. ICont m_icont;
  506. };
  507. template<class Node, class KeyOfValue>
  508. struct key_of_node : KeyOfValue
  509. {
  510. typedef typename KeyOfValue::type type;
  511. inline key_of_node()
  512. {}
  513. inline const type& operator()(const Node& x) const
  514. { return this->KeyOfValue::operator()(x.get_data()); }
  515. };
  516. } //namespace dtl {
  517. } //namespace container {
  518. } //namespace boost {
  519. #include <boost/container/detail/config_end.hpp>
  520. #endif // BOOST_CONTAINER_DETAIL_NODE_ALLOC_HPP_