OSDN Git Service

2005-12-18 Benjamin Kosnik <bkoz@redhat.com>
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / include / debug / deque
1 // Debugging deque implementation -*- C++ -*-
2
3 // Copyright (C) 2003, 2004, 2005
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, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
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_DEQUE
32 #define _GLIBCXX_DEBUG_DEQUE 1
33
34 #include <deque>
35 #include <debug/safe_sequence.h>
36 #include <debug/safe_iterator.h>
37
38 namespace std
39 {
40 namespace __gnu_debug_def
41 {
42   template<typename _Tp, typename _Allocator = std::allocator<_Tp> >
43     class deque
44     : public _GLIBCXX_STD::deque<_Tp, _Allocator>,
45       public __gnu_debug::_Safe_sequence<deque<_Tp, _Allocator> >
46     {
47       typedef  _GLIBCXX_STD::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<_Tp,_Allocator>& __x) : _Base(__x), _Safe_base() { }
84
85       deque(const _Base& __x) : _Base(__x), _Safe_base() { }
86
87       ~deque() { }
88
89       deque<_Tp,_Allocator>&
90       operator=(const deque<_Tp,_Allocator>& __x)
91       {
92         *static_cast<_Base*>(this) = __x;
93         this->_M_invalidate_all();
94         return *this;
95       }
96
97       template<class _InputIterator>
98         void
99         assign(_InputIterator __first, _InputIterator __last)
100         {
101           __glibcxx_check_valid_range(__first, __last);
102           _Base::assign(__first, __last);
103           this->_M_invalidate_all();
104         }
105
106       void
107       assign(size_type __n, const _Tp& __t)
108       {
109         _Base::assign(__n, __t);
110         this->_M_invalidate_all();
111       }
112
113       using _Base::get_allocator;
114
115       // iterators:
116       iterator
117       begin()
118       { return iterator(_Base::begin(), this); }
119
120       const_iterator
121       begin() const
122       { return const_iterator(_Base::begin(), this); }
123
124       iterator
125       end()
126       { return iterator(_Base::end(), this); }
127
128       const_iterator
129       end() const
130       { return const_iterator(_Base::end(), this); }
131
132       reverse_iterator
133       rbegin()
134       { return reverse_iterator(end()); }
135
136       const_reverse_iterator
137       rbegin() const
138       { return const_reverse_iterator(end()); }
139
140       reverse_iterator
141       rend()
142       { return reverse_iterator(begin()); }
143
144       const_reverse_iterator
145       rend() const
146       { return const_reverse_iterator(begin()); }
147
148       // 23.2.1.2 capacity:
149       using _Base::size;
150       using _Base::max_size;
151
152       void
153       resize(size_type __sz, _Tp __c = _Tp())
154       {
155         typedef typename _Base::const_iterator _Base_const_iterator;
156         typedef __gnu_debug::_After_nth_from<_Base_const_iterator> _After_nth;
157
158         bool __invalidate_all = __sz > this->size();
159         if (__sz < this->size())
160           this->_M_invalidate_if(_After_nth(__sz, _M_base().begin()));
161
162         _Base::resize(__sz, __c);
163
164         if (__invalidate_all)
165           this->_M_invalidate_all();
166       }
167
168       using _Base::empty;
169
170       // element access:
171       reference
172       operator[](size_type __n)
173       {
174         __glibcxx_check_subscript(__n);
175         return _M_base()[__n];
176       }
177
178       const_reference
179       operator[](size_type __n) const
180       {
181         __glibcxx_check_subscript(__n);
182         return _M_base()[__n];
183       }
184
185       using _Base::at;
186
187       reference
188       front()
189       {
190         __glibcxx_check_nonempty();
191         return _Base::front();
192       }
193
194       const_reference
195       front() const
196       {
197         __glibcxx_check_nonempty();
198         return _Base::front();
199       }
200
201       reference
202       back()
203       {
204         __glibcxx_check_nonempty();
205         return _Base::back();
206       }
207
208       const_reference
209       back() const
210       {
211         __glibcxx_check_nonempty();
212         return _Base::back();
213       }
214
215       // 23.2.1.3 modifiers:
216       void
217       push_front(const _Tp& __x)
218       {
219         _Base::push_front(__x);
220         this->_M_invalidate_all();
221       }
222
223       void
224       push_back(const _Tp& __x)
225       {
226         _Base::push_back(__x);
227         this->_M_invalidate_all();
228       }
229
230       iterator
231       insert(iterator __position, const _Tp& __x)
232       {
233         __glibcxx_check_insert(__position);
234         typename _Base::iterator __res = _Base::insert(__position.base(), __x);
235         this->_M_invalidate_all();
236         return iterator(__res, this);
237       }
238
239       void
240       insert(iterator __position, size_type __n, const _Tp& __x)
241       {
242         __glibcxx_check_insert(__position);
243         _Base::insert(__position.base(), __n, __x);
244         this->_M_invalidate_all();
245       }
246
247       template<class _InputIterator>
248         void
249         insert(iterator __position,
250                _InputIterator __first, _InputIterator __last)
251         {
252           __glibcxx_check_insert_range(__position, __first, __last);
253           _Base::insert(__position.base(), __first, __last);
254           this->_M_invalidate_all();
255         }
256
257       void
258       pop_front()
259       {
260         __glibcxx_check_nonempty();
261         iterator __victim = begin();
262         __victim._M_invalidate();
263         _Base::pop_front();
264       }
265
266       void
267       pop_back()
268       {
269         __glibcxx_check_nonempty();
270         iterator __victim = end();
271         --__victim;
272         __victim._M_invalidate();
273         _Base::pop_back();
274       }
275
276       iterator
277       erase(iterator __position)
278       {
279         __glibcxx_check_erase(__position);
280         if (__position == begin() || __position == end()-1)
281           {
282             __position._M_invalidate();
283             return iterator(_Base::erase(__position.base()), this);
284           }
285         else
286           {
287             typename _Base::iterator __res = _Base::erase(__position.base());
288             this->_M_invalidate_all();
289             return iterator(__res, this);
290           }
291       }
292
293       iterator
294       erase(iterator __first, iterator __last)
295       {
296         // _GLIBCXX_RESOLVE_LIB_DEFECTS
297         // 151. can't currently clear() empty container
298         __glibcxx_check_erase_range(__first, __last);
299         if (__first == begin() || __last == end())
300           {
301             this->_M_detach_singular();
302             for (iterator __position = __first; __position != __last; )
303               {
304                 iterator __victim = __position++;
305                 __victim._M_invalidate();
306               }
307             try
308               {
309                 return iterator(_Base::erase(__first.base(), __last.base()),
310                                 this);
311               }
312             catch (...)
313               {
314                 this->_M_revalidate_singular();
315                 __throw_exception_again;
316               }
317           }
318         else
319           {
320             typename _Base::iterator __res = _Base::erase(__first.base(),
321                                                           __last.base());
322             this->_M_invalidate_all();
323             return iterator(__res, this);
324           }
325       }
326
327       void
328       swap(deque<_Tp,_Allocator>& __x)
329       {
330         _Base::swap(__x);
331         this->_M_swap(__x);
332       }
333
334       void
335       clear()
336       {
337         _Base::clear();
338         this->_M_invalidate_all();
339       }
340
341       _Base&
342       _M_base()       { return *this; }
343
344       const _Base&
345       _M_base() const { return *this; }
346     };
347
348   template<typename _Tp, typename _Alloc>
349     inline bool
350     operator==(const deque<_Tp, _Alloc>& __lhs,
351                const deque<_Tp, _Alloc>& __rhs)
352     { return __lhs._M_base() == __rhs._M_base(); }
353
354   template<typename _Tp, typename _Alloc>
355     inline bool
356     operator!=(const deque<_Tp, _Alloc>& __lhs,
357                const deque<_Tp, _Alloc>& __rhs)
358     { return __lhs._M_base() != __rhs._M_base(); }
359
360   template<typename _Tp, typename _Alloc>
361     inline bool
362     operator<(const deque<_Tp, _Alloc>& __lhs, const deque<_Tp, _Alloc>& __rhs)
363     { return __lhs._M_base() < __rhs._M_base(); }
364
365   template<typename _Tp, typename _Alloc>
366     inline bool
367     operator<=(const deque<_Tp, _Alloc>& __lhs,
368                const deque<_Tp, _Alloc>& __rhs)
369     { return __lhs._M_base() <= __rhs._M_base(); }
370
371   template<typename _Tp, typename _Alloc>
372     inline bool
373     operator>=(const deque<_Tp, _Alloc>& __lhs,
374                const deque<_Tp, _Alloc>& __rhs)
375     { return __lhs._M_base() >= __rhs._M_base(); }
376
377   template<typename _Tp, typename _Alloc>
378     inline bool
379     operator>(const deque<_Tp, _Alloc>& __lhs, const deque<_Tp, _Alloc>& __rhs)
380     { return __lhs._M_base() > __rhs._M_base(); }
381
382   template<typename _Tp, typename _Alloc>
383     inline void
384     swap(deque<_Tp, _Alloc>& __lhs, deque<_Tp, _Alloc>& __rhs)
385     { __lhs.swap(__rhs); }
386 } // namespace __gnu_debug_def
387 } // namespace std
388
389 #endif