2 // Boost.Pointer Container
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)
9 // For more information, see http://www.boost.org/libs/ptr_container/
12 #ifndef BOOST_PTR_CONTAINER_DETAIL_PTR_MAP_ADAPTER_HPP
13 #define BOOST_PTR_CONTAINER_DETAIL_PTR_MAP_ADAPTER_HPP
15 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
19 #include <boost/ptr_container/detail/map_iterator.hpp>
20 #include <boost/ptr_container/detail/associative_ptr_container.hpp>
21 #include <boost/static_assert.hpp>
22 #include <boost/range/iterator_range.hpp>
26 namespace ptr_container_detail
36 typedef BOOST_DEDUCED_TYPENAME remove_nullable<T>::type
41 typedef BOOST_DEDUCED_TYPENAME VoidPtrMap::allocator_type
44 typedef BOOST_DEDUCED_TYPENAME VoidPtrMap::key_compare
47 typedef BOOST_DEDUCED_TYPENAME VoidPtrMap::value_compare
50 typedef BOOST_DEDUCED_TYPENAME VoidPtrMap::key_type
55 typedef ptr_map_iterator< BOOST_DEDUCED_TYPENAME VoidPtrMap::iterator, key_type, U* const >
58 typedef ptr_map_iterator< BOOST_DEDUCED_TYPENAME VoidPtrMap::const_iterator, key_type, const U* const>
61 template< class Iter >
62 static U* get_pointer( Iter i )
67 template< class Iter >
68 static const U* get_const_pointer( Iter i )
73 BOOST_STATIC_CONSTANT( bool, allow_null = boost::is_nullable<T>::value );
84 class ptr_map_adapter_base :
85 public ptr_container_detail::associative_ptr_container< map_config<T,VoidPtrMap>,
88 typedef ptr_container_detail::associative_ptr_container< map_config<T,VoidPtrMap>,
92 typedef map_config<T,VoidPtrMap> config;
94 typedef ptr_map_adapter_base<T,VoidPtrMap,CloneAllocator> this_type;
98 typedef BOOST_DEDUCED_TYPENAME base_type::allocator_type
100 typedef BOOST_DEDUCED_TYPENAME base_type::iterator
102 typedef BOOST_DEDUCED_TYPENAME base_type::const_iterator
104 typedef BOOST_DEDUCED_TYPENAME base_type::size_type
106 typedef BOOST_DEDUCED_TYPENAME base_type::key_type
108 typedef BOOST_DEDUCED_TYPENAME base_type::auto_type
110 typedef BOOST_DEDUCED_TYPENAME base_type::value_type
112 typedef BOOST_DEDUCED_TYPENAME base_type::reference
114 typedef BOOST_DEDUCED_TYPENAME base_type::const_reference
115 const_mapped_reference;
116 typedef BOOST_DEDUCED_TYPENAME iterator_value<iterator>::type
120 typedef BOOST_DEDUCED_TYPENAME iterator_value<const_iterator>::type
124 typedef const_reference
128 const_mapped_reference lookup( const key_type& key ) const
130 const_iterator i = this->find( key );
131 if( i != this->end() )
134 BOOST_PTR_CONTAINER_THROW_EXCEPTION( true, bad_ptr_container_operation,
135 "'ptr_map/multimap::at()' could"
139 struct eraser // scope guard
143 const key_type& key_;
145 eraser( VoidPtrMap* m, const key_type& key )
146 : released_(false), m_(m), key_(key)
155 void release() { released_ = true; }
158 mapped_reference insert_lookup( const key_type& key )
160 void*& ref = this->c_private()[key];
163 return *static_cast<mapped_type>(ref);
167 eraser e(&this->c_private(),key); // nothrow
168 mapped_type res = new T(); // strong
169 ref = res; // nothrow
170 e.release(); // nothrow
177 ptr_map_adapter_base( const allocator_type& a = allocator_type() )
181 template< class InputIterator >
182 ptr_map_adapter_base( InputIterator first, InputIterator last,
183 const allocator_type& a = allocator_type() )
184 : base_type( first, last, a )
187 template< class Compare, class Allocator >
188 explicit ptr_map_adapter_base( const Compare& comp,
190 : base_type( comp, a )
193 template< class PtrContainer >
194 ptr_map_adapter_base( std::auto_ptr<PtrContainer> clone )
198 template< typename PtrContainer >
199 void operator=( std::auto_ptr<PtrContainer> clone )
201 base_type::operator=( clone );
204 iterator find( const key_type& x )
206 return iterator( this->c_private().find( x ) );
209 const_iterator find( const key_type& x ) const
211 return const_iterator( this->c_private().find( x ) );
214 size_type count( const key_type& x ) const
216 return this->c_private().count( x );
219 iterator lower_bound( const key_type& x )
221 return iterator( this->c_private().lower_bound( x ) );
224 const_iterator lower_bound( const key_type& x ) const
226 return const_iterator( this->c_private().lower_bound( x ) );
229 iterator upper_bound( const key_type& x )
231 return iterator( this->c_private().upper_bound( x ) );
234 const_iterator upper_bound( const key_type& x ) const
236 return const_iterator( this->c_private().upper_bound( x ) );
239 iterator_range<iterator> equal_range( const key_type& x )
241 std::pair<BOOST_DEDUCED_TYPENAME base_type::ptr_iterator,
242 BOOST_DEDUCED_TYPENAME base_type::ptr_iterator>
243 p = this->c_private().equal_range( x );
244 return make_iterator_range( iterator( p.first ), iterator( p.second ) );
247 iterator_range<const_iterator> equal_range( const key_type& x ) const
249 std::pair<BOOST_DEDUCED_TYPENAME base_type::ptr_const_iterator,
250 BOOST_DEDUCED_TYPENAME base_type::ptr_const_iterator>
251 p = this->c_private().equal_range( x );
252 return make_iterator_range( const_iterator( p.first ),
253 const_iterator( p.second ) );
256 mapped_reference at( const key_type& key )
258 return const_cast<mapped_reference>( lookup( key ) );
261 const_mapped_reference at( const key_type& key ) const
263 return lookup( key );
266 mapped_reference operator[]( const key_type& key )
268 return insert_lookup( key );
271 auto_type replace( iterator where, mapped_type x ) // strong
273 BOOST_ASSERT( where != this->end() );
275 this->enforce_null_policy( x, "Null pointer in 'replace()'" );
279 BOOST_PTR_CONTAINER_THROW_EXCEPTION( this->empty(),
280 bad_ptr_container_operation,
281 "'replace()' on empty container" );
283 auto_type old( where->second ); // nothrow
284 where.base()->second = ptr.release(); // nothrow, commit
289 auto_type replace( iterator where, std::auto_ptr<U> x )
291 return replace( where, x.release() );
294 public: // serialization
296 template< class Archive >
297 void save( Archive& ar, const unsigned ) const
299 ar & ptr_container_detail::serialize_as_const( this->size() );
301 const_iterator i = this->begin(), e = this->end();
305 ar & ptr_container_detail::serialize_as_const( i->second );
310 } // ptr_container_detail
312 /////////////////////////////////////////////////////////////////////////
314 /////////////////////////////////////////////////////////////////////////
320 class CloneAllocator = heap_clone_allocator
322 class ptr_map_adapter :
323 public ptr_container_detail::ptr_map_adapter_base<T,VoidPtrMap,CloneAllocator>
325 typedef ptr_container_detail::ptr_map_adapter_base<T,VoidPtrMap,CloneAllocator>
329 typedef BOOST_DEDUCED_TYPENAME base_type::iterator
331 typedef BOOST_DEDUCED_TYPENAME base_type::const_iterator
333 typedef BOOST_DEDUCED_TYPENAME base_type::size_type
335 typedef BOOST_DEDUCED_TYPENAME base_type::key_type
337 typedef BOOST_DEDUCED_TYPENAME base_type::const_reference
339 typedef BOOST_DEDUCED_TYPENAME base_type::auto_type
341 typedef BOOST_DEDUCED_TYPENAME VoidPtrMap::key_compare
343 typedef BOOST_DEDUCED_TYPENAME VoidPtrMap::allocator_type
345 typedef BOOST_DEDUCED_TYPENAME base_type::mapped_type
349 void safe_insert( const key_type& key, auto_type ptr ) // strong
351 std::pair<BOOST_DEDUCED_TYPENAME base_type::ptr_iterator,bool>
353 this->c_private().insert( std::make_pair( key, ptr.get() ) ); // strong, commit
354 if( res.second ) // nothrow
355 ptr.release(); // nothrow
359 void map_basic_clone_and_insert( II first, II last )
361 while( first != last )
363 if( this->find( first->first ) == this->end() )
365 const_reference p = *first.base(); // nothrow
366 auto_type ptr( this->null_policy_allocate_clone( p.second ) );
368 this->safe_insert( p.first, ptr_container_detail::
369 move( ptr ) );// strong, commit
377 explicit ptr_map_adapter( const key_compare& comp = key_compare(),
378 const allocator_type& a = allocator_type() )
379 : base_type( comp, a ) { }
381 template< class InputIterator >
382 ptr_map_adapter( InputIterator first, InputIterator last,
383 const key_compare& comp = key_compare(),
384 const allocator_type& a = allocator_type() )
385 : base_type( comp, a )
387 map_basic_clone_and_insert( first, last );
391 ptr_map_adapter( std::auto_ptr<U> r ) : base_type( r )
395 void operator=( std::auto_ptr<U> r )
397 base_type::operator=( r );
400 using base_type::release;
402 template< typename InputIterator >
403 void insert( InputIterator first, InputIterator last ) // basic
405 map_basic_clone_and_insert( first, last );
408 template< class Range >
409 void insert( const Range& r )
411 insert( boost::begin(r), boost::end(r) );
415 std::pair<iterator,bool> insert_impl( const key_type& key, mapped_type x ) // strong
417 this->enforce_null_policy( x, "Null pointer in ptr_map_adapter::insert()" );
418 auto_type ptr( x ); // nothrow
420 std::pair<BOOST_DEDUCED_TYPENAME base_type::ptr_iterator,bool>
421 res = this->c_private().insert( std::make_pair( key, x ) ); // strong, commit
422 if( res.second ) // nothrow
423 ptr.release(); // nothrow
424 return std::make_pair( iterator( res.first ), res.second ); // nothrow
429 std::pair<iterator,bool> insert( key_type& key, mapped_type x )
431 return insert_impl( key, x );
435 std::pair<iterator,bool> insert( const key_type& key, std::auto_ptr<U> x )
437 return insert_impl( key, x.release() );
440 template< class PtrMapAdapter >
441 bool transfer( BOOST_DEDUCED_TYPENAME PtrMapAdapter::iterator object,
442 PtrMapAdapter& from ) // strong
444 return this->single_transfer( object, from );
447 template< class PtrMapAdapter >
448 size_type transfer( BOOST_DEDUCED_TYPENAME PtrMapAdapter::iterator first,
449 BOOST_DEDUCED_TYPENAME PtrMapAdapter::iterator last,
450 PtrMapAdapter& from ) // basic
452 return this->single_transfer( first, last, from );
455 #if defined(BOOST_NO_SFINAE) || BOOST_WORKAROUND(__SUNPRO_CC, <= 0x580)
458 template< class PtrMapAdapter, class Range >
459 BOOST_DEDUCED_TYPENAME boost::disable_if< boost::is_same< Range,
460 BOOST_DEDUCED_TYPENAME PtrMapAdapter::iterator >,
462 transfer( const Range& r, PtrMapAdapter& from ) // basic
464 return transfer( boost::begin(r), boost::end(r), from );
469 template< class PtrMapAdapter >
470 size_type transfer( PtrMapAdapter& from ) // basic
472 return transfer( from.begin(), from.end(), from );
475 public: // serialization
477 template< class Archive >
478 void load( Archive& ar, const unsigned ) // strong
484 for( size_type i = 0u; i != n; ++i )
490 std::pair<iterator,bool> p = this->insert( key, value );
491 ar.reset_object_address( &p.first->first, &key );
495 BOOST_SERIALIZATION_SPLIT_MEMBER()
499 /////////////////////////////////////////////////////////////////////////
500 // ptr_multimap_adapter
501 /////////////////////////////////////////////////////////////////////////
506 class VoidPtrMultiMap,
507 class CloneAllocator = heap_clone_allocator
509 class ptr_multimap_adapter :
510 public ptr_container_detail::ptr_map_adapter_base<T,VoidPtrMultiMap,CloneAllocator>
512 typedef ptr_container_detail::ptr_map_adapter_base<T,VoidPtrMultiMap,CloneAllocator>
516 typedef BOOST_DEDUCED_TYPENAME base_type::iterator
518 typedef BOOST_DEDUCED_TYPENAME base_type::const_iterator
520 typedef BOOST_DEDUCED_TYPENAME base_type::size_type
522 typedef BOOST_DEDUCED_TYPENAME base_type::key_type
524 typedef BOOST_DEDUCED_TYPENAME base_type::const_reference
526 typedef BOOST_DEDUCED_TYPENAME base_type::mapped_type
528 typedef BOOST_DEDUCED_TYPENAME base_type::auto_type
530 typedef BOOST_DEDUCED_TYPENAME VoidPtrMultiMap::key_compare
532 typedef BOOST_DEDUCED_TYPENAME VoidPtrMultiMap::allocator_type
536 void safe_insert( const key_type& key, auto_type ptr ) // strong
538 this->c_private().insert(
539 std::make_pair( key, ptr.get() ) ); // strong, commit
540 ptr.release(); // nothrow
543 template< typename II >
544 void map_basic_clone_and_insert( II first, II last )
546 while( first != last )
548 const_reference pair = *first.base(); // nothrow
549 auto_type ptr( this->null_policy_allocate_clone( pair.second ) );
551 safe_insert( pair.first, ptr_container_detail::
552 move( ptr ) ); // strong, commit
559 explicit ptr_multimap_adapter( const key_compare& comp = key_compare(),
560 const allocator_type& a = allocator_type() )
561 : base_type( comp, a ) { }
563 template< class InputIterator >
564 ptr_multimap_adapter( InputIterator first, InputIterator last,
565 const key_compare& comp = key_compare(),
566 const allocator_type& a = allocator_type() )
567 : base_type( comp, a )
569 map_basic_clone_and_insert( first, last );
573 ptr_multimap_adapter( std::auto_ptr<U> r ) : base_type( r )
577 void operator=( std::auto_ptr<U> r )
579 base_type::operator=( r );
582 using base_type::release;
584 template< typename InputIterator >
585 void insert( InputIterator first, InputIterator last ) // basic
587 map_basic_clone_and_insert( first, last );
590 template< class Range >
591 void insert( const Range& r )
593 insert( boost::begin(r), boost::end(r) );
596 iterator insert( key_type& key, mapped_type x ) // strong
598 this->enforce_null_policy( x,
599 "Null pointer in 'ptr_multimap_adapter::insert()'" );
601 auto_type ptr( x ); // nothrow
602 BOOST_DEDUCED_TYPENAME base_type::ptr_iterator
603 res = this->c_private().insert( std::make_pair( key, x ) );
605 ptr.release(); // notrow
606 return iterator( res );
610 iterator insert( key_type& key, std::auto_ptr<U> x )
612 return insert( key, x.release() );
615 template< class PtrMapAdapter >
616 void transfer( BOOST_DEDUCED_TYPENAME PtrMapAdapter::iterator object,
617 PtrMapAdapter& from ) // strong
619 this->multi_transfer( object, from );
622 template< class PtrMapAdapter >
623 size_type transfer( BOOST_DEDUCED_TYPENAME PtrMapAdapter::iterator first,
624 BOOST_DEDUCED_TYPENAME PtrMapAdapter::iterator last,
625 PtrMapAdapter& from ) // basic
627 return this->multi_transfer( first, last, from );
630 #if defined(BOOST_NO_SFINAE) || BOOST_WORKAROUND(__SUNPRO_CC, <= 0x580)
633 template< class PtrMapAdapter, class Range >
634 BOOST_DEDUCED_TYPENAME boost::disable_if< boost::is_same< Range,
635 BOOST_DEDUCED_TYPENAME PtrMapAdapter::iterator >,
637 transfer( const Range& r, PtrMapAdapter& from ) // basic
639 return transfer( boost::begin(r), boost::end(r), from );
643 template< class PtrMapAdapter >
644 void transfer( PtrMapAdapter& from ) // basic
646 transfer( from.begin(), from.end(), from );
647 BOOST_ASSERT( from.empty() );
650 public: // serialization
652 template< class Archive >
653 void load( Archive& ar, const unsigned ) // basic
659 for( size_type i = 0u; i != n; ++i )
665 iterator p = this->insert( key, value );
666 ar.reset_object_address( &p->first, &key );
670 BOOST_SERIALIZATION_SPLIT_MEMBER()
674 template< class I, class F, class S >
675 inline bool is_null( const ptr_map_iterator<I,F,S>& i )
677 return i->second == 0;
680 } // namespace 'boost'