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->assing( 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<void*,N>, |
---|
60 | CloneAllocator > |
---|
61 | { |
---|
62 | private: |
---|
63 | typedef ptr_sequence_adapter< T, |
---|
64 | ptr_container_detail::ptr_array_impl<void*,N>, |
---|
65 | CloneAllocator > |
---|
66 | base_class; |
---|
67 | |
---|
68 | typedef BOOST_DEDUCED_TYPENAME remove_nullable<T>::type U; |
---|
69 | |
---|
70 | typedef ptr_array<T,N,CloneAllocator> |
---|
71 | this_type; |
---|
72 | |
---|
73 | ptr_array( const this_type& ); |
---|
74 | void operator=( const this_type& ); |
---|
75 | |
---|
76 | public: |
---|
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( std::auto_ptr<this_type> r ) |
---|
89 | : base_class( r ) { } |
---|
90 | |
---|
91 | void operator=( std::auto_ptr<this_type> r ) |
---|
92 | { |
---|
93 | base_class::operator=(r); |
---|
94 | } |
---|
95 | |
---|
96 | std::auto_ptr<this_type> release() |
---|
97 | { |
---|
98 | std::auto_ptr<this_type> ptr( new this_type ); |
---|
99 | this->swap( *ptr ); |
---|
100 | return ptr; |
---|
101 | } |
---|
102 | |
---|
103 | std::auto_ptr<this_type> clone() const |
---|
104 | { |
---|
105 | std::auto_ptr<this_type> pa( new this_type ); |
---|
106 | for( size_t i = 0; i != N; ++i ) |
---|
107 | { |
---|
108 | if( ! is_null(i) ) |
---|
109 | pa->replace( i, CloneAllocator::allocate_clone( (*this)[i] ) ); |
---|
110 | } |
---|
111 | return pa; |
---|
112 | } |
---|
113 | |
---|
114 | private: // hide some members |
---|
115 | using base_class::insert; |
---|
116 | using base_class::erase; |
---|
117 | using base_class::push_back; |
---|
118 | using base_class::push_front; |
---|
119 | using base_class::pop_front; |
---|
120 | using base_class::pop_back; |
---|
121 | using base_class::transfer; |
---|
122 | using base_class::get_allocator; |
---|
123 | |
---|
124 | public: // compile-time interface |
---|
125 | |
---|
126 | template< size_t idx > |
---|
127 | auto_type replace( U* r ) // strong |
---|
128 | { |
---|
129 | BOOST_STATIC_ASSERT( idx < N ); |
---|
130 | |
---|
131 | this->enforce_null_policy( r, "Null pointer in 'ptr_array::replace()'" ); |
---|
132 | |
---|
133 | auto_type res( static_cast<U*>( this->c_private()[idx] ) ); // nothrow |
---|
134 | this->c_private()[idx] = r; // nothrow |
---|
135 | return move(res); // nothrow |
---|
136 | } |
---|
137 | |
---|
138 | auto_type replace( size_t idx, U* r ) // strong |
---|
139 | { |
---|
140 | this->enforce_null_policy( r, "Null pointer in 'ptr_array::replace()'" ); |
---|
141 | |
---|
142 | auto_type ptr( r ); |
---|
143 | |
---|
144 | if( idx >= N ) |
---|
145 | throw bad_index( "'replace()' aout of bounds" ); |
---|
146 | |
---|
147 | auto_type res( static_cast<U*>( this->c_private()[idx] ) ); // nothrow |
---|
148 | this->c_private()[idx] = ptr.release(); // nothrow |
---|
149 | return move(res); // nothrow |
---|
150 | } |
---|
151 | |
---|
152 | using base_class::at; |
---|
153 | |
---|
154 | template< size_t idx > |
---|
155 | T& at() |
---|
156 | { |
---|
157 | BOOST_STATIC_ASSERT( idx < N ); |
---|
158 | return (*this)[idx]; |
---|
159 | } |
---|
160 | |
---|
161 | template< size_t idx > |
---|
162 | const T& at() const |
---|
163 | { |
---|
164 | BOOST_STATIC_ASSERT( idx < N ); |
---|
165 | return (*this)[idx]; |
---|
166 | } |
---|
167 | |
---|
168 | bool is_null( size_t idx ) const |
---|
169 | { |
---|
170 | return base_class::is_null(idx); |
---|
171 | } |
---|
172 | |
---|
173 | template< size_t idx > |
---|
174 | bool is_null() const |
---|
175 | { |
---|
176 | BOOST_STATIC_ASSERT( idx < N ); |
---|
177 | return this->c_private()[idx] == 0; |
---|
178 | } |
---|
179 | |
---|
180 | }; |
---|
181 | |
---|
182 | ////////////////////////////////////////////////////////////////////////////// |
---|
183 | // clonability |
---|
184 | |
---|
185 | template< typename T, size_t size, typename CA > |
---|
186 | inline ptr_array<T,size,CA>* new_clone( const ptr_array<T,size,CA>& r ) |
---|
187 | { |
---|
188 | return r.clone().release(); |
---|
189 | } |
---|
190 | |
---|
191 | ///////////////////////////////////////////////////////////////////////// |
---|
192 | // swap |
---|
193 | |
---|
194 | template< typename T, size_t size, typename CA > |
---|
195 | inline void swap( ptr_array<T,size,CA>& l, ptr_array<T,size,CA>& r ) |
---|
196 | { |
---|
197 | l.swap(r); |
---|
198 | } |
---|
199 | } |
---|
200 | |
---|
201 | #endif |
---|