Changes between Version 3 and Version 4 of code/doc/Iterator
- Timestamp:
- Sep 27, 2008, 4:19:36 AM (16 years ago)
Legend:
- Unmodified
- Added
- Removed
- Modified
-
code/doc/Iterator
v3 v4 2 2 [[TracNav(TracNav/TOC_Development)]] 3 3 4 == Description == 4 The 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'''. 5 5 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 8 Iterator 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. 7 9 8 10 == Example == 11 {{{ 12 Identifier* identifier = someFunctionReturningAnIdentifier(); 9 13 10 The following example uses the class-tree below. 14 for (Iterator<BaseObject> it = identifier->getObjects()->begin(); 15 it != identifier->getObjects()->end(); ++it) 16 { 17 it->callSomeFunction(...); 18 BaseObject* storeTheObject = (*it); 19 } 20 }}} 11 21 12 22 {{{ 13 #!cpp 14 new A1(); 15 new A1(); 16 new A1(); 23 for (Iterator<BaseClass> it = ObjectList<ChildClass>::begin(); 24 it != ObjectList<ChildClass>::end(); ++it) 25 { 26 it->callSomeFunction(...); 27 BaseObject* storeTheObject = (*it); 28 } 17 29 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 43 33 }}} 44 45 [[Image(Core:testclass_tree.gif)]]