libstdc++
safe_iterator.tcc
Go to the documentation of this file.
1 // Debugging iterator implementation (out of line) -*- C++ -*-
2 
3 // Copyright (C) 2003-2019 Free Software Foundation, Inc.
4 //
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 3, or (at your option)
9 // any later version.
10 
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15 
16 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
19 
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
24 
25 /** @file debug/safe_iterator.tcc
26  * This file is a GNU debug extension to the Standard C++ Library.
27  */
28 
29 #ifndef _GLIBCXX_DEBUG_SAFE_ITERATOR_TCC
30 #define _GLIBCXX_DEBUG_SAFE_ITERATOR_TCC 1
31 
32 namespace __gnu_debug
33 {
34  template<typename _Iterator, typename _Sequence, typename _Category>
35  typename _Distance_traits<_Iterator>::__type
36  _Safe_iterator<_Iterator, _Sequence, _Category>::
37  _M_get_distance_from_begin() const
38  {
39  typedef _Sequence_traits<_Sequence> _SeqTraits;
40 
41  // No need to consider before_begin as this function is only used in
42  // _M_can_advance which won't be used for forward_list iterators.
43  if (_M_is_begin())
44  return std::make_pair(0, __dp_exact);
45 
46  if (_M_is_end())
47  return _SeqTraits::_S_size(*_M_get_sequence());
48 
49  typename _Distance_traits<_Iterator>::__type __res
50  = __get_distance(_M_get_sequence()->_M_base().begin(), base());
51 
52  if (__res.second == __dp_equality)
53  return std::make_pair(1, __dp_sign);
54 
55  return __res;
56  }
57 
58  template<typename _Iterator, typename _Sequence, typename _Category>
59  typename _Distance_traits<_Iterator>::__type
60  _Safe_iterator<_Iterator, _Sequence, _Category>::
61  _M_get_distance_to_end() const
62  {
63  typedef _Sequence_traits<_Sequence> _SeqTraits;
64 
65  // No need to consider before_begin as this function is only used in
66  // _M_can_advance which won't be used for forward_list iterators.
67  if (_M_is_begin())
68  return _SeqTraits::_S_size(*_M_get_sequence());
69 
70  if (_M_is_end())
71  return std::make_pair(0, __dp_exact);
72 
73  typename _Distance_traits<_Iterator>::__type __res
74  = __get_distance(base(), _M_get_sequence()->_M_base().end());
75 
76  if (__res.second == __dp_equality)
77  return std::make_pair(1, __dp_sign);
78 
79  return __res;
80  }
81 
82  template<typename _Iterator, typename _Sequence, typename _Category>
83  bool
84  _Safe_iterator<_Iterator, _Sequence, _Category>::
85  _M_can_advance(difference_type __n) const
86  {
87  if (this->_M_singular())
88  return false;
89 
90  if (__n == 0)
91  return true;
92 
93  if (__n < 0)
94  {
96  _M_get_distance_from_begin();
97  bool __ok = ((__dist.second == __dp_exact && __dist.first >= -__n)
98  || (__dist.second != __dp_exact && __dist.first > 0));
99  return __ok;
100  }
101  else
102  {
104  _M_get_distance_to_end();
105  bool __ok = ((__dist.second == __dp_exact && __dist.first >= __n)
106  || (__dist.second != __dp_exact && __dist.first > 0));
107  return __ok;
108  }
109  }
110 
111  template<typename _Iterator, typename _Sequence, typename _Category>
112  typename _Distance_traits<_Iterator>::__type
113  _Safe_iterator<_Iterator, _Sequence, _Category>::
114  _M_get_distance_to(const _Safe_iterator& __rhs) const
115  {
116  typedef typename _Distance_traits<_Iterator>::__type _Diff;
117  typedef _Sequence_traits<_Sequence> _SeqTraits;
118 
119  if (this->base() == __rhs.base())
120  return std::make_pair(0, __dp_exact);
121 
122  if (this->_M_is_before_begin())
123  {
124  if (__rhs._M_is_begin())
125  return std::make_pair(1, __dp_exact);
126 
127  return std::make_pair(1, __dp_sign);
128  }
129 
130  if (this->_M_is_begin())
131  {
132  if (__rhs._M_is_before_begin())
133  return std::make_pair(-1, __dp_exact);
134 
135  if (__rhs._M_is_end())
136  return _SeqTraits::_S_size(*this->_M_get_sequence());
137 
138  return std::make_pair(1, __dp_sign);
139  }
140 
141  if (this->_M_is_end())
142  {
143  if (__rhs._M_is_before_begin())
144  return std::make_pair(-1, __dp_exact);
145 
146  if (__rhs._M_is_begin())
147  {
148  _Diff __diff = _SeqTraits::_S_size(*this->_M_get_sequence());
149  return std::make_pair(-__diff.first, __diff.second);
150  }
151 
152  return std::make_pair(-1, __dp_sign);
153  }
154 
155  if (__rhs._M_is_before_begin() || __rhs._M_is_begin())
156  return std::make_pair(-1, __dp_sign);
157 
158  if (__rhs._M_is_end())
159  return std::make_pair(1, __dp_sign);
160 
161  return std::make_pair(1, __dp_equality);
162  }
163 
164  template<typename _Iterator, typename _Sequence, typename _Category>
165  bool
166  _Safe_iterator<_Iterator, _Sequence, _Category>::
167  _M_valid_range(const _Safe_iterator& __rhs,
169  bool __check_dereferenceable) const
170  {
171  if (!_M_can_compare(__rhs))
172  return false;
173 
174  /* Determine iterators order */
175  __dist = _M_get_distance_to(__rhs);
176  switch (__dist.second)
177  {
178  case __dp_equality:
179  if (__dist.first == 0)
180  return true;
181  break;
182 
183  case __dp_sign:
184  case __dp_exact:
185  // If range is not empty first iterator must be dereferenceable.
186  if (__dist.first > 0)
187  return !__check_dereferenceable || _M_dereferenceable();
188  return __dist.first == 0;
189  }
190 
191  // Assume that this is a valid range; we can't check anything else.
192  return true;
193  }
194 
195  template<typename _Iterator, typename _Sequence>
196  bool
197  _Safe_iterator<_Iterator, _Sequence, std::random_access_iterator_tag>::
198  _M_valid_range(const _Safe_iterator& __rhs,
199  std::pair<difference_type,
200  _Distance_precision>& __dist) const
201  {
202  if (!this->_M_can_compare(__rhs))
203  return false;
204 
205  /* Determine iterators order */
206  __dist = std::make_pair(__rhs.base() - this->base(), __dp_exact);
207 
208  // If range is not empty first iterator must be dereferenceable.
209  if (__dist.first > 0)
210  return this->_M_dereferenceable();
211  return __dist.first == 0;
212  }
213 } // namespace __gnu_debug
214 
215 #endif
constexpr const _Tp * end(initializer_list< _Tp > __ils) noexcept
Return an iterator pointing to one past the last element of the initializer_list. ...
_T2 second
first is a copy of the first object
Definition: stl_pair.h:215
_T1 first
second_type is the second bound type
Definition: stl_pair.h:214
constexpr const _Tp * begin(initializer_list< _Tp > __ils) noexcept
Return an iterator pointing to the first element of the initializer_list.
GNU debug classes for public use.
constexpr pair< typename __decay_and_strip< _T1 >::__type, typename __decay_and_strip< _T2 >::__type > make_pair(_T1 &&__x, _T2 &&__y)
A convenience wrapper for creating a pair from two objects.
Definition: stl_pair.h:524
Struct holding two objects of arbitrary type.
Definition: stl_pair.h:208
_Distance_traits< _Iterator >::__type __get_distance(_Iterator __lhs, _Iterator __rhs, std::random_access_iterator_tag)