1 // Debugging deque implementation -*- C++ -*-
3 // Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008
4 // Free Software Foundation, Inc.
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)
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.
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,
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.
32 * This file is a GNU debug extension to the Standard C++ Library.
35 #ifndef _GLIBCXX_DEBUG_DEQUE
36 #define _GLIBCXX_DEBUG_DEQUE 1
39 #include <debug/safe_sequence.h>
40 #include <debug/safe_iterator.h>
46 template<typename _Tp, typename _Allocator = std::allocator<_Tp> >
48 : public _GLIBCXX_STD_D::deque<_Tp, _Allocator>,
49 public __gnu_debug::_Safe_sequence<deque<_Tp, _Allocator> >
51 typedef _GLIBCXX_STD_D::deque<_Tp, _Allocator> _Base;
52 typedef __gnu_debug::_Safe_sequence<deque> _Safe_base;
55 typedef typename _Base::reference reference;
56 typedef typename _Base::const_reference const_reference;
58 typedef __gnu_debug::_Safe_iterator<typename _Base::iterator,deque>
60 typedef __gnu_debug::_Safe_iterator<typename _Base::const_iterator,deque>
63 typedef typename _Base::size_type size_type;
64 typedef typename _Base::difference_type difference_type;
66 typedef _Tp value_type;
67 typedef _Allocator allocator_type;
68 typedef typename _Base::pointer pointer;
69 typedef typename _Base::const_pointer const_pointer;
70 typedef std::reverse_iterator<iterator> reverse_iterator;
71 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
73 // 23.2.1.1 construct/copy/destroy:
74 explicit deque(const _Allocator& __a = _Allocator())
77 explicit deque(size_type __n, const _Tp& __value = _Tp(),
78 const _Allocator& __a = _Allocator())
79 : _Base(__n, __value, __a) { }
81 template<class _InputIterator>
82 deque(_InputIterator __first, _InputIterator __last,
83 const _Allocator& __a = _Allocator())
84 : _Base(__gnu_debug::__check_valid_range(__first, __last), __last, __a)
87 deque(const deque& __x)
88 : _Base(__x), _Safe_base() { }
90 deque(const _Base& __x)
91 : _Base(__x), _Safe_base() { }
93 #ifdef __GXX_EXPERIMENTAL_CXX0X__
95 : _Base(std::forward<deque>(__x)), _Safe_base()
96 { this->_M_swap(__x); }
102 operator=(const deque& __x)
104 *static_cast<_Base*>(this) = __x;
105 this->_M_invalidate_all();
109 #ifdef __GXX_EXPERIMENTAL_CXX0X__
111 operator=(deque&& __x)
120 template<class _InputIterator>
122 assign(_InputIterator __first, _InputIterator __last)
124 __glibcxx_check_valid_range(__first, __last);
125 _Base::assign(__first, __last);
126 this->_M_invalidate_all();
130 assign(size_type __n, const _Tp& __t)
132 _Base::assign(__n, __t);
133 this->_M_invalidate_all();
136 using _Base::get_allocator;
141 { return iterator(_Base::begin(), this); }
145 { return const_iterator(_Base::begin(), this); }
149 { return iterator(_Base::end(), this); }
153 { return const_iterator(_Base::end(), this); }
157 { return reverse_iterator(end()); }
159 const_reverse_iterator
161 { return const_reverse_iterator(end()); }
165 { return reverse_iterator(begin()); }
167 const_reverse_iterator
169 { return const_reverse_iterator(begin()); }
171 #ifdef __GXX_EXPERIMENTAL_CXX0X__
174 { return const_iterator(_Base::begin(), this); }
178 { return const_iterator(_Base::end(), this); }
180 const_reverse_iterator
182 { return const_reverse_iterator(end()); }
184 const_reverse_iterator
186 { return const_reverse_iterator(begin()); }
189 // 23.2.1.2 capacity:
191 using _Base::max_size;
194 resize(size_type __sz, _Tp __c = _Tp())
196 typedef typename _Base::const_iterator _Base_const_iterator;
197 typedef __gnu_debug::_After_nth_from<_Base_const_iterator> _After_nth;
199 bool __invalidate_all = __sz > this->size();
200 if (__sz < this->size())
201 this->_M_invalidate_if(_After_nth(__sz, _M_base().begin()));
203 _Base::resize(__sz, __c);
205 if (__invalidate_all)
206 this->_M_invalidate_all();
213 operator[](size_type __n)
215 __glibcxx_check_subscript(__n);
216 return _M_base()[__n];
220 operator[](size_type __n) const
222 __glibcxx_check_subscript(__n);
223 return _M_base()[__n];
231 __glibcxx_check_nonempty();
232 return _Base::front();
238 __glibcxx_check_nonempty();
239 return _Base::front();
245 __glibcxx_check_nonempty();
246 return _Base::back();
252 __glibcxx_check_nonempty();
253 return _Base::back();
256 // 23.2.1.3 modifiers:
258 push_front(const _Tp& __x)
260 _Base::push_front(__x);
261 this->_M_invalidate_all();
265 push_back(const _Tp& __x)
267 _Base::push_back(__x);
268 this->_M_invalidate_all();
271 #ifdef __GXX_EXPERIMENTAL_CXX0X__
273 push_front(_Tp&& __x)
274 { emplace_front(std::move(__x)); }
278 { emplace_back(std::move(__x)); }
280 template<typename... _Args>
282 emplace_front(_Args&&... __args)
284 _Base::emplace_front(std::forward<_Args>(__args)...);
285 this->_M_invalidate_all();
288 template<typename... _Args>
290 emplace_back(_Args&&... __args)
292 _Base::emplace_back(std::forward<_Args>(__args)...);
293 this->_M_invalidate_all();
296 template<typename... _Args>
298 emplace(iterator __position, _Args&&... __args)
300 __glibcxx_check_insert(__position);
301 typename _Base::iterator __res = _Base::emplace(__position.base(),
302 std::forward<_Args>(__args)...);
303 this->_M_invalidate_all();
304 return iterator(__res, this);
309 insert(iterator __position, const _Tp& __x)
311 __glibcxx_check_insert(__position);
312 typename _Base::iterator __res = _Base::insert(__position.base(), __x);
313 this->_M_invalidate_all();
314 return iterator(__res, this);
317 #ifdef __GXX_EXPERIMENTAL_CXX0X__
319 insert(iterator __position, _Tp&& __x)
320 { return emplace(__position, std::move(__x)); }
324 insert(iterator __position, size_type __n, const _Tp& __x)
326 __glibcxx_check_insert(__position);
327 _Base::insert(__position.base(), __n, __x);
328 this->_M_invalidate_all();
331 template<class _InputIterator>
333 insert(iterator __position,
334 _InputIterator __first, _InputIterator __last)
336 __glibcxx_check_insert_range(__position, __first, __last);
337 _Base::insert(__position.base(), __first, __last);
338 this->_M_invalidate_all();
344 __glibcxx_check_nonempty();
345 iterator __victim = begin();
346 __victim._M_invalidate();
353 __glibcxx_check_nonempty();
354 iterator __victim = end();
356 __victim._M_invalidate();
361 erase(iterator __position)
363 __glibcxx_check_erase(__position);
364 if (__position == begin() || __position == end()-1)
366 __position._M_invalidate();
367 return iterator(_Base::erase(__position.base()), this);
371 typename _Base::iterator __res = _Base::erase(__position.base());
372 this->_M_invalidate_all();
373 return iterator(__res, this);
378 erase(iterator __first, iterator __last)
380 // _GLIBCXX_RESOLVE_LIB_DEFECTS
381 // 151. can't currently clear() empty container
382 __glibcxx_check_erase_range(__first, __last);
383 if (__first == begin() || __last == end())
385 this->_M_detach_singular();
386 for (iterator __position = __first; __position != __last; )
388 iterator __victim = __position++;
389 __victim._M_invalidate();
393 return iterator(_Base::erase(__first.base(), __last.base()),
398 this->_M_revalidate_singular();
399 __throw_exception_again;
404 typename _Base::iterator __res = _Base::erase(__first.base(),
406 this->_M_invalidate_all();
407 return iterator(__res, this);
412 #ifdef __GXX_EXPERIMENTAL_CXX0X__
426 this->_M_invalidate_all();
430 _M_base() { return *this; }
433 _M_base() const { return *this; }
436 template<typename _Tp, typename _Alloc>
438 operator==(const deque<_Tp, _Alloc>& __lhs,
439 const deque<_Tp, _Alloc>& __rhs)
440 { return __lhs._M_base() == __rhs._M_base(); }
442 template<typename _Tp, typename _Alloc>
444 operator!=(const deque<_Tp, _Alloc>& __lhs,
445 const deque<_Tp, _Alloc>& __rhs)
446 { return __lhs._M_base() != __rhs._M_base(); }
448 template<typename _Tp, typename _Alloc>
450 operator<(const deque<_Tp, _Alloc>& __lhs,
451 const deque<_Tp, _Alloc>& __rhs)
452 { return __lhs._M_base() < __rhs._M_base(); }
454 template<typename _Tp, typename _Alloc>
456 operator<=(const deque<_Tp, _Alloc>& __lhs,
457 const deque<_Tp, _Alloc>& __rhs)
458 { return __lhs._M_base() <= __rhs._M_base(); }
460 template<typename _Tp, typename _Alloc>
462 operator>=(const deque<_Tp, _Alloc>& __lhs,
463 const deque<_Tp, _Alloc>& __rhs)
464 { return __lhs._M_base() >= __rhs._M_base(); }
466 template<typename _Tp, typename _Alloc>
468 operator>(const deque<_Tp, _Alloc>& __lhs,
469 const deque<_Tp, _Alloc>& __rhs)
470 { return __lhs._M_base() > __rhs._M_base(); }
472 template<typename _Tp, typename _Alloc>
474 swap(deque<_Tp, _Alloc>& __lhs, deque<_Tp, _Alloc>& __rhs)
475 { __lhs.swap(__rhs); }
477 #ifdef __GXX_EXPERIMENTAL_CXX0X__
478 template<typename _Tp, typename _Alloc>
480 swap(deque<_Tp, _Alloc>&& __lhs, deque<_Tp, _Alloc>& __rhs)
481 { __lhs.swap(__rhs); }
483 template<typename _Tp, typename _Alloc>
485 swap(deque<_Tp, _Alloc>& __lhs, deque<_Tp, _Alloc>&& __rhs)
486 { __lhs.swap(__rhs); }
489 } // namespace __debug