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_PTR_SEQUENCE_ADAPTER_HPP
13 #define BOOST_PTR_CONTAINER_PTR_SEQUENCE_ADAPTER_HPP
15 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
20 #include <boost/ptr_container/detail/reversible_ptr_container.hpp>
21 #include <boost/ptr_container/indirect_fun.hpp>
22 #include <boost/ptr_container/detail/void_ptr_iterator.hpp>
23 #include <boost/type_traits/remove_pointer.hpp>
24 #include <boost/type_traits/is_same.hpp>
29 namespace ptr_container_detail
36 struct sequence_config
38 typedef BOOST_DEDUCED_TYPENAME remove_nullable<T>::type
43 typedef BOOST_DEDUCED_TYPENAME VoidPtrSeq::allocator_type
48 typedef void_ptr_iterator<
49 BOOST_DEDUCED_TYPENAME VoidPtrSeq::iterator, U >
52 typedef void_ptr_iterator<
53 BOOST_DEDUCED_TYPENAME VoidPtrSeq::const_iterator, const U >
56 #if defined(BOOST_NO_SFINAE) || defined(BOOST_NO_FUNCTION_TEMPLATE_ORDERING)
58 template< class Iter >
59 static U* get_pointer( Iter i )
61 return static_cast<U*>( *i.base() );
65 template< class Iter >
66 static U* get_pointer( void_ptr_iterator<Iter,U> i )
68 return static_cast<U*>( *i.base() );
71 template< class Iter >
72 static U* get_pointer( Iter i )
78 #if defined(BOOST_NO_SFINAE) && !BOOST_WORKAROUND(__MWERKS__, <= 0x3003)
80 template< class Iter >
81 static const U* get_const_pointer( Iter i )
83 return static_cast<const U*>( *i.base() );
86 #else // BOOST_NO_SFINAE
88 #if BOOST_WORKAROUND(__MWERKS__, <= 0x3003)
89 template< class Iter >
90 static const U* get_const_pointer( void_ptr_iterator<Iter,U> i )
92 return static_cast<const U*>( *i.base() );
94 #else // BOOST_WORKAROUND
95 template< class Iter >
96 static const U* get_const_pointer( void_ptr_iterator<Iter,const U> i )
98 return static_cast<const U*>( *i.base() );
100 #endif // BOOST_WORKAROUND
102 template< class Iter >
103 static const U* get_const_pointer( Iter i )
107 #endif // BOOST_NO_SFINAE
109 BOOST_STATIC_CONSTANT(bool, allow_null = boost::is_nullable<T>::value );
112 } // ptr_container_detail
115 template< class Iterator, class T >
116 inline bool is_null( void_ptr_iterator<Iterator,T> i )
118 return *i.base() == 0;
127 class CloneAllocator = heap_clone_allocator
129 class ptr_sequence_adapter : public
130 ptr_container_detail::reversible_ptr_container< ptr_container_detail::sequence_config<T,VoidPtrSeq>,
133 typedef ptr_container_detail::reversible_ptr_container< ptr_container_detail::sequence_config<T,VoidPtrSeq>,
137 typedef ptr_sequence_adapter<T,VoidPtrSeq,CloneAllocator>
141 typedef BOOST_DEDUCED_TYPENAME base_type::scoped_deleter scoped_deleter;
144 typedef BOOST_DEDUCED_TYPENAME base_type::value_type value_type;
145 typedef BOOST_DEDUCED_TYPENAME base_type::reference reference;
146 typedef BOOST_DEDUCED_TYPENAME base_type::const_reference
148 typedef BOOST_DEDUCED_TYPENAME base_type::auto_type auto_type;
149 typedef BOOST_DEDUCED_TYPENAME base_type::clone_allocator_type
150 clone_allocator_type;
151 typedef BOOST_DEDUCED_TYPENAME base_type::iterator iterator;
152 typedef BOOST_DEDUCED_TYPENAME base_type::size_type size_type;
153 typedef BOOST_DEDUCED_TYPENAME base_type::allocator_type
156 ptr_sequence_adapter()
159 template< class Allocator >
160 explicit ptr_sequence_adapter( const Allocator& a )
164 template< class SizeType >
165 ptr_sequence_adapter( SizeType n,
166 ptr_container_detail::fixed_length_sequence_tag tag )
167 : base_type( n, tag )
170 template< class SizeType, class Allocator >
171 ptr_sequence_adapter( SizeType n, const Allocator& a,
172 ptr_container_detail::fixed_length_sequence_tag tag )
173 : base_type( n, a, tag )
176 template< class InputIterator >
177 ptr_sequence_adapter( InputIterator first, InputIterator last )
178 : base_type( first, last )
181 template< class InputIterator, class Allocator >
182 ptr_sequence_adapter( InputIterator first, InputIterator last,
184 : base_type( first, last, a )
187 template< class ForwardIterator >
188 ptr_sequence_adapter( ForwardIterator first,
189 ForwardIterator last,
190 ptr_container_detail::fixed_length_sequence_tag tag )
191 : base_type( first, last, tag )
194 template< class SizeType, class ForwardIterator >
195 ptr_sequence_adapter( SizeType n,
196 ForwardIterator first,
197 ForwardIterator last,
198 ptr_container_detail::fixed_length_sequence_tag tag )
199 : base_type( n, first, last, tag )
202 ptr_sequence_adapter( const ptr_sequence_adapter& r )
207 ptr_sequence_adapter( const ptr_sequence_adapter<U,VoidPtrSeq,CloneAllocator>& r )
211 ptr_sequence_adapter( const ptr_sequence_adapter& r,
212 ptr_container_detail::fixed_length_sequence_tag tag )
213 : base_type( r, tag )
217 ptr_sequence_adapter( const ptr_sequence_adapter<U,VoidPtrSeq,CloneAllocator>& r,
218 ptr_container_detail::fixed_length_sequence_tag tag )
219 : base_type( r, tag )
222 template< class PtrContainer >
223 explicit ptr_sequence_adapter( std::auto_ptr<PtrContainer> clone )
227 ptr_sequence_adapter& operator=( const ptr_sequence_adapter r )
233 template< class PtrContainer >
234 ptr_sequence_adapter& operator=( std::auto_ptr<PtrContainer> clone )
236 base_type::operator=( clone );
240 /////////////////////////////////////////////////////////////
242 /////////////////////////////////////////////////////////////
244 void push_back( value_type x ) // strong
246 this->enforce_null_policy( x, "Null pointer in 'push_back()'" );
247 auto_type ptr( x, *this ); // notrow
248 this->base().push_back( x ); // strong, commit
249 ptr.release(); // nothrow
253 void push_back( std::auto_ptr<U> x )
255 push_back( x.release() );
258 void push_front( value_type x )
260 this->enforce_null_policy( x, "Null pointer in 'push_front()'" );
261 auto_type ptr( x, *this ); // nothrow
262 this->base().push_front( x ); // strong, commit
263 ptr.release(); // nothrow
267 void push_front( std::auto_ptr<U> x )
269 push_front( x.release() );
274 BOOST_ASSERT( !this->empty() &&
275 "'pop_back()' on empty container" );
276 auto_type ptr( static_cast<value_type>(this->base().back()), *this );
278 this->base().pop_back(); // nothrow
279 return ptr_container_detail::move( ptr ); // nothrow
282 auto_type pop_front()
284 BOOST_ASSERT( !this->empty() &&
285 "'pop_front()' on empty container" );
286 auto_type ptr( static_cast<value_type>(this->base().front()), *this );
288 this->base().pop_front(); // nothrow
289 return ptr_container_detail::move( ptr );
294 BOOST_ASSERT( !this->empty() &&
295 "accessing 'front()' on empty container" );
297 BOOST_ASSERT( !::boost::is_null( this->begin() ) );
298 return *this->begin();
301 const_reference front() const
303 return const_cast<ptr_sequence_adapter*>(this)->front();
308 BOOST_ASSERT( !this->empty() &&
309 "accessing 'back()' on empty container" );
310 BOOST_ASSERT( !::boost::is_null( --this->end() ) );
311 return *--this->end();
314 const_reference back() const
316 return const_cast<ptr_sequence_adapter*>(this)->back();
319 public: // deque/vector inerface
321 reference operator[]( size_type n ) // nothrow
323 BOOST_ASSERT( n < this->size() );
324 BOOST_ASSERT( !this->is_null( n ) );
325 return *static_cast<value_type>( this->base()[n] );
328 const_reference operator[]( size_type n ) const // nothrow
330 BOOST_ASSERT( n < this->size() );
331 BOOST_ASSERT( !this->is_null( n ) );
332 return *static_cast<value_type>( this->base()[n] );
335 reference at( size_type n )
337 BOOST_PTR_CONTAINER_THROW_EXCEPTION( n >= this->size(), bad_index,
338 "'at()' out of bounds" );
339 BOOST_ASSERT( !this->is_null( n ) );
343 const_reference at( size_type n ) const
345 BOOST_PTR_CONTAINER_THROW_EXCEPTION( n >= this->size(), bad_index,
346 "'at()' out of bounds" );
347 BOOST_ASSERT( !this->is_null( n ) );
351 public: // vector interface
353 size_type capacity() const
355 return this->base().capacity();
358 void reserve( size_type n )
360 this->base().reserve( n );
365 this->base().reverse();
368 public: // assign, insert, transfer
370 // overhead: 1 heap allocation (very cheap compared to cloning)
371 template< class InputIterator >
372 void assign( InputIterator first, InputIterator last ) // strong
374 base_type temp( first, last );
378 template< class Range >
379 void assign( const Range& r ) // strong
381 assign( boost::begin(r), boost::end(r ) );
386 void insert_impl( iterator before, I first, I last, std::input_iterator_tag ) // strong
388 ptr_sequence_adapter temp(first,last); // strong
389 transfer( before, temp ); // strong, commit
393 void insert_impl( iterator before, I first, I last, std::forward_iterator_tag ) // strong
397 scoped_deleter sd( *this, first, last ); // strong
398 this->insert_clones_and_release( sd, before ); // strong, commit
403 using base_type::insert;
405 template< class InputIterator >
406 void insert( iterator before, InputIterator first, InputIterator last ) // strong
408 insert_impl( before, first, last, BOOST_DEDUCED_TYPENAME
409 iterator_category<InputIterator>::type() );
412 #if defined(BOOST_NO_SFINAE) || defined(BOOST_NO_FUNCTION_TEMPLATE_ORDERING)
414 template< class Range >
415 BOOST_DEDUCED_TYPENAME
416 boost::disable_if< ptr_container_detail::is_pointer_or_integral<Range> >::type
417 insert( iterator before, const Range& r )
419 insert( before, boost::begin(r), boost::end(r) );
424 template< class PtrSeqAdapter >
425 void transfer( iterator before,
426 BOOST_DEDUCED_TYPENAME PtrSeqAdapter::iterator first,
427 BOOST_DEDUCED_TYPENAME PtrSeqAdapter::iterator last,
428 PtrSeqAdapter& from ) // strong
430 BOOST_ASSERT( (void*)&from != (void*)this );
434 insert( before.base(), first.base(), last.base() ); // strong
435 from.base().erase( first.base(), last.base() ); // nothrow
438 template< class PtrSeqAdapter >
439 void transfer( iterator before,
440 BOOST_DEDUCED_TYPENAME PtrSeqAdapter::iterator object,
441 PtrSeqAdapter& from ) // strong
443 BOOST_ASSERT( (void*)&from != (void*)this );
446 this->base().insert( before.base(), *object.base() ); // strong
447 from.base().erase( object.base() ); // nothrow
450 #if defined(BOOST_NO_SFINAE) || defined(BOOST_NO_FUNCTION_TEMPLATE_ORDERING)
453 template< class PtrSeqAdapter, class Range >
454 BOOST_DEDUCED_TYPENAME boost::disable_if< boost::is_same< Range,
455 BOOST_DEDUCED_TYPENAME PtrSeqAdapter::iterator > >::type
456 transfer( iterator before, const Range& r, PtrSeqAdapter& from ) // strong
458 transfer( before, boost::begin(r), boost::end(r), from );
462 template< class PtrSeqAdapter >
463 void transfer( iterator before, PtrSeqAdapter& from ) // strong
465 BOOST_ASSERT( (void*)&from != (void*)this );
469 insert( before.base(),
470 from.begin().base(), from.end().base() ); // strong
471 from.base().clear(); // nothrow
474 public: // C-array support
476 void transfer( iterator before, value_type* from,
477 size_type size, bool delete_from = true ) // strong
479 BOOST_ASSERT( from != 0 );
482 BOOST_DEDUCED_TYPENAME base_type::scoped_deleter
483 deleter( *this, from, size ); // nothrow
484 this->base().insert( before.base(), from, from + size ); // strong
485 deleter.release(); // nothrow
489 this->base().insert( before.base(), from, from + size ); // strong
493 value_type* c_array() // nothrow
497 T** res = reinterpret_cast<T**>( &this->begin().base()[0] );
501 public: // null functions
503 bool is_null( size_type idx ) const
505 BOOST_ASSERT( idx < this->size() );
506 return this->base()[idx] == 0;
511 void resize( size_type size ) // basic
513 size_type old_size = this->size();
514 if( old_size > size )
516 this->erase( boost::next( this->begin(), size ), this->end() );
518 else if( size > old_size )
520 for( ; old_size != size; ++old_size )
521 this->push_back( new BOOST_DEDUCED_TYPENAME
522 boost::remove_pointer<value_type>::type() );
525 BOOST_ASSERT( this->size() == size );
528 void resize( size_type size, value_type to_clone ) // basic
530 size_type old_size = this->size();
531 if( old_size > size )
533 this->erase( boost::next( this->begin(), size ), this->end() );
535 else if( size > old_size )
537 for( ; old_size != size; ++old_size )
538 this->push_back( this->null_policy_allocate_clone( to_clone ) );
541 BOOST_ASSERT( this->size() == size );
544 void rresize( size_type size ) // basic
546 size_type old_size = this->size();
547 if( old_size > size )
549 this->erase( this->begin(),
550 boost::next( this->begin(), old_size - size ) );
552 else if( size > old_size )
554 for( ; old_size != size; ++old_size )
555 this->push_front( new BOOST_DEDUCED_TYPENAME
556 boost::remove_pointer<value_type>::type() );
559 BOOST_ASSERT( this->size() == size );
562 void rresize( size_type size, value_type to_clone ) // basic
564 size_type old_size = this->size();
565 if( old_size > size )
567 this->erase( this->begin(),
568 boost::next( this->begin(), old_size - size ) );
570 else if( size > old_size )
572 for( ; old_size != size; ++old_size )
573 this->push_front( this->null_policy_allocate_clone( to_clone ) );
576 BOOST_ASSERT( this->size() == size );
579 public: // algorithms
581 void sort( iterator first, iterator last )
583 sort( first, last, std::less<T>() );
588 sort( this->begin(), this->end() );
591 template< class Compare >
592 void sort( iterator first, iterator last, Compare comp )
594 BOOST_ASSERT( first <= last && "out of range sort()" );
595 BOOST_ASSERT( this->begin() <= first && "out of range sort()" );
596 BOOST_ASSERT( last <= this->end() && "out of range sort()" );
597 // some static assert on the arguments of the comparison
598 std::sort( first.base(), last.base(),
599 void_ptr_indirect_fun<Compare,T>(comp) );
602 template< class Compare >
603 void sort( Compare comp )
605 sort( this->begin(), this->end(), comp );
608 void unique( iterator first, iterator last )
610 unique( first, last, std::equal_to<T>() );
615 unique( this->begin(), this->end() );
619 struct is_not_zero_ptr
622 bool operator()( const U* r ) const
629 template< class Fun, class Arg1 >
630 class void_ptr_delete_if
635 void_ptr_delete_if() : fun(Fun())
638 void_ptr_delete_if( Fun f ) : fun(f)
641 bool operator()( void* r ) const
643 BOOST_ASSERT( r != 0 );
644 Arg1 arg1 = static_cast<Arg1>(r);
647 clone_allocator_type::deallocate_clone( arg1 );
655 void compact_and_erase_nulls( iterator first, iterator last ) // nothrow
657 typename base_type::ptr_iterator p = std::stable_partition(
661 this->base().erase( p, this->end().base() );
665 void range_check_impl( iterator, iterator,
666 std::bidirectional_iterator_tag )
669 void range_check_impl( iterator first, iterator last,
670 std::random_access_iterator_tag )
672 BOOST_ASSERT( first <= last && "out of range unique()/erase_if()" );
673 BOOST_ASSERT( this->begin() <= first && "out of range unique()/erase_if()" );
674 BOOST_ASSERT( last <= this->end() && "out of range unique()/erase_if)(" );
677 void range_check( iterator first, iterator last )
679 range_check_impl( first, last,
680 BOOST_DEDUCED_TYPENAME iterator_category<iterator>::type() );
685 template< class Compare >
686 void unique( iterator first, iterator last, Compare comp )
688 range_check(first,last);
690 iterator prev = first;
691 iterator next = first;
693 for( ; next != last; ++next )
695 BOOST_ASSERT( !::boost::is_null(prev) );
696 BOOST_ASSERT( !::boost::is_null(next) );
697 if( comp( *prev, *next ) )
699 this->remove( next ); // delete object
700 *next.base() = 0; // mark pointer as deleted
709 compact_and_erase_nulls( first, last );
712 template< class Compare >
713 void unique( Compare comp )
715 unique( this->begin(), this->end(), comp );
718 template< class Pred >
719 void erase_if( iterator first, iterator last, Pred pred )
721 range_check(first,last);
722 this->base().erase( std::remove_if( first.base(), last.base(),
723 void_ptr_delete_if<Pred,value_type>(pred) ),
727 template< class Pred >
728 void erase_if( Pred pred )
730 erase_if( this->begin(), this->end(), pred );
734 void merge( iterator first, iterator last,
735 ptr_sequence_adapter& from )
737 merge( first, last, from, std::less<T>() );
740 template< class BinPred >
741 void merge( iterator first, iterator last,
742 ptr_sequence_adapter& from, BinPred pred )
744 void_ptr_indirect_fun<BinPred,T> bin_pred(pred);
745 size_type current_size = this->size();
746 this->transfer( this->end(), first, last, from );
747 typename base_type::ptr_iterator middle = this->begin().base();
748 std::advance(middle,current_size);
749 std::inplace_merge( this->begin().base(),
755 void merge( ptr_sequence_adapter& r )
757 merge( r, std::less<T>() );
758 BOOST_ASSERT( r.empty() );
761 template< class BinPred >
762 void merge( ptr_sequence_adapter& r, BinPred pred )
764 merge( r.begin(), r.end(), r, pred );
765 BOOST_ASSERT( r.empty() );
771 } // namespace 'boost'