const_traversable Class

class const_traversable

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

vector v;

becomes...

const_traversable > v;

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

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

The const_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 const_traversable template derives from the _coll parameter and mixes in the IConstTraversableT 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:

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

A note on lifetime management. The IConstTraversableT interface makes lifetime management feasible through its AddRef and Release members. However, because const_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 const_traversable template. Use the sister template refcounted_const_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.