// Version: $Id: bfd62f8a98673b5b2ed2c111fee6e32dd8028c8b $ // // // Commentary: // // // Change Log: // // // Code: #pragma once #include template class dtkAbstractIterator; // /////////////////////////////////////////////////////////////////// // dtkIterator // /////////////////////////////////////////////////////////////////// template class dtkIterator { public: dtkAbstractIterator *it; public: typedef qlonglong difference_type; typedef T value_type; typedef T *pointer; typedef T& reference; public: explicit dtkIterator(dtkAbstractIterator *iterator = 0); public: dtkIterator(const dtkIterator& o); dtkIterator(dtkIterator&& o); ~dtkIterator(void); public: dtkIterator& operator = (const dtkIterator& o); dtkIterator& operator = (dtkIterator&& o); public: bool operator == (const dtkIterator& o) const; bool operator != (const dtkIterator& o) const; bool operator < (const dtkIterator& o) const; bool operator <= (const dtkIterator& o) const; bool operator > (const dtkIterator& o) const; bool operator >= (const dtkIterator& o) const; public: T& operator * (void) const; T *operator -> (void) const; T& operator [] (qlonglong j) const; public: dtkIterator& operator ++ (void); dtkIterator operator ++ (int); dtkIterator& operator -- (void); dtkIterator operator -- (int); dtkIterator& operator += (qlonglong j); dtkIterator& operator -= (qlonglong j); dtkIterator operator + (qlonglong j) const; dtkIterator operator - (qlonglong j) const; public: operator T *() const; }; // /////////////////////////////////////////////////////////////////// // dtkAbstractIterator // /////////////////////////////////////////////////////////////////// template class dtkAbstractIterator { public: virtual ~dtkAbstractIterator(void) { ; } public: virtual dtkAbstractIterator *clone(void) const = 0; virtual void copy(const dtkAbstractIterator& o) = 0; public: virtual bool equal(const dtkAbstractIterator& o) const = 0; virtual bool lowerThan(const dtkAbstractIterator& o) const = 0; virtual bool greaterThan(const dtkAbstractIterator& o) const = 0; public: virtual T& ref(void) const = 0; public: virtual void advance(void) = 0; virtual void rewind(void) = 0; virtual void advance(qlonglong j) = 0; virtual void rewind(qlonglong j) = 0; }; // /////////////////////////////////////////////////////////////////// // // /////////////////////////////////////////////////////////////////// template inline dtkIterator::dtkIterator(dtkAbstractIterator *iterator) : it(iterator) { } template inline dtkIterator::dtkIterator(const dtkIterator& o) : it(o.it->clone()) { } template inline dtkIterator::dtkIterator(dtkIterator&& o) : it(o.it) { o.it = 0; } template inline dtkIterator::~dtkIterator(void) { if (it) delete it; it = 0; } template inline dtkIterator& dtkIterator::operator = (const dtkIterator& o) { it->copy(*(o.it)); return *this; } template inline dtkIterator& dtkIterator::operator = (dtkIterator&& o) { std::swap(it, o.it); return *this; } template inline bool dtkIterator::operator == (const dtkIterator& o) const { return it->equal(*(o.it)); } template inline bool dtkIterator::operator != (const dtkIterator& o) const { return !(it->equal(*(o.it))); } template inline bool dtkIterator::operator < (const dtkIterator& o) const { return it->lowerThan(*(o.it)); } template inline bool dtkIterator::operator <= (const dtkIterator& o) const { return (it->lowerThan(*(o.it)) || it->equal(*(o.it))); } template inline bool dtkIterator::operator > (const dtkIterator& o) const { return it->greaterThan(*(o.it)); } template inline bool dtkIterator::operator >= (const dtkIterator& o) const { return (it->greaterThan(*(o.it)) || it->equal(*(o.it))); } template inline T& dtkIterator::operator * (void) const { return it->ref(); } template inline T *dtkIterator::operator -> (void) const { return &(it->ref()); } template inline T& dtkIterator::operator [] (qlonglong j) const { return *(*this + j); } template inline dtkIterator& dtkIterator::operator ++ (void) { it->advance(); return *this; } template inline dtkIterator dtkIterator::operator ++ (int) { dtkIterator o(*this); it->advance(); return o; } template inline dtkIterator& dtkIterator::operator -- (void) { it->rewind(); return *this; } template inline dtkIterator dtkIterator::operator -- (int) { dtkIterator o(*this); it->rewind(); return o; } template inline dtkIterator& dtkIterator::operator += (qlonglong j) { it->advance(j); return *this; } template inline dtkIterator& dtkIterator::operator -= (qlonglong j) { it->rewind(j); return *this; } template inline dtkIterator dtkIterator::operator + (qlonglong j) const { dtkIterator o(*this); o += j; return o; } template inline dtkIterator dtkIterator::operator - (qlonglong j) const { dtkIterator o(*this); o -= j; return o; } template inline dtkIterator::operator T *() const { return &(it->ref()); } // /////////////////////////////////////////////////////////////////// // dtkIteratorBase CRTP // /////////////////////////////////////////////////////////////////// template class dtkIteratorBase : public dtkAbstractIterator { public: dtkIteratorBase *clone(void) const; void copy(const dtkAbstractIterator& o); public: bool equal(const dtkAbstractIterator& o) const; bool lowerThan(const dtkAbstractIterator& o) const; bool greaterThan(const dtkAbstractIterator& o) const; public: T& ref(void) const; public: void advance(void); void rewind(void); void advance(qlonglong j); void rewind(qlonglong j); }; // /////////////////////////////////////////////////////////////////// template inline dtkIteratorBase *dtkIteratorBase::clone(void) const { return new Iterator(static_cast(*this)); } template inline void dtkIteratorBase::copy(const dtkAbstractIterator& o) { static_cast(*this) = static_cast(o); } template inline bool dtkIteratorBase::equal(const dtkAbstractIterator& o) const { return (static_cast(*this) == static_cast(o)); } template inline bool dtkIteratorBase::lowerThan(const dtkAbstractIterator& o) const { return (static_cast(*this) < static_cast(o)); } template inline bool dtkIteratorBase::greaterThan(const dtkAbstractIterator& o) const { return (static_cast(*this) > static_cast(o)); } template inline T& dtkIteratorBase::ref(void) const { return *(static_cast(*this)); } template inline void dtkIteratorBase::advance(void) { ++(static_cast(*this)); } template inline void dtkIteratorBase::advance(qlonglong j) { static_cast(*this) += j; } template inline void dtkIteratorBase::rewind(void) { --(static_cast(*this)); } template inline void dtkIteratorBase::rewind(qlonglong j) { static_cast(*this) -= j; } // /////////////////////////////////////////////////////////////////// // dtkIteratorGeneric // /////////////////////////////////////////////////////////////////// template class dtkIteratorGeneric : public dtkIteratorBase< typename Iterator::value_type, dtkIteratorGeneric > { Iterator it; public: typedef typename Iterator::iterator_category iterator_category; typedef typename Iterator::difference_type difference_type; typedef typename Iterator::value_type value_type; typedef typename Iterator::pointer pointer; typedef typename Iterator::reference reference; public: dtkIteratorGeneric(const Iterator ite) : it(ite) {} dtkIteratorGeneric(const dtkIteratorGeneric& o) : it(o.it) {} public: dtkIteratorGeneric& operator = (const dtkIteratorGeneric& o) { it = o.it; return *this; } public: bool operator == (const dtkIteratorGeneric& o) const { return it == o.it; } bool operator != (const dtkIteratorGeneric& o) const { return it != o.it; } bool operator < (const dtkIteratorGeneric& o) const { return it < o.it; } bool operator <= (const dtkIteratorGeneric& o) const { return it <= o.it; } bool operator > (const dtkIteratorGeneric& o) const { return it > o.it; } bool operator >= (const dtkIteratorGeneric& o) const { return it >= o.it; } public: reference operator * (void) const { return *it; } pointer operator -> (void) const { return &(*it); } reference operator [] (qlonglong j) const { return it[j]; } public: dtkIteratorGeneric& operator ++ (void) { ++it; return *this; } dtkIteratorGeneric operator ++ (int) { dtkIteratorGeneric o(it); ++it; return o; } dtkIteratorGeneric& operator -- (void) { --it; return *this; } dtkIteratorGeneric operator -- (int) { dtkIteratorGeneric o(it); --it; return o; } dtkIteratorGeneric& operator += (qlonglong j) { it += j; return *this; } dtkIteratorGeneric& operator -= (qlonglong j) { it -= j; return *this; } dtkIteratorGeneric operator + (qlonglong j) const { return dtkIteratorGeneric(it + j); } dtkIteratorGeneric operator - (qlonglong j) const { return dtkIteratorGeneric(it - j); } public: operator pointer () const { return &(*it); } }; // // dtkIterator.h ends here