1 // Profiling iterator implementation -*- C++ -*-
3 // Copyright (C) 2009-2017 Free Software Foundation, Inc.
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)
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.
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.
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/>.
25 /** @file profile/iterator_tracker.h
26 * This file is a GNU profile extension to the Standard C++ Library.
29 #ifndef _GLIBCXX_PROFILE_ITERATOR_TRACKER
30 #define _GLIBCXX_PROFILE_ITERATOR_TRACKER 1
32 #include <ext/type_traits.h>
34 namespace std
_GLIBCXX_VISIBILITY(default)
38 template<typename _Iterator
, typename _Sequence
>
39 class __iterator_tracker
41 typedef __iterator_tracker _Self
;
43 // The underlying iterator
46 // The underlying data structure
47 const _Sequence
* _M_ds
;
48 typedef std::iterator_traits
<_Iterator
> _Traits
;
51 typedef _Iterator _Base_iterator
;
52 typedef typename
_Traits::iterator_category iterator_category
;
53 typedef typename
_Traits::value_type value_type
;
54 typedef typename
_Traits::difference_type difference_type
;
55 typedef typename
_Traits::reference reference
;
56 typedef typename
_Traits::pointer pointer
;
58 __iterator_tracker() _GLIBCXX_NOEXCEPT
59 : _M_current(), _M_ds(0) { }
61 __iterator_tracker(const _Iterator
& __i
, const _Sequence
* __seq
)
63 : _M_current(__i
), _M_ds(__seq
) { }
65 __iterator_tracker(const __iterator_tracker
& __x
) _GLIBCXX_NOEXCEPT
66 : _M_current(__x
._M_current
), _M_ds(__x
._M_ds
) { }
68 template<typename _MutableIterator
>
69 __iterator_tracker(const __iterator_tracker
<_MutableIterator
,
70 typename
__gnu_cxx::__enable_if
71 <(std::__are_same
<_MutableIterator
, typename
72 _Sequence::iterator::_Base_iterator
>::__value
),
73 _Sequence
>::__type
>& __x
) _GLIBCXX_NOEXCEPT
74 : _M_current(__x
.base()), _M_ds(__x
._M_get_sequence()) { }
77 base() const _GLIBCXX_NOEXCEPT
{ return _M_current
; }
80 * @brief Conversion to underlying non-debug iterator to allow
81 * better interaction with non-profile containers.
83 operator _Iterator() const _GLIBCXX_NOEXCEPT
{ return _M_current
; }
86 operator->() const _GLIBCXX_NOEXCEPT
{ return &*_M_current
; }
89 operator++() _GLIBCXX_NOEXCEPT
91 _M_ds
->_M_profile_iterate();
97 operator++(int) _GLIBCXX_NOEXCEPT
99 _M_ds
->_M_profile_iterate();
100 __iterator_tracker
__tmp(*this);
106 operator--() _GLIBCXX_NOEXCEPT
108 _M_ds
->_M_profile_iterate(1);
114 operator--(int) _GLIBCXX_NOEXCEPT
116 _M_ds
->_M_profile_iterate(1);
117 __iterator_tracker
__tmp(*this);
123 operator=(const __iterator_tracker
& __x
) _GLIBCXX_NOEXCEPT
125 _M_current
= __x
._M_current
;
131 operator*() const _GLIBCXX_NOEXCEPT
132 { return *_M_current
; }
134 // ------ Random access iterator requirements ------
136 operator[](const difference_type
& __n
) const _GLIBCXX_NOEXCEPT
137 { return _M_current
[__n
]; }
140 operator+=(const difference_type
& __n
) _GLIBCXX_NOEXCEPT
147 operator+(const difference_type
& __n
) const _GLIBCXX_NOEXCEPT
149 __iterator_tracker
__tmp(*this);
155 operator-=(const difference_type
& __n
) _GLIBCXX_NOEXCEPT
162 operator-(const difference_type
& __n
) const _GLIBCXX_NOEXCEPT
164 __iterator_tracker
__tmp(*this);
170 _M_get_sequence() const
171 { return static_cast<const _Sequence
*>(_M_ds
); }
174 template<typename _IteratorL
, typename _IteratorR
, typename _Sequence
>
176 operator==(const __iterator_tracker
<_IteratorL
, _Sequence
>& __lhs
,
177 const __iterator_tracker
<_IteratorR
, _Sequence
>& __rhs
)
179 { return __lhs
.base() == __rhs
.base(); }
181 template<typename _Iterator
, typename _Sequence
>
183 operator==(const __iterator_tracker
<_Iterator
, _Sequence
>& __lhs
,
184 const __iterator_tracker
<_Iterator
, _Sequence
>& __rhs
)
186 { return __lhs
.base() == __rhs
.base(); }
188 template<typename _IteratorL
, typename _IteratorR
, typename _Sequence
>
190 operator!=(const __iterator_tracker
<_IteratorL
, _Sequence
>& __lhs
,
191 const __iterator_tracker
<_IteratorR
, _Sequence
>& __rhs
)
193 { return __lhs
.base() != __rhs
.base(); }
195 template<typename _Iterator
, typename _Sequence
>
197 operator!=(const __iterator_tracker
<_Iterator
, _Sequence
>& __lhs
,
198 const __iterator_tracker
<_Iterator
, _Sequence
>& __rhs
)
200 { return __lhs
.base() != __rhs
.base(); }
202 template<typename _IteratorL
, typename _IteratorR
, typename _Sequence
>
204 operator<(const __iterator_tracker
<_IteratorL
, _Sequence
>& __lhs
,
205 const __iterator_tracker
<_IteratorR
, _Sequence
>& __rhs
)
207 { return __lhs
.base() < __rhs
.base(); }
209 template<typename _Iterator
, typename _Sequence
>
211 operator<(const __iterator_tracker
<_Iterator
, _Sequence
>& __lhs
,
212 const __iterator_tracker
<_Iterator
, _Sequence
>& __rhs
)
214 { return __lhs
.base() < __rhs
.base(); }
216 template<typename _IteratorL
, typename _IteratorR
, typename _Sequence
>
218 operator<=(const __iterator_tracker
<_IteratorL
, _Sequence
>& __lhs
,
219 const __iterator_tracker
<_IteratorR
, _Sequence
>& __rhs
)
221 { return __lhs
.base() <= __rhs
.base(); }
223 template<typename _Iterator
, typename _Sequence
>
225 operator<=(const __iterator_tracker
<_Iterator
, _Sequence
>& __lhs
,
226 const __iterator_tracker
<_Iterator
, _Sequence
>& __rhs
)
228 { return __lhs
.base() <= __rhs
.base(); }
230 template<typename _IteratorL
, typename _IteratorR
, typename _Sequence
>
232 operator>(const __iterator_tracker
<_IteratorL
, _Sequence
>& __lhs
,
233 const __iterator_tracker
<_IteratorR
, _Sequence
>& __rhs
)
235 { return __lhs
.base() > __rhs
.base(); }
237 template<typename _Iterator
, typename _Sequence
>
239 operator>(const __iterator_tracker
<_Iterator
, _Sequence
>& __lhs
,
240 const __iterator_tracker
<_Iterator
, _Sequence
>& __rhs
)
242 { return __lhs
.base() > __rhs
.base(); }
244 template<typename _IteratorL
, typename _IteratorR
, typename _Sequence
>
246 operator>=(const __iterator_tracker
<_IteratorL
, _Sequence
>& __lhs
,
247 const __iterator_tracker
<_IteratorR
, _Sequence
>& __rhs
)
249 { return __lhs
.base() >= __rhs
.base(); }
251 template<typename _Iterator
, typename _Sequence
>
253 operator>=(const __iterator_tracker
<_Iterator
, _Sequence
>& __lhs
,
254 const __iterator_tracker
<_Iterator
, _Sequence
>& __rhs
)
256 { return __lhs
.base() >= __rhs
.base(); }
258 // _GLIBCXX_RESOLVE_LIB_DEFECTS
259 // According to the resolution of DR179 not only the various comparison
260 // operators but also operator- must accept mixed iterator/const_iterator
262 template<typename _IteratorL
, typename _IteratorR
, typename _Sequence
>
263 inline typename __iterator_tracker
<_IteratorL
, _Sequence
>::difference_type
264 operator-(const __iterator_tracker
<_IteratorL
, _Sequence
>& __lhs
,
265 const __iterator_tracker
<_IteratorR
, _Sequence
>& __rhs
)
267 { return __lhs
.base() - __rhs
.base(); }
269 template<typename _Iterator
, typename _Sequence
>
270 inline typename __iterator_tracker
<_Iterator
, _Sequence
>::difference_type
271 operator-(const __iterator_tracker
<_Iterator
, _Sequence
>& __lhs
,
272 const __iterator_tracker
<_Iterator
, _Sequence
>& __rhs
)
274 { return __lhs
.base() - __rhs
.base(); }
276 template<typename _Iterator
, typename _Sequence
>
277 inline __iterator_tracker
<_Iterator
, _Sequence
>
278 operator+(typename __iterator_tracker
<_Iterator
,_Sequence
>::difference_type
280 const __iterator_tracker
<_Iterator
, _Sequence
>& __i
)
282 { return __i
+ __n
; }
284 } // namespace __profile