NAME
OTC_Collection -
Abstract base class for a collection of objects.
SYNOPSIS
#include <OTC/collctn/collctn.hh>
template<class T>
class OTC_Collection
{
public:
virtual ~OTC_Collection();
OTC_Collection();
virtual void removeAll();
virtual OTC_Boolean isEmpty() const;
virtual u_int population() const;
virtual OTC_Iterator<T> items(
OTC_Protection theProtection=OTCLIB_SAFE
) const;
};
CLASS TYPE
Concrete
DESCRIPTION
This class is the ultimate base class of any collection. It
embodies the abstractions that all collections must provide.
These are:
- The ability to return a class that will allow iteration
over all items contained in the collection.
- The ability to determine the number of items in the collection.
- The ability to empty the collection.
EXAMPLE
An example of how a collection class could be used.
void sum(OTC_Collection<int>& theColl)
{
int theSum = 0;
if (!theColl.isEmpty())
{
OTC_Iterator<int> aIter = 0;
anIter = theColl.items();
for (aIter.reset(); aIter.isValid(); aIter.next())
theSum += aIter.item();
theColl.removeAll();
}
cout << theSum << endl;
}
If a function only requires the ability to iterate over a
collection as above, the function should be written to accept the
collection base class. This would allow any type of collection to
be passed to the function and not just the particular one that
was being used at that time.
CONSTRUCTION
OTC_Collection();
Creates an empty collection.
DESTRUCTION
virtual void removeAll();
Removes all items in the collection.
ENQUIRY
virtual OTC_Boolean isEmpty() const;
Returns OTCLIB_TRUE if the collection is
empty.
virtual u_int population() const;
Returns the number of items in the
collection.
ITERATION
virtual OTC_Iterator<T> items(
OTC_Protection theProtection=OTCLIB_SAFE
) const;
Returns a class which will allow iteration
over all items contained in the collection.
NOTES
removeAll(), population() and items() must be redefined in a
derived class. isEmpty() should also be redefined in a derived
class if possible, as the default for isEmpty() is to compare
the result of population() against 0. This is potentially
inefficient, depending on the implementation of a particular
collection.
It is possible to create an instance of the OTC_Collection
class. If you do, it will behave like an empty collection,
always returning a population of 0 and a null iterator.
SEE ALSO
OTC_Iterator
LIBRARY
OTC
AUTHOR(S)
Graham Dumpleton
COPYRIGHT
Copyright 1991 1992 1993 OTC LIMITED
Copyright 1994 DUMPLETON SOFTWARE CONSULTING PTY LIMITED