]>
Commit | Line | Data |
---|---|---|
285b36d6 BK |
1 | // Debugging vector implementation -*- C++ -*- |
2 | ||
3 | // Copyright (C) 2003 | |
4 | // Free Software Foundation, Inc. | |
5 | // | |
6 | // This file is part of the GNU ISO C++ Library. This library is free | |
7 | // software; you can redistribute it and/or modify it under the | |
8 | // terms of the GNU General Public License as published by the | |
9 | // Free Software Foundation; either version 2, or (at your option) | |
10 | // any later version. | |
11 | ||
12 | // This library is distributed in the hope that it will be useful, | |
13 | // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | // GNU General Public License for more details. | |
16 | ||
17 | // You should have received a copy of the GNU General Public License along | |
18 | // with this library; see the file COPYING. If not, write to the Free | |
19 | // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, | |
20 | // USA. | |
21 | ||
22 | // As a special exception, you may use this file as part of a free software | |
23 | // library without restriction. Specifically, if other files instantiate | |
24 | // templates or use macros or inline functions from this file, or you compile | |
25 | // this file and link it with other files to produce an executable, this | |
26 | // file does not by itself cause the resulting executable to be covered by | |
27 | // the GNU General Public License. This exception does not however | |
28 | // invalidate any other reasons why the executable file might be covered by | |
29 | // the GNU General Public License. | |
30 | ||
31 | #ifndef _GLIBCXX_DEBUG_VECTOR | |
32 | #define _GLIBCXX_DEBUG_VECTOR 1 | |
33 | ||
34 | #include <vector> | |
35 | #include <debug/safe_sequence.h> | |
36 | #include <debug/safe_iterator.h> | |
37 | ||
38 | namespace __gnu_debug_def | |
39 | { | |
40 | template<typename _Tp, | |
41 | typename _Allocator = std::allocator<_Tp> > | |
42 | class vector | |
43 | : public __gnu_norm::vector<_Tp, _Allocator>, | |
44 | public __gnu_debug::_Safe_sequence<vector<_Tp, _Allocator> > | |
45 | { | |
46 | typedef __gnu_norm::vector<_Tp, _Allocator> _Base; | |
47 | typedef __gnu_debug::_Safe_sequence<vector> _Safe_base; | |
48 | ||
49 | typedef typename _Base::const_iterator _Base_const_iterator; | |
50 | typedef __gnu_debug::_After_nth_from<_Base_const_iterator> _After_nth; | |
51 | ||
52 | public: | |
53 | typedef typename _Base::reference reference; | |
54 | typedef typename _Base::const_reference const_reference; | |
55 | ||
56 | typedef __gnu_debug::_Safe_iterator<typename _Base::iterator,vector> | |
57 | iterator; | |
58 | typedef __gnu_debug::_Safe_iterator<typename _Base::const_iterator,vector> | |
59 | const_iterator; | |
60 | ||
61 | typedef typename _Base::size_type size_type; | |
62 | typedef typename _Base::difference_type difference_type; | |
63 | ||
64 | typedef _Tp value_type; | |
65 | typedef _Allocator allocator_type; | |
66 | typedef typename _Allocator::pointer pointer; | |
67 | typedef typename _Allocator::const_pointer const_pointer; | |
68 | typedef std::reverse_iterator<iterator> reverse_iterator; | |
69 | typedef std::reverse_iterator<const_iterator> const_reverse_iterator; | |
70 | ||
71 | // 23.2.4.1 construct/copy/destroy: | |
72 | explicit vector(const _Allocator& __a = _Allocator()) | |
73 | : _Base(__a), _M_guaranteed_capacity(0) { } | |
74 | ||
75 | explicit vector(size_type __n, const _Tp& __value = _Tp(), | |
76 | const _Allocator& __a = _Allocator()) | |
77 | : _Base(__n, __value, __a), _M_guaranteed_capacity(__n) { } | |
78 | ||
79 | template<class _InputIterator> | |
80 | vector(_InputIterator __first, _InputIterator __last, | |
81 | const _Allocator& __a = _Allocator()) | |
82 | : _Base(__gnu_debug::__check_valid_range(__first, __last), | |
83 | __last, __a), | |
84 | _M_guaranteed_capacity(0) | |
85 | { _M_update_guaranteed_capacity(); } | |
86 | ||
87 | vector(const vector<_Tp,_Allocator>& __x) | |
88 | : _Base(__x), _Safe_base(), _M_guaranteed_capacity(__x.size()) { } | |
89 | ||
90 | /// Construction from a release-mode vector | |
91 | vector(const _Base& __x) | |
92 | : _Base(__x), _Safe_base(), _M_guaranteed_capacity(__x.size()) { } | |
93 | ||
94 | ~vector() { } | |
95 | ||
96 | vector<_Tp,_Allocator>& | |
97 | operator=(const vector<_Tp,_Allocator>& __x) | |
98 | { | |
99 | static_cast<_Base&>(*this) = __x; | |
100 | this->_M_invalidate_all(); | |
101 | _M_update_guaranteed_capacity(); | |
102 | return *this; | |
103 | } | |
104 | ||
105 | template<typename _InputIterator> | |
106 | void | |
107 | assign(_InputIterator __first, _InputIterator __last) | |
108 | { | |
109 | __glibcxx_check_valid_range(__first, __last); | |
110 | _Base::assign(__first, __last); | |
111 | this->_M_invalidate_all(); | |
112 | _M_update_guaranteed_capacity(); | |
113 | } | |
114 | ||
115 | void | |
116 | assign(size_type __n, const _Tp& __u) | |
117 | { | |
118 | _Base::assign(__n, __u); | |
119 | this->_M_invalidate_all(); | |
120 | _M_update_guaranteed_capacity(); | |
121 | } | |
122 | ||
123 | using _Base::get_allocator; | |
124 | ||
125 | // iterators: | |
126 | iterator | |
127 | begin() | |
128 | { return iterator(_Base::begin(), this); } | |
129 | ||
130 | const_iterator | |
131 | begin() const | |
132 | { return const_iterator(_Base::begin(), this); } | |
133 | ||
134 | iterator | |
135 | end() | |
136 | { return iterator(_Base::end(), this); } | |
137 | ||
138 | const_iterator | |
139 | end() const | |
140 | { return const_iterator(_Base::end(), this); } | |
141 | ||
142 | reverse_iterator | |
143 | rbegin() | |
144 | { return reverse_iterator(end()); } | |
145 | ||
146 | const_reverse_iterator | |
147 | rbegin() const | |
148 | { return const_reverse_iterator(end()); } | |
149 | ||
150 | reverse_iterator | |
151 | rend() | |
152 | { return reverse_iterator(begin()); } | |
153 | ||
154 | const_reverse_iterator | |
155 | rend() const | |
156 | { return const_reverse_iterator(begin()); } | |
157 | ||
158 | // 23.2.4.2 capacity: | |
159 | using _Base::size; | |
160 | using _Base::max_size; | |
161 | ||
162 | void | |
163 | resize(size_type __sz, _Tp __c = _Tp()) | |
164 | { | |
165 | bool __realloc = _M_requires_reallocation(__sz); | |
166 | if (__sz < this->size()) | |
167 | this->_M_invalidate_if(_After_nth(__sz, _M_base().begin())); | |
168 | _Base::resize(__sz, __c); | |
169 | if (__realloc) | |
170 | this->_M_invalidate_all(); | |
171 | } | |
172 | ||
173 | using _Base::capacity; | |
174 | using _Base::empty; | |
175 | ||
176 | void | |
177 | reserve(size_type __n) | |
178 | { | |
179 | bool __realloc = _M_requires_reallocation(__n); | |
180 | _Base::reserve(__n); | |
181 | if (__n > _M_guaranteed_capacity) | |
182 | _M_guaranteed_capacity = __n; | |
183 | if (__realloc) | |
184 | this->_M_invalidate_all(); | |
185 | } | |
186 | ||
187 | // element access: | |
188 | reference | |
189 | operator[](size_type __n) | |
190 | { | |
191 | __glibcxx_check_subscript(__n); | |
192 | return _M_base()[__n]; | |
193 | } | |
194 | ||
195 | const_reference | |
196 | operator[](size_type __n) const | |
197 | { | |
198 | __glibcxx_check_subscript(__n); | |
199 | return _M_base()[__n]; | |
200 | } | |
201 | ||
202 | using _Base::at; | |
203 | ||
204 | reference | |
205 | front() | |
206 | { | |
207 | __glibcxx_check_nonempty(); | |
208 | return _Base::front(); | |
209 | } | |
210 | ||
211 | const_reference | |
212 | front() const | |
213 | { | |
214 | __glibcxx_check_nonempty(); | |
215 | return _Base::front(); | |
216 | } | |
217 | ||
218 | reference | |
219 | back() | |
220 | { | |
221 | __glibcxx_check_nonempty(); | |
222 | return _Base::back(); | |
223 | } | |
224 | ||
225 | const_reference | |
226 | back() const | |
227 | { | |
228 | __glibcxx_check_nonempty(); | |
229 | return _Base::back(); | |
230 | } | |
231 | ||
232 | // 23.2.4.3 modifiers: | |
233 | void | |
234 | push_back(const _Tp& __x) | |
235 | { | |
236 | bool __realloc = _M_requires_reallocation(this->size() + 1); | |
237 | _Base::push_back(__x); | |
238 | if (__realloc) | |
239 | this->_M_invalidate_all(); | |
240 | _M_update_guaranteed_capacity(); | |
241 | } | |
242 | ||
243 | void | |
244 | pop_back() | |
245 | { | |
246 | __glibcxx_check_nonempty(); | |
247 | iterator __victim = end() - 1; | |
248 | __victim._M_invalidate(); | |
249 | _Base::pop_back(); | |
250 | } | |
251 | ||
252 | iterator | |
253 | insert(iterator __position, const _Tp& __x) | |
254 | { | |
255 | __glibcxx_check_insert(__position); | |
256 | bool __realloc = _M_requires_reallocation(this->size() + 1); | |
257 | difference_type __offset = __position - begin(); | |
258 | typename _Base::iterator __res = _Base::insert(__position.base(),__x); | |
259 | if (__realloc) | |
260 | this->_M_invalidate_all(); | |
261 | else | |
262 | this->_M_invalidate_if(_After_nth(__offset, _M_base().begin())); | |
263 | _M_update_guaranteed_capacity(); | |
264 | return iterator(__res, this); | |
265 | } | |
266 | ||
267 | void | |
268 | insert(iterator __position, size_type __n, const _Tp& __x) | |
269 | { | |
270 | __glibcxx_check_insert(__position); | |
271 | bool __realloc = _M_requires_reallocation(this->size() + __n); | |
272 | difference_type __offset = __position - begin(); | |
273 | _Base::insert(__position.base(), __n, __x); | |
274 | if (__realloc) | |
275 | this->_M_invalidate_all(); | |
276 | else | |
277 | this->_M_invalidate_if(_After_nth(__offset, _M_base().begin())); | |
278 | _M_update_guaranteed_capacity(); | |
279 | } | |
280 | ||
281 | template<class _InputIterator> | |
282 | void | |
283 | insert(iterator __position, | |
284 | _InputIterator __first, _InputIterator __last) | |
285 | { | |
286 | __glibcxx_check_insert_range(__position, __first, __last); | |
287 | ||
288 | /* Hard to guess if invalidation will occur, because __last | |
289 | - __first can't be calculated in all cases, so we just | |
290 | punt here by checking if it did occur. */ | |
291 | typename _Base::iterator __old_begin = _M_base().begin(); | |
292 | difference_type __offset = __position - begin(); | |
293 | _Base::insert(__position.base(), __first, __last); | |
294 | ||
295 | if (_M_base().begin() != __old_begin) | |
296 | this->_M_invalidate_all(); | |
297 | else | |
298 | this->_M_invalidate_if(_After_nth(__offset, _M_base().begin())); | |
299 | _M_update_guaranteed_capacity(); | |
300 | } | |
301 | ||
302 | iterator | |
303 | erase(iterator __position) | |
304 | { | |
305 | __glibcxx_check_erase(__position); | |
306 | difference_type __offset = __position - begin(); | |
307 | typename _Base::iterator __res = _Base::erase(__position.base()); | |
308 | this->_M_invalidate_if(_After_nth(__offset, _M_base().begin())); | |
309 | return iterator(__res, this); | |
310 | } | |
311 | ||
312 | iterator | |
313 | erase(iterator __first, iterator __last) | |
314 | { | |
315 | // _GLIBCXX_RESOLVE_LIB_DEFECTS | |
316 | // 151. can't currently clear() empty container | |
317 | __glibcxx_check_erase_range(__first, __last); | |
318 | ||
319 | difference_type __offset = __first - begin(); | |
320 | typename _Base::iterator __res = _Base::erase(__first.base(), | |
321 | __last.base()); | |
322 | this->_M_invalidate_if(_After_nth(__offset, _M_base().begin())); | |
323 | return iterator(__res, this); | |
324 | } | |
325 | ||
326 | void | |
327 | swap(vector<_Tp,_Allocator>& __x) | |
328 | { | |
329 | _Base::swap(__x); | |
330 | this->_M_swap(__x); | |
331 | } | |
332 | ||
333 | void | |
334 | clear() | |
335 | { | |
336 | _Base::clear(); | |
337 | this->_M_invalidate_all(); | |
338 | } | |
339 | ||
340 | _Base& | |
341 | _M_base() { return *this; } | |
342 | ||
343 | const _Base& | |
344 | _M_base() const { return *this; } | |
345 | ||
346 | private: | |
347 | size_type _M_guaranteed_capacity; | |
348 | ||
349 | bool | |
350 | _M_requires_reallocation(size_type __elements) | |
351 | { | |
352 | #ifdef _GLIBCXX_DEBUG_PEDANTIC | |
353 | return __elements > this->capacity(); | |
354 | #else | |
355 | return __elements > _M_guaranteed_capacity; | |
356 | #endif | |
357 | } | |
358 | ||
359 | void | |
360 | _M_update_guaranteed_capacity() | |
361 | { | |
362 | if (this->size() > _M_guaranteed_capacity) | |
363 | _M_guaranteed_capacity = this->size(); | |
364 | } | |
365 | }; | |
366 | ||
367 | template<typename _Tp, typename _Alloc> | |
368 | inline bool | |
369 | operator==(const vector<_Tp, _Alloc>& __lhs, | |
370 | const vector<_Tp, _Alloc>& __rhs) | |
371 | { return __lhs._M_base() == __rhs._M_base(); } | |
372 | ||
373 | template<typename _Tp, typename _Alloc> | |
374 | inline bool | |
375 | operator!=(const vector<_Tp, _Alloc>& __lhs, | |
376 | const vector<_Tp, _Alloc>& __rhs) | |
377 | { return __lhs._M_base() != __rhs._M_base(); } | |
378 | ||
379 | template<typename _Tp, typename _Alloc> | |
380 | inline bool | |
381 | operator<(const vector<_Tp, _Alloc>& __lhs, | |
382 | const vector<_Tp, _Alloc>& __rhs) | |
383 | { return __lhs._M_base() < __rhs._M_base(); } | |
384 | ||
385 | template<typename _Tp, typename _Alloc> | |
386 | inline bool | |
387 | operator<=(const vector<_Tp, _Alloc>& __lhs, | |
388 | const vector<_Tp, _Alloc>& __rhs) | |
389 | { return __lhs._M_base() <= __rhs._M_base(); } | |
390 | ||
391 | template<typename _Tp, typename _Alloc> | |
392 | inline bool | |
393 | operator>=(const vector<_Tp, _Alloc>& __lhs, | |
394 | const vector<_Tp, _Alloc>& __rhs) | |
395 | { return __lhs._M_base() >= __rhs._M_base(); } | |
396 | ||
397 | template<typename _Tp, typename _Alloc> | |
398 | inline bool | |
399 | operator>(const vector<_Tp, _Alloc>& __lhs, | |
400 | const vector<_Tp, _Alloc>& __rhs) | |
401 | { return __lhs._M_base() > __rhs._M_base(); } | |
402 | ||
403 | template<typename _Tp, typename _Alloc> | |
404 | inline void | |
405 | swap(vector<_Tp, _Alloc>& __lhs, vector<_Tp, _Alloc>& __rhs) | |
406 | { __lhs.swap(__rhs); } | |
407 | } // namespace __gnu_debug_def | |
408 | ||
409 | #endif |