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/iterator/reverse_iterator.hpp>
22 #include <boost/iterator/iterator_traits.hpp>
23 #include <boost/mpl/if.hpp>
24 #include <boost/multi_index/detail/scope_guard.hpp>
25 #include <boost/swap.hpp>
26 #include <boost/type_traits/aligned_storage.hpp>
27 #include <boost/type_traits/alignment_of.hpp>
28 #include <boost/type_traits/has_nothrow_copy.hpp>
29 #include <boost/type_traits/has_nothrow_assign.hpp>
30 #include <boost/type_traits/has_trivial_assign.hpp>
31 #include <boost/type_traits/has_trivial_constructor.hpp>
32 #include <boost/type_traits/has_trivial_destructor.hpp>
46 // Policies for creating the stack buffer.
48 template< unsigned N >
49 struct store_n_objects
51 BOOST_STATIC_CONSTANT( unsigned, value = N );
54 template< unsigned N >
57 BOOST_STATIC_CONSTANT( unsigned, value = N );
60 namespace auto_buffer_detail
62 template< class Policy, class T >
63 struct compute_buffer_size
65 BOOST_STATIC_CONSTANT( unsigned, value = Policy::value * sizeof(T) );
68 template< unsigned N, class T >
69 struct compute_buffer_size< store_n_bytes<N>, T >
71 BOOST_STATIC_CONSTANT( unsigned, value = N );
74 template< class Policy, class T >
75 struct compute_buffer_objects
77 BOOST_STATIC_CONSTANT( unsigned, value = Policy::value );
80 template< unsigned N, class T >
81 struct compute_buffer_objects< store_n_bytes<N>, T >
83 BOOST_STATIC_CONSTANT( unsigned, value = N / sizeof(T) );
87 struct default_grow_policy
89 template< class SizeType >
90 static SizeType new_capacity( SizeType capacity )
93 // @remark: we grow the capacity quite agressively.
94 // this is justified since we aim to minimize
95 // heap-allocations, and because we mostly use
96 // the buffer locally.
100 template< class SizeType >
101 static bool should_shrink( SizeType, SizeType )
104 // @remark: when defining a new grow policy, one might
105 // choose that if the waated space is less
106 // than a certain percentage, then it is of
107 // little use to shrink.
114 class StackBufferPolicy = store_n_objects<256>,
115 class GrowPolicy = default_grow_policy,
116 class Allocator = std::allocator<T> >
124 class StackBufferPolicy,
128 class auto_buffer : Allocator
131 enum { N = auto_buffer_detail::
132 compute_buffer_objects<StackBufferPolicy,T>::value };
134 BOOST_STATIC_CONSTANT( bool, is_stack_buffer_empty = N == 0u );
136 typedef auto_buffer<T, store_n_objects<0>, GrowPolicy, Allocator>
140 typedef Allocator allocator_type;
141 typedef T value_type;
142 typedef typename Allocator::size_type size_type;
143 typedef typename Allocator::difference_type difference_type;
145 typedef typename Allocator::pointer allocator_pointer;
146 typedef const T* const_pointer;
147 typedef T& reference;
148 typedef const T& const_reference;
149 typedef pointer iterator;
150 typedef const_pointer const_iterator;
151 typedef boost::reverse_iterator<iterator> reverse_iterator;
152 typedef boost::reverse_iterator<const_iterator> const_reverse_iterator;
153 typedef typename boost::mpl::if_c< boost::has_trivial_assign<T>::value
154 && sizeof(T) <= sizeof(long double),
156 const_reference >::type
157 optimized_const_reference;
160 pointer allocate( size_type capacity_arg )
162 if( capacity_arg > N )
163 return &*get_allocator().allocate( capacity_arg );
165 return static_cast<T*>( members_.address() );
168 void deallocate( pointer where, size_type capacity_arg )
170 if( capacity_arg <= N )
172 get_allocator().deallocate( allocator_pointer(where), capacity_arg );
176 static void copy_impl( I begin, I end, pointer where, std::random_access_iterator_tag )
178 copy_rai( begin, end, where, boost::has_trivial_assign<T>() );
181 static void copy_rai( const T* begin, const T* end,
182 pointer where, const boost::true_type& )
184 std::memcpy( where, begin, sizeof(T) * std::distance(begin,end) );
187 template< class I, bool b >
188 static void copy_rai( I begin, I end,
189 pointer where, const boost::integral_constant<bool, b>& )
191 std::uninitialized_copy( begin, end, where );
195 static void copy_impl( I begin, I end, pointer where, std::bidirectional_iterator_tag )
197 std::uninitialized_copy( begin, end, where );
201 static void copy_impl( I begin, I end, pointer where )
203 copy_impl( begin, end, where,
204 typename std::iterator_traits<I>::iterator_category() );
207 template< class I, class I2 >
208 static void assign_impl( I begin, I end, I2 where )
210 assign_impl( begin, end, where, boost::has_trivial_assign<T>() );
213 template< class I, class I2 >
214 static void assign_impl( I begin, I end, I2 where, const boost::true_type& )
216 std::memcpy( where, begin, sizeof(T) * std::distance(begin,end) );
219 template< class I, class I2 >
220 static void assign_impl( I begin, I end, I2 where, const boost::false_type& )
222 for( ; begin != end; ++begin, ++where )
226 void unchecked_push_back_n( size_type n, const boost::true_type& )
228 std::uninitialized_fill( end(), end() + n, T() );
232 void unchecked_push_back_n( size_type n, const boost::false_type& )
234 for( size_type i = 0u; i < n; ++i )
235 unchecked_push_back();
238 void auto_buffer_destroy( pointer where, const boost::false_type& )
243 void auto_buffer_destroy( pointer, const boost::true_type& )
246 void auto_buffer_destroy( pointer where )
248 auto_buffer_destroy( where, boost::has_trivial_destructor<T>() );
251 void auto_buffer_destroy()
253 BOOST_ASSERT( is_valid() );
254 if( buffer_ ) // do we need this check? Yes, but only
255 // for N = 0u + local instances in one_sided_swap()
256 auto_buffer_destroy( boost::has_trivial_destructor<T>() );
259 void destroy_back_n( size_type n, const boost::false_type& )
261 BOOST_ASSERT( n > 0 );
262 pointer buffer = buffer_ + size_ - 1u;
263 pointer new_end = buffer - n;
264 for( ; buffer > new_end; --buffer )
265 auto_buffer_destroy( buffer );
268 void destroy_back_n( size_type, const boost::true_type& )
271 void destroy_back_n( size_type n )
273 destroy_back_n( n, boost::has_trivial_destructor<T>() );
276 void auto_buffer_destroy( const boost::false_type& x )
279 destroy_back_n( size_, x );
280 deallocate( buffer_, members_.capacity_ );
283 void auto_buffer_destroy( const boost::true_type& )
285 deallocate( buffer_, members_.capacity_ );
288 pointer move_to_new_buffer( size_type new_capacity, const boost::false_type& )
290 pointer new_buffer = allocate( new_capacity ); // strong
291 boost::multi_index::detail::scope_guard guard =
292 boost::multi_index::detail::make_obj_guard( *this,
293 &auto_buffer::deallocate,
296 copy_impl( begin(), end(), new_buffer ); // strong
297 guard.dismiss(); // nothrow
301 pointer move_to_new_buffer( size_type new_capacity, const boost::true_type& )
303 pointer new_buffer = allocate( new_capacity ); // strong
304 copy_impl( begin(), end(), new_buffer ); // nothrow
308 void reserve_impl( size_type new_capacity )
310 pointer new_buffer = move_to_new_buffer( new_capacity,
311 boost::has_nothrow_copy<T>() );
312 auto_buffer_destroy();
313 buffer_ = new_buffer;
314 members_.capacity_ = new_capacity;
315 BOOST_ASSERT( size_ <= members_.capacity_ );
318 size_type new_capacity_impl( size_type n )
320 BOOST_ASSERT( n > members_.capacity_ );
321 size_type new_capacity = GrowPolicy::new_capacity( members_.capacity_ );
322 // @todo: consider to check for allocator.max_size()
323 return (std::max)(new_capacity,n);
326 static void swap_helper( auto_buffer& l, auto_buffer& r,
327 const boost::true_type& )
329 BOOST_ASSERT( l.is_on_stack() && r.is_on_stack() );
331 auto_buffer temp( l.begin(), l.end() );
332 assign_impl( r.begin(), r.end(), l.begin() );
333 assign_impl( temp.begin(), temp.end(), r.begin() );
334 boost::swap( l.size_, r.size_ );
335 boost::swap( l.members_.capacity_, r.members_.capacity_ );
338 static void swap_helper( auto_buffer& l, auto_buffer& r,
339 const boost::false_type& )
341 BOOST_ASSERT( l.is_on_stack() && r.is_on_stack() );
342 size_type min_size = (std::min)(l.size_,r.size_);
343 size_type max_size = (std::max)(l.size_,r.size_);
344 size_type diff = max_size - min_size;
345 auto_buffer* smallest = l.size_ == min_size ? &l : &r;
346 auto_buffer* largest = smallest == &l ? &r : &l;
348 // @remark: the implementation below is not as fast
349 // as it could be if we assumed T had a default
353 for( ; i < min_size; ++i )
354 boost::swap( (*smallest)[i], (*largest)[i] );
356 for( ; i < max_size; ++i )
357 smallest->unchecked_push_back( (*largest)[i] );
359 largest->pop_back_n( diff );
360 boost::swap( l.members_.capacity_, r.members_.capacity_ );
363 void one_sided_swap( auto_buffer& temp ) // nothrow
365 BOOST_ASSERT( !temp.is_on_stack() );
366 auto_buffer_destroy();
367 // @remark: must be nothrow
368 get_allocator() = temp.get_allocator();
369 members_.capacity_ = temp.members_.capacity_;
370 buffer_ = temp.buffer_;
371 BOOST_ASSERT( temp.size_ >= size_ + 1u );
374 BOOST_ASSERT( temp.is_valid() );
378 void insert_impl( const_iterator before, I begin_arg, I end_arg,
379 std::input_iterator_tag )
381 for( ; begin_arg != end_arg; ++begin_arg )
383 before = insert( before, *begin_arg );
388 void grow_back( size_type n, const boost::true_type& )
390 BOOST_ASSERT( size_ + n <= members_.capacity_ );
394 void grow_back( size_type n, const boost::false_type& )
396 unchecked_push_back_n(n);
399 void grow_back( size_type n )
401 grow_back( n, boost::has_trivial_constructor<T>() );
404 void grow_back_one( const boost::true_type& )
406 BOOST_ASSERT( size_ + 1 <= members_.capacity_ );
410 void grow_back_one( const boost::false_type& )
412 unchecked_push_back();
417 grow_back_one( boost::has_trivial_constructor<T>() );
421 void insert_impl( const_iterator before, I begin_arg, I end_arg,
422 std::forward_iterator_tag )
424 difference_type n = std::distance(begin_arg, end_arg);
426 if( size_ + n <= members_.capacity_ )
428 bool is_back_insertion = before == cend();
429 if( !is_back_insertion )
432 iterator where = const_cast<T*>(before);
433 std::copy( before, cend() - n, where + n );
434 assign_impl( begin_arg, end_arg, where );
438 unchecked_push_back( begin_arg, end_arg );
440 BOOST_ASSERT( is_valid() );
444 auto_buffer temp( new_capacity_impl( size_ + n ) );
445 temp.unchecked_push_back( cbegin(), before );
446 temp.unchecked_push_back( begin_arg, end_arg );
447 temp.unchecked_push_back( before, cend() );
448 one_sided_swap( temp );
449 BOOST_ASSERT( is_valid() );
453 bool is_valid() const // invariant
455 // @remark: allowed for N==0 and when
456 // using a locally instance
457 // in insert()/one_sided_swap()
461 if( members_.capacity_ < N )
464 if( !is_on_stack() && members_.capacity_ <= N )
467 if( buffer_ == members_.address() )
468 if( members_.capacity_ > N )
471 if( size_ > members_.capacity_ )
479 buffer_( static_cast<T*>(members_.address()) ),
482 BOOST_ASSERT( is_valid() );
485 auto_buffer( const auto_buffer& r )
486 : members_( (std::max)(r.size_,size_type(N)) ),
487 buffer_( allocate( members_.capacity_ ) ),
490 copy_impl( r.begin(), r.end(), buffer_ );
492 BOOST_ASSERT( is_valid() );
495 auto_buffer& operator=( const auto_buffer& r ) // basic
500 difference_type diff = size_ - r.size_;
503 pop_back_n( static_cast<size_type>(diff) );
504 assign_impl( r.begin(), r.end(), begin() );
508 if( members_.capacity_ >= r.size() )
510 unchecked_push_back_n( static_cast<size_type>(-diff) );
511 assign_impl( r.begin(), r.end(), begin() );
515 // @remark: we release memory as early as possible
516 // since we only give the basic guarantee
517 auto_buffer_destroy();
519 pointer new_buffer = allocate( r.size() );
520 boost::multi_index::detail::scope_guard guard =
521 boost::multi_index::detail::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)