_iterator.h

00001 /*
00002  *
00003  * Copyright (c) 1997
00004  * Moscow Center for SPARC Technology
00005  *
00006  * Copyright (c) 1999 
00007  * Boris Fomitchev
00008  *
00009  * This material is provided "as is", with absolutely no warranty expressed
00010  * or implied. Any use is at your own risk.
00011  *
00012  * Permission to use or copy this software for any purpose is hereby granted 
00013  * without fee, provided the above notices are retained on all copies.
00014  * Permission to modify the code and to distribute modified code is granted,
00015  * provided the above notices are retained, and a notice that the code was
00016  * modified is included with the above copyright notice.
00017  *
00018  */
00019 
00020 #ifndef _STLP_DBG_ITERATOR_H
00021 # define _STLP_DBG_ITERATOR_H
00022 
00023 # include <stl/_pair.h>
00024 # include <stl/_alloc.h>
00025 
00026 # define _STLP_DBG_ALLOCATOR_SELECT( _Tp ) _STLP_DEFAULT_ALLOCATOR_SELECT( _Tp )
00027 
00028 _STLP_BEGIN_NAMESPACE
00029 
00030 //============================================================
00031 
00032 template <class _Iterator>
00033 void _Decrement(_Iterator& __it, const bidirectional_iterator_tag &) {
00034   --__it;
00035 }
00036 
00037 template <class _Iterator>
00038 void _Decrement(_Iterator& __it, const random_access_iterator_tag &) {
00039   --__it;
00040 }
00041 
00042 template <class _Iterator>
00043 void _Decrement(_Iterator& __it, const forward_iterator_tag &) {
00044   _STLP_ASSERT(0)
00045 }
00046 
00047 template <class _Iterator>
00048 void _Advance(_Iterator&, ptrdiff_t, const forward_iterator_tag &) {
00049   _STLP_ASSERT(0)
00050 }
00051 
00052 template <class _Iterator>
00053 void _Advance(_Iterator& __it, ptrdiff_t, const bidirectional_iterator_tag &) {
00054   _STLP_ASSERT(0)  
00055 }
00056 
00057 template <class _Iterator>
00058 void _Advance(_Iterator& __it, ptrdiff_t __n, const random_access_iterator_tag &) {
00059   __it += __n;
00060 }
00061 
00062 template <class _Iterator>
00063 ptrdiff_t _DBG_distance(const _Iterator& __x, const _Iterator& __y, const random_access_iterator_tag &) {
00064   return __x - __y;
00065 }
00066 
00067 template <class _Iterator>
00068 ptrdiff_t _DBG_distance(const _Iterator&, const _Iterator&, const forward_iterator_tag &) {
00069   _STLP_ASSERT(0)
00070   return 0;
00071 }
00072 
00073 template <class _Iterator>
00074 ptrdiff_t _DBG_distance(const _Iterator&, const _Iterator&, const bidirectional_iterator_tag &) {
00075   _STLP_ASSERT(0)  
00076   return 0;
00077 }
00078 
00079 template <class _Iterator>
00080 bool _CompareIt(const _Iterator&, const _Iterator&, const forward_iterator_tag &) {
00081   _STLP_ASSERT(0)
00082   return false;
00083 }
00084 
00085 template <class _Iterator>
00086 bool _CompareIt(const _Iterator&, const _Iterator&, const bidirectional_iterator_tag &) {
00087   _STLP_ASSERT(0)  
00088   return false;
00089 }
00090 
00091 template <class _Iterator>
00092 bool _CompareIt(const _Iterator& __x, const _Iterator& __y, const random_access_iterator_tag &) {
00093   return __x < __y;
00094 }
00095 
00096 
00097 template <class _Iterator>
00098 bool _Dereferenceable(_Iterator __it) {
00099   return (__it._Get_container_ptr() !=0) && !(__it._M_iterator == (__it._Get_container_ptr())->end());
00100 }
00101 
00102 
00103 template <class _Iterator>
00104 bool _Incrementable(const _Iterator& __it, ptrdiff_t __n, const forward_iterator_tag &) {
00105   return (__n == 1) && _Dereferenceable(__it);
00106 }
00107 
00108 template <class _Iterator>
00109 bool _Incrementable(const _Iterator& __it, ptrdiff_t __n, const bidirectional_iterator_tag &) {
00110   typedef typename _Iterator::_Container_type __container_type;
00111   __container_type* __c = __it._Get_container_ptr();
00112   return (__c!=0) && ((__n == 1 && __it._M_iterator != __c->end() ) ||
00113     (__n == -1 && __it._M_iterator != __c->begin()));
00114 }
00115 
00116 template <class _Iterator>
00117 bool _Incrementable(const _Iterator& __it, ptrdiff_t __n, const random_access_iterator_tag &) {
00118   typedef typename _Iterator::_Container_type __container_type;
00119   __container_type* __c = __it._Get_container_ptr();
00120   if (!__c) return false;
00121   ptrdiff_t __new_pos = (__it._M_iterator - __c->begin()) + __n;
00122   return  (__new_pos >=0) && (__STATIC_CAST(typename __container_type::size_type,__new_pos) <=__c->size());
00123 }
00124 
00125 
00126 template <class _Container>
00127 struct _DBG_iter_base : public __owned_link {
00128 public:
00129   typedef typename _Container::value_type value_type;
00130   typedef typename _Container::reference  reference;
00131   typedef typename _Container::pointer    pointer;
00132   typedef ptrdiff_t difference_type;
00133   //private:
00134   typedef typename _Container::iterator        _Nonconst_iterator;
00135   typedef typename _Container::const_iterator  _Const_iterator;
00136   typedef _Container                     _Container_type;
00137     
00138 # ifdef _STLP_CLASS_PARTIAL_SPECIALIZATION
00139   typedef typename iterator_traits<_Const_iterator>::iterator_category _Iterator_category;
00140 # else
00141   typedef typename _Container::_Iterator_category  _Iterator_category;
00142 # endif
00143   typedef _Iterator_category iterator_category;
00144 
00145   _DBG_iter_base() : __owned_link(0)  {}
00146   _DBG_iter_base(const __owned_list* __c, const _Const_iterator& __it) :
00147 # ifdef __HP_aCC
00148     __owned_link(__c), _M_iterator(*__REINTERPRET_CAST(const _Nonconst_iterator *, &__it)) {}
00149 # else
00150     __owned_link(__c), _M_iterator(*(const _Nonconst_iterator*)&__it) {}
00151 # endif  
00152   _Container* _Get_container_ptr() const { 
00153     return (_Container*)__stl_debugger::_Get_container_ptr(this); 
00154   }
00155 
00156   void __increment() {
00157     _STLP_DEBUG_CHECK(_Incrementable(*this,1,_Iterator_category()))
00158     ++_M_iterator;
00159   }
00160   
00161   void __decrement() {
00162     _STLP_DEBUG_CHECK(_Incrementable(*this,-1,_Iterator_category()))
00163     _Decrement(_M_iterator, _Iterator_category());
00164   }
00165 
00166   void __advance(difference_type __n) {
00167     _STLP_DEBUG_CHECK(_Incrementable(*this,__n, _Iterator_category()))
00168     _Advance(_M_iterator,__n, _Iterator_category());
00169   }
00170 
00171 // protected:
00172   _Nonconst_iterator _M_iterator;
00173 };
00174 
00175 template <class _Container>
00176 ptrdiff_t operator-(const _DBG_iter_base<_Container>& __x,
00177                     const _DBG_iter_base<_Container>& __y ) {
00178   typedef typename _DBG_iter_base<_Container>::_Iterator_category  _Iterator_category;
00179   _STLP_DEBUG_CHECK(__check_same_owner(__x, __y))
00180   return _DBG_distance(__x._M_iterator,__y._M_iterator, _Iterator_category());
00181 }
00182 
00183 template <class _Container, class _Traits>
00184 struct _DBG_iter_mid : public _DBG_iter_base<_Container>
00185 {
00186   typedef _DBG_iter_mid<_Container, typename _Traits::_Non_const_traits> _Nonconst_self;
00187   typedef typename _Container::iterator        _Nonconst_iterator;
00188   typedef typename _Container::const_iterator  _Const_iterator;
00189 
00190   _DBG_iter_mid() {}
00191 
00192   explicit _DBG_iter_mid(const _Nonconst_self& __it) :
00193       _DBG_iter_base<_Container>(__it) {}
00194         
00195   _DBG_iter_mid(const __owned_list* __c, const _Const_iterator& __it) :
00196       _DBG_iter_base<_Container>(__c, __it) {}
00197 };
00198 
00199 template <class _Container, class _Traits>
00200 struct _DBG_iter : public _DBG_iter_mid<_Container, _Traits> {
00201   typedef _DBG_iter_base<_Container>          _Base;
00202 public:
00203   typedef typename _Base::value_type value_type;
00204   typedef typename _Base::difference_type difference_type;    
00205   typedef typename _Traits::reference  reference;
00206   typedef typename _Traits::pointer    pointer;
00207 
00208 private:
00209   typedef typename _Base::_Nonconst_iterator _Nonconst_iterator;
00210   typedef typename _Base::_Const_iterator _Const_iterator;
00211 
00212   typedef _DBG_iter<_Container, _Traits>     _Self;
00213   typedef _DBG_iter_mid<_Container, typename _Traits::_Non_const_traits> _Nonconst_mid;
00214 
00215 public:
00216 
00217 # ifdef _STLP_CLASS_PARTIAL_SPECIALIZATION
00218   typedef typename _Base::iterator_category iterator_category;
00219 # endif
00220   typedef typename _Base::_Iterator_category  _Iterator_category;
00221     
00222 public:
00223   _DBG_iter() {}
00224     // boris : real type of iter would be nice
00225   _DBG_iter(const __owned_list* __c, const _Const_iterator& __it) :
00226     _DBG_iter_mid<_Container, _Traits>(__c, __it) {}
00227 
00228   // This allows conversions from iterator to const_iterator without being
00229   // redundant with the copy constructor below.
00230   _DBG_iter(const _Nonconst_mid& __rhs) :
00231     _DBG_iter_mid<_Container, _Traits>(__rhs) {}
00232 
00233   _DBG_iter(const  _Self& __rhs) :
00234     _DBG_iter_mid<_Container, _Traits>(__rhs) {}
00235   
00236   // This allows conversions from iterator to const_iterator without being
00237   // redundant with the copy assignment operator below.
00238   _Self& operator=(const _Nonconst_mid& __rhs)  
00239   {
00240     (_Base&)*this = __rhs;
00241     return *this;
00242   }
00243 
00244   _Self& operator=(const  _Self& __rhs) 
00245   {
00246     (_Base&)*this = __rhs;
00247     return *this;
00248   }
00249   
00250   reference operator*() const {
00251     _STLP_DEBUG_CHECK(_Dereferenceable(*this))
00252     return *this->_M_iterator;
00253   }
00254 
00255   _STLP_DEFINE_ARROW_OPERATOR
00256   
00257   _Self& operator++() {
00258     this->__increment();
00259     return *this;
00260   }
00261   _Self operator++(int) {
00262     _Self __tmp = *this;
00263     this->__increment();
00264     return __tmp;
00265   }
00266   _Self& operator--() {
00267     this->__decrement();
00268     return *this;
00269   }
00270   _Self operator--(int) {
00271     _Self __tmp = *this;
00272     this->__decrement();
00273     return __tmp;
00274   }
00275   
00276   _Self& operator+=(difference_type __n) {
00277     this->__advance(__n);
00278     return *this;
00279   }
00280   
00281   _Self& operator-=(difference_type __n) {
00282     this->__advance(-__n);
00283     return *this;
00284   }
00285   _Self operator+(difference_type __n) const {
00286     _Self __tmp(*this);
00287     __tmp.__advance(__n);
00288     return __tmp;
00289   }
00290   _Self operator-(difference_type __n) const {
00291     _Self __tmp(*this);
00292     __tmp.__advance(-__n);
00293     return __tmp;
00294   }
00295   reference operator[](difference_type __n) const { return *(*this + __n); }
00296 };
00297 
00298 template <class _Container>
00299 inline bool 
00300 operator==(const _DBG_iter_base<_Container>& __x, const _DBG_iter_base<_Container>& __y) {
00301   _STLP_DEBUG_CHECK(__check_same_owner_or_null(__x, __y))
00302   return __x._M_iterator==__y._M_iterator;
00303 }
00304 
00305 template <class _Container>
00306 inline bool 
00307 operator<(const _DBG_iter_base<_Container>& __x, const _DBG_iter_base<_Container>& __y) {
00308   _STLP_DEBUG_CHECK(__check_same_owner_or_null(__x, __y))
00309   typedef typename _DBG_iter_base<_Container>::_Iterator_category _Category;
00310   return _CompareIt(__x._M_iterator , __y._M_iterator, _Category());
00311 }
00312 
00313 template <class _Container>
00314 inline bool 
00315 operator>(const _DBG_iter_base<_Container>& __x,
00316           const _DBG_iter_base<_Container>& __y) { 
00317   typedef typename _DBG_iter_base<_Container>::_Iterator_category _Category;
00318   return _CompareIt(__y._M_iterator , __x._M_iterator, _Category());
00319 }
00320 
00321 template <class _Container>
00322 inline bool 
00323 operator>=(const _DBG_iter_base<_Container>& __x, const _DBG_iter_base<_Container>& __y) {
00324   _STLP_DEBUG_CHECK(__check_same_owner_or_null(__x, __y))
00325   typedef typename _DBG_iter_base<_Container>::_Iterator_category _Category;
00326   return !_CompareIt(__x._M_iterator , __y._M_iterator, _Category());
00327 }
00328 
00329 template <class _Container>
00330 inline bool 
00331 operator<=(const _DBG_iter_base<_Container>& __x,
00332           const _DBG_iter_base<_Container>& __y) {
00333   typedef typename _DBG_iter_base<_Container>::_Iterator_category _Category; 
00334   return !_CompareIt(__y._M_iterator , __x._M_iterator, _Category());
00335 }
00336 
00337 template <class _Container>
00338 inline bool 
00339 operator!=(const _DBG_iter_base<_Container>& __x, 
00340            const _DBG_iter_base<_Container>& __y) {
00341   _STLP_DEBUG_CHECK(__check_same_owner_or_null(__x, __y))
00342   return __x._M_iterator != __y._M_iterator;
00343 }
00344 
00345 //------------------------------------------
00346 
00347 template <class _Container, class _Traits>
00348 inline _DBG_iter<_Container, _Traits> 
00349 operator+(ptrdiff_t __n, const _DBG_iter<_Container, _Traits>& __it) {
00350     _DBG_iter<_Container, _Traits> __tmp(__it);
00351     return __tmp += __n;
00352 }
00353 
00354 
00355 # ifdef _STLP_USE_OLD_HP_ITERATOR_QUERIES
00356 # if defined (_STLP_NESTED_TYPE_PARAM_BUG) \
00357    || ( defined (__SUNPRO_CC) && __SUNPRO_CC < 0x600) \
00358    || ( defined (_STLP_MSVC) && (_STLP_MSVC < 1100) )
00359 #  define _STLP_DEBUG_USE_DISTINCT_VALUE_TYPE_HELPERS 1
00360 # endif
00361 
00362 template <class _Container>
00363 inline ptrdiff_t* 
00364 distance_type(const  _DBG_iter_base<_Container>&) { return (ptrdiff_t*) 0; }
00365 
00366 # if !defined (_STLP_DEBUG_USE_DISTINCT_VALUE_TYPE_HELPERS)
00367 template <class _Container>
00368 inline _STLP_TYPENAME_ON_RETURN_TYPE _DBG_iter_base<_Container>::value_type*
00369 value_type(const  _DBG_iter_base<_Container>&) {
00370   typedef typename _DBG_iter_base<_Container>::value_type _Val;
00371   return (_Val*)0;
00372 }
00373 
00374 template <class _Container>
00375 inline _STLP_TYPENAME_ON_RETURN_TYPE _DBG_iter_base<_Container>::_Iterator_category 
00376 iterator_category(const  _DBG_iter_base<_Container>&) {
00377   typedef typename _DBG_iter_base<_Container>::_Iterator_category _Category;
00378   return _Category();
00379 }
00380 # endif
00381 
00382 #  endif /* _STLP_USE_OLD_HP_ITERATOR_QUERIES */
00383 
00384 # define _Get_iter(__x)   __x
00385 # define _Debug_iter(__x, __y) __y
00386 
00387 _STLP_END_NAMESPACE
00388 
00389 #endif /* INTERNAL_H */
00390 
00391 // Local Variables:
00392 // mode:C++
00393 // End:
00394 

Generated on Mon Jun 5 10:20:46 2006 for Intelligence.kdevelop by  doxygen 1.4.6