Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Version 3 (modified by landauf, 16 years ago) (diff)

ObjectListIterator

TracNav(TracNav/TOC_Development)?

The ObjectListIterator of a given class T can iterate exactly through all objects of class T and inheritors. The only way to create such an iterator is by calling one of ObjectLists functions begin, end, rbegin or rend, where the ObjectList has to be of the same T as the ObjectListIterator.

Another way to iterate through object-lists is by using Iterator. ObjectListIterator however is much more performant as it knows about the right class an therefore needs no dynamic-cast. That's why you should always remember:

Important: Always use ObjectListIterator instead of Iterator if you know which class you want to iterate through.

Examples

for (ObjectListIterator<myClass> it = ObjectList<myClass>::begin();
                                 it != ObjectList<myClass>::end(); ++it)
{
    it->someFunction(...);
    myClass* myObject = *it;
}

The following example uses the class-tree below:

new A1();
new A1();
new A1();

new A3();
new A3B1();
new A3B1C1();

int count = 0;
for (ObjectListIterator<BaseObject> it = ObjectList<BaseObject>::begin();
                                    it != ObjectList<BaseObject>::end(); ++it)
{
  count++;
}
std::cout << "BaseObject: " << count << std::endl;

count = 0;
for (ObjectListIterator<A1> it = ObjectList<A1>::begin();
                            it != ObjectList<A1>::end(); ++it)
{
  count++;
}
std::cout << "A1:         " << count << std::endl;

count = 0;
for (ObjectListIterator<A3B1> it = ObjectList<A3B1>::begin();
                              it != ObjectList<A3B1>::end(); ++it)
{
  count++;
}
std::cout << "A3B1:       " << count << std::endl;

/*
return:
BaseObject: 6
A1:         3
A3B1:       2
*/

No image "testclass_tree.gif" attached to Core