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
00078 BALL_INLINE void toBegin();
00079
00081 BALL_INLINE bool isBegin() const;
00082
00086 BALL_INLINE void toEnd();
00087
00089 BALL_INLINE bool isEnd() const;
00090
00094 static ConstForwardIterator begin(const Container& container);
00095
00099 static ConstForwardIterator end(const Container& container);
00100
00104 BALL_INLINE ConstForwardIterator& operator ++ ()
00105 {
00106 BALL_PRECONDITION_EXCEPTION(Base::isValid(), "cannot increment invalid iterator")
00107 Base::getTraits().forward();
00108 return *this;
00109 }
00110
00114 BALL_INLINE ConstForwardIterator operator ++ (int)
00115 {
00116 BALL_PRECONDITION_EXCEPTION(Base::isValid(), "cannot increment invalid iterator")
00117 ConstForwardIterator tmp(*this);
00118 ++(*this);
00119 return tmp;
00120 }
00121
00123
00124 protected:
00125
00127 BALL_INLINE ConstForwardIterator(const Container& container)
00128 : BaseIterator<Container, DataType, Position, Traits>(container)
00129 {
00130 }
00131 };
00133
00134 template <typename Container, typename DataType, typename Position, typename Traits>
00135 BALL_INLINE
00136 void ConstForwardIterator<Container, DataType, Position, Traits>::toBegin()
00137 {
00138 BALL_PRECONDITION_EXCEPTION(!Base::isSingular(), "cannot move singular iterator to begin")
00139 Base::getTraits().toBegin();
00140 }
00141
00142 template <typename Container, typename DataType, typename Position, typename Traits>
00143 BALL_INLINE
00144 ConstForwardIterator<Container, DataType, Position, Traits>
00145 ConstForwardIterator<Container, DataType, Position, Traits>::begin(const Container& container)
00146 {
00147 ConstForwardIterator<Container, DataType, Position, Traits> iterator(container);
00148 iterator.toBegin();
00149 return iterator;
00150 }
00151
00152 template <typename Container, typename DataType, typename Position, typename Traits>
00153 BALL_INLINE
00154 bool ConstForwardIterator<Container, DataType, Position, Traits>::isBegin() const
00155
00156 {
00157 if (Base::getTraits().isSingular())
00158 {
00159 return false;
00160 }
00161 return Base::getTraits().isBegin();
00162 }
00163
00164 template <typename Container, typename DataType, typename Position, typename Traits>
00165 BALL_INLINE
00166 void ConstForwardIterator<Container, DataType, Position, Traits>::toEnd()
00167 {
00168 BALL_PRECONDITION_EXCEPTION(!Base::isSingular(), "cannot move singular iterator to end")
00169 Base::getTraits().toEnd();
00170 }
00171
00172 template <typename Container, typename DataType, typename Position, typename Traits>
00173 BALL_INLINE
00174 ConstForwardIterator<Container, DataType, Position, Traits>
00175 ConstForwardIterator<Container, DataType, Position, Traits>::end(const Container& container)
00176 {
00177 ConstForwardIterator iterator(container);
00178 iterator.toEnd();
00179 return iterator;
00180 }
00181
00182 template <typename Container, typename DataType, typename Position, typename Traits>
00183 BALL_INLINE
00184 bool ConstForwardIterator<Container, DataType, Position, Traits>::isEnd() const
00185
00186 {
00187 if (Base::isSingular())
00188 {
00189 return false;
00190 }
00191 return Base::getTraits().isEnd();
00192 }
00193
00196 template <typename Container, typename DataType, typename Position, typename Traits>
00197 class ForwardIterator
00198 : public ConstForwardIterator<Container, DataType, Position, Traits>
00199 {
00200 public:
00201
00205
00207 typedef DataType& reference;
00209 typedef DataType* pointer;
00210
00211 typedef ConstForwardIterator<Container, DataType, Position, Traits> Base;
00213
00217
00219 BALL_INLINE ForwardIterator() {}
00220
00222 BALL_INLINE ForwardIterator(const ForwardIterator& iterator)
00223 : ConstForwardIterator<Container, DataType, Position, Traits>(iterator)
00224 {
00225 }
00226
00228 BALL_INLINE ~ForwardIterator() {}
00230
00234
00235 BALL_INLINE ForwardIterator& operator = (const ForwardIterator& iterator)
00236 {
00237 ConstForwardIterator<Container, DataType, Position, Traits>::operator = (iterator);
00238 return *this;
00239 }
00240
00242 BALL_INLINE void swap(ForwardIterator& iterator) { std::swap(Base::getTraits(), iterator.getTraits()); }
00244
00248
00252 static ForwardIterator begin(const Container& container);
00253
00257 static ForwardIterator end(const Container& container);
00258
00260 BALL_INLINE reference operator * () const
00261 {
00262 return (reference)Base::getTraits().getData();
00263 }
00264
00266 BALL_INLINE pointer operator -> () const
00267 {
00268 return (pointer)&Base::getTraits().getData();
00269 }
00270
00274 BALL_INLINE ForwardIterator& operator ++ ()
00275 {
00276 BALL_PRECONDITION_EXCEPTION(Base::isValid(), "cannot increment invalid iterator")
00277 Base::getTraits().forward();
00278 return *this;
00279 }
00280
00284 BALL_INLINE ForwardIterator operator ++ (int)
00285 {
00286 BALL_PRECONDITION_EXCEPTION(Base::isValid(), "cannot increment invalid iterator")
00287 ForwardIterator tmp(*this);
00288 ++(*this);
00289 return tmp;
00290 }
00291
00293
00294 protected:
00295
00297 BALL_INLINE ForwardIterator(const Container& container)
00298 : Base(container)
00299 {
00300 }
00301 };
00302
00303
00304 template <typename Container, typename DataType, typename Position, typename Traits>
00305 BALL_INLINE
00306 ForwardIterator<Container, DataType, Position, Traits>
00307 ForwardIterator<Container, DataType, Position, Traits>::begin(const Container& container)
00308 {
00309 ForwardIterator iterator(container);
00310 iterator.toBegin();
00311 return iterator;
00312 }
00313
00314 template <typename Container, typename DataType, typename Position, typename Traits>
00315 BALL_INLINE
00316 ForwardIterator<Container, DataType, Position, Traits>
00317 ForwardIterator<Container, DataType, Position, Traits>::end(const Container& container)
00318 {
00319 ForwardIterator iterator(container);
00320 iterator.toEnd();
00321 return iterator;
00322 }
00323
00324
00325 }
00326
00327 #endif // BALL_CONCEPT_FORWARDITERATOR_H