fix doc example typo
[boost.git] / boost / ptr_container / detail / scoped_deleter.hpp
bloba2e7aba2ea632af747122983624f738a02f2ed5d
1 //
2 // Boost.Pointer Container
3 //
4 // Copyright Thorsten Ottosen 2003-2005. 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)
8 //
9 // For more information, see http://www.boost.org/libs/ptr_container/
12 #ifndef BOOST_PTR_CONTAINER_SCOPED_DELETER_HPP
13 #define BOOST_PTR_CONTAINER_SCOPED_DELETER_HPP
15 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
16 # pragma once
17 #endif
19 #include <iterator>
20 #include <cstddef>
21 #include <boost/scoped_array.hpp>
23 namespace boost
26 namespace ptr_container_detail
28 template< class T, class CloneAllocator >
29 class scoped_deleter
31 typedef std::size_t size_type;
32 scoped_array<T*> ptrs_;
33 size_type stored_;
34 bool released_;
36 public:
37 scoped_deleter( T** a, size_type size )
38 : ptrs_( a ), stored_( size ), released_( false )
40 BOOST_ASSERT( a );
43 scoped_deleter( size_type size )
44 : ptrs_( new T*[size] ), stored_( 0 ),
45 released_( false )
47 BOOST_ASSERT( size > 0 );
52 scoped_deleter( size_type n, const T& x ) // strong
53 : ptrs_( new T*[n] ), stored_(0),
54 released_( false )
56 for( size_type i = 0; i != n; i++ )
57 add( CloneAllocator::allocate_clone( &x ) );
58 BOOST_ASSERT( stored_ > 0 );
63 template< class InputIterator >
64 scoped_deleter ( InputIterator first, InputIterator last ) // strong
65 : ptrs_( new T*[ std::distance(first,last) ] ),
66 stored_(0),
67 released_( false )
69 for( ; first != last; ++first )
70 add( CloneAllocator::allocate_clone_from_iterator( first ) );
71 BOOST_ASSERT( stored_ > 0 );
76 ~scoped_deleter()
78 if ( !released_ )
80 for( size_type i = 0u; i != stored_; ++i )
81 CloneAllocator::deallocate_clone( ptrs_[i] );
87 void add( T* t )
89 BOOST_ASSERT( ptrs_.get() != 0 );
90 ptrs_[stored_] = t;
91 ++stored_;
96 void release()
98 released_ = true;
103 T** begin()
105 BOOST_ASSERT( ptrs_.get() != 0 );
106 return &ptrs_[0];
111 T** end()
113 BOOST_ASSERT( ptrs_.get() != 0 );
114 return &ptrs_[stored_];
117 }; // class 'scoped_deleter'
121 #endif