1 _iterator.h |
1 /* |
|
2 * |
|
3 * Copyright (c) 1997 |
|
4 * Moscow Center for SPARC Technology |
|
5 * |
|
6 * Copyright (c) 1999 |
|
7 * Boris Fomitchev |
|
8 * |
|
9 * This material is provided "as is", with absolutely no warranty expressed |
|
10 * or implied. Any use is at your own risk. |
|
11 * |
|
12 * Permission to use or copy this software for any purpose is hereby granted |
|
13 * without fee, provided the above notices are retained on all copies. |
|
14 * Permission to modify the code and to distribute modified code is granted, |
|
15 * provided the above notices are retained, and a notice that the code was |
|
16 * modified is included with the above copyright notice. |
|
17 * |
|
18 */ |
|
19 |
|
20 #ifndef _STLP_DBG_ITERATOR_H |
|
21 # define _STLP_DBG_ITERATOR_H |
|
22 |
|
23 # include <stl/_pair.h> |
|
24 # include <stl/_alloc.h> |
|
25 |
|
26 # define _STLP_DBG_ALLOCATOR_SELECT( _Tp ) _STLP_DEFAULT_ALLOCATOR_SELECT( _Tp ) |
|
27 |
|
28 _STLP_BEGIN_NAMESPACE |
|
29 |
|
30 //============================================================ |
|
31 |
|
32 template <class _Iterator> |
|
33 void _Decrement(_Iterator& __it, const bidirectional_iterator_tag &) { |
|
34 --__it; |
|
35 } |
|
36 |
|
37 template <class _Iterator> |
|
38 void _Decrement(_Iterator& __it, const random_access_iterator_tag &) { |
|
39 --__it; |
|
40 } |
|
41 |
|
42 template <class _Iterator> |
|
43 void _Decrement(_Iterator& __it, const forward_iterator_tag &) { |
|
44 _STLP_ASSERT(0) |
|
45 } |
|
46 |
|
47 template <class _Iterator> |
|
48 void _Advance(_Iterator&, ptrdiff_t, const forward_iterator_tag &) { |
|
49 _STLP_ASSERT(0) |
|
50 } |
|
51 |
|
52 template <class _Iterator> |
|
53 void _Advance(_Iterator& __it, ptrdiff_t, const bidirectional_iterator_tag &) { |
|
54 _STLP_ASSERT(0) |
|
55 } |
|
56 |
|
57 template <class _Iterator> |
|
58 void _Advance(_Iterator& __it, ptrdiff_t __n, const random_access_iterator_tag &) { |
|
59 __it += __n; |
|
60 } |
|
61 |
|
62 template <class _Iterator> |
|
63 ptrdiff_t _DBG_distance(const _Iterator& __x, const _Iterator& __y, const random_access_iterator_tag &) { |
|
64 return __x - __y; |
|
65 } |
|
66 |
|
67 template <class _Iterator> |
|
68 ptrdiff_t _DBG_distance(const _Iterator&, const _Iterator&, const forward_iterator_tag &) { |
|
69 _STLP_ASSERT(0) |
|
70 return 0; |
|
71 } |
|
72 |
|
73 template <class _Iterator> |
|
74 ptrdiff_t _DBG_distance(const _Iterator&, const _Iterator&, const bidirectional_iterator_tag &) { |
|
75 _STLP_ASSERT(0) |
|
76 return 0; |
|
77 } |
|
78 |
|
79 template <class _Iterator> |
|
80 bool _CompareIt(const _Iterator&, const _Iterator&, const forward_iterator_tag &) { |
|
81 _STLP_ASSERT(0) |
|
82 return false; |
|
83 } |
|
84 |
|
85 template <class _Iterator> |
|
86 bool _CompareIt(const _Iterator&, const _Iterator&, const bidirectional_iterator_tag &) { |
|
87 _STLP_ASSERT(0) |
|
88 return false; |
|
89 } |
|
90 |
|
91 template <class _Iterator> |
|
92 bool _CompareIt(const _Iterator& __x, const _Iterator& __y, const random_access_iterator_tag &) { |
|
93 return __x < __y; |
|
94 } |
|
95 |
|
96 |
|
97 template <class _Iterator> |
|
98 bool _Dereferenceable(_Iterator __it) { |
|
99 return (__it._Get_container_ptr() !=0) && !(__it._M_iterator == (__it._Get_container_ptr())->end()); |
|
100 } |
|
101 |
|
102 |
|
103 template <class _Iterator> |
|
104 bool _Incrementable(const _Iterator& __it, ptrdiff_t __n, const forward_iterator_tag &) { |
|
105 return (__n == 1) && _Dereferenceable(__it); |
|
106 } |
|
107 |
|
108 template <class _Iterator> |
|
109 bool _Incrementable(const _Iterator& __it, ptrdiff_t __n, const bidirectional_iterator_tag &) { |
|
110 typedef typename _Iterator::_Container_type __container_type; |
|
111 __container_type* __c = __it._Get_container_ptr(); |
|
112 return (__c!=0) && ((__n == 1 && __it._M_iterator != __c->end() ) || |
|
113 (__n == -1 && __it._M_iterator != __c->begin())); |
|
114 } |
|
115 |
|
116 template <class _Iterator> |
|
117 bool _Incrementable(const _Iterator& __it, ptrdiff_t __n, const random_access_iterator_tag &) { |
|
118 typedef typename _Iterator::_Container_type __container_type; |
|
119 __container_type* __c = __it._Get_container_ptr(); |
|
120 if (!__c) return false; |
|
121 ptrdiff_t __new_pos = (__it._M_iterator - __c->begin()) + __n; |
|
122 return (__new_pos >=0) && (__STATIC_CAST(typename __container_type::size_type,__new_pos) <=__c->size()); |
|
123 } |
|
124 |
|
125 |
|
126 template <class _Container> |
|
127 struct _DBG_iter_base : public __owned_link { |
|
128 public: |
|
129 typedef typename _Container::value_type value_type; |
|
130 typedef typename _Container::reference reference; |
|
131 typedef typename _Container::pointer pointer; |
|
132 typedef ptrdiff_t difference_type; |
|
133 //private: |
|
134 typedef typename _Container::iterator _Nonconst_iterator; |
|
135 typedef typename _Container::const_iterator _Const_iterator; |
|
136 typedef _Container _Container_type; |
|
137 |
|
138 # ifdef _STLP_CLASS_PARTIAL_SPECIALIZATION |
|
139 typedef typename iterator_traits<_Const_iterator>::iterator_category _Iterator_category; |
|
140 # else |
|
141 typedef typename _Container::_Iterator_category _Iterator_category; |
|
142 # endif |
|
143 typedef _Iterator_category iterator_category; |
|
144 |
|
145 _DBG_iter_base() : __owned_link(0) {} |
|
146 _DBG_iter_base(const __owned_list* __c, const _Const_iterator& __it) : |
|
147 # ifdef __HP_aCC |
|
148 __owned_link(__c), _M_iterator(*__REINTERPRET_CAST(const _Nonconst_iterator *, &__it)) {} |
|
149 # else |
|
150 __owned_link(__c), _M_iterator(*(const _Nonconst_iterator*)&__it) {} |
|
151 # endif |
|
152 _Container* _Get_container_ptr() const { |
|
153 return (_Container*)__stl_debugger::_Get_container_ptr(this); |
|
154 } |
|
155 |
|
156 void __increment() { |
|
157 _STLP_DEBUG_CHECK(_Incrementable(*this,1,_Iterator_category())) |
|
158 ++_M_iterator; |
|
159 } |
|
160 |
|
161 void __decrement() { |
|
162 _STLP_DEBUG_CHECK(_Incrementable(*this,-1,_Iterator_category())) |
|
163 _Decrement(_M_iterator, _Iterator_category()); |
|
164 } |
|
165 |
|
166 void __advance(difference_type __n) { |
|
167 _STLP_DEBUG_CHECK(_Incrementable(*this,__n, _Iterator_category())) |
|
168 _Advance(_M_iterator,__n, _Iterator_category()); |
|
169 } |
|
170 |
|
171 // protected: |
|
172 _Nonconst_iterator _M_iterator; |
|
173 }; |
|
174 |
|
175 template <class _Container> |
|
176 ptrdiff_t operator-(const _DBG_iter_base<_Container>& __x, |
|
177 const _DBG_iter_base<_Container>& __y ) { |
|
178 typedef typename _DBG_iter_base<_Container>::_Iterator_category _Iterator_category; |
|
179 _STLP_DEBUG_CHECK(__check_same_owner(__x, __y)) |
|
180 return _DBG_distance(__x._M_iterator,__y._M_iterator, _Iterator_category()); |
|
181 } |
|
182 |
|
183 template <class _Container, class _Traits> |
|
184 struct _DBG_iter_mid : public _DBG_iter_base<_Container> |
|
185 { |
|
186 typedef _DBG_iter_mid<_Container, typename _Traits::_Non_const_traits> _Nonconst_self; |
|
187 typedef typename _Container::iterator _Nonconst_iterator; |
|
188 typedef typename _Container::const_iterator _Const_iterator; |
|
189 |
|
190 _DBG_iter_mid() {} |
|
191 |
|
192 explicit _DBG_iter_mid(const _Nonconst_self& __it) : |
|
193 _DBG_iter_base<_Container>(__it) {} |
|
194 |
|
195 _DBG_iter_mid(const __owned_list* __c, const _Const_iterator& __it) : |
|
196 _DBG_iter_base<_Container>(__c, __it) {} |
|
197 }; |
|
198 |
|
199 template <class _Container, class _Traits> |
|
200 struct _DBG_iter : public _DBG_iter_mid<_Container, _Traits> { |
|
201 typedef _DBG_iter_base<_Container> _Base; |
|
202 public: |
|
203 typedef typename _Base::value_type value_type; |
|
204 typedef typename _Base::difference_type difference_type; |
|
205 typedef typename _Traits::reference reference; |
|
206 typedef typename _Traits::pointer pointer; |
|
207 |
|
208 private: |
|
209 typedef typename _Base::_Nonconst_iterator _Nonconst_iterator; |
|
210 typedef typename _Base::_Const_iterator _Const_iterator; |
|
211 |
|
212 typedef _DBG_iter<_Container, _Traits> _Self; |
|
213 typedef _DBG_iter_mid<_Container, typename _Traits::_Non_const_traits> _Nonconst_mid; |
|
214 |
|
215 public: |
|
216 |
|
217 # ifdef _STLP_CLASS_PARTIAL_SPECIALIZATION |
|
218 typedef typename _Base::iterator_category iterator_category; |
|
219 # endif |
|
220 typedef typename _Base::_Iterator_category _Iterator_category; |
|
221 |
|
222 public: |
|
223 _DBG_iter() {} |
|
224 // boris : real type of iter would be nice |
|
225 _DBG_iter(const __owned_list* __c, const _Const_iterator& __it) : |
|
226 _DBG_iter_mid<_Container, _Traits>(__c, __it) {} |
|
227 |
|
228 // This allows conversions from iterator to const_iterator without being |
|
229 // redundant with the copy constructor below. |
|
230 _DBG_iter(const _Nonconst_mid& __rhs) : |
|
231 _DBG_iter_mid<_Container, _Traits>(__rhs) {} |
|
232 |
|
233 _DBG_iter(const _Self& __rhs) : |
|
234 _DBG_iter_mid<_Container, _Traits>(__rhs) {} |
|
235 |
|
236 // This allows conversions from iterator to const_iterator without being |
|
237 // redundant with the copy assignment operator below. |
|
238 _Self& operator=(const _Nonconst_mid& __rhs) |
|
239 { |
|
240 (_Base&)*this = __rhs; |
|
241 return *this; |
|
242 } |
|
243 |
|
244 _Self& operator=(const _Self& __rhs) |
|
245 { |
|
246 (_Base&)*this = __rhs; |
|
247 return *this; |
|
248 } |
|
249 |
|
250 reference operator*() const { |
|
251 _STLP_DEBUG_CHECK(_Dereferenceable(*this)) |
|
252 return *this->_M_iterator; |
|
253 } |
|
254 |
|
255 _STLP_DEFINE_ARROW_OPERATOR |
|
256 |
|
257 _Self& operator++() { |
|
258 this->__increment(); |
|
259 return *this; |
|
260 } |
|
261 _Self operator++(int) { |
|
262 _Self __tmp = *this; |
|
263 this->__increment(); |
|
264 return __tmp; |
|
265 } |
|
266 _Self& operator--() { |
|
267 this->__decrement(); |
|
268 return *this; |
|
269 } |
|
270 _Self operator--(int) { |
|
271 _Self __tmp = *this; |
|
272 this->__decrement(); |
|
273 return __tmp; |
|
274 } |
|
275 |
|
276 _Self& operator+=(difference_type __n) { |
|
277 this->__advance(__n); |
|
278 return *this; |
|
279 } |
|
280 |
|
281 _Self& operator-=(difference_type __n) { |
|
282 this->__advance(-__n); |
|
283 return *this; |
|
284 } |
|
285 _Self operator+(difference_type __n) const { |
|
286 _Self __tmp(*this); |
|
287 __tmp.__advance(__n); |
|
288 return __tmp; |
|
289 } |
|
290 _Self operator-(difference_type __n) const { |
|
291 _Self __tmp(*this); |
|
292 __tmp.__advance(-__n); |
|
293 return __tmp; |
|
294 } |
|
295 reference operator[](difference_type __n) const { return *(*this + __n); } |
|
296 }; |
|
297 |
|
298 template <class _Container> |
|
299 inline bool |
|
300 operator==(const _DBG_iter_base<_Container>& __x, const _DBG_iter_base<_Container>& __y) { |
|
301 _STLP_DEBUG_CHECK(__check_same_owner_or_null(__x, __y)) |
|
302 return __x._M_iterator==__y._M_iterator; |
|
303 } |
|
304 |
|
305 template <class _Container> |
|
306 inline bool |
|
307 operator<(const _DBG_iter_base<_Container>& __x, const _DBG_iter_base<_Container>& __y) { |
|
308 _STLP_DEBUG_CHECK(__check_same_owner_or_null(__x, __y)) |
|
309 typedef typename _DBG_iter_base<_Container>::_Iterator_category _Category; |
|
310 return _CompareIt(__x._M_iterator , __y._M_iterator, _Category()); |
|
311 } |
|
312 |
|
313 template <class _Container> |
|
314 inline bool |
|
315 operator>(const _DBG_iter_base<_Container>& __x, |
|
316 const _DBG_iter_base<_Container>& __y) { |
|
317 typedef typename _DBG_iter_base<_Container>::_Iterator_category _Category; |
|
318 return _CompareIt(__y._M_iterator , __x._M_iterator, _Category()); |
|
319 } |
|
320 |
|
321 template <class _Container> |
|
322 inline bool |
|
323 operator>=(const _DBG_iter_base<_Container>& __x, const _DBG_iter_base<_Container>& __y) { |
|
324 _STLP_DEBUG_CHECK(__check_same_owner_or_null(__x, __y)) |
|
325 typedef typename _DBG_iter_base<_Container>::_Iterator_category _Category; |
|
326 return !_CompareIt(__x._M_iterator , __y._M_iterator, _Category()); |
|
327 } |
|
328 |
|
329 template <class _Container> |
|
330 inline bool |
|
331 operator<=(const _DBG_iter_base<_Container>& __x, |
|
332 const _DBG_iter_base<_Container>& __y) { |
|
333 typedef typename _DBG_iter_base<_Container>::_Iterator_category _Category; |
|
334 return !_CompareIt(__y._M_iterator , __x._M_iterator, _Category()); |
|
335 } |
|
336 |
|
337 template <class _Container> |
|
338 inline bool |
|
339 operator!=(const _DBG_iter_base<_Container>& __x, |
|
340 const _DBG_iter_base<_Container>& __y) { |
|
341 _STLP_DEBUG_CHECK(__check_same_owner_or_null(__x, __y)) |
|
342 return __x._M_iterator != __y._M_iterator; |
|
343 } |
|
344 |
|
345 //------------------------------------------ |
|
346 |
|
347 template <class _Container, class _Traits> |
|
348 inline _DBG_iter<_Container, _Traits> |
|
349 operator+(ptrdiff_t __n, const _DBG_iter<_Container, _Traits>& __it) { |
|
350 _DBG_iter<_Container, _Traits> __tmp(__it); |
|
351 return __tmp += __n; |
|
352 } |
|
353 |
|
354 |
|
355 # ifdef _STLP_USE_OLD_HP_ITERATOR_QUERIES |
|
356 # if defined (_STLP_NESTED_TYPE_PARAM_BUG) \ |
|
357 || ( defined (__SUNPRO_CC) && __SUNPRO_CC < 0x600) \ |
|
358 || ( defined (_STLP_MSVC) && (_STLP_MSVC < 1100) ) |
|
359 # define _STLP_DEBUG_USE_DISTINCT_VALUE_TYPE_HELPERS 1 |
|
360 # endif |
|
361 |
|
362 template <class _Container> |
|
363 inline ptrdiff_t* |
|
364 distance_type(const _DBG_iter_base<_Container>&) { return (ptrdiff_t*) 0; } |
|
365 |
|
366 # if !defined (_STLP_DEBUG_USE_DISTINCT_VALUE_TYPE_HELPERS) |
|
367 template <class _Container> |
|
368 inline _STLP_TYPENAME_ON_RETURN_TYPE _DBG_iter_base<_Container>::value_type* |
|
369 value_type(const _DBG_iter_base<_Container>&) { |
|
370 typedef typename _DBG_iter_base<_Container>::value_type _Val; |
|
371 return (_Val*)0; |
|
372 } |
|
373 |
|
374 template <class _Container> |
|
375 inline _STLP_TYPENAME_ON_RETURN_TYPE _DBG_iter_base<_Container>::_Iterator_category |
|
376 iterator_category(const _DBG_iter_base<_Container>&) { |
|
377 typedef typename _DBG_iter_base<_Container>::_Iterator_category _Category; |
|
378 return _Category(); |
|
379 } |
|
380 # endif |
|
381 |
|
382 # endif /* _STLP_USE_OLD_HP_ITERATOR_QUERIES */ |
|
383 |
|
384 # define _Get_iter(__x) __x |
|
385 # define _Debug_iter(__x, __y) __y |
|
386 |
|
387 _STLP_END_NAMESPACE |
|
388 |
|
389 #endif /* INTERNAL_H */ |
|
390 |
|
391 // Local Variables: |
|
392 // mode:C++ |
|
393 // End: |
|
394 |