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