Chris@16: // Chris@16: // Boost.Pointer Container Chris@16: // Chris@16: // Copyright Thorsten Ottosen 2008. Use, modification and Chris@16: // distribution is subject to the Boost Software License, Version Chris@16: // 1.0. (See accompanying file LICENSE_1_0.txt or copy at Chris@16: // http://www.boost.org/LICENSE_1_0.txt) Chris@16: // Chris@16: // For more information, see http://www.boost.org/libs/ptr_container/ Chris@16: // Chris@16: Chris@16: #ifndef BOOST_PTR_CONTAINER_PTR_CIRCULAR_BUFFER_HPP Chris@16: #define BOOST_PTR_CONTAINER_PTR_CIRCULAR_BUFFER_HPP Chris@16: Chris@16: #if defined(_MSC_VER) && (_MSC_VER >= 1200) Chris@16: # pragma once Chris@16: #endif Chris@16: Chris@16: #include Chris@16: #include Chris@16: Chris@16: namespace boost Chris@16: { Chris@16: Chris@16: template Chris@16: < Chris@16: class T, Chris@16: class CloneAllocator = heap_clone_allocator, Chris@16: class Allocator = std::allocator Chris@16: > Chris@16: class ptr_circular_buffer : public Chris@16: ptr_sequence_adapter< T, Chris@16: boost::circular_buffer, Chris@16: CloneAllocator > Chris@16: { Chris@16: typedef ptr_sequence_adapter< T, Chris@16: boost::circular_buffer, Chris@16: CloneAllocator > Chris@16: base_type; Chris@16: Chris@16: typedef boost::circular_buffer circular_buffer_type; Chris@16: typedef ptr_circular_buffer this_type; Chris@16: Chris@16: public: // typedefs Chris@16: typedef typename base_type::value_type value_type; Chris@16: typedef value_type* pointer; Chris@16: typedef const value_type* const_pointer; Chris@16: typedef typename base_type::size_type size_type; Chris@16: typedef typename base_type::allocator_type allocator_type; Chris@16: typedef typename base_type::iterator iterator; Chris@16: typedef typename base_type::const_iterator const_iterator; Chris@16: typedef typename base_type::auto_type auto_type; Chris@16: Chris@16: typedef std::pair array_range; Chris@16: typedef std::pair const_array_range; Chris@16: typedef typename circular_buffer_type::capacity_type capacity_type; Chris@16: Chris@16: public: // constructors Chris@16: ptr_circular_buffer() Chris@16: { } Chris@16: Chris@16: explicit ptr_circular_buffer( capacity_type n ) Chris@16: : base_type( n, ptr_container_detail::fixed_length_sequence_tag() ) Chris@16: { } Chris@16: Chris@16: ptr_circular_buffer( capacity_type n, Chris@16: const allocator_type& alloc ) Chris@16: : base_type( n, alloc, ptr_container_detail::fixed_length_sequence_tag() ) Chris@16: { } Chris@16: Chris@16: template< class ForwardIterator > Chris@16: ptr_circular_buffer( ForwardIterator first, ForwardIterator last ) Chris@16: : base_type( first, last, ptr_container_detail::fixed_length_sequence_tag() ) Chris@16: { } Chris@16: Chris@16: template< class InputIterator > Chris@16: ptr_circular_buffer( capacity_type n, InputIterator first, InputIterator last ) Chris@16: : base_type( n, first, last, ptr_container_detail::fixed_length_sequence_tag() ) Chris@16: { } Chris@16: Chris@16: ptr_circular_buffer( const ptr_circular_buffer& r ) Chris@16: : base_type( r.size(), r.begin(), r.end(), Chris@16: ptr_container_detail::fixed_length_sequence_tag() ) Chris@16: { } Chris@16: Chris@16: template< class U > Chris@16: ptr_circular_buffer( const ptr_circular_buffer& r ) Chris@16: : base_type( r.size(), r.begin(), r.end(), Chris@16: ptr_container_detail::fixed_length_sequence_tag() ) Chris@16: { } Chris@16: Chris@16: ptr_circular_buffer& operator=( ptr_circular_buffer r ) Chris@16: { Chris@16: this->swap( r ); Chris@16: return *this; Chris@16: } Chris@16: Chris@16: BOOST_PTR_CONTAINER_DEFINE_RELEASE_AND_CLONE( ptr_circular_buffer, Chris@16: base_type, this_type ) Chris@16: Chris@16: public: // allocators Chris@16: allocator_type& get_allocator() Chris@16: { Chris@16: return this->base().get_allocator(); Chris@16: } Chris@16: Chris@16: allocator_type get_allocator() const Chris@16: { Chris@16: return this->base().get_allocator(); Chris@16: } Chris@16: Chris@16: public: // circular buffer functions Chris@16: array_range array_one() // nothrow Chris@16: { Chris@16: typename circular_buffer_type::array_range r = this->base().array_one(); Chris@16: return array_range( reinterpret_cast(r.first), r.second ); Chris@16: } Chris@16: Chris@16: const_array_range array_one() const // nothrow Chris@16: { Chris@16: typename circular_buffer_type::const_array_range r = this->base().array_one(); Chris@16: return const_array_range( reinterpret_cast(r.first), r.second ); Chris@16: } Chris@16: Chris@16: array_range array_two() // nothrow Chris@16: { Chris@16: typename circular_buffer_type::array_range r = this->base().array_two(); Chris@16: return array_range( reinterpret_cast(r.first), r.second ); Chris@16: } Chris@16: Chris@16: const_array_range array_two() const // nothrow Chris@16: { Chris@16: typename circular_buffer_type::const_array_range r = this->base().array_two(); Chris@16: return const_array_range( reinterpret_cast(r.first), r.second ); Chris@16: } Chris@16: Chris@16: pointer linearize() // nothrow Chris@16: { Chris@16: return reinterpret_cast(this->base().linearize()); Chris@16: } Chris@16: Chris@16: bool full() const // nothrow Chris@16: { Chris@16: return this->base().full(); Chris@16: } Chris@16: Chris@16: size_type reserve() const // nothrow Chris@16: { Chris@16: return this->base().reserve(); Chris@16: } Chris@16: Chris@16: void reserve( size_type n ) // strong Chris@16: { Chris@16: if( capacity() < n ) Chris@16: set_capacity( n ); Chris@16: } Chris@16: Chris@16: capacity_type capacity() const // nothrow Chris@16: { Chris@16: return this->base().capacity(); Chris@16: } Chris@16: Chris@16: void set_capacity( capacity_type new_capacity ) // strong Chris@16: { Chris@16: if( this->size() > new_capacity ) Chris@16: { Chris@16: this->erase( this->begin() + new_capacity, this->end() ); Chris@16: } Chris@16: this->base().set_capacity( new_capacity ); Chris@16: } Chris@16: Chris@16: void rset_capacity( capacity_type new_capacity ) // strong Chris@16: { Chris@16: if( this->size() > new_capacity ) Chris@16: { Chris@16: this->erase( this->begin(), Chris@16: this->begin() + (this->size()-new_capacity) ); Chris@16: } Chris@16: this->base().rset_capacity( new_capacity ); Chris@16: } Chris@16: Chris@16: void resize( size_type size ) // basic Chris@16: { Chris@16: size_type old_size = this->size(); Chris@16: if( old_size > size ) Chris@16: { Chris@16: this->erase( boost::next( this->begin(), size ), this->end() ); Chris@16: } Chris@16: else if( size > old_size ) Chris@16: { Chris@16: for( ; old_size != size; ++old_size ) Chris@16: this->push_back( new BOOST_DEDUCED_TYPENAME Chris@16: boost::remove_pointer::type() ); Chris@16: } Chris@16: Chris@16: BOOST_ASSERT( this->size() == size ); Chris@16: } Chris@16: Chris@16: void resize( size_type size, value_type to_clone ) // basic Chris@16: { Chris@16: size_type old_size = this->size(); Chris@16: if( old_size > size ) Chris@16: { Chris@16: this->erase( boost::next( this->begin(), size ), this->end() ); Chris@16: } Chris@16: else if( size > old_size ) Chris@16: { Chris@16: for( ; old_size != size; ++old_size ) Chris@16: this->push_back( this->null_policy_allocate_clone( to_clone ) ); Chris@16: } Chris@16: Chris@16: BOOST_ASSERT( this->size() == size ); Chris@16: } Chris@16: Chris@16: void rresize( size_type size ) // basic Chris@16: { Chris@16: size_type old_size = this->size(); Chris@16: if( old_size > size ) Chris@16: { Chris@16: this->erase( this->begin(), Chris@16: boost::next( this->begin(), old_size - size ) ); Chris@16: } Chris@16: else if( size > old_size ) Chris@16: { Chris@16: for( ; old_size != size; ++old_size ) Chris@16: this->push_front( new BOOST_DEDUCED_TYPENAME Chris@16: boost::remove_pointer::type() ); Chris@16: } Chris@16: Chris@16: BOOST_ASSERT( this->size() == size ); Chris@16: } Chris@16: Chris@16: void rresize( size_type size, value_type to_clone ) // basic Chris@16: { Chris@16: size_type old_size = this->size(); Chris@16: if( old_size > size ) Chris@16: { Chris@16: this->erase( this->begin(), Chris@16: boost::next( this->begin(), old_size - size ) ); Chris@16: } Chris@16: else if( size > old_size ) Chris@16: { Chris@16: for( ; old_size != size; ++old_size ) Chris@16: this->push_front( this->null_policy_allocate_clone( to_clone ) ); Chris@16: } Chris@16: Chris@16: BOOST_ASSERT( this->size() == size ); Chris@16: } Chris@16: Chris@16: template< class InputIterator > Chris@16: void assign( InputIterator first, InputIterator last ) // strong Chris@16: { Chris@16: ptr_circular_buffer temp( first, last ); Chris@16: this->swap( temp ); Chris@16: } Chris@16: Chris@16: template< class Range > Chris@16: void assign( const Range& r ) // strong Chris@16: { Chris@16: assign( boost::begin(r), boost::end(r ) ); Chris@16: } Chris@16: Chris@16: void assign( size_type n, value_type to_clone ) // strong Chris@16: { Chris@16: ptr_circular_buffer temp( n ); Chris@16: for( size_type i = 0u; i != n; ++i ) Chris@16: temp.push_back( this->null_policy_allocate_clone( to_clone ) ); Chris@16: this->swap( temp ); Chris@16: } Chris@16: Chris@16: void assign( capacity_type capacity, size_type n, Chris@16: value_type to_clone ) // basic Chris@16: { Chris@16: this->assign( (std::min)(n,capacity), to_clone ); Chris@16: } Chris@16: Chris@16: template< class InputIterator > Chris@16: void assign( capacity_type capacity, Chris@16: InputIterator first, InputIterator last ) // basic Chris@16: { Chris@16: this->assign( first, last ); Chris@16: this->set_capacity( capacity ); Chris@16: } Chris@16: Chris@16: void push_back( value_type ptr ) // nothrow Chris@16: { Chris@16: BOOST_ASSERT( capacity() > 0 ); Chris@16: this->enforce_null_policy( ptr, "Null pointer in 'push_back()'" ); Chris@16: Chris@16: auto_type old_ptr; Chris@16: if( full() ) Chris@16: old_ptr.reset( &*this->begin() ); Chris@16: this->base().push_back( ptr ); Chris@16: } Chris@16: Chris@16: template< class U > Chris@16: void push_back( std::auto_ptr ptr ) // nothrow Chris@16: { Chris@16: push_back( ptr.release() ); Chris@16: } Chris@16: Chris@16: void push_front( value_type ptr ) // nothrow Chris@16: { Chris@16: BOOST_ASSERT( capacity() > 0 ); Chris@16: this->enforce_null_policy( ptr, "Null pointer in 'push_front()'" ); Chris@16: Chris@16: auto_type old_ptr; Chris@16: if( full() ) Chris@16: old_ptr.reset( &*(--this->end()) ); Chris@16: this->base().push_front( ptr ); Chris@16: } Chris@16: Chris@16: template< class U > Chris@16: void push_front( std::auto_ptr ptr ) // nothrow Chris@16: { Chris@16: push_front( ptr.release() ); Chris@16: } Chris@16: Chris@16: iterator insert( iterator pos, value_type ptr ) // nothrow Chris@16: { Chris@16: BOOST_ASSERT( capacity() > 0 ); Chris@16: this->enforce_null_policy( ptr, "Null pointer in 'insert()'" ); Chris@16: Chris@16: auto_type new_ptr( ptr ); Chris@16: iterator b = this->begin(); Chris@16: if( full() && pos == b ) Chris@16: return b; Chris@16: Chris@16: auto_type old_ptr; Chris@16: if( full() ) Chris@16: old_ptr.reset( &*this->begin() ); Chris@16: Chris@16: new_ptr.release(); Chris@16: return this->base().insert( pos.base(), ptr ); Chris@16: } Chris@16: Chris@16: template< class U > Chris@16: iterator insert( iterator pos, std::auto_ptr ptr ) // nothrow Chris@16: { Chris@16: return insert( pos, ptr.release() ); Chris@16: } Chris@16: Chris@16: template< class InputIterator > Chris@16: void insert( iterator pos, InputIterator first, InputIterator last ) // basic Chris@16: { Chris@16: for( ; first != last; ++first, ++pos ) Chris@16: pos = insert( pos, this->null_policy_allocate_clone( &*first ) ); Chris@16: } Chris@16: Chris@16: #if defined(BOOST_NO_SFINAE) || defined(BOOST_NO_FUNCTION_TEMPLATE_ORDERING) Chris@16: #else Chris@16: template< class Range > Chris@16: BOOST_DEDUCED_TYPENAME Chris@16: boost::disable_if< ptr_container_detail::is_pointer_or_integral >::type Chris@16: insert( iterator before, const Range& r ) Chris@16: { Chris@16: insert( before, boost::begin(r), boost::end(r) ); Chris@16: } Chris@16: Chris@16: #endif Chris@16: Chris@16: iterator rinsert( iterator pos, value_type ptr ) // nothrow Chris@16: { Chris@16: BOOST_ASSERT( capacity() > 0 ); Chris@16: this->enforce_null_policy( ptr, "Null pointer in 'rinsert()'" ); Chris@16: Chris@16: auto_type new_ptr( ptr ); Chris@16: iterator b = this->end(); Chris@16: if (full() && pos == b) Chris@16: return b; Chris@16: Chris@16: auto_type old_ptr; Chris@16: if( full() ) Chris@16: old_ptr.reset( &this->back() ); Chris@16: Chris@16: new_ptr.release(); Chris@16: return this->base().rinsert( pos.base(), ptr ); Chris@16: } Chris@16: Chris@16: template< class U > Chris@16: iterator rinsert( iterator pos, std::auto_ptr ptr ) // nothrow Chris@16: { Chris@16: return rinsert( pos, ptr.release() ); Chris@16: } Chris@16: Chris@16: Chris@16: template< class InputIterator > Chris@16: void rinsert( iterator pos, InputIterator first, InputIterator last ) // basic Chris@16: { Chris@16: for( ; first != last; ++first, ++pos ) Chris@16: pos = rinsert( pos, this->null_policy_allocate_clone( &*first ) ); Chris@16: } Chris@16: Chris@16: #if defined(BOOST_NO_SFINAE) || defined(BOOST_NO_FUNCTION_TEMPLATE_ORDERING) Chris@16: #else Chris@16: template< class Range > Chris@16: BOOST_DEDUCED_TYPENAME Chris@16: boost::disable_if< ptr_container_detail::is_pointer_or_integral >::type Chris@16: rinsert( iterator before, const Range& r ) Chris@16: { Chris@16: rinsert( before, boost::begin(r), boost::end(r) ); Chris@16: } Chris@16: Chris@16: #endif Chris@16: Chris@16: iterator rerase( iterator pos ) // nothrow Chris@16: { Chris@16: BOOST_ASSERT( !this->empty() ); Chris@16: BOOST_ASSERT( pos != this->end() ); Chris@16: Chris@16: this->remove( pos ); Chris@16: return iterator( this->base().rerase( pos.base() ) ); Chris@16: } Chris@16: Chris@16: iterator rerase( iterator first, iterator last ) // nothrow Chris@16: { Chris@16: this->remove( first, last ); Chris@16: return iterator( this->base().rerase( first.base(), Chris@16: last.base() ) ); Chris@16: } Chris@16: Chris@16: template< class Range > Chris@16: iterator rerase( const Range& r ) // nothrow Chris@16: { Chris@16: return rerase( boost::begin(r), boost::end(r) ); Chris@16: } Chris@16: Chris@16: void rotate( const_iterator new_begin ) // nothrow Chris@16: { Chris@16: this->base().rotate( new_begin.base() ); Chris@16: } Chris@16: Chris@16: public: // transfer Chris@16: template< class PtrSeqAdapter > Chris@16: void transfer( iterator before, Chris@16: BOOST_DEDUCED_TYPENAME PtrSeqAdapter::iterator first, Chris@16: BOOST_DEDUCED_TYPENAME PtrSeqAdapter::iterator last, Chris@16: PtrSeqAdapter& from ) // nothrow Chris@16: { Chris@16: BOOST_ASSERT( (void*)&from != (void*)this ); Chris@16: if( from.empty() ) Chris@16: return; Chris@16: for( BOOST_DEDUCED_TYPENAME PtrSeqAdapter::iterator begin = first; Chris@16: begin != last; ++begin, ++before ) Chris@16: before = insert( before, &*begin ); // nothrow Chris@16: from.base().erase( first.base(), last.base() ); // nothrow Chris@16: } Chris@16: Chris@16: template< class PtrSeqAdapter > Chris@16: void transfer( iterator before, Chris@16: BOOST_DEDUCED_TYPENAME PtrSeqAdapter::iterator object, Chris@16: PtrSeqAdapter& from ) // nothrow Chris@16: { Chris@16: BOOST_ASSERT( (void*)&from != (void*)this ); Chris@16: if( from.empty() ) Chris@16: return; Chris@16: insert( before, &*object ); // nothrow Chris@16: from.base().erase( object.base() ); // nothrow Chris@16: } Chris@16: Chris@16: #if defined(BOOST_NO_SFINAE) || defined(BOOST_NO_FUNCTION_TEMPLATE_ORDERING) Chris@16: #else Chris@16: Chris@16: template< class PtrSeqAdapter, class Range > Chris@16: BOOST_DEDUCED_TYPENAME boost::disable_if< boost::is_same< Range, Chris@16: BOOST_DEDUCED_TYPENAME PtrSeqAdapter::iterator > >::type Chris@16: transfer( iterator before, const Range& r, PtrSeqAdapter& from ) // nothrow Chris@16: { Chris@16: transfer( before, boost::begin(r), boost::end(r), from ); Chris@16: } Chris@16: Chris@16: #endif Chris@16: template< class PtrSeqAdapter > Chris@16: void transfer( iterator before, PtrSeqAdapter& from ) // nothrow Chris@16: { Chris@16: transfer( before, from.begin(), from.end(), from ); Chris@16: } Chris@16: Chris@16: public: // C-array support Chris@16: Chris@16: void transfer( iterator before, value_type* from, Chris@16: size_type size, bool delete_from = true ) // nothrow Chris@16: { Chris@16: BOOST_ASSERT( from != 0 ); Chris@16: if( delete_from ) Chris@16: { Chris@16: BOOST_DEDUCED_TYPENAME base_type::scoped_deleter Chris@16: deleter( from, size ); // nothrow Chris@16: for( size_type i = 0u; i != size; ++i, ++before ) Chris@16: before = insert( before, *(from+i) ); // nothrow Chris@16: deleter.release(); // nothrow Chris@16: } Chris@16: else Chris@16: { Chris@16: for( size_type i = 0u; i != size; ++i, ++before ) Chris@16: before = insert( before, *(from+i) ); // nothrow Chris@16: } Chris@16: } Chris@16: Chris@16: value_type* c_array() // nothrow Chris@16: { Chris@16: if( this->empty() ) Chris@16: return 0; Chris@16: this->linearize(); Chris@16: T** res = reinterpret_cast( &this->begin().base()[0] ); Chris@16: return res; Chris@16: } Chris@16: Chris@16: }; Chris@16: Chris@16: ////////////////////////////////////////////////////////////////////////////// Chris@16: // clonability Chris@16: Chris@16: template< typename T, typename CA, typename A > Chris@16: inline ptr_circular_buffer* new_clone( const ptr_circular_buffer& r ) Chris@16: { Chris@16: return r.clone().release(); Chris@16: } Chris@16: Chris@16: ///////////////////////////////////////////////////////////////////////// Chris@16: // swap Chris@16: Chris@16: template< typename T, typename CA, typename A > Chris@16: inline void swap( ptr_circular_buffer& l, ptr_circular_buffer& r ) Chris@16: { Chris@16: l.swap(r); Chris@16: } Chris@16: Chris@16: } Chris@16: Chris@16: #endif