New features: Class BaseCollection
There is a common abstract base class for all collections for a given item type (e.g., gp_Pnt). Developer X can arbitrarily name this base class like MyPackage_BaseCollPnt in the examples above. This name is further used in the declarations of any (non-abstract) collection class to designate the C++ inheritance.
This base class has the public API:
abstract class Iterator as the base class for all Iterators descried above;
Iterator& CreateIterator () const - creates and returns the Iterator on this collection;
Standard_Integer Size () const - returns the number of items in this collection;
void Assign (const NCollection_BaseCollection& theOther) - copies the contents of the Other to this collection object;
These members enable accessing any collection without knowing its exact type. In particular, it makes possible to implement methods receiving objects of the abstract collection type:
#include <NColection_Map.hxx>
typedef NCollection_Map<gp_Pnt> MyPackage_MapOfPnt;
typedef NCollection_BaseCollection<gp_Pnt> MyPackage_BaseCollPnt;
MyPackage_MapOfPnt aMapPnt;
....
gp_Pnt aResult = COG (aMapPnt);
....
gp_Pnt COG(const MyPackage_BaseCollPnt& theColl)
{
gp_XYZ aCentreOfGravity(0., 0., 0.);
// create type-independent iterator (it is abstract type instance)
MyPackage_BaseCollString::Iterator& anIter = theColl.CreateIterator();
for (; anIter.More(); anIter.Next()) {
aCentreOfGravity += anIter.Value().XYZ();
}
return aCentreOfGravity / theColl.Size();
}
Note that there are fundamental differences between the shown type-independent iterator and the iterator belonging to a particular non-abstract collection:
Type-independent iterator can only be obtained via the call CreateIterator(); the typed iterator - only via the explicit construction.
Type-independent iterator is an abstract class, so it is impossible to copy it or to assign it to another collection object; the typed iterators can be copied and reassigned using the method Init() .
Type-independent iterator is actually destroyed when its collection object is destroyed; the typed iterator is destroyed as any other C++ object in the corresponding C++ scope.
The common point between them is that it is possible to create any number of both types of iterators on the same collection object.