traversable Class

class traversable

The traversable template is a helper that takes the grunt work out of implementing the ITraversableT interface. Use this template to automatically "decorate" an STL or STL-compatible collection with const traversability. To use the traversable template, simply wrap an existing collection declaration as follows:

vector v;

becomes...

traversable > v;

With this simple modification, the vector "v" now implements the ITraversableT interface and therefore supports polymorphic iteration over its contents. For example:

traversable > v; const_iterator i(&v); for (i.begin(); !i.at_end(); i++) { //do something }

The traversable template has many template parameters. However, only the first two are likely to be used. The first template parameter, _coll, is the collection to decorate with const traversability. The traversable template derives from the _coll parameter and mixes in the ITraversableT interface. Because the template derives from _coll, the original interface to the collection is preserved, allowing the collection to be initialized and used as before.

The second parameter is the predicate used with sets and maps. If you are decorating a vector, list, or deque, this parameter is irrelevant because those structures don't use a predicate. However, sets and maps use them. If you need to pass a predicate to a set or map constructor, you'll have to specify this parameter. For example:

traversable >, std::greater > s(std::greater());

A note on lifetime management. The ITraversableT interface makes lifetime management feasible through its AddRef and Release members. However, because traversable derives from the wrapped collection, it is up to the underlying aggregate to actually manage its own lifetime in accordance with the reference count. STL collections do not manage their lifetime in this way. Therefore, the AddRef and Release members are no-ops in the traversable template. Use the sister template refcounted_traversable to wrap an aggregate that does support reference counting.

Defined in: Traversable.h

Class Template Arguments

_coll

The collection type to derive from and mix in IConstTraversable.

predicate_type

The predicate to use with STL sets and maps.