00001
00002
00003
00004
00005 #ifndef BALL_CONCEPT_FORWARDITERATOR_H
00006 #define BALL_CONCEPT_FORWARDITERATOR_H
00007
00008 #ifndef BALL_CONCEPT_BASEITERATOR_H
00009 # include <BALL/CONCEPT/baseIterator.h>
00010 #endif
00011
00012 namespace BALL
00013 {
00014
00020
00023 template <typename Container, typename DataType, typename Position, typename Traits>
00024 class ConstForwardIterator
00025 : public BaseIterator<Container, DataType, Position, Traits>
00026 {
00027 public:
00028
00032
00034 typedef std::forward_iterator_tag iterator_category;
00035
00036 typedef BaseIterator<Container, DataType, Position, Traits> Base;
00038
00042
00044 BALL_INLINE ConstForwardIterator() {}
00045
00047 BALL_INLINE ConstForwardIterator(const ConstForwardIterator& iterator)
00048 : BaseIterator<Container, DataType, Position, Traits>(iterator)
00049 {
00050 }
00051
00053 BALL_INLINE ~ConstForwardIterator() {}
00054
00056
00060
00061 BALL_INLINE ConstForwardIterator& operator = (const ConstForwardIterator& iterator)
00062 {
00063 BaseIterator<Container, DataType, Position, Traits>::operator = (iterator);
00064 return *this;
00065 }
00066
00068 BALL_INLINE void swap(ConstForwardIterator& iterator) { std::swap(ConstForwardIterator<Container, DataType, Position, Traits>::getTraits(), iterator.getTraits()); }
00070
00074
00076 BALL_INLINE void toBegin() throw(Exception::Precondition);
00077
00079 BALL_INLINE bool isBegin() const ;
00080
00082 BALL_INLINE void toEnd() throw(Exception::Precondition);
00083
00085 BALL_INLINE bool isEnd() const ;
00086
00088 static ConstForwardIterator begin(const Container& container)
00089 throw(Exception::Precondition);
00090
00092 static ConstForwardIterator end(const Container& container)
00093 throw(Exception::Precondition);
00094
00096 BALL_INLINE ConstForwardIterator& operator ++ () throw(Exception::Precondition)
00097 {
00098 BALL_PRECONDITION_EXCEPTION(Base::isValid(), "cannot increment invalid iterator")
00099 Base::getTraits().forward();
00100 return *this;
00101 }
00102
00104 BALL_INLINE ConstForwardIterator operator ++ (int) throw(Exception::Precondition)
00105 {
00106 BALL_PRECONDITION_EXCEPTION(Base::isValid(), "cannot increment invalid iterator")
00107 ConstForwardIterator tmp(*this);
00108 ++(*this);
00109 return tmp;
00110 }
00111
00113
00114 protected:
00115
00117 BALL_INLINE ConstForwardIterator(const Container& container)
00118 : BaseIterator<Container, DataType, Position, Traits>(container)
00119 {
00120 }
00121 };
00123
00124 template <typename Container, typename DataType, typename Position, typename Traits>
00125 BALL_INLINE
00126 void ConstForwardIterator<Container, DataType, Position, Traits>::toBegin()
00127 throw(Exception::Precondition)
00128 {
00129 BALL_PRECONDITION_EXCEPTION(!Base::isSingular(), "cannot move singular iterator to begin")
00130 Base::getTraits().toBegin();
00131 }
00132
00133 template <typename Container, typename DataType, typename Position, typename Traits>
00134 BALL_INLINE
00135 ConstForwardIterator<Container, DataType, Position, Traits>
00136 ConstForwardIterator<Container, DataType, Position, Traits>::begin(const Container& container)
00137 throw(Exception::Precondition)
00138 {
00139 ConstForwardIterator<Container, DataType, Position, Traits> iterator(container);
00140 iterator.toBegin();
00141 return iterator;
00142 }
00143
00144 template <typename Container, typename DataType, typename Position, typename Traits>
00145 BALL_INLINE
00146 bool ConstForwardIterator<Container, DataType, Position, Traits>::isBegin() const
00147
00148 {
00149 if (Base::getTraits().isSingular())
00150 {
00151 return false;
00152 }
00153 return Base::getTraits().isBegin();
00154 }
00155
00156 template <typename Container, typename DataType, typename Position, typename Traits>
00157 BALL_INLINE
00158 void ConstForwardIterator<Container, DataType, Position, Traits>::toEnd()
00159 throw(Exception::Precondition)
00160 {
00161 BALL_PRECONDITION_EXCEPTION(!Base::isSingular(), "cannot move singular iterator to end")
00162 Base::getTraits().toEnd();
00163 }
00164
00165 template <typename Container, typename DataType, typename Position, typename Traits>
00166 BALL_INLINE
00167 ConstForwardIterator<Container, DataType, Position, Traits>
00168 ConstForwardIterator<Container, DataType, Position, Traits>::end(const Container& container)
00169 throw(Exception::Precondition)
00170 {
00171 ConstForwardIterator iterator(container);
00172 iterator.toEnd();
00173 return iterator;
00174 }
00175
00176 template <typename Container, typename DataType, typename Position, typename Traits>
00177 BALL_INLINE
00178 bool ConstForwardIterator<Container, DataType, Position, Traits>::isEnd() const
00179
00180 {
00181 if (Base::isSingular())
00182 {
00183 return false;
00184 }
00185 return Base::getTraits().isEnd();
00186 }
00187
00190 template <typename Container, typename DataType, typename Position, typename Traits>
00191 class ForwardIterator
00192 : public ConstForwardIterator<Container, DataType, Position, Traits>
00193 {
00194 public:
00195
00199
00201 typedef DataType& reference;
00203 typedef DataType* pointer;
00204
00205 typedef ConstForwardIterator<Container, DataType, Position, Traits> Base;
00207
00211
00213 BALL_INLINE ForwardIterator() {}
00214
00216 BALL_INLINE ForwardIterator(const ForwardIterator& iterator)
00217 : ConstForwardIterator<Container, DataType, Position, Traits>(iterator)
00218 {
00219 }
00220
00222 BALL_INLINE ~ForwardIterator() {}
00224
00228
00229 BALL_INLINE ForwardIterator& operator = (const ForwardIterator& iterator)
00230 {
00231 ConstForwardIterator<Container, DataType, Position, Traits>::operator = (iterator);
00232 return *this;
00233 }
00234
00236 BALL_INLINE void swap(ForwardIterator& iterator) { std::swap(Base::getTraits(), iterator.getTraits()); }
00238
00242
00244 static ForwardIterator begin(const Container& container) throw(Exception::Precondition);
00245
00247 static ForwardIterator end(const Container& container) throw(Exception::Precondition);
00248
00250 BALL_INLINE reference operator * () const
00251 {
00252 return (reference)Base::getTraits().getData();
00253 }
00254
00256 BALL_INLINE pointer operator -> () const
00257 {
00258 return (pointer)&Base::getTraits().getData();
00259 }
00260
00262 BALL_INLINE ForwardIterator& operator ++ () throw(Exception::Precondition)
00263 {
00264 BALL_PRECONDITION_EXCEPTION(Base::isValid(), "cannot increment invalid iterator")
00265 Base::getTraits().forward();
00266 return *this;
00267 }
00268
00270 BALL_INLINE ForwardIterator operator ++ (int) throw(Exception::Precondition)
00271 {
00272 BALL_PRECONDITION_EXCEPTION(Base::isValid(), "cannot increment invalid iterator")
00273 ForwardIterator tmp(*this);
00274 ++(*this);
00275 return tmp;
00276 }
00277
00279
00280 protected:
00281
00283 BALL_INLINE ForwardIterator(const Container& container)
00284 : Base(container)
00285 {
00286 }
00287 };
00288
00289
00290 template <typename Container, typename DataType, typename Position, typename Traits>
00291 BALL_INLINE
00292 ForwardIterator<Container, DataType, Position, Traits>
00293 ForwardIterator<Container, DataType, Position, Traits>::begin(const Container& container)
00294 throw(Exception::Precondition)
00295 {
00296 ForwardIterator iterator(container);
00297 iterator.toBegin();
00298 return iterator;
00299 }
00300
00301 template <typename Container, typename DataType, typename Position, typename Traits>
00302 BALL_INLINE
00303 ForwardIterator<Container, DataType, Position, Traits>
00304 ForwardIterator<Container, DataType, Position, Traits>::end(const Container& container)
00305 throw(Exception::Precondition)
00306 {
00307 ForwardIterator iterator(container);
00308 iterator.toEnd();
00309 return iterator;
00310 }
00311
00312
00313 }
00314
00315 #endif // BALL_CONCEPT_FORWARDITERATOR_H