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