libstdc++
safe_iterator.tcc
Go to the documentation of this file.
00001 // Debugging iterator implementation (out of line) -*- C++ -*-
00002 
00003 // Copyright (C) 2003-2016 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       if (this->_M_singular())
00040         return false;
00041 
00042       if (__n == 0)
00043         return true;
00044 
00045       if (__n < 0)
00046         {
00047           std::pair<difference_type, _Distance_precision> __dist =
00048             __get_distance_from_begin(*this);
00049           bool __ok =  ((__dist.second == __dp_exact && __dist.first >= -__n)
00050                         || (__dist.second != __dp_exact && __dist.first > 0));
00051           return __ok;
00052         }
00053       else
00054         {
00055           std::pair<difference_type, _Distance_precision> __dist =
00056             __get_distance_to_end(*this);
00057           bool __ok = ((__dist.second == __dp_exact && __dist.first >= __n)
00058                        || (__dist.second != __dp_exact && __dist.first > 0));
00059           return __ok;
00060         }
00061     }
00062 
00063   template<typename _Iterator, typename _Sequence>
00064     bool
00065     _Safe_iterator<_Iterator, _Sequence>::
00066     _M_valid_range(const _Safe_iterator& __rhs,
00067                    std::pair<difference_type, _Distance_precision>& __dist,
00068                    bool __check_dereferenceable) const
00069     {
00070       if (!_M_can_compare(__rhs))
00071         return false;
00072 
00073       /* Determine iterators order */
00074       __dist = __get_distance(*this, __rhs);
00075       switch (__dist.second)
00076         {
00077         case __dp_equality:
00078           if (__dist.first == 0)
00079             return true;
00080           break;
00081 
00082         case __dp_sign:
00083         case __dp_exact:
00084           // If range is not empty first iterator must be dereferenceable.
00085           if (__dist.first > 0)
00086             return !__check_dereferenceable || _M_dereferenceable();
00087           return __dist.first == 0;
00088         }
00089 
00090       // Assume that this is a valid range; we can't check anything else.
00091       return true;
00092     }
00093 } // namespace __gnu_debug
00094 
00095 #endif
00096