File indexing completed on 2025-01-18 09:50:35
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012 #ifndef BOOST_PTR_CONTAINER_SCOPED_DELETER_HPP
0013 #define BOOST_PTR_CONTAINER_SCOPED_DELETER_HPP
0014
0015 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
0016 # pragma once
0017 #endif
0018
0019 #include <iterator>
0020 #include <cstddef>
0021 #include <boost/scoped_array.hpp>
0022
0023 namespace boost
0024 {
0025
0026 namespace ptr_container_detail
0027 {
0028 template< class Container >
0029 class scoped_deleter
0030 {
0031 typedef BOOST_DEDUCED_TYPENAME Container::size_type size_type;
0032 typedef BOOST_DEDUCED_TYPENAME Container::object_type T;
0033
0034 Container& cont_;
0035 scoped_array<T*> ptrs_;
0036 size_type stored_;
0037 bool released_;
0038
0039 public:
0040 scoped_deleter( Container& cont, T** a, size_type size )
0041 : cont_(cont),
0042 ptrs_( a ),
0043 stored_( size ),
0044 released_( false )
0045 {
0046 BOOST_ASSERT( a );
0047 }
0048
0049 scoped_deleter( Container& cont, size_type size )
0050 : cont_(cont),
0051 ptrs_( new T*[size] ),
0052 stored_( 0 ),
0053 released_( false )
0054 {
0055 BOOST_ASSERT( size > 0 );
0056 }
0057
0058
0059
0060 scoped_deleter( Container& cont, size_type n, const T& x )
0061 : cont_(cont),
0062 ptrs_( new T*[n] ),
0063 stored_(0),
0064 released_( false )
0065 {
0066 for( size_type i = 0; i != n; i++ )
0067 add( cont_.null_policy_allocate_clone( &x ) );
0068 BOOST_ASSERT( stored_ > 0 );
0069 }
0070
0071
0072
0073 template< class InputIterator >
0074 scoped_deleter ( Container& cont, InputIterator first, InputIterator last )
0075 : cont_(cont),
0076 ptrs_( new T*[ std::distance(first,last) ] ),
0077 stored_(0),
0078 released_( false )
0079 {
0080 for( ; first != last; ++first )
0081 add( cont_.null_policy_allocate_clone_from_iterator( first ) );
0082 BOOST_ASSERT( stored_ > 0 );
0083 }
0084
0085
0086
0087 ~scoped_deleter()
0088 {
0089 if ( !released_ )
0090 {
0091 for( size_type i = 0u; i != stored_; ++i )
0092 cont_.null_policy_deallocate_clone( ptrs_[i] );
0093 }
0094 }
0095
0096
0097
0098 void add( T* t )
0099 {
0100 BOOST_ASSERT( ptrs_.get() != 0 );
0101 ptrs_[stored_] = t;
0102 ++stored_;
0103 }
0104
0105
0106
0107 void release()
0108 {
0109 released_ = true;
0110 }
0111
0112
0113
0114 T** begin()
0115 {
0116 BOOST_ASSERT( ptrs_.get() != 0 );
0117 return &ptrs_[0];
0118 }
0119
0120
0121
0122 T** end()
0123 {
0124 BOOST_ASSERT( ptrs_.get() != 0 );
0125 return &ptrs_[stored_];
0126 }
0127
0128 };
0129 }
0130 }
0131
0132 #endif