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/ |
---|
10 | // |
---|
11 | |
---|
12 | #ifndef BOOST_PTR_CONTAINER_SCOPED_DELETER_HPP |
---|
13 | #define BOOST_PTR_CONTAINER_SCOPED_DELETER_HPP |
---|
14 | |
---|
15 | #if defined(_MSC_VER) && (_MSC_VER >= 1200) |
---|
16 | # pragma once |
---|
17 | #endif |
---|
18 | |
---|
19 | #include <iterator> |
---|
20 | #include <cstddef> |
---|
21 | #include <boost/scoped_array.hpp> |
---|
22 | |
---|
23 | namespace boost |
---|
24 | { |
---|
25 | |
---|
26 | namespace ptr_container_detail |
---|
27 | { |
---|
28 | template< class T, class CloneAllocator > |
---|
29 | class scoped_deleter |
---|
30 | { |
---|
31 | typedef std::size_t size_type; |
---|
32 | scoped_array<T*> ptrs_; |
---|
33 | size_type stored_; |
---|
34 | bool released_; |
---|
35 | |
---|
36 | public: |
---|
37 | scoped_deleter( size_type size ) : |
---|
38 | ptrs_( new T*[size] ), stored_( 0 ), |
---|
39 | released_( false ) |
---|
40 | { |
---|
41 | BOOST_ASSERT( size > 0 ); |
---|
42 | } |
---|
43 | |
---|
44 | |
---|
45 | |
---|
46 | scoped_deleter( size_type n, const T& x ) // strong |
---|
47 | : ptrs_( new T*[n] ), stored_(0), |
---|
48 | released_( false ) |
---|
49 | { |
---|
50 | for( size_type i = 0; i != n; i++ ) |
---|
51 | add( CloneAllocator::allocate_clone( &x ) ); |
---|
52 | BOOST_ASSERT( stored_ > 0 ); |
---|
53 | } |
---|
54 | |
---|
55 | |
---|
56 | |
---|
57 | template< class InputIterator > |
---|
58 | scoped_deleter ( InputIterator first, InputIterator last ) // strong |
---|
59 | : ptrs_( new T*[ std::distance(first,last) ] ), |
---|
60 | stored_(0), |
---|
61 | released_( false ) |
---|
62 | { |
---|
63 | for( ; first != last; ++first ) |
---|
64 | add( CloneAllocator::allocate_clone_from_iterator( first ) ); |
---|
65 | BOOST_ASSERT( stored_ > 0 ); |
---|
66 | } |
---|
67 | |
---|
68 | |
---|
69 | |
---|
70 | ~scoped_deleter() |
---|
71 | { |
---|
72 | if ( !released_ ) |
---|
73 | { |
---|
74 | for( size_type i = 0u; i != stored_; ++i ) |
---|
75 | CloneAllocator::deallocate_clone( ptrs_[i] ); |
---|
76 | } |
---|
77 | } |
---|
78 | |
---|
79 | |
---|
80 | |
---|
81 | void add( T* t ) |
---|
82 | { |
---|
83 | BOOST_ASSERT( ptrs_.get() != 0 ); |
---|
84 | ptrs_[stored_] = t; |
---|
85 | ++stored_; |
---|
86 | } |
---|
87 | |
---|
88 | |
---|
89 | |
---|
90 | void release() |
---|
91 | { |
---|
92 | released_ = true; |
---|
93 | } |
---|
94 | |
---|
95 | |
---|
96 | |
---|
97 | T** begin() |
---|
98 | { |
---|
99 | BOOST_ASSERT( ptrs_.get() != 0 ); |
---|
100 | return &ptrs_[0]; |
---|
101 | } |
---|
102 | |
---|
103 | |
---|
104 | |
---|
105 | T** end() |
---|
106 | { |
---|
107 | BOOST_ASSERT( ptrs_.get() != 0 ); |
---|
108 | return &ptrs_[stored_]; |
---|
109 | } |
---|
110 | |
---|
111 | }; // class 'scoped_deleter' |
---|
112 | } |
---|
113 | } |
---|
114 | |
---|
115 | #endif |
---|