ptr_map_adapter.hpp 33 KB

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