forwardIterator.h

Go to the documentation of this file.
00001 // -*- Mode: C++; tab-width: 2; -*-
00002 // vi: set ts=2:
00003 //
00004 // $Id: forwardIterator.h,v 1.28 2004/02/26 15:04:02 sneumann Exp $
00005 //
00006 
00007 #ifndef BALL_CONCEPT_FORWARDITERATOR_H
00008 #define BALL_CONCEPT_FORWARDITERATOR_H
00009 
00010 #ifndef BALL_CONCEPT_BASEITERATOR_H
00011 # include <BALL/CONCEPT/baseIterator.h>
00012 #endif
00013 
00014 namespace BALL 
00015 {
00016 
00022 
00025   template <typename Container, typename DataType, typename Position, typename Traits>
00026   class ConstForwardIterator
00027     : public BaseIterator<Container, DataType, Position, Traits>
00028   {
00029     public:
00030   
00034 
00036     typedef std::forward_iterator_tag iterator_category;
00037     // convenience typedef
00038     typedef BaseIterator<Container, DataType, Position, Traits> Base;
00040 
00044 
00046     BALL_INLINE ConstForwardIterator()  {}
00047   
00049     BALL_INLINE ConstForwardIterator(const ConstForwardIterator& iterator) 
00050       : BaseIterator<Container, DataType, Position, Traits>(iterator)
00051     {
00052     }
00053       
00055     BALL_INLINE ~ConstForwardIterator()  {}
00056 
00058 
00062 
00063     BALL_INLINE ConstForwardIterator& operator = (const ConstForwardIterator& iterator) 
00064     {
00065       BaseIterator<Container, DataType, Position, Traits>::operator = (iterator);
00066       return *this;
00067     }
00068 
00070     BALL_INLINE void swap(ConstForwardIterator& iterator)  { std::swap(ConstForwardIterator<Container, DataType, Position, Traits>::getTraits(), iterator.getTraits()); }
00072 
00076 
00078     BALL_INLINE void toBegin() throw(Exception::Precondition);
00079 
00081     BALL_INLINE bool isBegin() const ;
00082 
00084     BALL_INLINE void toEnd() throw(Exception::Precondition);
00085 
00087     BALL_INLINE bool isEnd() const ;
00088 
00090     static ConstForwardIterator begin(const Container& container)
00091       throw(Exception::Precondition);
00092 
00094     static ConstForwardIterator end(const Container& container) 
00095       throw(Exception::Precondition);
00096 
00098     BALL_INLINE ConstForwardIterator& operator ++ () throw(Exception::Precondition)
00099     {
00100       BALL_PRECONDITION_EXCEPTION(Base::isValid(), "cannot increment invalid iterator")
00101       Base::getTraits().forward();
00102       return *this;
00103     }
00104 
00106     BALL_INLINE ConstForwardIterator operator ++ (int) throw(Exception::Precondition)
00107     {
00108       BALL_PRECONDITION_EXCEPTION(Base::isValid(), "cannot increment invalid iterator")
00109       ConstForwardIterator tmp(*this);
00110       ++(*this);
00111       return tmp;
00112     }
00113  
00115 
00116     protected:
00117 
00119     BALL_INLINE ConstForwardIterator(const Container& container) 
00120       : BaseIterator<Container, DataType, Position, Traits>(container)
00121     {
00122     }
00123   };
00125 
00126   template <typename Container, typename DataType, typename Position, typename Traits>
00127   BALL_INLINE
00128   void ConstForwardIterator<Container, DataType, Position, Traits>::toBegin()
00129     throw(Exception::Precondition)
00130   {
00131     BALL_PRECONDITION_EXCEPTION(!Base::isSingular(),  "cannot move singular iterator to begin")
00132     Base::getTraits().toBegin();
00133   }
00134 
00135   template <typename Container, typename DataType, typename Position, typename Traits>
00136   BALL_INLINE
00137   ConstForwardIterator<Container, DataType, Position, Traits> 
00138     ConstForwardIterator<Container, DataType, Position, Traits>::begin(const Container& container)
00139     throw(Exception::Precondition)
00140   {
00141     ConstForwardIterator<Container, DataType, Position, Traits> iterator(container);
00142     iterator.toBegin();
00143     return iterator;
00144   }
00145 
00146   template <typename Container, typename DataType, typename Position, typename Traits>
00147   BALL_INLINE
00148   bool ConstForwardIterator<Container, DataType, Position, Traits>::isBegin() const
00149     
00150   {
00151     if (Base::getTraits().isSingular())
00152     {
00153       return false;
00154     }
00155     return Base::getTraits().isBegin();
00156   }
00157 
00158   template <typename Container, typename DataType, typename Position, typename Traits>
00159   BALL_INLINE
00160   void ConstForwardIterator<Container, DataType, Position, Traits>::toEnd()
00161     throw(Exception::Precondition)
00162   {
00163     BALL_PRECONDITION_EXCEPTION(!Base::isSingular(), "cannot move singular iterator to end")
00164     Base::getTraits().toEnd();
00165   }
00166 
00167   template <typename Container, typename DataType, typename Position, typename Traits>
00168   BALL_INLINE
00169   ConstForwardIterator<Container, DataType, Position, Traits> 
00170     ConstForwardIterator<Container, DataType, Position, Traits>::end(const Container& container)
00171     throw(Exception::Precondition)
00172   {
00173     ConstForwardIterator iterator(container);
00174     iterator.toEnd();
00175     return iterator;
00176   }
00177 
00178   template <typename Container, typename DataType, typename Position, typename Traits>
00179   BALL_INLINE
00180   bool ConstForwardIterator<Container, DataType, Position, Traits>::isEnd() const
00181     
00182   {
00183     if (Base::isSingular())
00184     {
00185       return false;
00186     }
00187     return Base::getTraits().isEnd();
00188   }
00189       
00192   template <typename Container, typename DataType, typename Position, typename Traits>
00193   class ForwardIterator
00194     : public ConstForwardIterator<Container, DataType, Position, Traits>
00195   {
00196     public:
00197 
00201 
00203     typedef DataType& reference;
00205     typedef DataType* pointer;
00206     // convenience typedef    
00207     typedef ConstForwardIterator<Container, DataType, Position, Traits> Base;
00209 
00213 
00215     BALL_INLINE ForwardIterator()  {}
00216   
00218     BALL_INLINE ForwardIterator(const ForwardIterator& iterator) 
00219       : ConstForwardIterator<Container, DataType, Position, Traits>(iterator)
00220     {
00221     }
00222 
00224     BALL_INLINE ~ForwardIterator()  {}      
00226 
00230 
00231     BALL_INLINE ForwardIterator& operator = (const ForwardIterator& iterator) 
00232     {
00233       ConstForwardIterator<Container, DataType, Position, Traits>::operator = (iterator);
00234       return *this;
00235     }
00236 
00238     BALL_INLINE void swap(ForwardIterator& iterator)  { std::swap(Base::getTraits(), iterator.getTraits()); }
00240 
00244 
00246     static ForwardIterator begin(const Container& container) throw(Exception::Precondition);
00247 
00249     static ForwardIterator end(const Container& container) throw(Exception::Precondition);
00250 
00252     BALL_INLINE reference operator * () const  
00253     { 
00254       return (reference)Base::getTraits().getData(); 
00255     }
00256 
00258     BALL_INLINE pointer operator -> () const  
00259     { 
00260       return (pointer)&Base::getTraits().getData(); 
00261     }
00262 
00264     BALL_INLINE ForwardIterator& operator ++ () throw(Exception::Precondition)
00265     {
00266       BALL_PRECONDITION_EXCEPTION(Base::isValid(), "cannot increment invalid iterator")
00267       Base::getTraits().forward();
00268       return *this;
00269     }
00270 
00272     BALL_INLINE ForwardIterator operator ++ (int) throw(Exception::Precondition)
00273     {
00274       BALL_PRECONDITION_EXCEPTION(Base::isValid(), "cannot increment invalid iterator")
00275       ForwardIterator tmp(*this);
00276       ++(*this);
00277       return tmp;
00278     }
00279 
00281 
00282     protected:
00283 
00285     BALL_INLINE ForwardIterator(const Container& container)   
00286       : Base(container)
00287     {
00288     }
00289   };
00290 
00291 
00292   template <typename Container, typename DataType, typename Position, typename Traits>
00293   BALL_INLINE
00294   ForwardIterator<Container, DataType, Position, Traits> 
00295     ForwardIterator<Container, DataType, Position, Traits>::begin(const Container& container)
00296     throw(Exception::Precondition)
00297   {
00298     ForwardIterator iterator(container);
00299     iterator.toBegin();
00300     return iterator;
00301   }
00302 
00303   template <typename Container, typename DataType, typename Position, typename Traits>
00304   BALL_INLINE
00305   ForwardIterator<Container, DataType, Position, Traits> 
00306     ForwardIterator<Container, DataType, Position, Traits>::end(const Container& container)
00307     throw(Exception::Precondition)
00308   {
00309     ForwardIterator iterator(container);
00310     iterator.toEnd();
00311     return iterator;
00312   }
00313 
00314 
00315 } // namespace BALL
00316 
00317 #endif // BALL_CONCEPT_FORWARDITERATOR_H