OSDN Git Service

2010-08-05 Paolo Carlini <paolo.carlini@oracle.com>
[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
71       deque(const _Allocator& __a = _Allocator())
72       : _Base(__a) { }
73
74 #ifdef __GXX_EXPERIMENTAL_CXX0X__
75       explicit
76       deque(size_type __n)
77       : _Base(__n) { }
78
79       deque(size_type __n, const _Tp& __value,
80             const _Allocator& __a = _Allocator())
81       : _Base(__n, __value, __a) { }
82 #else
83       explicit
84       deque(size_type __n, const _Tp& __value = _Tp(),
85             const _Allocator& __a = _Allocator())
86       : _Base(__n, __value, __a) { }
87 #endif
88
89       template<class _InputIterator>
90         deque(_InputIterator __first, _InputIterator __last,
91               const _Allocator& __a = _Allocator())
92         : _Base(__gnu_debug::__check_valid_range(__first, __last), __last, __a)
93         { }
94
95       deque(const deque& __x)
96       : _Base(__x), _Safe_base() { }
97
98       deque(const _Base& __x)
99       : _Base(__x), _Safe_base() { }
100
101 #ifdef __GXX_EXPERIMENTAL_CXX0X__
102       deque(deque&& __x)
103       : _Base(std::move(__x)), _Safe_base()
104       { this->_M_swap(__x); }
105
106       deque(initializer_list<value_type> __l,
107             const allocator_type& __a = allocator_type())
108       : _Base(__l, __a), _Safe_base() { }
109 #endif
110
111       ~deque() { }
112
113       deque&
114       operator=(const deque& __x)
115       {
116         *static_cast<_Base*>(this) = __x;
117         this->_M_invalidate_all();
118         return *this;
119       }
120
121 #ifdef __GXX_EXPERIMENTAL_CXX0X__
122       deque&
123       operator=(deque&& __x)
124       {
125         // NB: DR 1204.
126         // NB: DR 675.
127         clear();
128         swap(__x);
129         return *this;
130       }
131
132       deque&
133       operator=(initializer_list<value_type> __l)
134       {
135         *static_cast<_Base*>(this) = __l;
136         this->_M_invalidate_all();
137         return *this;
138       }
139 #endif
140
141       template<class _InputIterator>
142         void
143         assign(_InputIterator __first, _InputIterator __last)
144         {
145           __glibcxx_check_valid_range(__first, __last);
146           _Base::assign(__first, __last);
147           this->_M_invalidate_all();
148         }
149
150       void
151       assign(size_type __n, const _Tp& __t)
152       {
153         _Base::assign(__n, __t);
154         this->_M_invalidate_all();
155       }
156
157 #ifdef __GXX_EXPERIMENTAL_CXX0X__
158       void
159       assign(initializer_list<value_type> __l)
160       {
161         _Base::assign(__l);
162         this->_M_invalidate_all();
163       }
164 #endif
165
166       using _Base::get_allocator;
167
168       // iterators:
169       iterator
170       begin()
171       { return iterator(_Base::begin(), this); }
172
173       const_iterator
174       begin() const
175       { return const_iterator(_Base::begin(), this); }
176
177       iterator
178       end()
179       { return iterator(_Base::end(), this); }
180
181       const_iterator
182       end() const
183       { return const_iterator(_Base::end(), this); }
184
185       reverse_iterator
186       rbegin()
187       { return reverse_iterator(end()); }
188
189       const_reverse_iterator
190       rbegin() const
191       { return const_reverse_iterator(end()); }
192
193       reverse_iterator
194       rend()
195       { return reverse_iterator(begin()); }
196
197       const_reverse_iterator
198       rend() const
199       { return const_reverse_iterator(begin()); }
200
201 #ifdef __GXX_EXPERIMENTAL_CXX0X__
202       const_iterator
203       cbegin() const
204       { return const_iterator(_Base::begin(), this); }
205
206       const_iterator
207       cend() const
208       { return const_iterator(_Base::end(), this); }
209
210       const_reverse_iterator
211       crbegin() const
212       { return const_reverse_iterator(end()); }
213
214       const_reverse_iterator
215       crend() const
216       { return const_reverse_iterator(begin()); }
217 #endif
218
219       // 23.2.1.2 capacity:
220       using _Base::size;
221       using _Base::max_size;
222
223 #ifdef __GXX_EXPERIMENTAL_CXX0X__
224       void
225       resize(size_type __sz)
226       {
227         typedef typename _Base::const_iterator _Base_const_iterator;
228         typedef __gnu_debug::_After_nth_from<_Base_const_iterator> _After_nth;
229
230         bool __invalidate_all = __sz > this->size();
231         if (__sz < this->size())
232           this->_M_invalidate_if(_After_nth(__sz, _M_base().begin()));
233
234         _Base::resize(__sz);
235
236         if (__invalidate_all)
237           this->_M_invalidate_all();
238       }
239
240       void
241       resize(size_type __sz, const _Tp& __c)
242       {
243         typedef typename _Base::const_iterator _Base_const_iterator;
244         typedef __gnu_debug::_After_nth_from<_Base_const_iterator> _After_nth;
245
246         bool __invalidate_all = __sz > this->size();
247         if (__sz < this->size())
248           this->_M_invalidate_if(_After_nth(__sz, _M_base().begin()));
249
250         _Base::resize(__sz, __c);
251
252         if (__invalidate_all)
253           this->_M_invalidate_all();
254       }
255 #else
256       void
257       resize(size_type __sz, _Tp __c = _Tp())
258       {
259         typedef typename _Base::const_iterator _Base_const_iterator;
260         typedef __gnu_debug::_After_nth_from<_Base_const_iterator> _After_nth;
261
262         bool __invalidate_all = __sz > this->size();
263         if (__sz < this->size())
264           this->_M_invalidate_if(_After_nth(__sz, _M_base().begin()));
265
266         _Base::resize(__sz, __c);
267
268         if (__invalidate_all)
269           this->_M_invalidate_all();
270       }
271 #endif
272
273 #ifdef __GXX_EXPERIMENTAL_CXX0X__
274       using _Base::shrink_to_fit;
275 #endif
276
277       using _Base::empty;
278
279       // element access:
280       reference
281       operator[](size_type __n)
282       {
283         __glibcxx_check_subscript(__n);
284         return _M_base()[__n];
285       }
286
287       const_reference
288       operator[](size_type __n) const
289       {
290         __glibcxx_check_subscript(__n);
291         return _M_base()[__n];
292       }
293
294       using _Base::at;
295
296       reference
297       front()
298       {
299         __glibcxx_check_nonempty();
300         return _Base::front();
301       }
302
303       const_reference
304       front() const
305       {
306         __glibcxx_check_nonempty();
307         return _Base::front();
308       }
309
310       reference
311       back()
312       {
313         __glibcxx_check_nonempty();
314         return _Base::back();
315       }
316
317       const_reference
318       back() const
319       {
320         __glibcxx_check_nonempty();
321         return _Base::back();
322       }
323
324       // 23.2.1.3 modifiers:
325       void
326       push_front(const _Tp& __x)
327       {
328         _Base::push_front(__x);
329         this->_M_invalidate_all();
330       }
331
332       void
333       push_back(const _Tp& __x)
334       {
335         _Base::push_back(__x);
336         this->_M_invalidate_all();
337       }
338
339 #ifdef __GXX_EXPERIMENTAL_CXX0X__
340       void
341       push_front(_Tp&& __x)
342       { emplace_front(std::move(__x)); }
343
344       void
345       push_back(_Tp&& __x)
346       { emplace_back(std::move(__x)); }
347
348       template<typename... _Args>
349         void
350         emplace_front(_Args&&... __args)
351         {
352           _Base::emplace_front(std::forward<_Args>(__args)...);
353           this->_M_invalidate_all();
354         }
355
356       template<typename... _Args>
357         void
358         emplace_back(_Args&&... __args)
359         {
360           _Base::emplace_back(std::forward<_Args>(__args)...);
361           this->_M_invalidate_all();
362         }
363
364       template<typename... _Args>
365         iterator
366         emplace(iterator __position, _Args&&... __args)
367         {
368           __glibcxx_check_insert(__position);
369           typename _Base::iterator __res = _Base::emplace(__position.base(),
370                                             std::forward<_Args>(__args)...);
371           this->_M_invalidate_all();
372           return iterator(__res, this);
373         }
374 #endif
375
376       iterator
377       insert(iterator __position, const _Tp& __x)
378       {
379         __glibcxx_check_insert(__position);
380         typename _Base::iterator __res = _Base::insert(__position.base(), __x);
381         this->_M_invalidate_all();
382         return iterator(__res, this);
383       }
384
385 #ifdef __GXX_EXPERIMENTAL_CXX0X__
386       iterator
387       insert(iterator __position, _Tp&& __x)
388       { return emplace(__position, std::move(__x)); }
389
390       void
391       insert(iterator __p, initializer_list<value_type> __l)
392       {
393         _Base::insert(__p, __l);
394         this->_M_invalidate_all();
395       }
396 #endif
397
398       void
399       insert(iterator __position, size_type __n, const _Tp& __x)
400       {
401         __glibcxx_check_insert(__position);
402         _Base::insert(__position.base(), __n, __x);
403         this->_M_invalidate_all();
404       }
405
406       template<class _InputIterator>
407         void
408         insert(iterator __position,
409                _InputIterator __first, _InputIterator __last)
410         {
411           __glibcxx_check_insert_range(__position, __first, __last);
412           _Base::insert(__position.base(), __first, __last);
413           this->_M_invalidate_all();
414         }
415
416       void
417       pop_front()
418       {
419         __glibcxx_check_nonempty();
420         iterator __victim = begin();
421         __victim._M_invalidate();
422         _Base::pop_front();
423       }
424
425       void
426       pop_back()
427       {
428         __glibcxx_check_nonempty();
429         iterator __victim = end();
430         --__victim;
431         __victim._M_invalidate();
432         _Base::pop_back();
433       }
434
435       iterator
436       erase(iterator __position)
437       {
438         __glibcxx_check_erase(__position);
439         if (__position == begin() || __position == end()-1)
440           {
441             __position._M_invalidate();
442             return iterator(_Base::erase(__position.base()), this);
443           }
444         else
445           {
446             typename _Base::iterator __res = _Base::erase(__position.base());
447             this->_M_invalidate_all();
448             return iterator(__res, this);
449           }
450       }
451
452       iterator
453       erase(iterator __first, iterator __last)
454       {
455         // _GLIBCXX_RESOLVE_LIB_DEFECTS
456         // 151. can't currently clear() empty container
457         __glibcxx_check_erase_range(__first, __last);
458         if (__first == begin() || __last == end())
459           {
460             this->_M_detach_singular();
461             for (iterator __position = __first; __position != __last; )
462               {
463                 iterator __victim = __position++;
464                 __victim._M_invalidate();
465               }
466             __try
467               {
468                 return iterator(_Base::erase(__first.base(), __last.base()),
469                                 this);
470               }
471             __catch(...)
472               {
473                 this->_M_revalidate_singular();
474                 __throw_exception_again;
475               }
476           }
477         else
478           {
479             typename _Base::iterator __res = _Base::erase(__first.base(),
480                                                           __last.base());
481             this->_M_invalidate_all();
482             return iterator(__res, this);
483           }
484       }
485
486       void
487       swap(deque& __x)
488       {
489         _Base::swap(__x);
490         this->_M_swap(__x);
491       }
492
493       void
494       clear()
495       {
496         _Base::clear();
497         this->_M_invalidate_all();
498       }
499
500       _Base&
501       _M_base()       { return *this; }
502
503       const _Base&
504       _M_base() const { return *this; }
505     };
506
507   template<typename _Tp, typename _Alloc>
508     inline bool
509     operator==(const deque<_Tp, _Alloc>& __lhs,
510                const deque<_Tp, _Alloc>& __rhs)
511     { return __lhs._M_base() == __rhs._M_base(); }
512
513   template<typename _Tp, typename _Alloc>
514     inline bool
515     operator!=(const deque<_Tp, _Alloc>& __lhs,
516                const deque<_Tp, _Alloc>& __rhs)
517     { return __lhs._M_base() != __rhs._M_base(); }
518
519   template<typename _Tp, typename _Alloc>
520     inline bool
521     operator<(const deque<_Tp, _Alloc>& __lhs,
522               const deque<_Tp, _Alloc>& __rhs)
523     { return __lhs._M_base() < __rhs._M_base(); }
524
525   template<typename _Tp, typename _Alloc>
526     inline bool
527     operator<=(const deque<_Tp, _Alloc>& __lhs,
528                const deque<_Tp, _Alloc>& __rhs)
529     { return __lhs._M_base() <= __rhs._M_base(); }
530
531   template<typename _Tp, typename _Alloc>
532     inline bool
533     operator>=(const deque<_Tp, _Alloc>& __lhs,
534                const deque<_Tp, _Alloc>& __rhs)
535     { return __lhs._M_base() >= __rhs._M_base(); }
536
537   template<typename _Tp, typename _Alloc>
538     inline bool
539     operator>(const deque<_Tp, _Alloc>& __lhs,
540               const deque<_Tp, _Alloc>& __rhs)
541     { return __lhs._M_base() > __rhs._M_base(); }
542
543   template<typename _Tp, typename _Alloc>
544     inline void
545     swap(deque<_Tp, _Alloc>& __lhs, deque<_Tp, _Alloc>& __rhs)
546     { __lhs.swap(__rhs); }
547
548 } // namespace __debug
549 } // namespace std
550
551 #endif