# 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
    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, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
    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 debug/safe_iterator.tcc
    00032  *  This file is a GNU debug extension to the Standard C++ Library.
    00033  */
    00034
    00035 #ifndef _GLIBCXX_DEBUG_SAFE_ITERATOR_TCC
    00036 #define _GLIBCXX_DEBUG_SAFE_ITERATOR_TCC 1
    00037
    00038 namespace __gnu_debug
    00039 {
    00040   template<typename _Iterator, typename _Sequence>
    00041     bool
    00042     _Safe_iterator<_Iterator, _Sequence>::
    00043     _M_can_advance(const difference_type& __n) const
    00044     {
    00045       typedef typename _Sequence::const_iterator const_iterator;
    00046
    00047       if (this->_M_singular())
    00048     return false;
    00049       if (__n == 0)
    00050     return true;
    00051       if (__n < 0)
    00052     {
    00053       const_iterator __begin =
    00054         static_cast<const _Sequence*>(_M_sequence)->begin();
    00055       std::pair<difference_type, _Distance_precision> __dist =
    00056         this->_M_get_distance(__begin, *this);
    00057       bool __ok =  (__dist.second == __dp_exact && __dist.first >= -__n
    00058             || __dist.second != __dp_exact && __dist.first > 0);
    00059       return __ok;
    00060     }
    00061       else
    00062     {
    00063       const_iterator __end =
    00064         static_cast<const _Sequence*>(_M_sequence)->end();
    00065       std::pair<difference_type, _Distance_precision> __dist =
    00066         this->_M_get_distance(*this, __end);
    00067       bool __ok = (__dist.second == __dp_exact && __dist.first >= __n
    00068                || __dist.second != __dp_exact && __dist.first > 0);
    00069       return __ok;
    00070     }
    00071     }
    00072
    00073   template<typename _Iterator, typename _Sequence>
    00074     template<typename _Other>
    00075       bool
    00076       _Safe_iterator<_Iterator, _Sequence>::
    00077       _M_valid_range(const _Safe_iterator<_Other, _Sequence>& __rhs) const
    00078       {
    00079     if (!_M_can_compare(__rhs))
    00080       return false;
    00081
    00082     /* Determine if we can order the iterators without the help of
    00083        the container */
    00084     std::pair<difference_type, _Distance_precision> __dist =
    00085       this->_M_get_distance(*this, __rhs);
    00086     switch (__dist.second) {
    00087     case __dp_equality:
    00088       if (__dist.first == 0)
    00089         return true;
    00090       break;
    00091
    00092     case __dp_sign:
    00093     case __dp_exact:
    00094       return __dist.first >= 0;
    00095     }
    00096
    00097     /* We can only test for equality, but check if one of the
    00098        iterators is at an extreme. */
    00099     if (_M_is_begin() || __rhs._M_is_end())
    00100       return true;
    00101     else if (_M_is_end() || __rhs._M_is_begin())
    00102       return false;
    00103
    00104     // Assume that this is a valid range; we can't check anything else
    00105     return true;
    00106       }
    00107
    00108   template<typename _Iterator, typename _Sequence>
    00109     void
    00110     _Safe_iterator<_Iterator, _Sequence>::
    00111     _M_invalidate()
    00112     {
    00113       __gnu_cxx::__scoped_lock sentry(this->_M_get_mutex());
    00114       _M_invalidate_single();
    00115     }
    00116
    00117   template<typename _Iterator, typename _Sequence>
    00118     void
    00119     _Safe_iterator<_Iterator, _Sequence>::
    00120     _M_invalidate_single()
    00121     {
    00122       typedef typename _Sequence::iterator iterator;
    00123       typedef typename _Sequence::const_iterator const_iterator;
    00124
    00125       if (!this->_M_singular())
    00126     {
    00127       for (_Safe_iterator_base* __iter = _M_sequence->_M_iterators;
    00128            __iter; __iter = __iter->_M_next)
    00129         {
    00130           iterator* __victim = static_cast<iterator*>(__iter);
    00131           if (this->base() == __victim->base())
    00132         __victim->_M_version = 0;
    00133         }
    00134
    00135       for (_Safe_iterator_base* __iter2 = _M_sequence->_M_const_iterators;
    00136            __iter2; __iter2 = __iter2->_M_next)
    00137         {
    00138           const_iterator* __victim = static_cast<const_iterator*>(__iter2);
    00139           if (__victim->base() == this->base())
    00140         __victim->_M_version = 0;
    00141         }
    00142       _M_version = 0;
    00143     }
    00144     }
    00145 } // namespace __gnu_debug
    00146
    00147 #endif
    00148