Since IGraphicGroup allows creation of hierarchies, support for iterating the hierarchy has been built in this base class to be available polymorphically.
This function is pure virtual in the abstract base class IGraphicHierarchy and all subclasses must provide an implementation. Subclasses who do not wish to expose their children may return an empty iterator when this member function is invoked.
IGraphicIterator* createGraphicIterator() const = 0;
This function creates an IGraphicIterator which iterates through the first level of a hierarchy. In the bicycle example, the iterator created for IGraphicGroup, A iterates over B, C and F. To iterate further, you must create iterators for both B and C. All subclasses creating hierarchies must provide a concrete implementation.
IGraphicIterator is an active iterator that facilitates the iteration over the children of an IGraphicHierarchy. IGraphicIterator functions include:
const IMGraphic *IGraphicIterator::first();
const IMGraphic *IGraphicIterator::next();
const IMGraphic *IGraphicIterator::last();
The createIterator function is called by the IGraphicGroup::draw function. The returned iterator covers one level of the group. The entire group is covered because the IGraphicGroup::draw functions are called recursively.
IGraphicIterator IGraphicHierarchy::createIterator();
IGraphicIterator has first, next, and last functions for traversing the group. The order that graphics are placed into the group is important because it affects the drawing order. This order is determined by the adoptFirst and adoptLast functions of the IGraphicGroup.
2D graphics are added to a group with the adoptFirst and adoptLast functions. Function adoptFirst puts the adopted graphic into the first position in the group; adoptLast puts the adopted graphic into the last position in the group. You nest groups by adopting a IGraphicGroup instance, which is derived from IMGraphic. There is no limit to how deep you can nest groups, but the same graphic cannot be adopted by more than one group.
IGraphicGroup::adoptFirst( MGraphic* graphic );
IGraphicGroup::adoptLast( MGraphic* graphic );
IGraphicGroupIterator provides functions to traverse and manage a hierarchical tree of graphic objects.
![]()
IGraphic Hierarchies and Groups