]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/boost/ptr_container/ptr_array.hpp
update sources to v12.2.3
[ceph.git] / ceph / src / boost / boost / ptr_container / ptr_array.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_PTR_ARRAY_HPP
13 #define BOOST_PTR_CONTAINER_PTR_ARRAY_HPP
14
15 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
16 # pragma once
17 #endif
18
19 #include <boost/array.hpp>
20 #include <boost/static_assert.hpp>
21 #include <boost/ptr_container/ptr_sequence_adapter.hpp>
22
23 namespace boost
24 {
25
26 namespace ptr_container_detail
27 {
28 template
29 <
30 class T,
31 size_t N,
32 class Allocator = int // dummy
33 >
34 class ptr_array_impl : public boost::array<T,N>
35 {
36 public:
37 typedef Allocator allocator_type;
38
39 ptr_array_impl( Allocator /*a*/ = Allocator() )
40 {
41 this->assign( 0 );
42 }
43
44 ptr_array_impl( size_t, T*, Allocator /*a*/ = Allocator() )
45 {
46 this->assign( 0 );
47 }
48 };
49 }
50
51 template
52 <
53 class T,
54 size_t N,
55 class CloneAllocator = heap_clone_allocator
56 >
57 class ptr_array : public
58 ptr_sequence_adapter< T,
59 ptr_container_detail::ptr_array_impl<
60 typename ptr_container_detail::void_ptr<T>::type,N>,
61 CloneAllocator >
62 {
63 private:
64 typedef ptr_sequence_adapter< T,
65 ptr_container_detail::ptr_array_impl<
66 typename ptr_container_detail::void_ptr<T>::type,N>,
67 CloneAllocator >
68 base_class;
69
70 typedef BOOST_DEDUCED_TYPENAME remove_nullable<T>::type U;
71
72 typedef ptr_array<T,N,CloneAllocator>
73 this_type;
74
75 public:
76 typedef std::size_t size_type;
77 typedef U* value_type;
78 typedef U* pointer;
79 typedef U& reference;
80 typedef const U& const_reference;
81 typedef BOOST_DEDUCED_TYPENAME base_class::auto_type
82 auto_type;
83
84 public: // constructors
85 ptr_array() : base_class()
86 { }
87
88 ptr_array( const ptr_array& r )
89 {
90 size_t i = 0;
91 for( ; i != N; ++i )
92 this->base()[i] = this->null_policy_allocate_clone(
93 static_cast<const U*>( &r[i] ) );
94 }
95
96 template< class U >
97 ptr_array( const ptr_array<U,N>& r )
98 {
99 size_t i = 0;
100 for( ; i != N; ++i )
101 this->base()[i] = this->null_policy_allocate_clone(
102 static_cast<const T*>( &r[i] ) );
103 }
104
105 explicit ptr_array( std::auto_ptr<this_type> r )
106 : base_class( r ) { }
107
108 ptr_array& operator=( ptr_array r )
109 {
110 this->swap( r );
111 return *this;
112 }
113
114 ptr_array& operator=( std::auto_ptr<this_type> r )
115 {
116 base_class::operator=(r);
117 return *this;
118 }
119
120 std::auto_ptr<this_type> release()
121 {
122 std::auto_ptr<this_type> ptr( new this_type );
123 this->swap( *ptr );
124 return ptr;
125 }
126
127 std::auto_ptr<this_type> clone() const
128 {
129 std::auto_ptr<this_type> pa( new this_type );
130 for( size_t i = 0; i != N; ++i )
131 {
132 if( !this->is_null(i) )
133 pa->replace( i, pa->null_policy_allocate_clone( &(*this)[i] ) );
134 }
135 return pa;
136 }
137
138 private: // hide some members
139 using base_class::insert;
140 using base_class::erase;
141 using base_class::push_back;
142 using base_class::push_front;
143 using base_class::pop_front;
144 using base_class::pop_back;
145 using base_class::transfer;
146 using base_class::get_allocator;
147
148 public: // compile-time interface
149
150 template< size_t idx >
151 auto_type replace( U* r ) // strong
152 {
153 BOOST_STATIC_ASSERT( idx < N );
154
155 this->enforce_null_policy( r, "Null pointer in 'ptr_array::replace()'" );
156 auto_type res( static_cast<U*>(this->base()[idx]), *this ); // nothrow
157 this->base()[idx] = r; // nothrow
158 return boost::ptr_container::move(res); // nothrow
159 }
160
161 template< size_t idx, class V >
162 auto_type replace( std::auto_ptr<V> r )
163 {
164 return replace<idx>( r.release() );
165 }
166
167 auto_type replace( size_t idx, U* r ) // strong
168 {
169 this->enforce_null_policy( r, "Null pointer in 'ptr_array::replace()'" );
170
171 auto_type ptr( r, *this );
172 BOOST_PTR_CONTAINER_THROW_EXCEPTION( idx >= N, bad_index,
173 "'replace()' aout of bounds" );
174
175 auto_type res( static_cast<U*>(this->base()[idx]), *this ); // nothrow
176 this->base()[idx] = ptr.release(); // nothrow
177 return boost::ptr_container::move(res); // nothrow
178 }
179
180 template< class V >
181 auto_type replace( size_t idx, std::auto_ptr<V> r )
182 {
183 return replace( idx, r.release() );
184 }
185
186 using base_class::at;
187
188 template< size_t idx >
189 T& at()
190 {
191 BOOST_STATIC_ASSERT( idx < N );
192 return (*this)[idx];
193 }
194
195 template< size_t idx >
196 const T& at() const
197 {
198 BOOST_STATIC_ASSERT( idx < N );
199 return (*this)[idx];
200 }
201
202 bool is_null( size_t idx ) const
203 {
204 return base_class::is_null(idx);
205 }
206
207 template< size_t idx >
208 bool is_null() const
209 {
210 BOOST_STATIC_ASSERT( idx < N );
211 return this->base()[idx] == 0;
212 }
213 };
214
215 //////////////////////////////////////////////////////////////////////////////
216 // clonability
217
218 template< typename T, size_t size, typename CA >
219 inline ptr_array<T,size,CA>* new_clone( const ptr_array<T,size,CA>& r )
220 {
221 return r.clone().release();
222 }
223
224 /////////////////////////////////////////////////////////////////////////
225 // swap
226
227 template< typename T, size_t size, typename CA >
228 inline void swap( ptr_array<T,size,CA>& l, ptr_array<T,size,CA>& r )
229 {
230 l.swap(r);
231 }
232 }
233
234 #endif