#pragma once #include "gUtility.h" #include "stl_basics.h" #include "stl_vector.h" #include /////////////////////// // // abstract_iterator // // attempt to make an iterator that can // be used without knowing anything about the container // this just won't work without new & delete and a pImpl // idiom !!! // template class absitr_impl_base; template class abstract_iterator { public: typedef t_value value_type; typedef abstract_iterator this_type; abstract_iterator(const abstract_iterator & other) { m_pimpl = other.m_pimpl->clone(); gAssert(m_pimpl); } ~abstract_iterator() { gAssert(m_pimpl); delete m_pimpl; m_pimpl = NULL; } explicit abstract_iterator(absitr_impl_base * pimpl) { m_pimpl = pimpl; } void operator=(const this_type& rhs) { if ( m_pimpl ) delete m_pimpl; m_pimpl = rhs.m_pimpl->clone(); } // -> for non-classes will trigger warning #pragma warning(disable : 4284) value_type * operator->() const { return &(m_pimpl->reference()); } #pragma warning(default : 4284) value_type & operator*() const { return m_pimpl->reference(); } this_type& operator++() { m_pimpl->increment(); return *this; } this_type operator++(int) { this_type __tmp = *this; m_pimpl->increment(); return __tmp; } this_type& operator--() { m_pimpl->decrement(); return *this; } this_type operator--(int) { this_type __tmp = *this; m_pimpl->decrement(); return __tmp; } this_type& operator+=(int __n) { m_pimpl->advance(__n); return *this; } this_type& operator-=(int __n) { m_pimpl->advance(-__n); return *this; } this_type operator+(int __n) const { this_type __tmp(*this); __tmp += __n; return __tmp; } this_type operator-(int __n) const { this_type __tmp(*this); __tmp -= __n; return __tmp; } value_type& operator[](int __n) const { this_type __tmp(*this); __tmp += __n; return __tmp.m_pimpl->reference(); } int operator - (const this_type& it2) const { return m_pimpl->difference(it2.m_pimpl); } bool operator < (const this_type& rhs) const { return m_pimpl->less(rhs.m_pimpl); } bool operator > (const this_type& rhs) const { return rhs.m_pimpl->less(m_pimpl); } bool operator==(const this_type& rhs) const { return m_pimpl->equals(rhs.m_pimpl); } bool operator!=(const this_type& rhs) const { return ! operator==(rhs); } bool operator <= (const this_type& rhs) const { return m_pimpl->less(rhs.m_pimpl) || m_pimpl->equals(rhs.m_pimpl); } bool operator >= (const this_type& rhs) const { return rhs.m_pimpl->less(m_pimpl) || m_pimpl->equals(rhs.m_pimpl); } private: absitr_impl_base * m_pimpl; }; ///////////////////////////////////////////////////////////////////// __STL_BEGIN_NAMESPACE template inline _Tp* __VALUE_TYPE(const abstract_iterator<_Tp> &) { return (_Tp*)0; } template inline random_access_iterator_tag __ITERATOR_CATEGORY(const abstract_iterator<_Tp> &) { return random_access_iterator_tag(); } template inline ptrdiff_t* __DISTANCE_TYPE(const abstract_iterator<_Tp> &) { return 0; } __STL_END_NAMESPACE ///////////////////////////////////////////////////////////////////// template class absitr_impl_base { public: typedef t_value value_type; typedef absitr_impl_base this_type; absitr_impl_base() {} virtual ~absitr_impl_base() {} virtual this_type * clone() const = 0; virtual value_type & reference() const = 0; virtual void increment() = 0; virtual void decrement() = 0; virtual void advance(int n) = 0; virtual bool less( const this_type * rhs) const = 0; virtual bool equals( const this_type * rhs) const = 0; virtual int difference(const this_type * rhs) const = 0; }; /////////////////////////////////////////////////////////////////////////// template class abstract_iterator_impl : public absitr_impl_base { public: typedef absitr_impl_base parent_type; typedef abstract_iterator_impl this_type; abstract_iterator_impl() { } ~abstract_iterator_impl() { } abstract_iterator_impl(const base_iterator & it) { m_base = it; } private: virtual parent_type * clone() const { return new this_type( m_base ); } virtual value_type & reference() const { return *m_base; } virtual void increment() { ++m_base; } virtual void decrement() { --m_base; } virtual void advance(int n) { m_base += n; } virtual bool less( const parent_type * rhs) const { // @@ !!?? //if ( ! dynamic_cast(rhs) ) // return false; return ( m_base < ((this_type *)rhs)->m_base ); } virtual bool equals(const parent_type * rhs) const { // @@ !!?? //if ( ! dynamic_cast(rhs) ) // return false; return ( m_base == ((this_type *)rhs)->m_base ); } virtual int difference(const parent_type * rhs) const { // @@ !!?? //gAssert( dynamic_cast(rhs) != NULL ); return ( m_base - ((this_type *)rhs)->m_base ); } ///////////////////////////////////////// // data : base_iterator m_base; }; ///////////////////////////////////////////////////////////////////////////