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:

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:

The common point between them is that it is possible to create any number of both types of iterators on the same collection object.