#include "IterTest.h" void CommonHandler(Iter& ii) { while(ii.Next()) ii.Get() = Random(); } IterTest::IterTest() { CtrlLayout(*this, "Window title"); //generally, an iter interface needs its underlying container to live as long as interface is present. //no checks are performed to keep code small //an Iter interface will not modify the underlying container. //if container is changed while an Iter interface is bound to it, it may become undefined in behaviour (Vector i.e) //an Iter interface is always created on heap, so use means to delete it after usage, best is One > foo, destroyed when scope is left //========================================================================================== //Vector or any common linear container Vector vi; vi.SetCount(10); //use the explicit interface to do things One > ii = IterCreator::GetIter(vi); while(ii->Next()) ii->Get() = Random(); //an iterator can be copied, without knowing its underlying type //and can be reinitiated One > ii2 = ii->PartialCopy(); while(ii2->Next()) LOG(ii2->Get()); //the const variant of Iter, ConstIter One > ii3 = IterCreator::GetIter((const Vector&)vi); while(ii3->Next()) LOG(ii3->Get()); FOREACH(int, e, vi) LOG(e); //and the const version FOREACHC(int, e, (const Vector&)vi) LOG(e); //helpers to define the scope safe iterators ITER(int) _ii = IterCreator::GetIter(vi); ITERC(int) _cii = IterCreator::GetIter((const Vector&)vi); //a macro for usual containers only without using Iter interface //can speed up things because no virtual stuff involved FOREACHCONT(int, e, vi) LOG(e); //the const variant FOREACHCONTC(int, e, (const Vector&)vi) LOG(e); //other 'containers' also yield a Iter interface //usual pointer int in = 123; int* inp = ∈ FOREACH(int, e, inp) LOG(e); FOREACHC(int, e, (const int*)inp) LOG(e); //Ptr EditInt ei; Ptr eip(&ei); FOREACH(Ctrl, e, eip) e.SetData(123); FOREACHC(Ctrl, e, (const Ptr&)eip) e.GetData(); //One One oei; oei.Create(); FOREACH(Ctrl, e, oei) e.SetData(123); FOREACHC(Ctrl, e, (const One&)oei) e.GetData(); //Any Any a; a.Create() = 345; One > ia = IterCreator::GetIter(a); FOREACH(int, e, a) LOG(e); FOREACHC(int, e, (const Any&)a) LOG(e); //Value Value v = 789; One > iv = IterCreator::GetIter(v); FOREACH(int, e, v) LOG(e); FOREACHC(int, e, (const Value&)v) LOG(e); //Link LinkOwner lf; for(int i = 0; i < 10; i++) lf.InsertPrev()->d = i; FOREACH(Foo, e, (Link&)lf) LOG(e.d); FOREACHC(Foo, e, (const Link&)lf) LOG(e.d); //all containers can be handled in a common interface ITER(int) iii; iii = IterCreator::GetIter(vi); CommonHandler(*iii); Array ai; iii = IterCreator::GetIter(ai); CommonHandler(*iii); } GUI_APP_MAIN { IterTest().Run(); }