2 // Boost.Pointer Container
4 // Copyright Thorsten Ottosen 2008. 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_CIRCULAR_BUFFER_HPP
13 #define BOOST_PTR_CONTAINER_PTR_CIRCULAR_BUFFER_HPP
15 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
19 #include <boost/circular_buffer.hpp>
20 #include <boost/ptr_container/ptr_sequence_adapter.hpp>
28 class CloneAllocator = heap_clone_allocator,
29 class Allocator = std::allocator<void*>
31 class ptr_circular_buffer : public
32 ptr_sequence_adapter< T, boost::circular_buffer<
33 typename ptr_container_detail::void_ptr<T>::type,Allocator>,
36 typedef ptr_sequence_adapter< T, boost::circular_buffer<
37 typename ptr_container_detail::void_ptr<T>::type,Allocator>,
41 typedef boost::circular_buffer<typename
42 ptr_container_detail::void_ptr<T>::type,Allocator> circular_buffer_type;
43 typedef ptr_circular_buffer<T,CloneAllocator,Allocator> this_type;
46 typedef typename base_type::value_type value_type;
47 typedef value_type* pointer;
48 typedef const value_type* const_pointer;
49 typedef typename base_type::size_type size_type;
50 typedef typename base_type::allocator_type allocator_type;
51 typedef typename base_type::iterator iterator;
52 typedef typename base_type::const_iterator const_iterator;
53 typedef typename base_type::auto_type auto_type;
55 typedef std::pair<pointer,size_type> array_range;
56 typedef std::pair<const_pointer,size_type> const_array_range;
57 typedef typename circular_buffer_type::capacity_type capacity_type;
59 public: // constructors
63 explicit ptr_circular_buffer( capacity_type n )
64 : base_type( n, ptr_container_detail::fixed_length_sequence_tag() )
67 ptr_circular_buffer( capacity_type n,
68 const allocator_type& alloc )
69 : base_type( n, alloc, ptr_container_detail::fixed_length_sequence_tag() )
72 template< class ForwardIterator >
73 ptr_circular_buffer( ForwardIterator first, ForwardIterator last )
74 : base_type( first, last, ptr_container_detail::fixed_length_sequence_tag() )
77 template< class InputIterator >
78 ptr_circular_buffer( capacity_type n, InputIterator first, InputIterator last )
79 : base_type( n, first, last, ptr_container_detail::fixed_length_sequence_tag() )
82 ptr_circular_buffer( const ptr_circular_buffer& r )
83 : base_type( r.size(), r.begin(), r.end(),
84 ptr_container_detail::fixed_length_sequence_tag() )
88 ptr_circular_buffer( const ptr_circular_buffer<U>& r )
89 : base_type( r.size(), r.begin(), r.end(),
90 ptr_container_detail::fixed_length_sequence_tag() )
93 ptr_circular_buffer& operator=( ptr_circular_buffer r )
99 BOOST_PTR_CONTAINER_DEFINE_RELEASE_AND_CLONE( ptr_circular_buffer,
100 base_type, this_type )
102 public: // allocators
103 allocator_type& get_allocator()
105 return this->base().get_allocator();
108 allocator_type get_allocator() const
110 return this->base().get_allocator();
113 public: // circular buffer functions
114 array_range array_one() // nothrow
116 typename circular_buffer_type::array_range r = this->base().array_one();
117 return array_range( reinterpret_cast<pointer>(r.first), r.second );
120 const_array_range array_one() const // nothrow
122 typename circular_buffer_type::const_array_range r = this->base().array_one();
123 return const_array_range( reinterpret_cast<const_pointer>(r.first), r.second );
126 array_range array_two() // nothrow
128 typename circular_buffer_type::array_range r = this->base().array_two();
129 return array_range( reinterpret_cast<pointer>(r.first), r.second );
132 const_array_range array_two() const // nothrow
134 typename circular_buffer_type::const_array_range r = this->base().array_two();
135 return const_array_range( reinterpret_cast<const_pointer>(r.first), r.second );
138 pointer linearize() // nothrow
140 return reinterpret_cast<pointer>(this->base().linearize());
143 bool full() const // nothrow
145 return this->base().full();
148 size_type reserve() const // nothrow
150 return this->base().reserve();
153 void reserve( size_type n ) // strong
159 capacity_type capacity() const // nothrow
161 return this->base().capacity();
164 void set_capacity( capacity_type new_capacity ) // strong
166 if( this->size() > new_capacity )
168 this->erase( this->begin() + new_capacity, this->end() );
170 this->base().set_capacity( new_capacity );
173 void rset_capacity( capacity_type new_capacity ) // strong
175 if( this->size() > new_capacity )
177 this->erase( this->begin(),
178 this->begin() + (this->size()-new_capacity) );
180 this->base().rset_capacity( new_capacity );
183 void resize( size_type size ) // basic
185 size_type old_size = this->size();
186 if( old_size > size )
188 this->erase( boost::next( this->begin(), size ), this->end() );
190 else if( size > old_size )
192 for( ; old_size != size; ++old_size )
193 this->push_back( new BOOST_DEDUCED_TYPENAME
194 boost::remove_pointer<value_type>::type() );
197 BOOST_ASSERT( this->size() == size );
200 void resize( size_type size, value_type to_clone ) // basic
202 size_type old_size = this->size();
203 if( old_size > size )
205 this->erase( boost::next( this->begin(), size ), this->end() );
207 else if( size > old_size )
209 for( ; old_size != size; ++old_size )
210 this->push_back( this->null_policy_allocate_clone( to_clone ) );
213 BOOST_ASSERT( this->size() == size );
216 void rresize( size_type size ) // basic
218 size_type old_size = this->size();
219 if( old_size > size )
221 this->erase( this->begin(),
222 boost::next( this->begin(), old_size - size ) );
224 else if( size > old_size )
226 for( ; old_size != size; ++old_size )
227 this->push_front( new BOOST_DEDUCED_TYPENAME
228 boost::remove_pointer<value_type>::type() );
231 BOOST_ASSERT( this->size() == size );
234 void rresize( size_type size, value_type to_clone ) // basic
236 size_type old_size = this->size();
237 if( old_size > size )
239 this->erase( this->begin(),
240 boost::next( this->begin(), old_size - size ) );
242 else if( size > old_size )
244 for( ; old_size != size; ++old_size )
245 this->push_front( this->null_policy_allocate_clone( to_clone ) );
248 BOOST_ASSERT( this->size() == size );
251 template< class InputIterator >
252 void assign( InputIterator first, InputIterator last ) // strong
254 ptr_circular_buffer temp( first, last );
258 template< class Range >
259 void assign( const Range& r ) // strong
261 assign( boost::begin(r), boost::end(r ) );
264 void assign( size_type n, value_type to_clone ) // strong
266 ptr_circular_buffer temp( n );
267 for( size_type i = 0u; i != n; ++i )
268 temp.push_back( temp.null_policy_allocate_clone( to_clone ) );
272 void assign( capacity_type capacity, size_type n,
273 value_type to_clone ) // basic
275 this->assign( (std::min)(n,capacity), to_clone );
278 template< class InputIterator >
279 void assign( capacity_type capacity,
280 InputIterator first, InputIterator last ) // basic
282 this->assign( first, last );
283 this->set_capacity( capacity );
286 void push_back( value_type ptr ) // nothrow
288 BOOST_ASSERT( capacity() > 0 );
289 this->enforce_null_policy( ptr, "Null pointer in 'push_back()'" );
291 auto_type old_ptr( value_type(), *this );
293 old_ptr.reset( &*this->begin(), *this );
294 this->base().push_back( ptr );
298 void push_back( std::auto_ptr<U> ptr ) // nothrow
300 push_back( ptr.release() );
303 void push_front( value_type ptr ) // nothrow
305 BOOST_ASSERT( capacity() > 0 );
306 this->enforce_null_policy( ptr, "Null pointer in 'push_front()'" );
308 auto_type old_ptr( value_type(), *this );
310 old_ptr.reset( &*(--this->end()), *this );
311 this->base().push_front( ptr );
315 void push_front( std::auto_ptr<U> ptr ) // nothrow
317 push_front( ptr.release() );
320 iterator insert( iterator pos, value_type ptr ) // nothrow
322 BOOST_ASSERT( capacity() > 0 );
323 this->enforce_null_policy( ptr, "Null pointer in 'insert()'" );
325 auto_type new_ptr( ptr, *this );
326 iterator b = this->begin();
327 if( full() && pos == b )
331 auto_type old_ptr( value_type(), *this );
333 old_ptr.reset( &*this->begin(), *this );
335 return this->base().insert( pos.base(), ptr );
339 iterator insert( iterator pos, std::auto_ptr<U> ptr ) // nothrow
341 return insert( pos, ptr.release() );
344 template< class InputIterator >
345 void insert( iterator pos, InputIterator first, InputIterator last ) // basic
347 for( ; first != last; ++first, ++pos )
348 pos = insert( pos, this->null_policy_allocate_clone( &*first ) );
351 #if defined(BOOST_NO_SFINAE) || defined(BOOST_NO_FUNCTION_TEMPLATE_ORDERING)
353 template< class Range >
354 BOOST_DEDUCED_TYPENAME
355 boost::disable_if< ptr_container_detail::is_pointer_or_integral<Range> >::type
356 insert( iterator before, const Range& r )
358 insert( before, boost::begin(r), boost::end(r) );
363 iterator rinsert( iterator pos, value_type ptr ) // nothrow
365 BOOST_ASSERT( capacity() > 0 );
366 this->enforce_null_policy( ptr, "Null pointer in 'rinsert()'" );
368 auto_type new_ptr( ptr, *this );
369 iterator b = this->end();
370 if (full() && pos == b)
374 auto_type old_ptr( value_type(), *this );
376 old_ptr.reset( &this->back(), *this );
378 return this->base().rinsert( pos.base(), ptr );
382 iterator rinsert( iterator pos, std::auto_ptr<U> ptr ) // nothrow
384 return rinsert( pos, ptr.release() );
388 template< class InputIterator >
389 void rinsert( iterator pos, InputIterator first, InputIterator last ) // basic
391 for( ; first != last; ++first, ++pos )
392 pos = rinsert( pos, this->null_policy_allocate_clone( &*first ) );
395 #if defined(BOOST_NO_SFINAE) || defined(BOOST_NO_FUNCTION_TEMPLATE_ORDERING)
397 template< class Range >
398 BOOST_DEDUCED_TYPENAME
399 boost::disable_if< ptr_container_detail::is_pointer_or_integral<Range> >::type
400 rinsert( iterator before, const Range& r )
402 rinsert( before, boost::begin(r), boost::end(r) );
407 iterator rerase( iterator pos ) // nothrow
409 BOOST_ASSERT( !this->empty() );
410 BOOST_ASSERT( pos != this->end() );
413 return iterator( this->base().rerase( pos.base() ) );
416 iterator rerase( iterator first, iterator last ) // nothrow
418 this->remove( first, last );
419 return iterator( this->base().rerase( first.base(),
423 template< class Range >
424 iterator rerase( const Range& r ) // nothrow
426 return rerase( boost::begin(r), boost::end(r) );
429 void rotate( const_iterator new_begin ) // nothrow
431 this->base().rotate( new_begin.base() );
435 template< class PtrSeqAdapter >
436 void transfer( iterator before,
437 BOOST_DEDUCED_TYPENAME PtrSeqAdapter::iterator first,
438 BOOST_DEDUCED_TYPENAME PtrSeqAdapter::iterator last,
439 PtrSeqAdapter& from ) // nothrow
441 BOOST_ASSERT( (void*)&from != (void*)this );
444 for( BOOST_DEDUCED_TYPENAME PtrSeqAdapter::iterator begin = first;
445 begin != last; ++begin, ++before )
446 before = insert( before, &*begin ); // nothrow
447 from.base().erase( first.base(), last.base() ); // nothrow
450 template< class PtrSeqAdapter >
451 void transfer( iterator before,
452 BOOST_DEDUCED_TYPENAME PtrSeqAdapter::iterator object,
453 PtrSeqAdapter& from ) // nothrow
455 BOOST_ASSERT( (void*)&from != (void*)this );
458 insert( before, &*object ); // nothrow
459 from.base().erase( object.base() ); // nothrow
462 #if defined(BOOST_NO_SFINAE) || defined(BOOST_NO_FUNCTION_TEMPLATE_ORDERING)
465 template< class PtrSeqAdapter, class Range >
466 BOOST_DEDUCED_TYPENAME boost::disable_if< boost::is_same< Range,
467 BOOST_DEDUCED_TYPENAME PtrSeqAdapter::iterator > >::type
468 transfer( iterator before, const Range& r, PtrSeqAdapter& from ) // nothrow
470 transfer( before, boost::begin(r), boost::end(r), from );
474 template< class PtrSeqAdapter >
475 void transfer( iterator before, PtrSeqAdapter& from ) // nothrow
477 transfer( before, from.begin(), from.end(), from );
480 public: // C-array support
482 void transfer( iterator before, value_type* from,
483 size_type size, bool delete_from = true ) // nothrow
485 BOOST_ASSERT( from != 0 );
488 BOOST_DEDUCED_TYPENAME base_type::scoped_deleter
489 deleter( *this, from, size ); // nothrow
490 for( size_type i = 0u; i != size; ++i, ++before )
491 before = insert( before, *(from+i) ); // nothrow
492 deleter.release(); // nothrow
496 for( size_type i = 0u; i != size; ++i, ++before )
497 before = insert( before, *(from+i) ); // nothrow
501 value_type* c_array() // nothrow
506 T** res = reinterpret_cast<T**>( &this->begin().base()[0] );
512 //////////////////////////////////////////////////////////////////////////////
515 template< typename T, typename CA, typename A >
516 inline ptr_circular_buffer<T,CA,A>* new_clone( const ptr_circular_buffer<T,CA,A>& r )
518 return r.clone().release();
521 /////////////////////////////////////////////////////////////////////////
524 template< typename T, typename CA, typename A >
525 inline void swap( ptr_circular_buffer<T,CA,A>& l, ptr_circular_buffer<T,CA,A>& r )