xref: /openbsd-src/gnu/gcc/libstdc++-v3/include/debug/safe_iterator.tcc (revision 404b540a9034ac75a6199ad1a32d1bbc7a0d4210)
1*404b540aSrobert // Debugging iterator implementation (out of line) -*- C++ -*-
2*404b540aSrobert 
3*404b540aSrobert // Copyright (C) 2003, 2004, 2005, 2006
4*404b540aSrobert // Free Software Foundation, Inc.
5*404b540aSrobert //
6*404b540aSrobert // This file is part of the GNU ISO C++ Library.  This library is free
7*404b540aSrobert // software; you can redistribute it and/or modify it under the
8*404b540aSrobert // terms of the GNU General Public License as published by the
9*404b540aSrobert // Free Software Foundation; either version 2, or (at your option)
10*404b540aSrobert // any later version.
11*404b540aSrobert 
12*404b540aSrobert // This library is distributed in the hope that it will be useful,
13*404b540aSrobert // but WITHOUT ANY WARRANTY; without even the implied warranty of
14*404b540aSrobert // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15*404b540aSrobert // GNU General Public License for more details.
16*404b540aSrobert 
17*404b540aSrobert // You should have received a copy of the GNU General Public License along
18*404b540aSrobert // with this library; see the file COPYING.  If not, write to the Free
19*404b540aSrobert // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
20*404b540aSrobert // USA.
21*404b540aSrobert 
22*404b540aSrobert // As a special exception, you may use this file as part of a free software
23*404b540aSrobert // library without restriction.  Specifically, if other files instantiate
24*404b540aSrobert // templates or use macros or inline functions from this file, or you compile
25*404b540aSrobert // this file and link it with other files to produce an executable, this
26*404b540aSrobert // file does not by itself cause the resulting executable to be covered by
27*404b540aSrobert // the GNU General Public License.  This exception does not however
28*404b540aSrobert // invalidate any other reasons why the executable file might be covered by
29*404b540aSrobert // the GNU General Public License.
30*404b540aSrobert 
31*404b540aSrobert /** @file debug/safe_iterator.tcc
32*404b540aSrobert  *  This file is a GNU debug extension to the Standard C++ Library.
33*404b540aSrobert  */
34*404b540aSrobert 
35*404b540aSrobert #ifndef _GLIBCXX_DEBUG_SAFE_ITERATOR_TCC
36*404b540aSrobert #define _GLIBCXX_DEBUG_SAFE_ITERATOR_TCC 1
37*404b540aSrobert 
38*404b540aSrobert namespace __gnu_debug
39*404b540aSrobert {
40*404b540aSrobert   template<typename _Iterator, typename _Sequence>
41*404b540aSrobert     bool
42*404b540aSrobert     _Safe_iterator<_Iterator, _Sequence>::
_M_can_advance(const difference_type & __n) const43*404b540aSrobert     _M_can_advance(const difference_type& __n) const
44*404b540aSrobert     {
45*404b540aSrobert       typedef typename _Sequence::const_iterator const_iterator;
46*404b540aSrobert 
47*404b540aSrobert       if (this->_M_singular())
48*404b540aSrobert 	return false;
49*404b540aSrobert       if (__n == 0)
50*404b540aSrobert 	return true;
51*404b540aSrobert       if (__n < 0)
52*404b540aSrobert 	{
53*404b540aSrobert 	  const_iterator __begin =
54*404b540aSrobert 	    static_cast<const _Sequence*>(_M_sequence)->begin();
55*404b540aSrobert 	  std::pair<difference_type, _Distance_precision> __dist =
56*404b540aSrobert 	    this->_M_get_distance(__begin, *this);
57*404b540aSrobert 	  bool __ok =  (__dist.second == __dp_exact && __dist.first >= -__n
58*404b540aSrobert 			|| __dist.second != __dp_exact && __dist.first > 0);
59*404b540aSrobert 	  return __ok;
60*404b540aSrobert 	}
61*404b540aSrobert       else
62*404b540aSrobert 	{
63*404b540aSrobert 	  const_iterator __end =
64*404b540aSrobert 	    static_cast<const _Sequence*>(_M_sequence)->end();
65*404b540aSrobert 	  std::pair<difference_type, _Distance_precision> __dist =
66*404b540aSrobert 	    this->_M_get_distance(*this, __end);
67*404b540aSrobert 	  bool __ok = (__dist.second == __dp_exact && __dist.first >= __n
68*404b540aSrobert 		       || __dist.second != __dp_exact && __dist.first > 0);
69*404b540aSrobert 	  return __ok;
70*404b540aSrobert 	}
71*404b540aSrobert     }
72*404b540aSrobert 
73*404b540aSrobert   template<typename _Iterator, typename _Sequence>
74*404b540aSrobert     template<typename _Other>
75*404b540aSrobert       bool
76*404b540aSrobert       _Safe_iterator<_Iterator, _Sequence>::
_M_valid_range(const _Safe_iterator<_Other,_Sequence> & __rhs) const77*404b540aSrobert       _M_valid_range(const _Safe_iterator<_Other, _Sequence>& __rhs) const
78*404b540aSrobert       {
79*404b540aSrobert 	if (!_M_can_compare(__rhs))
80*404b540aSrobert 	  return false;
81*404b540aSrobert 
82*404b540aSrobert 	/* Determine if we can order the iterators without the help of
83*404b540aSrobert 	   the container */
84*404b540aSrobert 	std::pair<difference_type, _Distance_precision> __dist =
85*404b540aSrobert 	  this->_M_get_distance(*this, __rhs);
86*404b540aSrobert 	switch (__dist.second) {
87*404b540aSrobert 	case __dp_equality:
88*404b540aSrobert 	  if (__dist.first == 0)
89*404b540aSrobert 	    return true;
90*404b540aSrobert 	  break;
91*404b540aSrobert 
92*404b540aSrobert 	case __dp_sign:
93*404b540aSrobert 	case __dp_exact:
94*404b540aSrobert 	  return __dist.first >= 0;
95*404b540aSrobert 	}
96*404b540aSrobert 
97*404b540aSrobert 	/* We can only test for equality, but check if one of the
98*404b540aSrobert 	   iterators is at an extreme. */
99*404b540aSrobert 	if (_M_is_begin() || __rhs._M_is_end())
100*404b540aSrobert 	  return true;
101*404b540aSrobert 	else if (_M_is_end() || __rhs._M_is_begin())
102*404b540aSrobert 	  return false;
103*404b540aSrobert 
104*404b540aSrobert 	// Assume that this is a valid range; we can't check anything else
105*404b540aSrobert 	return true;
106*404b540aSrobert       }
107*404b540aSrobert 
108*404b540aSrobert   template<typename _Iterator, typename _Sequence>
109*404b540aSrobert     void
110*404b540aSrobert     _Safe_iterator<_Iterator, _Sequence>::
_M_invalidate()111*404b540aSrobert     _M_invalidate()
112*404b540aSrobert     {
113*404b540aSrobert       __gnu_cxx::__scoped_lock sentry(this->_M_get_mutex());
114*404b540aSrobert       _M_invalidate_single();
115*404b540aSrobert     }
116*404b540aSrobert 
117*404b540aSrobert   template<typename _Iterator, typename _Sequence>
118*404b540aSrobert     void
119*404b540aSrobert     _Safe_iterator<_Iterator, _Sequence>::
_M_invalidate_single()120*404b540aSrobert     _M_invalidate_single()
121*404b540aSrobert     {
122*404b540aSrobert       typedef typename _Sequence::iterator iterator;
123*404b540aSrobert       typedef typename _Sequence::const_iterator const_iterator;
124*404b540aSrobert 
125*404b540aSrobert       if (!this->_M_singular())
126*404b540aSrobert 	{
127*404b540aSrobert 	  for (_Safe_iterator_base* __iter = _M_sequence->_M_iterators;
128*404b540aSrobert 	       __iter; __iter = __iter->_M_next)
129*404b540aSrobert 	    {
130*404b540aSrobert 	      iterator* __victim = static_cast<iterator*>(__iter);
131*404b540aSrobert 	      if (this->base() == __victim->base())
132*404b540aSrobert 		__victim->_M_version = 0;
133*404b540aSrobert 	    }
134*404b540aSrobert 
135*404b540aSrobert 	  for (_Safe_iterator_base* __iter2 = _M_sequence->_M_const_iterators;
136*404b540aSrobert 	       __iter2; __iter2 = __iter2->_M_next)
137*404b540aSrobert 	    {
138*404b540aSrobert 	      const_iterator* __victim = static_cast<const_iterator*>(__iter2);
139*404b540aSrobert 	      if (__victim->base() == this->base())
140*404b540aSrobert 		__victim->_M_version = 0;
141*404b540aSrobert 	    }
142*404b540aSrobert 	  _M_version = 0;
143*404b540aSrobert 	}
144*404b540aSrobert     }
145*404b540aSrobert } // namespace __gnu_debug
146*404b540aSrobert 
147*404b540aSrobert #endif
148*404b540aSrobert 
149