libstdc++
safe_iterator.tcc
Go to the documentation of this file.
00001 // Debugging iterator implementation (out of line) -*- C++ -*-
00002 
00003 // Copyright (C) 2003, 2004, 2005, 2006, 2007, 2009, 2010, 2011
00004 // Free Software Foundation, Inc.
00005 //
00006 // This file is part of the GNU ISO C++ Library.  This library is free
00007 // software; you can redistribute it and/or modify it under the
00008 // terms of the GNU General Public License as published by the
00009 // Free Software Foundation; either version 3, or (at your option)
00010 // any later version.
00011 
00012 // This library is distributed in the hope that it will be useful,
00013 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015 // GNU General Public License for more details.
00016 
00017 // Under Section 7 of GPL version 3, you are granted additional
00018 // permissions described in the GCC Runtime Library Exception, version
00019 // 3.1, as published by the Free Software Foundation.
00020 
00021 // You should have received a copy of the GNU General Public License and
00022 // a copy of the GCC Runtime Library Exception along with this program;
00023 // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
00024 // <http://www.gnu.org/licenses/>.
00025 
00026 /** @file debug/safe_iterator.tcc
00027  *  This file is a GNU debug extension to the Standard C++ Library.
00028  */
00029 
00030 #ifndef _GLIBCXX_DEBUG_SAFE_ITERATOR_TCC
00031 #define _GLIBCXX_DEBUG_SAFE_ITERATOR_TCC 1
00032 
00033 namespace __gnu_debug
00034 {
00035   template<typename _Iterator, typename _Sequence>
00036     bool
00037     _Safe_iterator<_Iterator, _Sequence>::
00038     _M_can_advance(const difference_type& __n) const
00039     {
00040       typedef typename _Sequence::const_iterator const_debug_iterator;
00041       typedef typename const_debug_iterator::iterator_type const_iterator;
00042 
00043       if (this->_M_singular())
00044     return false;
00045       if (__n == 0)
00046     return true;
00047       if (__n < 0)
00048     {
00049       const_iterator __begin = _M_get_sequence()->_M_base().begin();
00050       std::pair<difference_type, _Distance_precision> __dist =
00051         __get_distance(__begin, base());
00052       bool __ok =  ((__dist.second == __dp_exact && __dist.first >= -__n)
00053             || (__dist.second != __dp_exact && __dist.first > 0));
00054       return __ok;
00055     }
00056       else
00057     {
00058       const_iterator __end = _M_get_sequence()->_M_base().end();
00059       std::pair<difference_type, _Distance_precision> __dist =
00060         __get_distance(base(), __end);
00061       bool __ok = ((__dist.second == __dp_exact && __dist.first >= __n)
00062                || (__dist.second != __dp_exact && __dist.first > 0));
00063       return __ok;
00064     }
00065     }
00066 
00067   template<typename _Iterator, typename _Sequence>
00068     template<typename _Other>
00069       bool
00070       _Safe_iterator<_Iterator, _Sequence>::
00071       _M_valid_range(const _Safe_iterator<_Other, _Sequence>& __rhs) const
00072       {
00073     if (!_M_can_compare(__rhs))
00074       return false;
00075 
00076     /* Determine if we can order the iterators without the help of
00077        the container */
00078     std::pair<difference_type, _Distance_precision> __dist =
00079       __get_distance(base(), __rhs.base());
00080     switch (__dist.second) {
00081     case __dp_equality:
00082       if (__dist.first == 0)
00083         return true;
00084       break;
00085 
00086     case __dp_sign:
00087     case __dp_exact:
00088       return __dist.first >= 0;
00089     }
00090 
00091     /* We can only test for equality, but check if one of the
00092        iterators is at an extreme. */
00093     /* Optim for classic [begin, it) or [it, end) ranges, limit checks
00094      * when code is valid. */
00095     if (_M_is_begin() || __rhs._M_is_end())
00096       return true;
00097     if (_M_is_end() || __rhs._M_is_begin())
00098       return false;
00099 
00100     // Assume that this is a valid range; we can't check anything else
00101     return true;
00102       }
00103 } // namespace __gnu_debug
00104 
00105 #endif
00106