CMS 3D CMS Logo

RefVectorIterator.h

Go to the documentation of this file.
00001 #ifndef DataFormats_Common_RefVectorIterator_h
00002 #define DataFormats_Common_RefVectorIterator_h
00003 
00004 /*----------------------------------------------------------------------
00005   
00006 RefVectorIterator: An iterator for a RefVector
00007 Note: this is actually a *const_iterator*
00008 
00009 $Id: RefVectorIterator.h,v 1.9 2009/02/12 19:46:18 wmtan Exp $
00010 
00011 ----------------------------------------------------------------------*/
00012 
00013 #include <memory>
00014 #include "DataFormats/Common/interface/RefItem.h"
00015 #include "DataFormats/Common/interface/RefCore.h"
00016 #include "DataFormats/Common/interface/Ref.h"
00017 
00018 namespace edm {
00019 
00020   template <typename C, typename T = typename Ref<C>::value_type, typename F = typename Ref<C>::finder_type>
00021   class RefVectorIterator : public std::iterator <std::random_access_iterator_tag, Ref<C, T, F> > {
00022   public:
00023     typedef Ref<C, T, F> value_type;
00024     typedef Ref<C, T, F> const const_reference;  // Otherwise boost::iterator_reference assumes '*it' returns 'Ref &'
00025     typedef const_reference    reference;        // This to prevent compilation of code that tries to modify the RefVector
00026                                                  // through this iterator
00027     typedef typename value_type::key_type key_type;
00028 
00029     typedef RefVectorIterator<C, T, F> iterator;
00030     typedef std::ptrdiff_t difference;
00031     typedef typename std::vector<RefItem<key_type> >::const_iterator itemIter;
00032     RefVectorIterator() : product_(), iter_() {}
00033     explicit RefVectorIterator(RefCore const& product, itemIter const& it) :
00034       product_(product), iter_(it) {}
00035     reference operator*() const {
00036       RefItem<key_type> const& item = *iter_;
00037       return value_type(product_, item);
00038     }
00039     reference operator[](difference n) const {
00040       RefItem<key_type> const& item = iter_[n];
00041       return value_type(product_, item);
00042     }
00043     std::auto_ptr<value_type> operator->() const {
00044       RefItem<key_type> const& item = *iter_;
00045       return std::auto_ptr<value_type>(new value_type(product_, item));
00046     }
00047     iterator & operator++() {++iter_; return *this;}
00048     iterator & operator--() {--iter_; return *this;}
00049     iterator & operator+=(difference n) {iter_ += n; return *this;}
00050     iterator & operator-=(difference n) {iter_ -= n; return *this;}
00051 
00052     iterator operator++(int) {iterator it(*this); ++iter_; return it;}
00053     iterator operator--(int) {iterator it(*this); --iter_; return it;}
00054     iterator operator+(difference n) const {iterator it(*this); it.iter_+=n; return it;}
00055     iterator operator-(difference n) const {iterator it(*this); it.iter_-=n; return it;}
00056 
00057     difference operator-(iterator const& rhs) const {return this->iter_ - rhs.iter_;}
00058 
00059     bool operator==(iterator const& rhs) const {return this->iter_ == rhs.iter_;}
00060     bool operator!=(iterator const& rhs) const {return this->iter_ != rhs.iter_;}
00061     bool operator<(iterator const& rhs) const {return this->iter_ < rhs.iter_;}
00062     bool operator>(iterator const& rhs) const {return this->iter_ > rhs.iter_;}
00063     bool operator<=(iterator const& rhs) const {return this->iter_ <= rhs.iter_;}
00064     bool operator>=(iterator const& rhs) const {return this->iter_ >= rhs.iter_;}
00065 
00066   private:
00067     RefCore product_;
00068     itemIter iter_;
00069   };
00070 
00071   template <typename C, typename T, typename F>
00072   inline
00073   RefVectorIterator<C, T, F> operator+(typename RefVectorIterator<C, T, F>::difference n, RefVectorIterator<C, T, F> const& iter) {
00074     return iter + n;
00075   } 
00076 }
00077 #endif

Generated on Tue Jun 9 17:30:27 2009 for CMSSW by  doxygen 1.5.4