# safe_iterator.tcc

Go to the documentation of this file.

    00001 // Debugging iterator implementation (out of line) -*- C++ -*-
    00002
    00003 // Copyright (C) 2003, 2004
    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 2, 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 // You should have received a copy of the GNU General Public License along
    00018 // with this library; see the file COPYING.  If not, write to the Free
    00019 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
    00020 // USA.
    00021
    00022 // As a special exception, you may use this file as part of a free software
    00023 // library without restriction.  Specifically, if other files instantiate
    00024 // templates or use macros or inline functions from this file, or you compile
    00025 // this file and link it with other files to produce an executable, this
    00026 // file does not by itself cause the resulting executable to be covered by
    00027 // the GNU General Public License.  This exception does not however
    00028 // invalidate any other reasons why the executable file might be covered by
    00029 // the GNU General Public License.
    00030
    00031 /** @file safe_iterator.tcc
    00032  *  This is an internal header file, included by other library headers.
    00033  *  You should not attempt to use it directly.
    00034  */
    00035
    00036 #ifndef _GLIBCXX_DEBUG_SAFE_ITERATOR_TCC
    00037 #define _GLIBCXX_DEBUG_SAFE_ITERATOR_TCC 1
    00038
    00039 namespace __gnu_debug
    00040 {
    00041   template<typename _Iterator, typename _Sequence>
    00042     bool
    00043     _Safe_iterator<_Iterator, _Sequence>::
    00044     _M_can_advance(const difference_type& __n) const
    00045     {
    00046       typedef typename _Sequence::const_iterator const_iterator;
    00047
    00048       if (this->_M_singular())
    00049     return false;
    00050       if (__n == 0)
    00051     return true;
    00052       if (__n < 0)
    00053     {
    00054       const_iterator __begin =
    00055         static_cast<const _Sequence*>(_M_sequence)->begin();
    00056       pair<difference_type, _Distance_precision> __dist =
    00057         this->_M_get_distance(__begin, *this);
    00058       bool __ok =  (__dist.second == __dp_exact && __dist.first >= -__n
    00059             || __dist.second != __dp_exact && __dist.first > 0);
    00060       return __ok;
    00061     }
    00062       else
    00063     {
    00064       const_iterator __end =
    00065         static_cast<const _Sequence*>(_M_sequence)->end();
    00066       pair<difference_type, _Distance_precision> __dist =
    00067         this->_M_get_distance(*this, __end);
    00068       bool __ok = (__dist.second == __dp_exact && __dist.first >= __n
    00069                || __dist.second != __dp_exact && __dist.first > 0);
    00070       return __ok;
    00071     }
    00072     }
    00073
    00074   template<typename _Iterator, typename _Sequence>
    00075     template<typename _Other>
    00076       bool
    00077       _Safe_iterator<_Iterator, _Sequence>::
    00078       _M_valid_range(const _Safe_iterator<_Other, _Sequence>& __rhs) const
    00079       {
    00080     if (!_M_can_compare(__rhs))
    00081       return false;
    00082
    00083     /* Determine if we can order the iterators without the help of
    00084        the container */
    00085     pair<difference_type, _Distance_precision> __dist =
    00086       this->_M_get_distance(*this, __rhs);
    00087     switch (__dist.second) {
    00088     case __dp_equality:
    00089       if (__dist.first == 0)
    00090         return true;
    00091       break;
    00092
    00093     case __dp_sign:
    00094     case __dp_exact:
    00095       return __dist.first >= 0;
    00096     }
    00097
    00098     /* We can only test for equality, but check if one of the
    00099        iterators is at an extreme. */
    00100     if (_M_is_begin() || __rhs._M_is_end())
    00101       return true;
    00102     else if (_M_is_end() || __rhs._M_is_begin())
    00103       return false;
    00104
    00105     // Assume that this is a valid range; we can't check anything else
    00106     return true;
    00107       }
    00108
    00109   template<typename _Iterator, typename _Sequence>
    00110     void
    00111     _Safe_iterator<_Iterator, _Sequence>::
    00112     _M_invalidate()
    00113     {
    00114       typedef typename _Sequence::iterator iterator;
    00115       typedef typename _Sequence::const_iterator const_iterator;
    00116
    00117       if (!this->_M_singular())
    00118     {
    00119       for (_Safe_iterator_base* iter = _M_sequence->_M_iterators; iter; )
    00120         {
    00121           iterator* __victim = static_cast<iterator*>(iter);
    00122           iter = iter->_M_next;
    00123           if (this->base() == __victim->base())
    00124         __victim->_M_version = 0;
    00125         }
    00126       for (_Safe_iterator_base* iter2 = _M_sequence->_M_const_iterators;
    00127            iter2; /* increment in loop */)
    00128         {
    00129           const_iterator* __victim = static_cast<const_iterator*>(iter2);
    00130           iter2 = iter2->_M_next;
    00131           if (this->base() == __victim->base())
    00132         __victim->_M_version = 0;
    00133         }
    00134       _M_version = 0;
    00135     }
    00136     }
    00137 } // namespace __gnu_debug
    00138
    00139 #endif
    00140