Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changes between Version 3 and Version 4 of code/doc/Iterator


Ignore:
Timestamp:
Sep 27, 2008, 4:19:36 AM (16 years ago)
Author:
landauf
Comment:

Legend:

Unmodified
Added
Removed
Modified
  • code/doc/Iterator

    v3 v4  
    22[[TracNav(TracNav/TOC_Development)]]
    33
    4 == Description ==
     4The Iterator allows you to iteratate through any [wiki:ObjectList] you like. It's possible to iterate through all objects of class '''B''' with an Iterator of class '''A''', under condition that '''B''' is a child of '''A'''. Iterator does a dynamic_cast to the requested class. Because this is not really performant, you should always use [wiki:ObjectListIterator] instead in case that '''A''' == '''B'''.
    55
    6 The [wiki:Iterator] allows you to easily iterate through all objects in an [wiki:ObjectList]. An [wiki:ObjectList] stores all objects of a given class (and all derivatives). To start iterating, you need the first element in the [wiki:ObjectList]. This gets returned by !ObjectList<!ClassName>::begin().
     6'''Important''': Always use [wiki:ObjectListIterator] if you know at compiletime through which class you want to iterate.
     7
     8Iterator is made to iterate through a list of objects which gets determined at runtime. For this reason you can create an Iterator not only by calling !ObjectList<T>::begin() but also through anyidentifier->getObjects()->begin(). Of course end(), rbegin() and rend() work too.
    79
    810== Example ==
     11{{{
     12Identifier* identifier = someFunctionReturningAnIdentifier();
    913
    10 The following example uses the class-tree below.
     14for (Iterator<BaseObject> it = identifier->getObjects()->begin();
     15                          it != identifier->getObjects()->end(); ++it)
     16{
     17    it->callSomeFunction(...);
     18    BaseObject* storeTheObject = (*it);
     19}
     20}}}
    1121
    1222{{{
    13 #!cpp
    14 new A1();
    15 new A1();
    16 new A1();
     23for (Iterator<BaseClass> it = ObjectList<ChildClass>::begin();
     24                          it != ObjectList<ChildClass>::end(); ++it)
     25{
     26    it->callSomeFunction(...);
     27    BaseObject* storeTheObject = (*it);
     28}
    1729
    18 new A3();
    19 new A3B1();
    20 new A3B1C1();
    21 
    22 int count = 0;
    23 for (Iterator<BaseObject> it = ObjectList<BaseObject>::begin(); it; ++it)
    24   count++;
    25 std::cout << "BaseObject: " << count << std::endl;
    26 
    27 count = 0;
    28 for (Iterator<A1> it = ObjectList<A1>::begin(); it; ++it)
    29   count++;
    30 std::cout << "A1:         " << count << std::endl;
    31 
    32 count = 0;
    33 for (Iterator<A3B1> it = ObjectList<A3B1>::begin(); it; ++it)
    34   count++;
    35 std::cout << "A3B1:       " << count << std::endl;
    36 
    37 /*
    38 return:
    39 BaseObject: 6
    40 A1:         3
    41 A3B1:       2
    42 */
     30// Note:
     31// In this case you should better use ObjectListIterator<ChildClass>
     32// to avoid a dynamic_cast
    4333}}}
    44 
    45 [[Image(Core:testclass_tree.gif)]]