NAME
OTC_Iterator -
A smart pointer for iterators.
SYNOPSIS
#include <OTC/collctn/iterator.hh>
template<class T>
class OTC_Iterator
{
public:
static os_typespec* get_os_typespec();
inline ~OTC_Iterator();
inline OTC_Iterator();
inline OTC_Iterator(OTC_Cursor<T>* anIter);
inline OTC_Iterator(OTC_Iterator<T> const& anIter);
inline OTC_Iterator<T>& operator=(OTC_Cursor<T>* anIter);
inline OTC_Iterator<T>& operator=(OTC_Iterator<T> const& anIter);
inline void next();
inline void reset();
inline T const& item() const;
inline OTC_Boolean isValid() const;
inline OTC_Iterator<T> clone() const;
inline void apply(OTC_Visitor<T>& theApplicator);
protected:
inline T& _item() const;
inline OTC_Cursor<T>* _clone() const;
inline void _apply(OTC_Visitor<T>& theApplicator);
inline void _apply(OTC_Worker<T>& theApplicator);
};
CLASS TYPE
Concrete
DESCRIPTION
This class exists so that the user of an iterator for a particular
type of collection, does not have to worry about deleting the
iterator when finished with it, nor need to know how to create an
iterator for a specific type of collection. This class will ensure
that the iterator is destroyed, when this class goes out of scope.
In order for this to work correctly, an instance of this class
should never be created using new.
EXAMPLE
void sum(OTC_Collection<int> const& aCollection)
{
OTC_Iterator<int> anIter = 0;
anIter = aCollection.items();
int i=0;
for (anIter.reset(); anIter.isValid(); anIter.next())
i += anIter.item();
cout << i << endl;
}
INITIALISATION
inline OTC_Iterator();
inline OTC_Iterator(OTC_Cursor<T>* anIter);
Constructor used when an iterator
for a particular type of collection is
first created. Passing in 0 will
result in a null iterator being
constructed.
inline OTC_Iterator(OTC_Iterator<T> const& anIter);
Copy constructor for when an iterator is
used as return type.
inline OTC_Iterator<T>& operator=(OTC_Cursor<T>* anIter);
Sets this iterator to anIter.
If anIter is 0, the iterator
is turned into a null iterator and
all associations with the previous
collection are lost.
inline OTC_Iterator<T>& operator=(OTC_Iterator<T> const& anIter);
Sets this iterator to anIter.
MOVEMENT
inline void next();
Moves the iterator to the next item.
inline void reset();
Resets the iterator to the start.
RETRIEVAL
inline T const& item() const;
Returns the item under the current
location of the iterator. Raises an
exception if there is no item under the
iterator, or if this is a null iterator.
inline OTC_Boolean isValid() const;
Returns OTCLIB_TRUE while there is a
valid data item under the current location
of the iterator.
CLONING
inline OTC_Iterator<T> clone() const;
Returns an iterator which is a clone of
this iterator. The new iterator will
initially be located over the same item as
this iterator, however subsequent
operations will be independent of this
iterator.
APPLICATOR
inline void apply(OTC_Visitor<T>& theApplicator);
Applies theApplicator to each item
accessible by this iterator.
LIBRARY
OTC
AUTHOR(S)
Graham Dumpleton
COPYRIGHT
Copyright 1991 1992 1993 OTC LIMITED
Copyright 1994 DUMPLETON SOFTWARE CONSULTING PTY LIMITED