]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/boost/ptr_container/detail/scoped_deleter.hpp
update sources to v12.2.3
[ceph.git] / ceph / src / boost / boost / ptr_container / detail / scoped_deleter.hpp
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 Container >
29 class scoped_deleter
30 {
31 typedef BOOST_DEDUCED_TYPENAME Container::size_type size_type;
32 typedef BOOST_DEDUCED_TYPENAME Container::object_type T;
33
34 Container& cont_;
35 scoped_array<T*> ptrs_;
36 size_type stored_;
37 bool released_;
38
39 public:
40 scoped_deleter( Container& cont, T** a, size_type size )
41 : cont_(cont),
42 ptrs_( a ),
43 stored_( size ),
44 released_( false )
45 {
46 BOOST_ASSERT( a );
47 }
48
49 scoped_deleter( Container& cont, size_type size )
50 : cont_(cont),
51 ptrs_( new T*[size] ),
52 stored_( 0 ),
53 released_( false )
54 {
55 BOOST_ASSERT( size > 0 );
56 }
57
58
59
60 scoped_deleter( Container& cont, size_type n, const T& x ) // strong
61 : cont_(cont),
62 ptrs_( new T*[n] ),
63 stored_(0),
64 released_( false )
65 {
66 for( size_type i = 0; i != n; i++ )
67 add( cont_.null_policy_allocate_clone( &x ) );
68 BOOST_ASSERT( stored_ > 0 );
69 }
70
71
72
73 template< class InputIterator >
74 scoped_deleter ( Container& cont, InputIterator first, InputIterator last ) // strong
75 : cont_(cont),
76 ptrs_( new T*[ std::distance(first,last) ] ),
77 stored_(0),
78 released_( false )
79 {
80 for( ; first != last; ++first )
81 add( cont_.null_policy_allocate_clone_from_iterator( first ) );
82 BOOST_ASSERT( stored_ > 0 );
83 }
84
85
86
87 ~scoped_deleter()
88 {
89 if ( !released_ )
90 {
91 for( size_type i = 0u; i != stored_; ++i )
92 cont_.null_policy_deallocate_clone( ptrs_[i] );
93 }
94 }
95
96
97
98 void add( T* t )
99 {
100 BOOST_ASSERT( ptrs_.get() != 0 );
101 ptrs_[stored_] = t;
102 ++stored_;
103 }
104
105
106
107 void release()
108 {
109 released_ = true;
110 }
111
112
113
114 T** begin()
115 {
116 BOOST_ASSERT( ptrs_.get() != 0 );
117 return &ptrs_[0];
118 }
119
120
121
122 T** end()
123 {
124 BOOST_ASSERT( ptrs_.get() != 0 );
125 return &ptrs_[stored_];
126 }
127
128 }; // class 'scoped_deleter'
129 }
130 }
131
132 #endif