ptr_map_adapter.hpp 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982
  1. //
  2. // Boost.Pointer Container
  3. //
  4. // Copyright Thorsten Ottosen 2003-2005. Use, modification and
  5. // distribution is subject to the Boost Software License, Version
  6. // 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  7. // http://www.boost.org/LICENSE_1_0.txt)
  8. //
  9. // For more information, see http://www.boost.org/libs/ptr_container/
  10. //
  11. #ifndef BOOST_PTR_CONTAINER_DETAIL_PTR_MAP_ADAPTER_HPP
  12. #define BOOST_PTR_CONTAINER_DETAIL_PTR_MAP_ADAPTER_HPP
  13. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  14. # pragma once
  15. #endif
  16. #include <boost/ptr_container/detail/map_iterator.hpp>
  17. #include <boost/ptr_container/detail/associative_ptr_container.hpp>
  18. #include <boost/ptr_container/detail/meta_functions.hpp>
  19. #include <boost/ptr_container/detail/ptr_container_disable_deprecated.hpp>
  20. #include <boost/static_assert.hpp>
  21. #include <boost/range/iterator_range.hpp>
  22. #if defined(BOOST_PTR_CONTAINER_DISABLE_DEPRECATED)
  23. #pragma GCC diagnostic push
  24. #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
  25. #endif
  26. namespace boost
  27. {
  28. namespace ptr_container_detail
  29. {
  30. template
  31. <
  32. class T,
  33. class VoidPtrMap,
  34. bool Ordered
  35. >
  36. struct map_config
  37. {
  38. typedef BOOST_DEDUCED_TYPENAME remove_nullable<T>::type
  39. U;
  40. typedef VoidPtrMap
  41. void_container_type;
  42. typedef BOOST_DEDUCED_TYPENAME VoidPtrMap::allocator_type
  43. allocator_type;
  44. typedef BOOST_DEDUCED_TYPENAME
  45. mpl::eval_if_c<Ordered,
  46. select_value_compare<VoidPtrMap>,
  47. mpl::identity<void> >::type
  48. value_compare;
  49. typedef BOOST_DEDUCED_TYPENAME
  50. mpl::eval_if_c<Ordered,
  51. select_key_compare<VoidPtrMap>,
  52. mpl::identity<void> >::type
  53. key_compare;
  54. typedef BOOST_DEDUCED_TYPENAME
  55. mpl::eval_if_c<Ordered,
  56. mpl::identity<void>,
  57. select_hasher<VoidPtrMap> >::type
  58. hasher;
  59. typedef BOOST_DEDUCED_TYPENAME
  60. mpl::eval_if_c<Ordered,
  61. mpl::identity<void>,
  62. select_key_equal<VoidPtrMap> >::type
  63. key_equal;
  64. typedef BOOST_DEDUCED_TYPENAME
  65. mpl::if_c<Ordered,
  66. ptr_container_detail::ordered_associative_container_tag,
  67. ptr_container_detail::unordered_associative_container_tag>::type
  68. container_type;
  69. typedef BOOST_DEDUCED_TYPENAME VoidPtrMap::key_type
  70. key_type;
  71. typedef U value_type;
  72. typedef ptr_map_iterator< BOOST_DEDUCED_TYPENAME VoidPtrMap::iterator, key_type, U* const >
  73. iterator;
  74. typedef ptr_map_iterator< BOOST_DEDUCED_TYPENAME VoidPtrMap::const_iterator, key_type, const U* const>
  75. const_iterator;
  76. typedef ptr_map_iterator<
  77. BOOST_DEDUCED_TYPENAME
  78. mpl::eval_if_c<Ordered,
  79. select_iterator<VoidPtrMap>,
  80. select_local_iterator<VoidPtrMap> >::type,
  81. key_type, U* const >
  82. local_iterator;
  83. typedef ptr_map_iterator<
  84. BOOST_DEDUCED_TYPENAME
  85. mpl::eval_if_c<Ordered,
  86. select_iterator<VoidPtrMap>,
  87. select_const_local_iterator<VoidPtrMap> >::type,
  88. key_type, const U* const >
  89. const_local_iterator;
  90. template< class Iter >
  91. static U* get_pointer( Iter i )
  92. {
  93. return i->second;
  94. }
  95. template< class Iter >
  96. static const U* get_const_pointer( Iter i )
  97. {
  98. return i->second;
  99. }
  100. BOOST_STATIC_CONSTANT( bool, allow_null = boost::is_nullable<T>::value );
  101. };
  102. template
  103. <
  104. class T,
  105. class VoidPtrMap,
  106. class CloneAllocator,
  107. bool Ordered
  108. >
  109. class ptr_map_adapter_base :
  110. public ptr_container_detail::associative_ptr_container< map_config<T,VoidPtrMap,Ordered>,
  111. CloneAllocator >
  112. {
  113. typedef ptr_container_detail::associative_ptr_container< map_config<T,VoidPtrMap,Ordered>,
  114. CloneAllocator >
  115. base_type;
  116. typedef map_config<T,VoidPtrMap,Ordered> config;
  117. typedef ptr_map_adapter_base<T,VoidPtrMap,CloneAllocator,Ordered> this_type;
  118. public:
  119. typedef BOOST_DEDUCED_TYPENAME base_type::allocator_type
  120. allocator_type;
  121. typedef BOOST_DEDUCED_TYPENAME base_type::iterator
  122. iterator;
  123. typedef BOOST_DEDUCED_TYPENAME base_type::const_iterator
  124. const_iterator;
  125. typedef BOOST_DEDUCED_TYPENAME base_type::size_type
  126. size_type;
  127. typedef BOOST_DEDUCED_TYPENAME base_type::key_type
  128. key_type;
  129. typedef BOOST_DEDUCED_TYPENAME base_type::auto_type
  130. auto_type;
  131. typedef BOOST_DEDUCED_TYPENAME base_type::value_type
  132. mapped_type;
  133. typedef BOOST_DEDUCED_TYPENAME base_type::reference
  134. mapped_reference;
  135. typedef BOOST_DEDUCED_TYPENAME base_type::const_reference
  136. const_mapped_reference;
  137. typedef BOOST_DEDUCED_TYPENAME iterator_value<iterator>::type
  138. value_type;
  139. typedef value_type
  140. reference;
  141. typedef BOOST_DEDUCED_TYPENAME iterator_value<const_iterator>::type
  142. const_reference;
  143. typedef value_type
  144. pointer;
  145. typedef const_reference
  146. const_pointer;
  147. private:
  148. const_mapped_reference lookup( const key_type& key ) const
  149. {
  150. const_iterator i = this->find( key );
  151. BOOST_PTR_CONTAINER_THROW_EXCEPTION( i == this->end(), bad_ptr_container_operation,
  152. "'ptr_map/multimap::at()' could"
  153. " not find key" );
  154. return *i->second;
  155. }
  156. struct eraser // scope guard
  157. {
  158. bool released_;
  159. VoidPtrMap* m_;
  160. const key_type& key_;
  161. eraser( VoidPtrMap* m, const key_type& key )
  162. : released_(false), m_(m), key_(key)
  163. {}
  164. ~eraser()
  165. {
  166. if( !released_ )
  167. m_->erase(key_);
  168. }
  169. void release() { released_ = true; }
  170. private:
  171. eraser& operator=(const eraser&);
  172. };
  173. mapped_reference insert_lookup( const key_type& key )
  174. {
  175. void*& ref = this->base()[key];
  176. if( ref )
  177. {
  178. return *static_cast<mapped_type>(ref);
  179. }
  180. else
  181. {
  182. eraser e(&this->base(),key); // nothrow
  183. mapped_type res = new T(); // strong
  184. ref = res; // nothrow
  185. e.release(); // nothrow
  186. return *res;
  187. }
  188. }
  189. public:
  190. ptr_map_adapter_base()
  191. { }
  192. template< class SizeType >
  193. explicit ptr_map_adapter_base( SizeType n,
  194. ptr_container_detail::unordered_associative_container_tag tag )
  195. : base_type( n, tag )
  196. { }
  197. template< class Compare, class Allocator >
  198. ptr_map_adapter_base( const Compare& comp,
  199. const Allocator& a )
  200. : base_type( comp, a )
  201. { }
  202. template< class Hash, class Pred, class Allocator >
  203. ptr_map_adapter_base( const Hash& hash,
  204. const Pred& pred,
  205. const Allocator& a )
  206. : base_type( hash, pred, a )
  207. { }
  208. template< class InputIterator >
  209. ptr_map_adapter_base( InputIterator first, InputIterator last )
  210. : base_type( first, last )
  211. { }
  212. template< class InputIterator, class Comp >
  213. ptr_map_adapter_base( InputIterator first, InputIterator last,
  214. const Comp& comp,
  215. const allocator_type& a = allocator_type() )
  216. : base_type( first, last, comp, a )
  217. { }
  218. template< class InputIterator, class Hash, class Pred, class Allocator >
  219. ptr_map_adapter_base( InputIterator first, InputIterator last,
  220. const Hash& hash,
  221. const Pred& pred,
  222. const Allocator& a )
  223. : base_type( first, last, hash, pred, a )
  224. { }
  225. #ifndef BOOST_NO_AUTO_PTR
  226. template< class PtrContainer >
  227. explicit ptr_map_adapter_base( std::auto_ptr<PtrContainer> clone )
  228. : base_type( clone )
  229. { }
  230. template< typename PtrContainer >
  231. ptr_map_adapter_base& operator=( std::auto_ptr<PtrContainer> clone )
  232. {
  233. base_type::operator=( clone );
  234. return *this;
  235. }
  236. #endif
  237. #ifndef BOOST_NO_CXX11_SMART_PTR
  238. template< class PtrContainer >
  239. explicit ptr_map_adapter_base( std::unique_ptr<PtrContainer> clone )
  240. : base_type( std::move( clone ) )
  241. { }
  242. template< typename PtrContainer >
  243. ptr_map_adapter_base& operator=( std::unique_ptr<PtrContainer> clone )
  244. {
  245. base_type::operator=( std::move( clone ) );
  246. return *this;
  247. }
  248. #endif
  249. iterator find( const key_type& x )
  250. {
  251. return iterator( this->base().find( x ) );
  252. }
  253. const_iterator find( const key_type& x ) const
  254. {
  255. return const_iterator( this->base().find( x ) );
  256. }
  257. size_type count( const key_type& x ) const
  258. {
  259. return this->base().count( x );
  260. }
  261. iterator lower_bound( const key_type& x )
  262. {
  263. return iterator( this->base().lower_bound( x ) );
  264. }
  265. const_iterator lower_bound( const key_type& x ) const
  266. {
  267. return const_iterator( this->base().lower_bound( x ) );
  268. }
  269. iterator upper_bound( const key_type& x )
  270. {
  271. return iterator( this->base().upper_bound( x ) );
  272. }
  273. const_iterator upper_bound( const key_type& x ) const
  274. {
  275. return const_iterator( this->base().upper_bound( x ) );
  276. }
  277. iterator_range<iterator> equal_range( const key_type& x )
  278. {
  279. std::pair<BOOST_DEDUCED_TYPENAME base_type::ptr_iterator,
  280. BOOST_DEDUCED_TYPENAME base_type::ptr_iterator>
  281. p = this->base().equal_range( x );
  282. return make_iterator_range( iterator( p.first ), iterator( p.second ) );
  283. }
  284. iterator_range<const_iterator> equal_range( const key_type& x ) const
  285. {
  286. std::pair<BOOST_DEDUCED_TYPENAME base_type::ptr_const_iterator,
  287. BOOST_DEDUCED_TYPENAME base_type::ptr_const_iterator>
  288. p = this->base().equal_range( x );
  289. return make_iterator_range( const_iterator( p.first ),
  290. const_iterator( p.second ) );
  291. }
  292. mapped_reference at( const key_type& key )
  293. {
  294. return const_cast<mapped_reference>( lookup( key ) );
  295. }
  296. const_mapped_reference at( const key_type& key ) const
  297. {
  298. return lookup( key );
  299. }
  300. mapped_reference operator[]( const key_type& key )
  301. {
  302. return insert_lookup( key );
  303. }
  304. auto_type replace( iterator where, mapped_type x ) // strong
  305. {
  306. BOOST_ASSERT( where != this->end() );
  307. this->enforce_null_policy( x, "Null pointer in 'replace()'" );
  308. auto_type ptr( x, *this );
  309. BOOST_PTR_CONTAINER_THROW_EXCEPTION( this->empty(),
  310. bad_ptr_container_operation,
  311. "'replace()' on empty container" );
  312. auto_type old( where->second, *this ); // nothrow
  313. where.base()->second = ptr.release(); // nothrow, commit
  314. return boost::ptr_container::move( old );
  315. }
  316. #ifndef BOOST_NO_AUTO_PTR
  317. template< class U >
  318. auto_type replace( iterator where, std::auto_ptr<U> x )
  319. {
  320. return replace( where, x.release() );
  321. }
  322. #endif
  323. #ifndef BOOST_NO_CXX11_SMART_PTR
  324. template< class U >
  325. auto_type replace( iterator where, std::unique_ptr<U> x )
  326. {
  327. return replace( where, x.release() );
  328. }
  329. #endif
  330. protected:
  331. size_type bucket( const key_type& key ) const
  332. {
  333. return this->base().bucket( key );
  334. }
  335. };
  336. } // ptr_container_detail
  337. /////////////////////////////////////////////////////////////////////////
  338. // ptr_map_adapter
  339. /////////////////////////////////////////////////////////////////////////
  340. template
  341. <
  342. class T,
  343. class VoidPtrMap,
  344. class CloneAllocator = heap_clone_allocator,
  345. bool Ordered = true
  346. >
  347. class ptr_map_adapter :
  348. public ptr_container_detail::ptr_map_adapter_base<T,VoidPtrMap,CloneAllocator,Ordered>
  349. {
  350. typedef ptr_container_detail::ptr_map_adapter_base<T,VoidPtrMap,CloneAllocator,Ordered>
  351. base_type;
  352. public:
  353. typedef BOOST_DEDUCED_TYPENAME base_type::iterator
  354. iterator;
  355. typedef BOOST_DEDUCED_TYPENAME base_type::const_iterator
  356. const_iterator;
  357. typedef BOOST_DEDUCED_TYPENAME base_type::size_type
  358. size_type;
  359. typedef BOOST_DEDUCED_TYPENAME base_type::key_type
  360. key_type;
  361. typedef BOOST_DEDUCED_TYPENAME base_type::const_reference
  362. const_reference;
  363. typedef BOOST_DEDUCED_TYPENAME base_type::auto_type
  364. auto_type;
  365. typedef BOOST_DEDUCED_TYPENAME VoidPtrMap::allocator_type
  366. allocator_type;
  367. typedef BOOST_DEDUCED_TYPENAME base_type::mapped_type
  368. mapped_type;
  369. private:
  370. void safe_insert( const key_type& key, auto_type ptr ) // strong
  371. {
  372. std::pair<BOOST_DEDUCED_TYPENAME base_type::ptr_iterator,bool>
  373. res =
  374. this->base().insert( std::make_pair( key, ptr.get() ) ); // strong, commit
  375. if( res.second ) // nothrow
  376. ptr.release(); // nothrow
  377. }
  378. template< class II >
  379. void map_basic_clone_and_insert( II first, II last )
  380. {
  381. while( first != last )
  382. {
  383. if( this->find( first->first ) == this->end() )
  384. {
  385. const_reference p = *first.base(); // nothrow
  386. auto_type ptr( this->null_policy_allocate_clone(p.second), *this );
  387. // strong
  388. this->safe_insert( p.first,
  389. boost::ptr_container::move( ptr ) );
  390. // strong, commit
  391. }
  392. ++first;
  393. }
  394. }
  395. public:
  396. ptr_map_adapter( )
  397. { }
  398. template< class Comp >
  399. explicit ptr_map_adapter( const Comp& comp,
  400. const allocator_type& a )
  401. : base_type( comp, a ) { }
  402. template< class SizeType >
  403. explicit ptr_map_adapter( SizeType n,
  404. ptr_container_detail::unordered_associative_container_tag tag )
  405. : base_type( n, tag ) { }
  406. template< class Hash, class Pred, class Allocator >
  407. ptr_map_adapter( const Hash& hash,
  408. const Pred& pred,
  409. const Allocator& a )
  410. : base_type( hash, pred, a )
  411. { }
  412. template< class InputIterator >
  413. ptr_map_adapter( InputIterator first, InputIterator last )
  414. {
  415. map_basic_clone_and_insert( first, last );
  416. }
  417. template< class InputIterator, class Comp >
  418. ptr_map_adapter( InputIterator first, InputIterator last,
  419. const Comp& comp,
  420. const allocator_type& a = allocator_type() )
  421. : base_type( comp, a )
  422. {
  423. map_basic_clone_and_insert( first, last );
  424. }
  425. template< class InputIterator, class Hash, class Pred, class Allocator >
  426. ptr_map_adapter( InputIterator first, InputIterator last,
  427. const Hash& hash,
  428. const Pred& pred,
  429. const Allocator& a )
  430. : base_type( hash, pred, a )
  431. {
  432. map_basic_clone_and_insert( first, last );
  433. }
  434. ptr_map_adapter( const ptr_map_adapter& r )
  435. {
  436. map_basic_clone_and_insert( r.begin(), r.end() );
  437. }
  438. template< class Key, class U, class CA, bool b >
  439. ptr_map_adapter( const ptr_map_adapter<Key,U,CA,b>& r )
  440. {
  441. map_basic_clone_and_insert( r.begin(), r.end() );
  442. }
  443. #ifndef BOOST_NO_AUTO_PTR
  444. template< class U >
  445. ptr_map_adapter( std::auto_ptr<U> r ) : base_type( r )
  446. { }
  447. #endif
  448. #ifndef BOOST_NO_CXX11_SMART_PTR
  449. template< class U >
  450. ptr_map_adapter( std::unique_ptr<U> r ) : base_type( std::move( r ) )
  451. { }
  452. #endif
  453. ptr_map_adapter& operator=( ptr_map_adapter r )
  454. {
  455. this->swap( r );
  456. return *this;
  457. }
  458. #ifndef BOOST_NO_AUTO_PTR
  459. template< class U >
  460. ptr_map_adapter& operator=( std::auto_ptr<U> r )
  461. {
  462. base_type::operator=( r );
  463. return *this;
  464. }
  465. #endif
  466. #ifndef BOOST_NO_CXX11_SMART_PTR
  467. template< class U >
  468. ptr_map_adapter& operator=( std::unique_ptr<U> r )
  469. {
  470. base_type::operator=( std::move( r ) );
  471. return *this;
  472. }
  473. #endif
  474. using base_type::release;
  475. template< typename InputIterator >
  476. void insert( InputIterator first, InputIterator last ) // basic
  477. {
  478. map_basic_clone_and_insert( first, last );
  479. }
  480. template< class Range >
  481. void insert( const Range& r )
  482. {
  483. insert( boost::begin(r), boost::end(r) );
  484. }
  485. private:
  486. std::pair<iterator,bool> insert_impl( const key_type& key, mapped_type x ) // strong
  487. {
  488. this->enforce_null_policy( x, "Null pointer in ptr_map_adapter::insert()" );
  489. auto_type ptr( x, *this ); // nothrow
  490. std::pair<BOOST_DEDUCED_TYPENAME base_type::ptr_iterator,bool>
  491. res = this->base().insert( std::make_pair( key, x ) ); // strong, commit
  492. if( res.second ) // nothrow
  493. ptr.release(); // nothrow
  494. return std::make_pair( iterator( res.first ), res.second ); // nothrow
  495. }
  496. iterator insert_impl( iterator before, const key_type& key, mapped_type x ) // strong
  497. {
  498. this->enforce_null_policy( x,
  499. "Null pointer in 'ptr_map_adapter::insert()'" );
  500. auto_type ptr( x, *this ); // nothrow
  501. BOOST_DEDUCED_TYPENAME base_type::ptr_iterator
  502. res = this->base().insert( before.base(), std::make_pair( key, x ) );
  503. // strong, commit
  504. ptr.release(); // notrow
  505. return iterator( res );
  506. }
  507. public:
  508. std::pair<iterator,bool> insert( key_type& key, mapped_type x )
  509. {
  510. return insert_impl( key, x );
  511. }
  512. #ifndef BOOST_NO_AUTO_PTR
  513. template< class U >
  514. std::pair<iterator,bool> insert( const key_type& key, std::auto_ptr<U> x )
  515. {
  516. return insert_impl( key, x.release() );
  517. }
  518. #endif
  519. #ifndef BOOST_NO_CXX11_SMART_PTR
  520. template< class U >
  521. std::pair<iterator,bool> insert( const key_type& key, std::unique_ptr<U> x )
  522. {
  523. return insert_impl( key, x.release() );
  524. }
  525. #endif
  526. template< class F, class S >
  527. iterator insert( iterator before, ptr_container_detail::ref_pair<F,S> p ) // strong
  528. {
  529. this->enforce_null_policy( p.second,
  530. "Null pointer in 'ptr_map_adapter::insert()'" );
  531. auto_type ptr( this->null_policy_allocate_clone(p.second), *this );
  532. BOOST_DEDUCED_TYPENAME base_type::ptr_iterator
  533. result = this->base().insert( before.base(),
  534. std::make_pair(p.first,ptr.get()) ); // strong
  535. if( ptr.get() == result->second )
  536. ptr.release();
  537. return iterator( result );
  538. }
  539. iterator insert( iterator before, key_type& key, mapped_type x ) // strong
  540. {
  541. return insert_impl( before, key, x );
  542. }
  543. #ifndef BOOST_NO_AUTO_PTR
  544. template< class U >
  545. iterator insert( iterator before, const key_type& key, std::auto_ptr<U> x ) // strong
  546. {
  547. return insert_impl( before, key, x.release() );
  548. }
  549. #endif
  550. #ifndef BOOST_NO_CXX11_SMART_PTR
  551. template< class U >
  552. iterator insert( iterator before, const key_type& key, std::unique_ptr<U> x ) // strong
  553. {
  554. return insert_impl( before, key, x.release() );
  555. }
  556. #endif
  557. template< class PtrMapAdapter >
  558. bool transfer( BOOST_DEDUCED_TYPENAME PtrMapAdapter::iterator object,
  559. PtrMapAdapter& from ) // strong
  560. {
  561. return this->single_transfer( object, from );
  562. }
  563. template< class PtrMapAdapter >
  564. size_type transfer( BOOST_DEDUCED_TYPENAME PtrMapAdapter::iterator first,
  565. BOOST_DEDUCED_TYPENAME PtrMapAdapter::iterator last,
  566. PtrMapAdapter& from ) // basic
  567. {
  568. return this->single_transfer( first, last, from );
  569. }
  570. #if defined(BOOST_NO_SFINAE) || defined(BOOST_NO_FUNCTION_TEMPLATE_ORDERING)
  571. #else
  572. template< class PtrMapAdapter, class Range >
  573. BOOST_DEDUCED_TYPENAME boost::disable_if< boost::is_same< Range,
  574. BOOST_DEDUCED_TYPENAME PtrMapAdapter::iterator >,
  575. size_type >::type
  576. transfer( const Range& r, PtrMapAdapter& from ) // basic
  577. {
  578. return transfer( boost::begin(r), boost::end(r), from );
  579. }
  580. #endif
  581. template< class PtrMapAdapter >
  582. size_type transfer( PtrMapAdapter& from ) // basic
  583. {
  584. return transfer( from.begin(), from.end(), from );
  585. }
  586. };
  587. /////////////////////////////////////////////////////////////////////////
  588. // ptr_multimap_adapter
  589. /////////////////////////////////////////////////////////////////////////
  590. template
  591. <
  592. class T,
  593. class VoidPtrMultiMap,
  594. class CloneAllocator = heap_clone_allocator,
  595. bool Ordered = true
  596. >
  597. class ptr_multimap_adapter :
  598. public ptr_container_detail::ptr_map_adapter_base<T,VoidPtrMultiMap,CloneAllocator,Ordered>
  599. {
  600. typedef ptr_container_detail::ptr_map_adapter_base<T,VoidPtrMultiMap,CloneAllocator,Ordered>
  601. base_type;
  602. public: // typedefs
  603. typedef BOOST_DEDUCED_TYPENAME base_type::iterator
  604. iterator;
  605. typedef BOOST_DEDUCED_TYPENAME base_type::const_iterator
  606. const_iterator;
  607. typedef BOOST_DEDUCED_TYPENAME base_type::size_type
  608. size_type;
  609. typedef BOOST_DEDUCED_TYPENAME base_type::key_type
  610. key_type;
  611. typedef BOOST_DEDUCED_TYPENAME base_type::const_reference
  612. const_reference;
  613. typedef BOOST_DEDUCED_TYPENAME base_type::mapped_type
  614. mapped_type;
  615. typedef BOOST_DEDUCED_TYPENAME base_type::auto_type
  616. auto_type;
  617. typedef BOOST_DEDUCED_TYPENAME VoidPtrMultiMap::allocator_type
  618. allocator_type;
  619. private:
  620. void safe_insert( const key_type& key, auto_type ptr ) // strong
  621. {
  622. this->base().insert(
  623. std::make_pair( key, ptr.get() ) ); // strong, commit
  624. ptr.release(); // nothrow
  625. }
  626. template< typename II >
  627. void map_basic_clone_and_insert( II first, II last )
  628. {
  629. while( first != last )
  630. {
  631. const_reference pair = *first.base(); // nothrow
  632. auto_type ptr( this->null_policy_allocate_clone(pair.second), *this );
  633. // strong
  634. safe_insert( pair.first,
  635. boost::ptr_container::move( ptr ) );
  636. // strong, commit
  637. ++first;
  638. }
  639. }
  640. public:
  641. ptr_multimap_adapter()
  642. { }
  643. template< class SizeType >
  644. ptr_multimap_adapter( SizeType n,
  645. ptr_container_detail::unordered_associative_container_tag tag )
  646. : base_type( n, tag )
  647. { }
  648. template< class Comp >
  649. explicit ptr_multimap_adapter( const Comp& comp,
  650. const allocator_type& a )
  651. : base_type( comp, a ) { }
  652. template< class Hash, class Pred, class Allocator >
  653. ptr_multimap_adapter( const Hash& hash,
  654. const Pred& pred,
  655. const Allocator& a )
  656. : base_type( hash, pred, a )
  657. { }
  658. template< class InputIterator >
  659. ptr_multimap_adapter( InputIterator first, InputIterator last )
  660. {
  661. map_basic_clone_and_insert( first, last );
  662. }
  663. template< class InputIterator, class Comp >
  664. ptr_multimap_adapter( InputIterator first, InputIterator last,
  665. const Comp& comp,
  666. const allocator_type& a )
  667. : base_type( comp, a )
  668. {
  669. map_basic_clone_and_insert( first, last );
  670. }
  671. template< class InputIterator, class Hash, class Pred, class Allocator >
  672. ptr_multimap_adapter( InputIterator first, InputIterator last,
  673. const Hash& hash,
  674. const Pred& pred,
  675. const Allocator& a )
  676. : base_type( hash, pred, a )
  677. {
  678. map_basic_clone_and_insert( first, last );
  679. }
  680. ptr_multimap_adapter( const ptr_multimap_adapter& r )
  681. {
  682. map_basic_clone_and_insert( r.begin(), r.end() );
  683. }
  684. template< class Key, class U, class CA, bool b >
  685. ptr_multimap_adapter( const ptr_multimap_adapter<Key,U,CA,b>& r )
  686. {
  687. map_basic_clone_and_insert( r.begin(), r.end() );
  688. }
  689. #ifndef BOOST_NO_AUTO_PTR
  690. template< class U >
  691. explicit ptr_multimap_adapter( std::auto_ptr<U> r ) : base_type( r )
  692. { }
  693. #endif
  694. #ifndef BOOST_NO_CXX11_SMART_PTR
  695. template< class U >
  696. explicit ptr_multimap_adapter( std::unique_ptr<U> r ) : base_type( std::move( r ) )
  697. { }
  698. #endif
  699. ptr_multimap_adapter& operator=( ptr_multimap_adapter r )
  700. {
  701. this->swap( r );
  702. return *this;
  703. }
  704. #ifndef BOOST_NO_AUTO_PTR
  705. template< class U >
  706. ptr_multimap_adapter& operator=( std::auto_ptr<U> r )
  707. {
  708. base_type::operator=( r );
  709. return *this;
  710. }
  711. #endif
  712. #ifndef BOOST_NO_CXX11_SMART_PTR
  713. template< class U >
  714. ptr_multimap_adapter& operator=( std::unique_ptr<U> r )
  715. {
  716. base_type::operator=( std::move( r ) );
  717. return *this;
  718. }
  719. #endif
  720. using base_type::release;
  721. private:
  722. iterator insert_impl( const key_type& key, mapped_type x ) // strong
  723. {
  724. this->enforce_null_policy( x,
  725. "Null pointer in 'ptr_multimap_adapter::insert()'" );
  726. auto_type ptr( x, *this ); // nothrow
  727. BOOST_DEDUCED_TYPENAME base_type::ptr_iterator
  728. res = this->base().insert( std::make_pair( key, x ) );
  729. // strong, commit
  730. ptr.release(); // notrow
  731. return iterator( res );
  732. }
  733. iterator insert_impl( iterator before, const key_type& key, mapped_type x ) // strong
  734. {
  735. this->enforce_null_policy( x,
  736. "Null pointer in 'ptr_multimap_adapter::insert()'" );
  737. auto_type ptr( x, *this ); // nothrow
  738. BOOST_DEDUCED_TYPENAME base_type::ptr_iterator
  739. res = this->base().insert( before.base(),
  740. std::make_pair( key, x ) );
  741. // strong, commit
  742. ptr.release(); // notrow
  743. return iterator( res );
  744. }
  745. public:
  746. template< typename InputIterator >
  747. void insert( InputIterator first, InputIterator last ) // basic
  748. {
  749. map_basic_clone_and_insert( first, last );
  750. }
  751. template< class Range >
  752. void insert( const Range& r )
  753. {
  754. insert( boost::begin(r), boost::end(r) );
  755. }
  756. iterator insert( key_type& key, mapped_type x ) // strong
  757. {
  758. return insert_impl( key, x );
  759. }
  760. #ifndef BOOST_NO_AUTO_PTR
  761. template< class U >
  762. iterator insert( const key_type& key, std::auto_ptr<U> x )
  763. {
  764. return insert_impl( key, x.release() );
  765. }
  766. #endif
  767. #ifndef BOOST_NO_CXX11_SMART_PTR
  768. template< class U >
  769. iterator insert( const key_type& key, std::unique_ptr<U> x )
  770. {
  771. return insert_impl( key, x.release() );
  772. }
  773. #endif
  774. template< class F, class S >
  775. iterator insert( iterator before, ptr_container_detail::ref_pair<F,S> p ) // strong
  776. {
  777. this->enforce_null_policy( p.second,
  778. "Null pointer in 'ptr_multimap_adapter::insert()'" );
  779. iterator res = insert_impl( before, p.first,
  780. this->null_policy_allocate_clone( p.second ) );
  781. return res;
  782. }
  783. iterator insert( iterator before, key_type& key, mapped_type x ) // strong
  784. {
  785. return insert_impl( before, key, x );
  786. }
  787. #ifndef BOOST_NO_AUTO_PTR
  788. template< class U >
  789. iterator insert( iterator before, const key_type& key, std::auto_ptr<U> x ) // strong
  790. {
  791. return insert_impl( before, key, x.release() );
  792. }
  793. #endif
  794. #ifndef BOOST_NO_CXX11_SMART_PTR
  795. template< class U >
  796. iterator insert( iterator before, const key_type& key, std::unique_ptr<U> x ) // strong
  797. {
  798. return insert_impl( before, key, x.release() );
  799. }
  800. #endif
  801. template< class PtrMapAdapter >
  802. void transfer( BOOST_DEDUCED_TYPENAME PtrMapAdapter::iterator object,
  803. PtrMapAdapter& from ) // strong
  804. {
  805. this->multi_transfer( object, from );
  806. }
  807. template< class PtrMapAdapter >
  808. size_type transfer( BOOST_DEDUCED_TYPENAME PtrMapAdapter::iterator first,
  809. BOOST_DEDUCED_TYPENAME PtrMapAdapter::iterator last,
  810. PtrMapAdapter& from ) // basic
  811. {
  812. return this->multi_transfer( first, last, from );
  813. }
  814. #if defined(BOOST_NO_SFINAE) || defined(BOOST_NO_FUNCTION_TEMPLATE_ORDERING)
  815. #else
  816. template< class PtrMapAdapter, class Range >
  817. BOOST_DEDUCED_TYPENAME boost::disable_if< boost::is_same< Range,
  818. BOOST_DEDUCED_TYPENAME PtrMapAdapter::iterator >,
  819. size_type >::type
  820. transfer( const Range& r, PtrMapAdapter& from ) // basic
  821. {
  822. return transfer( boost::begin(r), boost::end(r), from );
  823. }
  824. #endif
  825. template< class PtrMapAdapter >
  826. void transfer( PtrMapAdapter& from ) // basic
  827. {
  828. transfer( from.begin(), from.end(), from );
  829. BOOST_ASSERT( from.empty() );
  830. }
  831. };
  832. template< class I, class F, class S >
  833. inline bool is_null( const ptr_map_iterator<I,F,S>& i )
  834. {
  835. return i->second == 0;
  836. }
  837. } // namespace 'boost'
  838. #if defined(BOOST_PTR_CONTAINER_DISABLE_DEPRECATED)
  839. #pragma GCC diagnostic pop
  840. #endif
  841. #endif