Main Page   Namespace List   Class Hierarchy   Compound List   File List   Namespace Members   Compound Members   File Members  

rtiterator.h

Go to the documentation of this file.
00001 /*
00002  * rtiterator.h
00003  * 
00004  * Copyright (c) 2000-2004 by Florian Fischer (florianfischer@gmx.de)
00005  * and Martin Trautmann (martintrautmann@gmx.de) 
00006  * 
00007  * This file may be distributed and/or modified under the terms of the 
00008  * GNU General Public License version 2 as published by the Free Software 
00009  * Foundation. 
00010  * 
00011  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
00012  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
00013  * 
00014  */
00015 
00024 #ifndef __LRT_ITERATOR__
00025 #define __LRT_ITERATOR__
00026 
00027 #include "rtsystem.h"
00028 #include "rtdefs.h"
00029 
00030 namespace lrt {
00031 
00035 template<class T> class IIteratorImpl 
00036 {
00037 public:
00039         virtual ~IIteratorImpl() {}
00042         virtual void goToNext() = 0;
00045         virtual void goToPrev() = 0;
00050         virtual bool equals(const IIteratorImpl<T> *) const = 0;
00051 
00053         virtual bool hasElement() const = 0;
00055         virtual bool hasNext() const = 0;
00057         virtual bool hasPrev() const = 0;
00058 
00061         virtual T& get() = 0;
00064         virtual const T& get() const = 0;
00065 
00068         virtual bool canModify() = 0;
00071         virtual void remove() = 0;
00072 
00075         virtual IIteratorImpl<T> *clone() const = 0;
00076 };
00077 
00085 template<class T> class Iterator
00086 {
00087 public:
00089         Iterator(const Iterator<T>& iter) : impl(iter.impl->clone()) {}
00091         Iterator(IIteratorImpl<T> *impl) : impl(impl) { if(!impl) System::exit(-2, "Iterator implementations must not be 0!"); }
00093         inline IIteratorImpl<T>* getImpl() { return impl; }
00095         ~Iterator() { delete impl; }
00096         
00097         // C++-Iterator members
00099         inline Iterator<T> operator++(int) { Iterator<T> ret = clone(); impl->goToNext(); return ret; }
00101         inline Iterator<T> operator--(int) { Iterator<T> ret = clone(); impl->goToPrev(); return ret; }
00103         inline bool operator==( const Iterator<T> &it ) const { return impl->equals(it->impl); }
00105         inline Iterator<T>& operator=(const Iterator<T>& it) { delete impl; impl = it.impl->clone(); return *this; }
00106 
00109         inline T& operator*()                                   { return get(); }
00112         inline const T &operator*() const               { return get(); }
00114         inline Iterator<T>& operator++() { impl->goToNext(); return *this; }
00116         inline Iterator<T>& operator--() { impl->goToPrev(); return *this; }
00117 
00118         // Java Interface
00120         inline bool hasElement() const { return impl->hasElement(); }
00122         inline bool hasNext() const    { return impl->hasNext(); }
00124         inline bool hasPrev() const    { return impl->hasPrev(); }
00125         
00129         inline T& next()        { T& ret = get(); impl->goToNext(); return ret; }
00133         inline T& prev()        { T& ret = get(); impl->goToPrev(); return ret; }
00134         
00135 
00136         // our own, better methods
00139         inline T& get()                         { return impl->get(); }
00142         inline const T& get() const { return impl->get(); }
00145         inline bool canModify() const { return impl->canModify(); }
00149         inline void remove() { if(canModify()) impl->remove(); }
00150 
00152         inline Iterator<T> getNext() const              { return ++clone(); }
00154         inline Iterator<T> getPrev() const              { return --clone(); }
00155 
00156 protected:
00158         Iterator<T> clone() const { return Iterator(impl->clone()); }
00159 private:
00160         IIteratorImpl<T>* impl;
00161 };
00162 
00163 // MSVC++ Bug Workaround
00164 #ifndef __LRT_TPTR_DEFINED
00165 #define __LRT_TPTR_DEFINED
00166 LRT_DEFINE_PTR(T)
00167 #endif
00168 
00173 template<class LRT_NPTR(T)> void deleteCurrent(Iterator<LRT_PTR(T)>& pos);
00174 
00179 template<class LRT_NPTR(T)> void deleteAll(Iterator<LRT_PTR(T)> begin);
00180 
00181 } // namespace
00182 
00183 #include "rtiterator.templ.cpp"
00184 
00185 #endif

This is the RoboTour API documentation version 0.1 • Copyright (C) by Florian Fischer