Iterators and AlObjects
 
 
 

An iterator is a special form of callback function. By providing an overloaded class, you can also maintain data between each call to the callback function.

When using iterators to find AlObjects, you need to copy the wrapper rather than just storing a pointer to it. Below is an example of an iterator which looks for a DAG node named George.

class findGeorgeIterator : public { public: 
	findGeorgeIterator() : george( NULL ) {}; 	~findGeorgeIterator() {}; 	virtual int func( AlObject* obj ) 	{ 		if ( strcmp( obj->name(), "George" ) ) 		{ george = obj->copyWrapper();								// Copy the wrapper! 			return 1; 		} 	}; 	AlDagNode*				result() { return george; }; private: AlDagNode*				george; }  // ... then to use the class. int						result; findGeorgeIterator						iter; AlDagNode						*george = NULL; if ( sSuccess == AlUniverse::applyIteratorToDagNodes( 		&iter, result ) ) { 	george = iter.result(); }

If the result of such an iterator is always used, then the above is adequate. However, the return value could be ignored, a better implementation of the iterator would have the destructor delete the wrapper and have the result method return a copy of the saved wrapper.