VISH
0.2
|
On several occasions, a set of objects has to be iterated. This set of objects may be stored in several ways, but in all cases the STL is employed for storage. Thus, STL iterators may be used to traverse this list. However, there are practical real-life limitations which prohibit this. In particular, under Windows with VC6 it is not possible to iterate over an STL map which is part of an object in another DLL. The code just crashes as runtime. Thus, the design decision was made to hide usage of the STL from the user of VISH classes. STL is used internally, but for user-code iteration another approach is to be used. Though motivated through the bad implementation under Windows, this design choice turns out to lead to even cleaner interface, and is thus advantageous on other platforms.
The method used in VISH is through Iterator classes, which contain a virtual apply() function. The container class provides an iterate() function which takes an Iterator instance as parameter. The general definition of this class pair is as such
class VishClass { ... STL container class or whatever ... public: class Iterator { public: virtual bool apply(...) = 0; }; int iterate(Iterator&I); };
Note that the parameters of the Iterator's apply() functions are specific to the class. Returning false means to stop the iteration. The iterate() function will return how many iterations have been performed successfully. The user code will be like this:
int main() { struct MyIterator : VishClass::Iterator { override bool apply() { return true; } }; VishClass VC; MyIterator MI; int howmany = VC.iterate(MI); return howmany; }
Note that Iterator callback classes may well be defined locally within a function instead of global namespace, and that any parameters may be passed to the apply() functions via members of the Iterator class.