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/
13 #ifndef BOOST_PTR_CONTAINER_DETAIL_REVERSIBLE_PTR_CONTAINER_HPP
14 #define BOOST_PTR_CONTAINER_DETAIL_REVERSIBLE_PTR_CONTAINER_HPP
16 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
20 #include <boost/ptr_container/detail/throw_exception.hpp>
21 #include <boost/ptr_container/detail/scoped_deleter.hpp>
22 #include <boost/ptr_container/detail/static_move_ptr.hpp>
23 #include <boost/ptr_container/exception.hpp>
24 #include <boost/ptr_container/clone_allocator.hpp>
25 #include <boost/ptr_container/nullable.hpp>
27 #ifdef BOOST_NO_SFINAE
29 #include <boost/range/functions.hpp>
32 #include <boost/config.hpp>
33 #include <boost/iterator/reverse_iterator.hpp>
34 #include <boost/range/iterator.hpp>
35 #include <boost/utility/enable_if.hpp>
36 #include <boost/type_traits/is_pointer.hpp>
37 #include <boost/type_traits/is_integral.hpp>
41 #if BOOST_WORKAROUND(BOOST_MSVC, >= 1400)
43 #pragma warning(disable:4127)
49 namespace ptr_container_detail
51 template< class CloneAllocator >
55 void operator()( const T* p ) const
57 CloneAllocator::deallocate_clone( p );
62 struct is_pointer_or_integral
64 BOOST_STATIC_CONSTANT(bool, value = is_pointer<T>::value || is_integral<T>::value );
67 struct is_pointer_or_integral_tag {};
68 struct is_range_tag {};
69 struct sequence_tag {};
70 struct fixed_length_sequence_tag : sequence_tag {};
71 struct associative_container_tag {};
72 struct ordered_associative_container_tag : associative_container_tag {};
73 struct unordered_associative_container_tag : associative_container_tag {};
82 class reversible_ptr_container
85 BOOST_STATIC_CONSTANT( bool, allow_null = Config::allow_null );
87 typedef BOOST_DEDUCED_TYPENAME Config::value_type Ty_;
89 template< bool allow_null_values >
90 struct null_clone_allocator
92 template< class Iter >
93 static Ty_* allocate_clone_from_iterator( Iter i )
95 return allocate_clone( Config::get_const_pointer( i ) );
98 static Ty_* allocate_clone( const Ty_* x )
100 if( allow_null_values )
107 BOOST_ASSERT( x != 0 && "Cannot insert clone of null!" );
110 Ty_* res = CloneAllocator::allocate_clone( *x );
111 BOOST_ASSERT( typeid(*res) == typeid(*x) &&
112 "CloneAllocator::allocate_clone() does not clone the "
113 "object properly. Check that new_clone() is implemented"
118 static void deallocate_clone( const Ty_* x )
120 if( allow_null_values )
126 CloneAllocator::deallocate_clone( x );
130 typedef BOOST_DEDUCED_TYPENAME Config::void_container_type Cont;
131 #if BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x564))
132 typedef null_clone_allocator<reversible_ptr_container::allow_null>
135 typedef null_clone_allocator<allow_null> null_cloner_type;
137 typedef clone_deleter<null_cloner_type> Deleter;
142 Cont& base() { return c_; }
143 protected: // having this public could break encapsulation
144 const Cont& base() const { return c_; }
147 typedef Ty_* value_type;
148 typedef Ty_* pointer;
149 typedef Ty_& reference;
150 typedef const Ty_& const_reference;
152 typedef BOOST_DEDUCED_TYPENAME Config::iterator
154 typedef BOOST_DEDUCED_TYPENAME Config::const_iterator
156 typedef boost::reverse_iterator< iterator >
158 typedef boost::reverse_iterator< const_iterator >
159 const_reverse_iterator;
160 typedef BOOST_DEDUCED_TYPENAME Cont::difference_type
162 typedef BOOST_DEDUCED_TYPENAME Cont::size_type
164 typedef BOOST_DEDUCED_TYPENAME Config::allocator_type
166 typedef CloneAllocator clone_allocator_type;
167 typedef ptr_container_detail::static_move_ptr<Ty_,Deleter>
172 typedef ptr_container_detail::scoped_deleter<Ty_,null_cloner_type>
174 typedef BOOST_DEDUCED_TYPENAME Cont::iterator
176 typedef BOOST_DEDUCED_TYPENAME Cont::const_iterator
180 template< class InputIterator >
181 void copy( InputIterator first, InputIterator last )
183 std::copy( first, last, begin() );
186 void copy( const reversible_ptr_container& r )
188 copy( r.begin(), r.end() );
191 void copy_clones_and_release( scoped_deleter& sd ) // nothrow
193 BOOST_ASSERT( size_type( std::distance( sd.begin(), sd.end() ) ) == c_.size() );
194 std::copy( sd.begin(), sd.end(), c_.begin() );
198 template< class ForwardIterator >
199 void clone_assign( ForwardIterator first,
200 ForwardIterator last ) // strong
202 BOOST_ASSERT( first != last );
203 scoped_deleter sd( first, last ); // strong
204 copy_clones_and_release( sd ); // nothrow
207 template< class ForwardIterator >
208 void clone_back_insert( ForwardIterator first,
209 ForwardIterator last )
211 BOOST_ASSERT( first != last );
212 scoped_deleter sd( first, last );
213 insert_clones_and_release( sd, end() );
218 remove( begin(), end() );
223 void insert_clones_and_release( scoped_deleter& sd,
224 iterator where ) // strong
227 // 'c_.insert' always provides the strong guarantee for T* elements
228 // since a copy constructor of a pointer cannot throw
230 c_.insert( where.base(),
231 sd.begin(), sd.end() );
235 void insert_clones_and_release( scoped_deleter& sd ) // strong
237 c_.insert( sd.begin(), sd.end() );
242 void remove( U* ptr )
244 null_policy_deallocate_clone( ptr );
250 null_policy_deallocate_clone( Config::get_const_pointer(i) );
254 void remove( I first, I last )
256 for( ; first != last; ++first )
260 static void enforce_null_policy( const Ty_* x, const char* msg )
264 BOOST_PTR_CONTAINER_THROW_EXCEPTION( 0 == x && "null not allowed",
269 static Ty_* null_policy_allocate_clone( const Ty_* x )
271 return null_cloner_type::allocate_clone( x );
274 static void null_policy_deallocate_clone( const Ty_* x )
276 null_cloner_type::deallocate_clone( x );
280 template< class ForwardIterator >
281 ForwardIterator advance( ForwardIterator begin, size_type n )
283 ForwardIterator iter = begin;
284 std::advance( iter, n );
289 void constructor_impl( I first, I last, std::input_iterator_tag ) // basic
291 while( first != last )
293 insert( end(), null_cloner_type::allocate_clone_from_iterator(first) );
299 void constructor_impl( I first, I last, std::forward_iterator_tag ) // strong
303 clone_back_insert( first, last );
307 void associative_constructor_impl( I first, I last ) // strong
312 scoped_deleter sd( first, last );
313 insert_clones_and_release( sd );
316 public: // foundation! should be protected!
317 reversible_ptr_container()
320 template< class SizeType >
321 reversible_ptr_container( SizeType n, unordered_associative_container_tag )
325 template< class SizeType >
326 reversible_ptr_container( SizeType n, fixed_length_sequence_tag )
330 template< class SizeType >
331 reversible_ptr_container( SizeType n, const allocator_type& a,
332 fixed_length_sequence_tag )
336 explicit reversible_ptr_container( const allocator_type& a )
340 template< class PtrContainer >
341 explicit reversible_ptr_container( std::auto_ptr<PtrContainer> clone )
346 reversible_ptr_container( const reversible_ptr_container& r )
348 constructor_impl( r.begin(), r.end(), std::forward_iterator_tag() );
351 template< class C, class V >
352 reversible_ptr_container( const reversible_ptr_container<C,V>& r )
354 constructor_impl( r.begin(), r.end(), std::forward_iterator_tag() );
357 template< class PtrContainer >
358 reversible_ptr_container& operator=( std::auto_ptr<PtrContainer> clone ) // nothrow
364 reversible_ptr_container& operator=( reversible_ptr_container r ) // strong
370 // overhead: null-initilization of container pointer (very cheap compared to cloning)
371 // overhead: 1 heap allocation (very cheap compared to cloning)
372 template< class InputIterator >
373 reversible_ptr_container( InputIterator first,
375 const allocator_type& a = allocator_type() ) // basic, strong
378 constructor_impl( first, last,
379 #if BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x564))
381 BOOST_DEDUCED_TYPENAME
383 iterator_category<InputIterator>::type() );
386 template< class Compare >
387 reversible_ptr_container( const Compare& comp,
388 const allocator_type& a )
391 template< class ForwardIterator >
392 reversible_ptr_container( ForwardIterator first,
393 ForwardIterator last,
394 fixed_length_sequence_tag )
395 : c_( std::distance(first,last) )
397 constructor_impl( first, last,
398 std::forward_iterator_tag() );
401 template< class SizeType, class InputIterator >
402 reversible_ptr_container( SizeType n,
405 fixed_length_sequence_tag )
408 constructor_impl( first, last,
409 #if BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x564))
411 BOOST_DEDUCED_TYPENAME
413 iterator_category<InputIterator>::type() );
416 template< class Compare >
417 reversible_ptr_container( const Compare& comp,
418 const allocator_type& a,
419 associative_container_tag )
423 template< class InputIterator >
424 reversible_ptr_container( InputIterator first,
426 associative_container_tag )
428 associative_constructor_impl( first, last );
431 template< class InputIterator, class Compare >
432 reversible_ptr_container( InputIterator first,
435 const allocator_type& a,
436 associative_container_tag )
439 associative_constructor_impl( first, last );
442 explicit reversible_ptr_container( size_type n )
445 template< class Hash, class Pred >
446 reversible_ptr_container( const Hash& h,
448 const allocator_type& a )
449 : c_( h, pred, a ) {}
451 template< class InputIterator, class Hash, class Pred >
452 reversible_ptr_container( InputIterator first,
456 const allocator_type& a )
459 associative_constructor_impl( first, last );
463 ~reversible_ptr_container()
470 allocator_type get_allocator() const
472 return c_.get_allocator();
475 public: // container requirements
477 { return iterator( c_.begin() ); }
478 const_iterator begin() const
479 { return const_iterator( c_.begin() ); }
481 { return iterator( c_.end() ); }
482 const_iterator end() const
483 { return const_iterator( c_.end() ); }
485 reverse_iterator rbegin()
486 { return reverse_iterator( this->end() ); }
487 const_reverse_iterator rbegin() const
488 { return const_reverse_iterator( this->end() ); }
489 reverse_iterator rend()
490 { return reverse_iterator( this->begin() ); }
491 const_reverse_iterator rend() const
492 { return const_reverse_iterator( this->begin() ); }
494 const_iterator cbegin() const
495 { return const_iterator( c_.begin() ); }
496 const_iterator cend() const
497 { return const_iterator( c_.end() ); }
499 const_reverse_iterator crbegin() const
500 { return const_reverse_iterator( this->end() ); }
501 const_reverse_iterator crend() const
502 { return const_reverse_iterator( this->begin() ); }
504 void swap( reversible_ptr_container& r ) // nothrow
509 size_type size() const // nothrow
514 size_type max_size() const // nothrow
516 return c_.max_size();
519 bool empty() const // nothrow
524 public: // optional container requirements
526 bool operator==( const reversible_ptr_container& r ) const // nothrow
528 if( size() != r.size() )
531 return std::equal( begin(), end(), r.begin() );
534 bool operator!=( const reversible_ptr_container& r ) const // nothrow
536 return !(*this == r);
539 bool operator<( const reversible_ptr_container& r ) const // nothrow
541 return std::lexicographical_compare( begin(), end(), r.begin(), r.end() );
544 bool operator<=( const reversible_ptr_container& r ) const // nothrow
549 bool operator>( const reversible_ptr_container& r ) const // nothrow
554 bool operator>=( const reversible_ptr_container& r ) const // nothrow
561 iterator insert( iterator before, Ty_* x )
563 enforce_null_policy( x, "Null pointer in 'insert()'" );
565 auto_type ptr( x ); // nothrow
566 iterator res( c_.insert( before.base(), x ) ); // strong, commit
567 ptr.release(); // nothrow
572 iterator insert( iterator before, std::auto_ptr<U> x )
574 return insert( before, x.release() );
577 iterator erase( iterator x ) // nothrow
579 BOOST_ASSERT( !empty() );
580 BOOST_ASSERT( x != end() );
583 return iterator( c_.erase( x.base() ) );
586 iterator erase( iterator first, iterator last ) // nothrow
588 remove( first, last );
589 return iterator( c_.erase( first.base(),
593 template< class Range >
594 iterator erase( const Range& r )
596 return erase( boost::begin(r), boost::end(r) );
605 public: // access interface
607 auto_type release( iterator where )
609 BOOST_ASSERT( where != end() );
611 BOOST_PTR_CONTAINER_THROW_EXCEPTION( empty(), bad_ptr_container_operation,
612 "'release()' on empty container" );
614 auto_type ptr( Config::get_pointer( where ) ); // nothrow
615 c_.erase( where.base() ); // nothrow
616 return boost::ptr_container_detail::move( ptr );
619 auto_type replace( iterator where, Ty_* x ) // strong
621 BOOST_ASSERT( where != end() );
623 enforce_null_policy( x, "Null pointer in 'replace()'" );
627 BOOST_PTR_CONTAINER_THROW_EXCEPTION( empty(), bad_ptr_container_operation,
628 "'replace()' on empty container" );
630 auto_type old( Config::get_pointer( where ) ); // nothrow
631 const_cast<void*&>(*where.base()) = ptr.release();
632 return boost::ptr_container_detail::move( old );
636 auto_type replace( iterator where, std::auto_ptr<U> x )
638 return replace( where, x.release() );
641 auto_type replace( size_type idx, Ty_* x ) // strong
643 enforce_null_policy( x, "Null pointer in 'replace()'" );
647 BOOST_PTR_CONTAINER_THROW_EXCEPTION( idx >= size(), bad_index,
648 "'replace()' out of bounds" );
650 auto_type old( static_cast<Ty_*>( c_[idx] ) ); // nothrow
651 c_[idx] = ptr.release(); // nothrow, commit
652 return boost::ptr_container_detail::move( old );
656 auto_type replace( size_type idx, std::auto_ptr<U> x )
658 return replace( idx, x.release() );
661 }; // 'reversible_ptr_container'
664 #if BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x564))
665 #define BOOST_PTR_CONTAINER_DEFINE_RELEASE( base_type ) \
666 typename base_type::auto_type \
667 release( typename base_type::iterator i ) \
669 return boost::ptr_container_detail::move(base_type::release(i)); \
672 #define BOOST_PTR_CONTAINER_DEFINE_RELEASE( base_type ) \
673 using base_type::release;
677 // two-phase lookup of template functions
678 // is buggy on most compilers, so we use a macro instead
680 #define BOOST_PTR_CONTAINER_DEFINE_RELEASE_AND_CLONE( PC, base_type, this_type ) \
681 explicit PC( std::auto_ptr<this_type> r ) \
682 : base_type ( r ) { } \
684 PC& operator=( std::auto_ptr<this_type> r ) \
686 base_type::operator=( r ); \
690 std::auto_ptr<this_type> release() \
692 std::auto_ptr<this_type> ptr( new this_type );\
693 this->swap( *ptr ); \
696 BOOST_PTR_CONTAINER_DEFINE_RELEASE( base_type ) \
698 std::auto_ptr<this_type> clone() const \
700 return std::auto_ptr<this_type>( new this_type( this->begin(), this->end() ) ); \
703 #define BOOST_PTR_CONTAINER_DEFINE_COPY_CONSTRUCTORS( PC, base_type ) \
705 template< class U > \
706 PC( const PC<U>& r ) : base_type( r ) { } \
708 PC& operator=( PC r ) \
715 #define BOOST_PTR_CONTAINER_DEFINE_CONSTRUCTORS( PC, base_type ) \
716 typedef BOOST_DEDUCED_TYPENAME base_type::iterator iterator; \
717 typedef BOOST_DEDUCED_TYPENAME base_type::size_type size_type; \
718 typedef BOOST_DEDUCED_TYPENAME base_type::const_reference const_reference; \
719 typedef BOOST_DEDUCED_TYPENAME base_type::allocator_type allocator_type; \
721 explicit PC( const allocator_type& a ) : base_type(a) {} \
722 template< class InputIterator > \
723 PC( InputIterator first, InputIterator last ) : base_type( first, last ) {} \
724 template< class InputIterator > \
725 PC( InputIterator first, InputIterator last, \
726 const allocator_type& a ) : base_type( first, last, a ) {}
728 #define BOOST_PTR_CONTAINER_DEFINE_NON_INHERITED_MEMBERS( PC, base_type, this_type ) \
729 BOOST_PTR_CONTAINER_DEFINE_CONSTRUCTORS( PC, base_type ) \
730 BOOST_PTR_CONTAINER_DEFINE_RELEASE_AND_CLONE( PC, base_type, this_type )
732 #define BOOST_PTR_CONTAINER_DEFINE_SEQEUENCE_MEMBERS( PC, base_type, this_type ) \
733 BOOST_PTR_CONTAINER_DEFINE_NON_INHERITED_MEMBERS( PC, base_type, this_type ) \
734 BOOST_PTR_CONTAINER_DEFINE_COPY_CONSTRUCTORS( PC, base_type )
736 } // namespace 'ptr_container_detail'
739 // @remark: expose movability of internal move-pointer
741 namespace ptr_container
743 using ptr_container_detail::move;
746 } // namespace 'boost'
748 #if BOOST_WORKAROUND(BOOST_MSVC, >= 1400)