1132720Skan// Debugging deque implementation -*- C++ -*- 2132720Skan 3169691Skan// Copyright (C) 2003, 2004, 2005 4132720Skan// Free Software Foundation, Inc. 5132720Skan// 6132720Skan// This file is part of the GNU ISO C++ Library. This library is free 7132720Skan// software; you can redistribute it and/or modify it under the 8132720Skan// terms of the GNU General Public License as published by the 9132720Skan// Free Software Foundation; either version 2, or (at your option) 10132720Skan// any later version. 11132720Skan 12132720Skan// This library is distributed in the hope that it will be useful, 13132720Skan// but WITHOUT ANY WARRANTY; without even the implied warranty of 14132720Skan// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15132720Skan// GNU General Public License for more details. 16132720Skan 17132720Skan// You should have received a copy of the GNU General Public License along 18132720Skan// with this library; see the file COPYING. If not, write to the Free 19169691Skan// Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 20132720Skan// USA. 21132720Skan 22132720Skan// As a special exception, you may use this file as part of a free software 23132720Skan// library without restriction. Specifically, if other files instantiate 24132720Skan// templates or use macros or inline functions from this file, or you compile 25132720Skan// this file and link it with other files to produce an executable, this 26132720Skan// file does not by itself cause the resulting executable to be covered by 27132720Skan// the GNU General Public License. This exception does not however 28132720Skan// invalidate any other reasons why the executable file might be covered by 29132720Skan// the GNU General Public License. 30132720Skan 31169691Skan/** @file debug/deque 32169691Skan * This file is a GNU debug extension to the Standard C++ Library. 33169691Skan */ 34169691Skan 35132720Skan#ifndef _GLIBCXX_DEBUG_DEQUE 36132720Skan#define _GLIBCXX_DEBUG_DEQUE 1 37132720Skan 38132720Skan#include <deque> 39132720Skan#include <debug/safe_sequence.h> 40132720Skan#include <debug/safe_iterator.h> 41132720Skan 42169691Skannamespace std 43132720Skan{ 44169691Skannamespace __debug 45169691Skan{ 46132720Skan template<typename _Tp, typename _Allocator = std::allocator<_Tp> > 47132720Skan class deque 48132720Skan : public _GLIBCXX_STD::deque<_Tp, _Allocator>, 49132720Skan public __gnu_debug::_Safe_sequence<deque<_Tp, _Allocator> > 50132720Skan { 51132720Skan typedef _GLIBCXX_STD::deque<_Tp, _Allocator> _Base; 52132720Skan typedef __gnu_debug::_Safe_sequence<deque> _Safe_base; 53132720Skan 54132720Skan public: 55169691Skan typedef typename _Base::reference reference; 56169691Skan typedef typename _Base::const_reference const_reference; 57132720Skan 58132720Skan typedef __gnu_debug::_Safe_iterator<typename _Base::iterator,deque> 59132720Skan iterator; 60132720Skan typedef __gnu_debug::_Safe_iterator<typename _Base::const_iterator,deque> 61132720Skan const_iterator; 62132720Skan 63132720Skan typedef typename _Base::size_type size_type; 64132720Skan typedef typename _Base::difference_type difference_type; 65132720Skan 66132720Skan typedef _Tp value_type; 67132720Skan typedef _Allocator allocator_type; 68169691Skan typedef typename _Base::pointer pointer; 69169691Skan typedef typename _Base::const_pointer const_pointer; 70132720Skan typedef std::reverse_iterator<iterator> reverse_iterator; 71132720Skan typedef std::reverse_iterator<const_iterator> const_reverse_iterator; 72132720Skan 73132720Skan // 23.2.1.1 construct/copy/destroy: 74132720Skan explicit deque(const _Allocator& __a = _Allocator()) 75132720Skan : _Base(__a) { } 76132720Skan 77132720Skan explicit deque(size_type __n, const _Tp& __value = _Tp(), 78132720Skan const _Allocator& __a = _Allocator()) 79132720Skan : _Base(__n, __value, __a) { } 80132720Skan 81132720Skan template<class _InputIterator> 82132720Skan deque(_InputIterator __first, _InputIterator __last, 83132720Skan const _Allocator& __a = _Allocator()) 84132720Skan : _Base(__gnu_debug::__check_valid_range(__first, __last), __last, __a) 85132720Skan { } 86132720Skan 87132720Skan deque(const deque<_Tp,_Allocator>& __x) : _Base(__x), _Safe_base() { } 88132720Skan 89132720Skan deque(const _Base& __x) : _Base(__x), _Safe_base() { } 90132720Skan 91132720Skan ~deque() { } 92132720Skan 93132720Skan deque<_Tp,_Allocator>& 94132720Skan operator=(const deque<_Tp,_Allocator>& __x) 95132720Skan { 96132720Skan *static_cast<_Base*>(this) = __x; 97132720Skan this->_M_invalidate_all(); 98132720Skan return *this; 99132720Skan } 100132720Skan 101132720Skan template<class _InputIterator> 102132720Skan void 103132720Skan assign(_InputIterator __first, _InputIterator __last) 104132720Skan { 105132720Skan __glibcxx_check_valid_range(__first, __last); 106132720Skan _Base::assign(__first, __last); 107132720Skan this->_M_invalidate_all(); 108132720Skan } 109132720Skan 110132720Skan void 111132720Skan assign(size_type __n, const _Tp& __t) 112132720Skan { 113132720Skan _Base::assign(__n, __t); 114132720Skan this->_M_invalidate_all(); 115132720Skan } 116132720Skan 117132720Skan using _Base::get_allocator; 118132720Skan 119132720Skan // iterators: 120132720Skan iterator 121132720Skan begin() 122132720Skan { return iterator(_Base::begin(), this); } 123132720Skan 124132720Skan const_iterator 125132720Skan begin() const 126132720Skan { return const_iterator(_Base::begin(), this); } 127132720Skan 128132720Skan iterator 129132720Skan end() 130132720Skan { return iterator(_Base::end(), this); } 131132720Skan 132132720Skan const_iterator 133132720Skan end() const 134132720Skan { return const_iterator(_Base::end(), this); } 135132720Skan 136132720Skan reverse_iterator 137132720Skan rbegin() 138132720Skan { return reverse_iterator(end()); } 139132720Skan 140132720Skan const_reverse_iterator 141132720Skan rbegin() const 142132720Skan { return const_reverse_iterator(end()); } 143132720Skan 144132720Skan reverse_iterator 145132720Skan rend() 146132720Skan { return reverse_iterator(begin()); } 147132720Skan 148132720Skan const_reverse_iterator 149132720Skan rend() const 150132720Skan { return const_reverse_iterator(begin()); } 151132720Skan 152132720Skan // 23.2.1.2 capacity: 153132720Skan using _Base::size; 154132720Skan using _Base::max_size; 155132720Skan 156132720Skan void 157132720Skan resize(size_type __sz, _Tp __c = _Tp()) 158132720Skan { 159132720Skan typedef typename _Base::const_iterator _Base_const_iterator; 160132720Skan typedef __gnu_debug::_After_nth_from<_Base_const_iterator> _After_nth; 161132720Skan 162132720Skan bool __invalidate_all = __sz > this->size(); 163132720Skan if (__sz < this->size()) 164132720Skan this->_M_invalidate_if(_After_nth(__sz, _M_base().begin())); 165132720Skan 166132720Skan _Base::resize(__sz, __c); 167132720Skan 168132720Skan if (__invalidate_all) 169132720Skan this->_M_invalidate_all(); 170132720Skan } 171132720Skan 172132720Skan using _Base::empty; 173132720Skan 174132720Skan // element access: 175132720Skan reference 176132720Skan operator[](size_type __n) 177132720Skan { 178132720Skan __glibcxx_check_subscript(__n); 179132720Skan return _M_base()[__n]; 180132720Skan } 181132720Skan 182132720Skan const_reference 183132720Skan operator[](size_type __n) const 184132720Skan { 185132720Skan __glibcxx_check_subscript(__n); 186132720Skan return _M_base()[__n]; 187132720Skan } 188132720Skan 189132720Skan using _Base::at; 190132720Skan 191132720Skan reference 192132720Skan front() 193132720Skan { 194132720Skan __glibcxx_check_nonempty(); 195132720Skan return _Base::front(); 196132720Skan } 197132720Skan 198132720Skan const_reference 199132720Skan front() const 200132720Skan { 201132720Skan __glibcxx_check_nonempty(); 202132720Skan return _Base::front(); 203132720Skan } 204132720Skan 205132720Skan reference 206132720Skan back() 207132720Skan { 208132720Skan __glibcxx_check_nonempty(); 209132720Skan return _Base::back(); 210132720Skan } 211132720Skan 212132720Skan const_reference 213132720Skan back() const 214132720Skan { 215132720Skan __glibcxx_check_nonempty(); 216132720Skan return _Base::back(); 217132720Skan } 218132720Skan 219132720Skan // 23.2.1.3 modifiers: 220132720Skan void 221132720Skan push_front(const _Tp& __x) 222132720Skan { 223132720Skan _Base::push_front(__x); 224132720Skan this->_M_invalidate_all(); 225132720Skan } 226132720Skan 227132720Skan void 228132720Skan push_back(const _Tp& __x) 229132720Skan { 230132720Skan _Base::push_back(__x); 231132720Skan this->_M_invalidate_all(); 232132720Skan } 233132720Skan 234132720Skan iterator 235132720Skan insert(iterator __position, const _Tp& __x) 236132720Skan { 237132720Skan __glibcxx_check_insert(__position); 238132720Skan typename _Base::iterator __res = _Base::insert(__position.base(), __x); 239132720Skan this->_M_invalidate_all(); 240132720Skan return iterator(__res, this); 241132720Skan } 242132720Skan 243132720Skan void 244132720Skan insert(iterator __position, size_type __n, const _Tp& __x) 245132720Skan { 246132720Skan __glibcxx_check_insert(__position); 247132720Skan _Base::insert(__position.base(), __n, __x); 248132720Skan this->_M_invalidate_all(); 249132720Skan } 250132720Skan 251132720Skan template<class _InputIterator> 252132720Skan void 253132720Skan insert(iterator __position, 254132720Skan _InputIterator __first, _InputIterator __last) 255132720Skan { 256132720Skan __glibcxx_check_insert_range(__position, __first, __last); 257132720Skan _Base::insert(__position.base(), __first, __last); 258132720Skan this->_M_invalidate_all(); 259132720Skan } 260132720Skan 261132720Skan void 262132720Skan pop_front() 263132720Skan { 264132720Skan __glibcxx_check_nonempty(); 265132720Skan iterator __victim = begin(); 266132720Skan __victim._M_invalidate(); 267132720Skan _Base::pop_front(); 268132720Skan } 269132720Skan 270132720Skan void 271132720Skan pop_back() 272132720Skan { 273132720Skan __glibcxx_check_nonempty(); 274132720Skan iterator __victim = end(); 275132720Skan --__victim; 276132720Skan __victim._M_invalidate(); 277132720Skan _Base::pop_back(); 278132720Skan } 279132720Skan 280132720Skan iterator 281132720Skan erase(iterator __position) 282132720Skan { 283132720Skan __glibcxx_check_erase(__position); 284132720Skan if (__position == begin() || __position == end()-1) 285132720Skan { 286132720Skan __position._M_invalidate(); 287132720Skan return iterator(_Base::erase(__position.base()), this); 288132720Skan } 289132720Skan else 290132720Skan { 291132720Skan typename _Base::iterator __res = _Base::erase(__position.base()); 292132720Skan this->_M_invalidate_all(); 293132720Skan return iterator(__res, this); 294132720Skan } 295132720Skan } 296132720Skan 297132720Skan iterator 298132720Skan erase(iterator __first, iterator __last) 299132720Skan { 300132720Skan // _GLIBCXX_RESOLVE_LIB_DEFECTS 301132720Skan // 151. can't currently clear() empty container 302132720Skan __glibcxx_check_erase_range(__first, __last); 303132720Skan if (__first == begin() || __last == end()) 304132720Skan { 305132720Skan this->_M_detach_singular(); 306132720Skan for (iterator __position = __first; __position != __last; ) 307132720Skan { 308132720Skan iterator __victim = __position++; 309132720Skan __victim._M_invalidate(); 310132720Skan } 311132720Skan try 312132720Skan { 313132720Skan return iterator(_Base::erase(__first.base(), __last.base()), 314132720Skan this); 315132720Skan } 316132720Skan catch (...) 317132720Skan { 318132720Skan this->_M_revalidate_singular(); 319132720Skan __throw_exception_again; 320132720Skan } 321132720Skan } 322132720Skan else 323132720Skan { 324132720Skan typename _Base::iterator __res = _Base::erase(__first.base(), 325132720Skan __last.base()); 326132720Skan this->_M_invalidate_all(); 327132720Skan return iterator(__res, this); 328132720Skan } 329132720Skan } 330132720Skan 331132720Skan void 332132720Skan swap(deque<_Tp,_Allocator>& __x) 333132720Skan { 334132720Skan _Base::swap(__x); 335132720Skan this->_M_swap(__x); 336132720Skan } 337132720Skan 338132720Skan void 339132720Skan clear() 340132720Skan { 341132720Skan _Base::clear(); 342132720Skan this->_M_invalidate_all(); 343132720Skan } 344132720Skan 345132720Skan _Base& 346132720Skan _M_base() { return *this; } 347132720Skan 348132720Skan const _Base& 349132720Skan _M_base() const { return *this; } 350132720Skan }; 351132720Skan 352132720Skan template<typename _Tp, typename _Alloc> 353132720Skan inline bool 354132720Skan operator==(const deque<_Tp, _Alloc>& __lhs, 355132720Skan const deque<_Tp, _Alloc>& __rhs) 356132720Skan { return __lhs._M_base() == __rhs._M_base(); } 357132720Skan 358132720Skan template<typename _Tp, typename _Alloc> 359132720Skan inline bool 360132720Skan operator!=(const deque<_Tp, _Alloc>& __lhs, 361132720Skan const deque<_Tp, _Alloc>& __rhs) 362132720Skan { return __lhs._M_base() != __rhs._M_base(); } 363132720Skan 364132720Skan template<typename _Tp, typename _Alloc> 365132720Skan inline bool 366132720Skan operator<(const deque<_Tp, _Alloc>& __lhs, const deque<_Tp, _Alloc>& __rhs) 367132720Skan { return __lhs._M_base() < __rhs._M_base(); } 368132720Skan 369132720Skan template<typename _Tp, typename _Alloc> 370132720Skan inline bool 371132720Skan operator<=(const deque<_Tp, _Alloc>& __lhs, 372132720Skan const deque<_Tp, _Alloc>& __rhs) 373132720Skan { return __lhs._M_base() <= __rhs._M_base(); } 374132720Skan 375132720Skan template<typename _Tp, typename _Alloc> 376132720Skan inline bool 377132720Skan operator>=(const deque<_Tp, _Alloc>& __lhs, 378132720Skan const deque<_Tp, _Alloc>& __rhs) 379132720Skan { return __lhs._M_base() >= __rhs._M_base(); } 380132720Skan 381132720Skan template<typename _Tp, typename _Alloc> 382132720Skan inline bool 383132720Skan operator>(const deque<_Tp, _Alloc>& __lhs, const deque<_Tp, _Alloc>& __rhs) 384132720Skan { return __lhs._M_base() > __rhs._M_base(); } 385132720Skan 386132720Skan template<typename _Tp, typename _Alloc> 387132720Skan inline void 388132720Skan swap(deque<_Tp, _Alloc>& __lhs, deque<_Tp, _Alloc>& __rhs) 389132720Skan { __lhs.swap(__rhs); } 390169691Skan} // namespace __debug 391169691Skan} // namespace std 392132720Skan 393132720Skan#endif 394