/* * pointerator - iterator to T* that behaves like iterator to T * * Example usage: * * std::vector v; * v.push_back(new int(42)); * v.push_back(new int(17)); * * typedef pointerator::iterator> Iter; * // Note: if your compiler does not support partial template * // specialization, you should write: * // typedef pointerator::iterator, int> Iter; * * Iter i = std::find(Iter(v.begin()), Iter(v.end()), 17); * // finds the second element of v * * std::cout << *i.get_iterator() << " points to " << *i; * // *i is 17 * * Note: For ANSI-challenged compilers, you may want to #define * NO_PARTIAL_SPECIALIZATION. * * Written 07-Feb-1999 by Yonat Sharon */ #ifndef POINTERATOR_H #define POINTERATOR_H #ifndef NO_PARTIAL_SPECIALIZATION #include // for iterator_traits #else #include // for ptrdiff_t #endif #ifndef NO_PARTIAL_SPECIALIZATION template #else template #endif // NO_PARTIAL_SPECIALIZATION class pointerator { public: #ifndef NO_PARTIAL_SPECIALIZATION typedef pointerator its_type; template struct dereference {typedef void type;}; template struct dereference {typedef T type;}; typedef typename dereference::type Val; typedef typename std::iterator_traits::iterator_category iterator_category; typedef typename std::iterator_traits::difference_type difference_type; #else typedef pointerator its_type; typedef ptrdiff_t difference_type; #endif // NO_PARTIAL_SPECIALIZATION typedef Val value_type; typedef Val& reference; typedef const Val& const_reference; typedef Val* pointer; typedef const Val* const_pointer; pointerator() {} pointerator(Iter i) : itsIter(i) {} Iter get_iterator() const {return itsIter;} reference operator*() const {return **itsIter;} pointer operator->() const {return *itsIter;} reference operator[](difference_type n) const {return **itsIter[n];} its_type& operator++() {++itsIter; return *this;} its_type& operator--() {--itsIter; return *this;} its_type operator++(int) {its_type t(*this); ++itsIter; return t;} its_type operator--(int) {its_type t(*this); --itsIter; return t;} its_type& operator+=(difference_type n) {itsIter+=n; return *this;} its_type& operator-=(difference_type n) {itsIter-=n; return *this;} its_type operator+(difference_type n) const {return its_type(itsIter+n);} its_type operator-(difference_type n) const {return its_type(itsIter-n);} bool operator==(const its_type& r) const {return itsIter == r.itsIter;} bool operator!=(const its_type& r) const {return itsIter != r.itsIter;} bool operator<(const its_type& r) const {return itsIter < r.itsIter;} private: Iter itsIter; }; #ifndef NO_PARTIAL_SPECIALIZATION # define POINTERATOR pointerator # define TEMPLATE_ARGS template #else # define POINTERATOR pointerator # define TEMPLATE_ARGS template #endif TEMPLATE_ARGS inline POINTERATOR operator+(POINTERATOR ::difference_type n, const POINTERATOR& r) { return POINTERATOR(x.get_iterator() - n); } TEMPLATE_ARGS inline POINTERATOR ::difference_type operator-(const POINTERATOR& l, const POINTERATOR& r) { return l.get_iterator() - r.get_iterator(); } TEMPLATE_ARGS inline POINTERATOR make_pointerator(Iter it) { return POINTERATOR(it); } #undef POINTERATOR #undef TEMPLATE_ARGS #endif // POINTERATOR_H