1 // Copyright Thorsten Ottosen, 2009.
2 // Distributed under the Boost Software License, Version 1.0. (See
3 // accompanying file LICENSE_1_0.txt or copy at
4 // http://www.boost.org/LICENSE_1_0.txt)
6 #ifndef BOOST_SIGNALS2_DETAIL_AUTO_BUFFER_HPP_25_02_2009
7 #define BOOST_SIGNALS2_DETAIL_AUTO_BUFFER_HPP_25_02_2009
9 #include <boost/detail/workaround.hpp>
15 #if BOOST_WORKAROUND(BOOST_MSVC, >= 1400)
17 #pragma warning(disable:4996)
20 #include <boost/assert.hpp>
21 #include <boost/config.hpp>
22 #include <boost/core/allocator_access.hpp>
23 #include <boost/iterator/reverse_iterator.hpp>
24 #include <boost/iterator/iterator_traits.hpp>
25 #include <boost/mpl/if.hpp>
26 #include <boost/signals2/detail/scope_guard.hpp>
27 #include <boost/swap.hpp>
28 #include <boost/type_traits/aligned_storage.hpp>
29 #include <boost/type_traits/alignment_of.hpp>
30 #include <boost/type_traits/has_nothrow_copy.hpp>
31 #include <boost/type_traits/has_nothrow_assign.hpp>
32 #include <boost/type_traits/has_trivial_assign.hpp>
33 #include <boost/type_traits/has_trivial_constructor.hpp>
34 #include <boost/type_traits/has_trivial_destructor.hpp>
48 // Policies for creating the stack buffer.
50 template< unsigned N >
51 struct store_n_objects
53 BOOST_STATIC_CONSTANT( unsigned, value = N );
56 template< unsigned N >
59 BOOST_STATIC_CONSTANT( unsigned, value = N );
62 namespace auto_buffer_detail
64 template< class Policy, class T >
65 struct compute_buffer_size
67 BOOST_STATIC_CONSTANT( unsigned, value = Policy::value * sizeof(T) );
70 template< unsigned N, class T >
71 struct compute_buffer_size< store_n_bytes<N>, T >
73 BOOST_STATIC_CONSTANT( unsigned, value = N );
76 template< class Policy, class T >
77 struct compute_buffer_objects
79 BOOST_STATIC_CONSTANT( unsigned, value = Policy::value );
82 template< unsigned N, class T >
83 struct compute_buffer_objects< store_n_bytes<N>, T >
85 BOOST_STATIC_CONSTANT( unsigned, value = N / sizeof(T) );
89 struct default_grow_policy
91 template< class SizeType >
92 static SizeType new_capacity( SizeType capacity )
95 // @remark: we grow the capacity quite agressively.
96 // this is justified since we aim to minimize
97 // heap-allocations, and because we mostly use
98 // the buffer locally.
102 template< class SizeType >
103 static bool should_shrink( SizeType, SizeType )
106 // @remark: when defining a new grow policy, one might
107 // choose that if the waated space is less
108 // than a certain percentage, then it is of
109 // little use to shrink.
116 class StackBufferPolicy = store_n_objects<256>,
117 class GrowPolicy = default_grow_policy,
118 class Allocator = std::allocator<T> >
126 class StackBufferPolicy,
130 class auto_buffer : Allocator
133 enum { N = auto_buffer_detail::
134 compute_buffer_objects<StackBufferPolicy,T>::value };
136 BOOST_STATIC_CONSTANT( bool, is_stack_buffer_empty = N == 0u );
138 typedef auto_buffer<T, store_n_objects<0>, GrowPolicy, Allocator>
142 typedef Allocator allocator_type;
143 typedef T value_type;
144 typedef typename boost::allocator_size_type<Allocator>::type size_type;
145 typedef typename boost::allocator_difference_type<Allocator>::type difference_type;
147 typedef typename boost::allocator_pointer<Allocator>::type allocator_pointer;
148 typedef const T* const_pointer;
149 typedef T& reference;
150 typedef const T& const_reference;
151 typedef pointer iterator;
152 typedef const_pointer const_iterator;
153 typedef boost::reverse_iterator<iterator> reverse_iterator;
154 typedef boost::reverse_iterator<const_iterator> const_reverse_iterator;
155 typedef typename boost::mpl::if_c< boost::has_trivial_assign<T>::value
156 && sizeof(T) <= sizeof(long double),
158 const_reference >::type
159 optimized_const_reference;
162 pointer allocate( size_type capacity_arg )
164 if( capacity_arg > N )
165 return &*get_allocator().allocate( capacity_arg );
167 return static_cast<T*>( members_.address() );
170 void deallocate( pointer where, size_type capacity_arg )
172 if( capacity_arg <= N )
174 get_allocator().deallocate( allocator_pointer(where), capacity_arg );
178 static void copy_impl( I begin, I end, pointer where, std::random_access_iterator_tag )
180 copy_rai( begin, end, where, boost::has_trivial_assign<T>() );
183 static void copy_rai( const T* begin, const T* end,
184 pointer where, const boost::true_type& )
186 std::memcpy( where, begin, sizeof(T) * std::distance(begin,end) );
189 template< class I, bool b >
190 static void copy_rai( I begin, I end,
191 pointer where, const boost::integral_constant<bool, b>& )
193 std::uninitialized_copy( begin, end, where );
197 static void copy_impl( I begin, I end, pointer where, std::bidirectional_iterator_tag )
199 std::uninitialized_copy( begin, end, where );
203 static void copy_impl( I begin, I end, pointer where )
205 copy_impl( begin, end, where,
206 typename std::iterator_traits<I>::iterator_category() );
209 template< class I, class I2 >
210 static void assign_impl( I begin, I end, I2 where )
212 assign_impl( begin, end, where, boost::has_trivial_assign<T>() );
215 template< class I, class I2 >
216 static void assign_impl( I begin, I end, I2 where, const boost::true_type& )
218 std::memcpy( where, begin, sizeof(T) * std::distance(begin,end) );
221 template< class I, class I2 >
222 static void assign_impl( I begin, I end, I2 where, const boost::false_type& )
224 for( ; begin != end; ++begin, ++where )
228 void unchecked_push_back_n( size_type n, const boost::true_type& )
230 std::uninitialized_fill( end(), end() + n, T() );
234 void unchecked_push_back_n( size_type n, const boost::false_type& )
236 for( size_type i = 0u; i < n; ++i )
237 unchecked_push_back();
240 void auto_buffer_destroy( pointer where, const boost::false_type& )
245 void auto_buffer_destroy( pointer, const boost::true_type& )
248 void auto_buffer_destroy( pointer where )
250 auto_buffer_destroy( where, boost::has_trivial_destructor<T>() );
253 void auto_buffer_destroy()
255 BOOST_ASSERT( is_valid() );
256 if( buffer_ ) // do we need this check? Yes, but only
257 // for N = 0u + local instances in one_sided_swap()
258 auto_buffer_destroy( boost::has_trivial_destructor<T>() );
261 void destroy_back_n( size_type n, const boost::false_type& )
263 BOOST_ASSERT( n > 0 );
264 pointer buffer = buffer_ + size_ - 1u;
265 pointer new_end = buffer - n;
266 for( ; buffer > new_end; --buffer )
267 auto_buffer_destroy( buffer );
270 void destroy_back_n( size_type, const boost::true_type& )
273 void destroy_back_n( size_type n )
275 destroy_back_n( n, boost::has_trivial_destructor<T>() );
278 void auto_buffer_destroy( const boost::false_type& x )
281 destroy_back_n( size_, x );
282 deallocate( buffer_, members_.capacity_ );
285 void auto_buffer_destroy( const boost::true_type& )
287 deallocate( buffer_, members_.capacity_ );
290 pointer move_to_new_buffer( size_type new_capacity, const boost::false_type& )
292 pointer new_buffer = allocate( new_capacity ); // strong
293 scope_guard guard = make_obj_guard( *this,
294 &auto_buffer::deallocate,
297 copy_impl( begin(), end(), new_buffer ); // strong
298 guard.dismiss(); // nothrow
302 pointer move_to_new_buffer( size_type new_capacity, const boost::true_type& )
304 pointer new_buffer = allocate( new_capacity ); // strong
305 copy_impl( begin(), end(), new_buffer ); // nothrow
309 void reserve_impl( size_type new_capacity )
311 pointer new_buffer = move_to_new_buffer( new_capacity,
312 boost::has_nothrow_copy<T>() );
313 auto_buffer_destroy();
314 buffer_ = new_buffer;
315 members_.capacity_ = new_capacity;
316 BOOST_ASSERT( size_ <= members_.capacity_ );
319 size_type new_capacity_impl( size_type n )
321 BOOST_ASSERT( n > members_.capacity_ );
322 size_type new_capacity = GrowPolicy::new_capacity( members_.capacity_ );
323 // @todo: consider to check for allocator.max_size()
324 return (std::max)(new_capacity,n);
327 static void swap_helper( auto_buffer& l, auto_buffer& r,
328 const boost::true_type& )
330 BOOST_ASSERT( l.is_on_stack() && r.is_on_stack() );
332 auto_buffer temp( l.begin(), l.end() );
333 assign_impl( r.begin(), r.end(), l.begin() );
334 assign_impl( temp.begin(), temp.end(), r.begin() );
335 boost::swap( l.size_, r.size_ );
336 boost::swap( l.members_.capacity_, r.members_.capacity_ );
339 static void swap_helper( auto_buffer& l, auto_buffer& r,
340 const boost::false_type& )
342 BOOST_ASSERT( l.is_on_stack() && r.is_on_stack() );
343 size_type min_size = (std::min)(l.size_,r.size_);
344 size_type max_size = (std::max)(l.size_,r.size_);
345 size_type diff = max_size - min_size;
346 auto_buffer* smallest = l.size_ == min_size ? &l : &r;
347 auto_buffer* largest = smallest == &l ? &r : &l;
349 // @remark: the implementation below is not as fast
350 // as it could be if we assumed T had a default
354 for( ; i < min_size; ++i )
355 boost::swap( (*smallest)[i], (*largest)[i] );
357 for( ; i < max_size; ++i )
358 smallest->unchecked_push_back( (*largest)[i] );
360 largest->pop_back_n( diff );
361 boost::swap( l.members_.capacity_, r.members_.capacity_ );
364 void one_sided_swap( auto_buffer& temp ) // nothrow
366 BOOST_ASSERT( !temp.is_on_stack() );
367 auto_buffer_destroy();
368 // @remark: must be nothrow
369 get_allocator() = temp.get_allocator();
370 members_.capacity_ = temp.members_.capacity_;
371 buffer_ = temp.buffer_;
372 BOOST_ASSERT( temp.size_ >= size_ + 1u );
375 BOOST_ASSERT( temp.is_valid() );
379 void insert_impl( const_iterator before, I begin_arg, I end_arg,
380 std::input_iterator_tag )
382 for( ; begin_arg != end_arg; ++begin_arg )
384 before = insert( before, *begin_arg );
389 void grow_back( size_type n, const boost::true_type& )
391 BOOST_ASSERT( size_ + n <= members_.capacity_ );
395 void grow_back( size_type n, const boost::false_type& )
397 unchecked_push_back_n(n);
400 void grow_back( size_type n )
402 grow_back( n, boost::has_trivial_constructor<T>() );
405 void grow_back_one( const boost::true_type& )
407 BOOST_ASSERT( size_ + 1 <= members_.capacity_ );
411 void grow_back_one( const boost::false_type& )
413 unchecked_push_back();
418 grow_back_one( boost::has_trivial_constructor<T>() );
422 void insert_impl( const_iterator before, I begin_arg, I end_arg,
423 std::forward_iterator_tag )
425 difference_type n = std::distance(begin_arg, end_arg);
427 if( size_ + n <= members_.capacity_ )
429 bool is_back_insertion = before == cend();
430 if( !is_back_insertion )
433 iterator where = const_cast<T*>(before);
434 std::copy( before, cend() - n, where + n );
435 assign_impl( begin_arg, end_arg, where );
439 unchecked_push_back( begin_arg, end_arg );
441 BOOST_ASSERT( is_valid() );
445 auto_buffer temp( new_capacity_impl( size_ + n ) );
446 temp.unchecked_push_back( cbegin(), before );
447 temp.unchecked_push_back( begin_arg, end_arg );
448 temp.unchecked_push_back( before, cend() );
449 one_sided_swap( temp );
450 BOOST_ASSERT( is_valid() );
454 bool is_valid() const // invariant
456 // @remark: allowed for N==0 and when
457 // using a locally instance
458 // in insert()/one_sided_swap()
462 if( members_.capacity_ < N )
465 if( !is_on_stack() && members_.capacity_ <= N )
468 if( buffer_ == members_.address() )
469 if( members_.capacity_ > N )
472 if( size_ > members_.capacity_ )
480 buffer_( static_cast<T*>(members_.address()) ),
483 BOOST_ASSERT( is_valid() );
486 auto_buffer( const auto_buffer& r )
487 : members_( (std::max)(r.size_,size_type(N)) ),
488 buffer_( allocate( members_.capacity_ ) ),
491 copy_impl( r.begin(), r.end(), buffer_ );
493 BOOST_ASSERT( is_valid() );
496 auto_buffer& operator=( const auto_buffer& r ) // basic
501 difference_type diff = size_ - r.size_;
504 pop_back_n( static_cast<size_type>(diff) );
505 assign_impl( r.begin(), r.end(), begin() );
509 if( members_.capacity_ >= r.size() )
511 unchecked_push_back_n( static_cast<size_type>(-diff) );
512 assign_impl( r.begin(), r.end(), begin() );
516 // @remark: we release memory as early as possible
517 // since we only give the basic guarantee
518 auto_buffer_destroy();
520 pointer new_buffer = allocate( r.size() );
521 scope_guard guard = make_obj_guard( *this,
522 &auto_buffer::deallocate,
525 copy_impl( r.begin(), r.end(), new_buffer );
527 buffer_ = new_buffer;
528 members_.capacity_ = r.size();
529 size_ = members_.capacity_;
533 BOOST_ASSERT( size() == r.size() );
534 BOOST_ASSERT( is_valid() );
538 explicit auto_buffer( size_type capacity_arg )
539 : members_( (std::max)(capacity_arg, size_type(N)) ),
540 buffer_( allocate(members_.capacity_) ),
543 BOOST_ASSERT( is_valid() );
546 auto_buffer( size_type size_arg, optimized_const_reference init_value )
547 : members_( (std::max)(size_arg, size_type(N)) ),
548 buffer_( allocate(members_.capacity_) ),
551 std::uninitialized_fill( buffer_, buffer_ + size_arg, init_value );
553 BOOST_ASSERT( is_valid() );
556 auto_buffer( size_type capacity_arg, const allocator_type& a )
557 : allocator_type( a ),
558 members_( (std::max)(capacity_arg, size_type(N)) ),
559 buffer_( allocate(members_.capacity_) ),
562 BOOST_ASSERT( is_valid() );
565 auto_buffer( size_type size_arg, optimized_const_reference init_value,
566 const allocator_type& a )
567 : allocator_type( a ),
568 members_( (std::max)(size_arg, size_type(N)) ),
569 buffer_( allocate(members_.capacity_) ),
572 std::uninitialized_fill( buffer_, buffer_ + size_arg, init_value );
574 BOOST_ASSERT( is_valid() );
577 template< class ForwardIterator >
578 auto_buffer( ForwardIterator begin_arg, ForwardIterator end_arg )
580 members_( std::distance(begin_arg, end_arg) ),
581 buffer_( allocate(members_.capacity_) ),
584 copy_impl( begin_arg, end_arg, buffer_ );
585 size_ = members_.capacity_;
586 if( members_.capacity_ < N )
587 members_.capacity_ = N;
588 BOOST_ASSERT( is_valid() );
591 template< class ForwardIterator >
592 auto_buffer( ForwardIterator begin_arg, ForwardIterator end_arg,
593 const allocator_type& a )
594 : allocator_type( a ),
595 members_( std::distance(begin_arg, end_arg) ),
596 buffer_( allocate(members_.capacity_) ),
599 copy_impl( begin_arg, end_arg, buffer_ );
600 size_ = members_.capacity_;
601 if( members_.capacity_ < N )
602 members_.capacity_ = N;
603 BOOST_ASSERT( is_valid() );
608 auto_buffer_destroy();
619 return size_ == members_.capacity_;
622 bool is_on_stack() const
624 return members_.capacity_ <= N;
627 size_type size() const
632 size_type capacity() const
634 return members_.capacity_;
643 const_pointer data() const
648 allocator_type& get_allocator()
650 return static_cast<allocator_type&>(*this);
653 const allocator_type& get_allocator() const
655 return static_cast<const allocator_type&>(*this);
664 const_iterator begin() const
671 return buffer_ + size_;
674 const_iterator end() const
676 return buffer_ + size_;
679 reverse_iterator rbegin()
681 return reverse_iterator(end());
684 const_reverse_iterator rbegin() const
686 return const_reverse_iterator(end());
689 reverse_iterator rend()
691 return reverse_iterator(begin());
694 const_reverse_iterator rend() const
696 return const_reverse_iterator(begin());
699 const_iterator cbegin() const
701 return const_cast<const auto_buffer*>(this)->begin();
704 const_iterator cend() const
706 return const_cast<const auto_buffer*>(this)->end();
709 const_reverse_iterator crbegin() const
711 return const_cast<const auto_buffer*>(this)->rbegin();
714 const_reverse_iterator crend() const
716 return const_cast<const auto_buffer*>(this)->rend();
725 optimized_const_reference front() const
732 return buffer_[size_-1];
735 optimized_const_reference back() const
737 return buffer_[size_-1];
740 reference operator[]( size_type n )
742 BOOST_ASSERT( n < size_ );
746 optimized_const_reference operator[]( size_type n ) const
748 BOOST_ASSERT( n < size_ );
752 void unchecked_push_back()
754 BOOST_ASSERT( !full() );
755 new (buffer_ + size_) T;
759 void unchecked_push_back_n( size_type n )
761 BOOST_ASSERT( size_ + n <= members_.capacity_ );
762 unchecked_push_back_n( n, boost::has_trivial_assign<T>() );
765 void unchecked_push_back( optimized_const_reference x ) // non-growing
767 BOOST_ASSERT( !full() );
768 new (buffer_ + size_) T( x );
772 template< class ForwardIterator >
773 void unchecked_push_back( ForwardIterator begin_arg,
774 ForwardIterator end_arg ) // non-growing
776 BOOST_ASSERT( size_ + std::distance(begin_arg, end_arg) <= members_.capacity_ );
777 copy_impl( begin_arg, end_arg, buffer_ + size_ );
778 size_ += std::distance(begin_arg, end_arg);
781 void reserve_precisely( size_type n )
783 BOOST_ASSERT( members_.capacity_ >= N );
785 if( n <= members_.capacity_ )
788 BOOST_ASSERT( members_.capacity_ == n );
791 void reserve( size_type n ) // strong
793 BOOST_ASSERT( members_.capacity_ >= N );
795 if( n <= members_.capacity_ )
798 reserve_impl( new_capacity_impl( n ) );
799 BOOST_ASSERT( members_.capacity_ >= n );
804 if( size_ != members_.capacity_ )
806 unchecked_push_back();
810 reserve( size_ + 1u );
811 unchecked_push_back();
815 void push_back( optimized_const_reference x )
817 if( size_ != members_.capacity_ )
819 unchecked_push_back( x );
823 reserve( size_ + 1u );
824 unchecked_push_back( x );
828 template< class ForwardIterator >
829 void push_back( ForwardIterator begin_arg, ForwardIterator end_arg )
831 difference_type diff = std::distance(begin_arg, end_arg);
832 if( size_ + diff > members_.capacity_ )
833 reserve( size_ + diff );
834 unchecked_push_back( begin_arg, end_arg );
837 iterator insert( const_iterator before, optimized_const_reference x ) // basic
839 // @todo: consider if we want to support x in 'this'
840 if( size_ < members_.capacity_ )
842 bool is_back_insertion = before == cend();
843 iterator where = const_cast<T*>(before);
845 if( !is_back_insertion )
848 std::copy( before, cend() - 1u, where + 1u );
850 BOOST_ASSERT( is_valid() );
854 unchecked_push_back( x );
859 auto_buffer temp( new_capacity_impl( size_ + 1u ) );
860 temp.unchecked_push_back( cbegin(), before );
861 iterator result = temp.end();
862 temp.unchecked_push_back( x );
863 temp.unchecked_push_back( before, cend() );
864 one_sided_swap( temp );
865 BOOST_ASSERT( is_valid() );
869 void insert( const_iterator before, size_type n,
870 optimized_const_reference x )
872 // @todo: see problems above
873 if( size_ + n <= members_.capacity_ )
876 iterator where = const_cast<T*>(before);
877 std::copy( before, cend() - n, where + n );
878 std::fill( where, where + n, x );
879 BOOST_ASSERT( is_valid() );
883 auto_buffer temp( new_capacity_impl( size_ + n ) );
884 temp.unchecked_push_back( cbegin(), before );
885 std::uninitialized_fill_n( temp.end(), n, x );
887 temp.unchecked_push_back( before, cend() );
888 one_sided_swap( temp );
889 BOOST_ASSERT( is_valid() );
892 template< class ForwardIterator >
893 void insert( const_iterator before,
894 ForwardIterator begin_arg, ForwardIterator end_arg ) // basic
896 typedef typename std::iterator_traits<ForwardIterator>
897 ::iterator_category category;
898 insert_impl( before, begin_arg, end_arg, category() );
903 BOOST_ASSERT( !empty() );
904 auto_buffer_destroy( buffer_ + size_ - 1, boost::has_trivial_destructor<T>() );
908 void pop_back_n( size_type n )
910 BOOST_ASSERT( n <= size_ );
923 iterator erase( const_iterator where )
925 BOOST_ASSERT( !empty() );
926 BOOST_ASSERT( cbegin() <= where );
927 BOOST_ASSERT( cend() > where );
929 unsigned elements = cend() - where - 1u;
933 const_iterator start = where + 1u;
934 std::copy( start, start + elements,
935 const_cast<T*>(where) );
938 BOOST_ASSERT( !full() );
939 iterator result = const_cast<T*>( where );
940 BOOST_ASSERT( result <= end() );
944 iterator erase( const_iterator from, const_iterator to )
946 BOOST_ASSERT( !(std::distance(from,to)>0) ||
948 BOOST_ASSERT( cbegin() <= from );
949 BOOST_ASSERT( cend() >= to );
951 unsigned elements = std::distance(to,cend());
955 BOOST_ASSERT( elements > 0u );
956 std::copy( to, to + elements,
957 const_cast<T*>(from) );
959 pop_back_n( std::distance(from,to) );
960 BOOST_ASSERT( !full() );
961 iterator result = const_cast<T*>( from );
962 BOOST_ASSERT( result <= end() );
968 if( is_on_stack() || !GrowPolicy::should_shrink(size_,members_.capacity_) )
971 reserve_impl( size_ );
972 members_.capacity_ = (std::max)(size_type(N),members_.capacity_);
973 BOOST_ASSERT( is_on_stack() || size_ == members_.capacity_ );
974 BOOST_ASSERT( !is_on_stack() || size_ <= members_.capacity_ );
977 pointer uninitialized_grow( size_type n ) // strong
979 if( size_ + n > members_.capacity_ )
980 reserve( size_ + n );
987 void uninitialized_shrink( size_type n ) // nothrow
989 // @remark: test for wrap-around
990 BOOST_ASSERT( size_ - n <= members_.capacity_ );
994 void uninitialized_resize( size_type n )
997 uninitialized_grow( n - size() );
998 else if( n < size() )
999 uninitialized_shrink( size() - n );
1001 BOOST_ASSERT( size() == n );
1004 // nothrow - if both buffer are on the heap, or
1005 // - if one buffer is on the heap and one has
1006 // 'has_allocated_buffer() == false', or
1007 // - if copy-construction cannot throw
1008 // basic - otherwise (better guarantee impossible)
1009 // requirement: the allocator must be no-throw-swappable
1010 void swap( auto_buffer& r )
1012 bool on_stack = is_on_stack();
1013 bool r_on_stack = r.is_on_stack();
1014 bool both_on_heap = !on_stack && !r_on_stack;
1017 boost::swap( get_allocator(), r.get_allocator() );
1018 boost::swap( members_.capacity_, r.members_.capacity_ );
1019 boost::swap( buffer_, r.buffer_ );
1020 boost::swap( size_, r.size_ );
1021 BOOST_ASSERT( is_valid() );
1022 BOOST_ASSERT( r.is_valid() );
1026 BOOST_ASSERT( on_stack || r_on_stack );
1027 bool exactly_one_on_stack = (on_stack && !r_on_stack) ||
1028 (!on_stack && r_on_stack);
1031 // Remark: we now know that we can copy into
1032 // the unused stack buffer.
1034 if( exactly_one_on_stack )
1036 auto_buffer* one_on_stack = on_stack ? this : &r;
1037 auto_buffer* other = on_stack ? &r : this;
1038 pointer new_buffer = static_cast<T*>(other->members_.address());
1039 copy_impl( one_on_stack->begin(), one_on_stack->end(),
1040 new_buffer ); // strong
1041 one_on_stack->auto_buffer_destroy(); // nothrow
1042 boost::swap( get_allocator(), r.get_allocator() ); // assume nothrow
1043 boost::swap( members_.capacity_, r.members_.capacity_ );
1044 boost::swap( size_, r.size_ );
1045 one_on_stack->buffer_ = other->buffer_;
1046 other->buffer_ = new_buffer;
1047 BOOST_ASSERT( other->is_on_stack() );
1048 BOOST_ASSERT( !one_on_stack->is_on_stack() );
1049 BOOST_ASSERT( is_valid() );
1050 BOOST_ASSERT( r.is_valid() );
1054 BOOST_ASSERT( on_stack && r_on_stack );
1055 swap_helper( *this, r, boost::has_trivial_assign<T>() );
1056 BOOST_ASSERT( is_valid() );
1057 BOOST_ASSERT( r.is_valid() );
1061 typedef boost::aligned_storage< N * sizeof(T),
1062 boost::alignment_of<T>::value >
1065 struct members_type : storage /* to enable EBO */
1067 size_type capacity_;
1069 members_type( size_type capacity )
1070 : capacity_(capacity)
1073 void* address() const
1074 { return const_cast<storage&>(static_cast<const storage&>(*this)).address(); }
1077 members_type members_;
1083 template< class T, class SBP, class GP, class A >
1084 inline void swap( auto_buffer<T,SBP,GP,A>& l, auto_buffer<T,SBP,GP,A>& r )
1089 template< class T, class SBP, class GP, class A >
1090 inline bool operator==( const auto_buffer<T,SBP,GP,A>& l,
1091 const auto_buffer<T,SBP,GP,A>& r )
1093 if( l.size() != r.size() )
1095 return std::equal( l.begin(), l.end(), r.begin() );
1098 template< class T, class SBP, class GP, class A >
1099 inline bool operator!=( const auto_buffer<T,SBP,GP,A>& l,
1100 const auto_buffer<T,SBP,GP,A>& r )
1105 template< class T, class SBP, class GP, class A >
1106 inline bool operator<( const auto_buffer<T,SBP,GP,A>& l,
1107 const auto_buffer<T,SBP,GP,A>& r )
1109 return std::lexicographical_compare( l.begin(), l.end(),
1110 r.begin(), r.end() );
1113 template< class T, class SBP, class GP, class A >
1114 inline bool operator>( const auto_buffer<T,SBP,GP,A>& l,
1115 const auto_buffer<T,SBP,GP,A>& r )
1120 template< class T, class SBP, class GP, class A >
1121 inline bool operator<=( const auto_buffer<T,SBP,GP,A>& l,
1122 const auto_buffer<T,SBP,GP,A>& r )
1127 template< class T, class SBP, class GP, class A >
1128 inline bool operator>=( const auto_buffer<T,SBP,GP,A>& l,
1129 const auto_buffer<T,SBP,GP,A>& r )
1134 } // namespace detail
1135 } // namespace signals2
1138 #if BOOST_WORKAROUND(BOOST_MSVC, >= 1400)
1139 #pragma warning(pop)