OSDN Git Service

bb9aef9302d64ae15de18e15c6fb56b48b04a40a
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / include / debug / deque
1 // Debugging deque implementation -*- C++ -*-
2
3 // Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
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 3, 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 // Under Section 7 of GPL version 3, you are granted additional
18 // permissions described in the GCC Runtime Library Exception, version
19 // 3.1, as published by the Free Software Foundation.
20
21 // You should have received a copy of the GNU General Public License and
22 // a copy of the GCC Runtime Library Exception along with this program;
23 // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
24 // <http://www.gnu.org/licenses/>.
25
26 /** @file debug/deque
27  *  This file is a GNU debug extension to the Standard C++ Library.
28  */
29
30 #ifndef _GLIBCXX_DEBUG_DEQUE
31 #define _GLIBCXX_DEBUG_DEQUE 1
32
33 #include <deque>
34 #include <debug/safe_sequence.h>
35 #include <debug/safe_iterator.h>
36
37 namespace std
38 {
39 namespace __debug
40 {
41   /// Class std::deque with safety/checking/debug instrumentation.
42   template<typename _Tp, typename _Allocator = std::allocator<_Tp> >
43     class deque
44     : public _GLIBCXX_STD_D::deque<_Tp, _Allocator>,
45       public __gnu_debug::_Safe_sequence<deque<_Tp, _Allocator> >
46     {
47       typedef  _GLIBCXX_STD_D::deque<_Tp, _Allocator> _Base;
48       typedef __gnu_debug::_Safe_sequence<deque> _Safe_base;
49
50     public:
51       typedef typename _Base::reference             reference;
52       typedef typename _Base::const_reference       const_reference;
53
54       typedef __gnu_debug::_Safe_iterator<typename _Base::iterator,deque>
55                                                     iterator;
56       typedef __gnu_debug::_Safe_iterator<typename _Base::const_iterator,deque>
57                                                      const_iterator;
58
59       typedef typename _Base::size_type             size_type;
60       typedef typename _Base::difference_type       difference_type;
61
62       typedef _Tp                                   value_type;
63       typedef _Allocator                            allocator_type;
64       typedef typename _Base::pointer               pointer;
65       typedef typename _Base::const_pointer         const_pointer;
66       typedef std::reverse_iterator<iterator>       reverse_iterator;
67       typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
68
69       // 23.2.1.1 construct/copy/destroy:
70       explicit deque(const _Allocator& __a = _Allocator())
71       : _Base(__a) { }
72
73       explicit deque(size_type __n, const _Tp& __value = _Tp(),
74                      const _Allocator& __a = _Allocator())
75       : _Base(__n, __value, __a) { }
76
77       template<class _InputIterator>
78         deque(_InputIterator __first, _InputIterator __last,
79               const _Allocator& __a = _Allocator())
80         : _Base(__gnu_debug::__check_valid_range(__first, __last), __last, __a)
81         { }
82
83       deque(const deque& __x)
84       : _Base(__x), _Safe_base() { }
85
86       deque(const _Base& __x)
87       : _Base(__x), _Safe_base() { }
88
89 #ifdef __GXX_EXPERIMENTAL_CXX0X__
90       deque(deque&& __x)
91       : _Base(std::forward<deque>(__x)), _Safe_base()
92       { this->_M_swap(__x); }
93
94       deque(initializer_list<value_type> __l,
95             const allocator_type& __a = allocator_type())
96       : _Base(__l, __a), _Safe_base() { }
97 #endif
98
99       ~deque() { }
100
101       deque&
102       operator=(const deque& __x)
103       {
104         *static_cast<_Base*>(this) = __x;
105         this->_M_invalidate_all();
106         return *this;
107       }
108
109 #ifdef __GXX_EXPERIMENTAL_CXX0X__
110       deque&
111       operator=(deque&& __x)
112       {
113         // NB: DR 675.
114         clear();
115         swap(__x);        
116         return *this;
117       }
118
119       deque&
120       operator=(initializer_list<value_type> __l)
121       {
122         *static_cast<_Base*>(this) = __l;
123         this->_M_invalidate_all();
124         return *this;
125       }
126 #endif
127
128       template<class _InputIterator>
129         void
130         assign(_InputIterator __first, _InputIterator __last)
131         {
132           __glibcxx_check_valid_range(__first, __last);
133           _Base::assign(__first, __last);
134           this->_M_invalidate_all();
135         }
136
137       void
138       assign(size_type __n, const _Tp& __t)
139       {
140         _Base::assign(__n, __t);
141         this->_M_invalidate_all();
142       }
143
144 #ifdef __GXX_EXPERIMENTAL_CXX0X__
145       void
146       assign(initializer_list<value_type> __l)
147       {
148         _Base::assign(__l);
149         this->_M_invalidate_all();
150       }
151 #endif
152
153       using _Base::get_allocator;
154
155       // iterators:
156       iterator
157       begin()
158       { return iterator(_Base::begin(), this); }
159
160       const_iterator
161       begin() const
162       { return const_iterator(_Base::begin(), this); }
163
164       iterator
165       end()
166       { return iterator(_Base::end(), this); }
167
168       const_iterator
169       end() const
170       { return const_iterator(_Base::end(), this); }
171
172       reverse_iterator
173       rbegin()
174       { return reverse_iterator(end()); }
175
176       const_reverse_iterator
177       rbegin() const
178       { return const_reverse_iterator(end()); }
179
180       reverse_iterator
181       rend()
182       { return reverse_iterator(begin()); }
183
184       const_reverse_iterator
185       rend() const
186       { return const_reverse_iterator(begin()); }
187
188 #ifdef __GXX_EXPERIMENTAL_CXX0X__
189       const_iterator
190       cbegin() const
191       { return const_iterator(_Base::begin(), this); }
192
193       const_iterator
194       cend() const
195       { return const_iterator(_Base::end(), this); }
196
197       const_reverse_iterator
198       crbegin() const
199       { return const_reverse_iterator(end()); }
200
201       const_reverse_iterator
202       crend() const
203       { return const_reverse_iterator(begin()); }
204 #endif
205
206       // 23.2.1.2 capacity:
207       using _Base::size;
208       using _Base::max_size;
209
210       void
211       resize(size_type __sz, _Tp __c = _Tp())
212       {
213         typedef typename _Base::const_iterator _Base_const_iterator;
214         typedef __gnu_debug::_After_nth_from<_Base_const_iterator> _After_nth;
215
216         bool __invalidate_all = __sz > this->size();
217         if (__sz < this->size())
218           this->_M_invalidate_if(_After_nth(__sz, _M_base().begin()));
219
220         _Base::resize(__sz, __c);
221
222         if (__invalidate_all)
223           this->_M_invalidate_all();
224       }
225
226 #ifdef __GXX_EXPERIMENTAL_CXX0X__
227       using _Base::shrink_to_fit;
228 #endif
229
230       using _Base::empty;
231
232       // element access:
233       reference
234       operator[](size_type __n)
235       {
236         __glibcxx_check_subscript(__n);
237         return _M_base()[__n];
238       }
239
240       const_reference
241       operator[](size_type __n) const
242       {
243         __glibcxx_check_subscript(__n);
244         return _M_base()[__n];
245       }
246
247       using _Base::at;
248
249       reference
250       front()
251       {
252         __glibcxx_check_nonempty();
253         return _Base::front();
254       }
255
256       const_reference
257       front() const
258       {
259         __glibcxx_check_nonempty();
260         return _Base::front();
261       }
262
263       reference
264       back()
265       {
266         __glibcxx_check_nonempty();
267         return _Base::back();
268       }
269
270       const_reference
271       back() const
272       {
273         __glibcxx_check_nonempty();
274         return _Base::back();
275       }
276
277       // 23.2.1.3 modifiers:
278       void
279       push_front(const _Tp& __x)
280       {
281         _Base::push_front(__x);
282         this->_M_invalidate_all();
283       }
284
285       void
286       push_back(const _Tp& __x)
287       {
288         _Base::push_back(__x);
289         this->_M_invalidate_all();
290       }
291
292 #ifdef __GXX_EXPERIMENTAL_CXX0X__
293       void
294       push_front(_Tp&& __x)
295       { emplace_front(std::move(__x)); }
296
297       void
298       push_back(_Tp&& __x)
299       { emplace_back(std::move(__x)); }
300
301       template<typename... _Args>
302         void
303         emplace_front(_Args&&... __args)
304         {
305           _Base::emplace_front(std::forward<_Args>(__args)...);
306           this->_M_invalidate_all();
307         }
308
309       template<typename... _Args>
310         void
311         emplace_back(_Args&&... __args)
312         {
313           _Base::emplace_back(std::forward<_Args>(__args)...);
314           this->_M_invalidate_all();
315         }
316
317       template<typename... _Args>
318         iterator
319         emplace(iterator __position, _Args&&... __args)
320         {
321           __glibcxx_check_insert(__position);
322           typename _Base::iterator __res = _Base::emplace(__position.base(),
323                                             std::forward<_Args>(__args)...);
324           this->_M_invalidate_all();
325           return iterator(__res, this);
326         }
327 #endif
328
329       iterator
330       insert(iterator __position, const _Tp& __x)
331       {
332         __glibcxx_check_insert(__position);
333         typename _Base::iterator __res = _Base::insert(__position.base(), __x);
334         this->_M_invalidate_all();
335         return iterator(__res, this);
336       }
337
338 #ifdef __GXX_EXPERIMENTAL_CXX0X__
339       iterator
340       insert(iterator __position, _Tp&& __x)
341       { return emplace(__position, std::move(__x)); }
342
343       void
344       insert(iterator __p, initializer_list<value_type> __l)
345       {
346         _Base::insert(__p, __l);
347         this->_M_invalidate_all();
348       }
349 #endif
350
351       void
352       insert(iterator __position, size_type __n, const _Tp& __x)
353       {
354         __glibcxx_check_insert(__position);
355         _Base::insert(__position.base(), __n, __x);
356         this->_M_invalidate_all();
357       }
358
359       template<class _InputIterator>
360         void
361         insert(iterator __position,
362                _InputIterator __first, _InputIterator __last)
363         {
364           __glibcxx_check_insert_range(__position, __first, __last);
365           _Base::insert(__position.base(), __first, __last);
366           this->_M_invalidate_all();
367         }
368
369       void
370       pop_front()
371       {
372         __glibcxx_check_nonempty();
373         iterator __victim = begin();
374         __victim._M_invalidate();
375         _Base::pop_front();
376       }
377
378       void
379       pop_back()
380       {
381         __glibcxx_check_nonempty();
382         iterator __victim = end();
383         --__victim;
384         __victim._M_invalidate();
385         _Base::pop_back();
386       }
387
388       iterator
389       erase(iterator __position)
390       {
391         __glibcxx_check_erase(__position);
392         if (__position == begin() || __position == end()-1)
393           {
394             __position._M_invalidate();
395             return iterator(_Base::erase(__position.base()), this);
396           }
397         else
398           {
399             typename _Base::iterator __res = _Base::erase(__position.base());
400             this->_M_invalidate_all();
401             return iterator(__res, this);
402           }
403       }
404
405       iterator
406       erase(iterator __first, iterator __last)
407       {
408         // _GLIBCXX_RESOLVE_LIB_DEFECTS
409         // 151. can't currently clear() empty container
410         __glibcxx_check_erase_range(__first, __last);
411         if (__first == begin() || __last == end())
412           {
413             this->_M_detach_singular();
414             for (iterator __position = __first; __position != __last; )
415               {
416                 iterator __victim = __position++;
417                 __victim._M_invalidate();
418               }
419             __try
420               {
421                 return iterator(_Base::erase(__first.base(), __last.base()),
422                                 this);
423               }
424             __catch(...)
425               {
426                 this->_M_revalidate_singular();
427                 __throw_exception_again;
428               }
429           }
430         else
431           {
432             typename _Base::iterator __res = _Base::erase(__first.base(),
433                                                           __last.base());
434             this->_M_invalidate_all();
435             return iterator(__res, this);
436           }
437       }
438
439       void
440       swap(deque& __x)
441       {
442         _Base::swap(__x);
443         this->_M_swap(__x);
444       }
445
446       void
447       clear()
448       {
449         _Base::clear();
450         this->_M_invalidate_all();
451       }
452
453       _Base&
454       _M_base()       { return *this; }
455
456       const _Base&
457       _M_base() const { return *this; }
458     };
459
460   template<typename _Tp, typename _Alloc>
461     inline bool
462     operator==(const deque<_Tp, _Alloc>& __lhs,
463                const deque<_Tp, _Alloc>& __rhs)
464     { return __lhs._M_base() == __rhs._M_base(); }
465
466   template<typename _Tp, typename _Alloc>
467     inline bool
468     operator!=(const deque<_Tp, _Alloc>& __lhs,
469                const deque<_Tp, _Alloc>& __rhs)
470     { return __lhs._M_base() != __rhs._M_base(); }
471
472   template<typename _Tp, typename _Alloc>
473     inline bool
474     operator<(const deque<_Tp, _Alloc>& __lhs,
475               const deque<_Tp, _Alloc>& __rhs)
476     { return __lhs._M_base() < __rhs._M_base(); }
477
478   template<typename _Tp, typename _Alloc>
479     inline bool
480     operator<=(const deque<_Tp, _Alloc>& __lhs,
481                const deque<_Tp, _Alloc>& __rhs)
482     { return __lhs._M_base() <= __rhs._M_base(); }
483
484   template<typename _Tp, typename _Alloc>
485     inline bool
486     operator>=(const deque<_Tp, _Alloc>& __lhs,
487                const deque<_Tp, _Alloc>& __rhs)
488     { return __lhs._M_base() >= __rhs._M_base(); }
489
490   template<typename _Tp, typename _Alloc>
491     inline bool
492     operator>(const deque<_Tp, _Alloc>& __lhs,
493               const deque<_Tp, _Alloc>& __rhs)
494     { return __lhs._M_base() > __rhs._M_base(); }
495
496   template<typename _Tp, typename _Alloc>
497     inline void
498     swap(deque<_Tp, _Alloc>& __lhs, deque<_Tp, _Alloc>& __rhs)
499     { __lhs.swap(__rhs); }
500
501 } // namespace __debug
502 } // namespace std
503
504 #endif