gatb.core-API-0.0.0
SubjectIterator< Item > Class Template Reference

Iterator that notifies some listener during iteration. More...

#include <IteratorHelpers.hpp>

Inheritance diagram for SubjectIterator< Item >:
Inheritance graph

Public Member Functions

 SubjectIterator (Iterator< Item > *ref, u_int32_t modulo, IteratorListener *listener=0)
 
 ~SubjectIterator ()
 
void first ()
 
bool isDone ()
 
void next ()
 
Item & item ()
 
void setItem (Item &current)
 
void reset ()
 
std::vector< Iterator< Item > * > getComposition ()
 
- Public Member Functions inherited from Iterator< Item >
Item * operator-> ()
 
Item & operator* ()
 
template<typename Functor >
void iterate (const Functor &f)
 
bool get (std::vector< Item > &current)
 
virtual void finalize ()
 
- Public Member Functions inherited from SmartPointer
void use ()
 
void forget ()
 
- Public Member Functions inherited from ISmartPointer
virtual ~ISmartPointer ()
 
- Public Member Functions inherited from AbstractSubjectIterator
 AbstractSubjectIterator ()
 
 ~AbstractSubjectIterator ()
 
void addObserver (IteratorListener *f)
 
void removeObserver (IteratorListener *f)
 
void setMessage (const std::string &message)
 

Additional Inherited Members

- Protected Member Functions inherited from SmartPointer
 SmartPointer ()
 
virtual ~SmartPointer ()
 
- Protected Member Functions inherited from AbstractSubjectIterator
void notifyInc (u_int64_t current)
 
void notifyInit ()
 
void notifyFinish ()
 

Detailed Description

template<class Item>
class gatb::core::tools::dp::impl::SubjectIterator< Item >

Iterator that notifies some listener during iteration.

Implementation note: we have to keep reference (through pointers) on functors because we want them to be notified and not a copy of them.

Note also that we don't allow to have twice the same observer (we use a set as observers container).

/********************************************************************************/
// We a define a functor that will be called during iteration
struct ProgressFunctor : public IteratorListener
{
// we receive 'current' amount of done iterations since last call
void inc (u_int64_t current) { std::cout << "."; }
};
/********************************************************************************/
/* Iteration with progress information */
/********************************************************************************/
int main (int argc, char* argv[])
{
// We declare a STL list with some values.
int values[] = {1,2,3,5,8,13,21,34};
int valuesLen = sizeof(values)/sizeof(values[0]);
std::list<int> l (values, values + valuesLen);
// We create an iterator on the list.
ListIterator<int>* itList = new ListIterator<int> (l);
// We declare an iterator that will send progress status every 3 iterations.
// Note that it refers a ListIterator instance given as constructor parameter.
SubjectIterator<int> itNotif (itList, 3);
// We create some listener to be notified about progress and attach it to the iterator.
itNotif.addObserver (new ProgressFunctor ());
// We iterate the truncated list
for (itNotif.first(); !itNotif.isDone(); itNotif.next())
{
// We can do something in the loop, but there is nothing here about progress management
}
}

Constructor & Destructor Documentation

SubjectIterator ( Iterator< Item > *  ref,
u_int32_t  modulo,
IteratorListener listener = 0 
)
inline

Constructor

Parameters
[in]ref: the referred iterator
[in]modulo: notifies every 'modulo' time
[in]listener: default listener attached to this subject (default value is 0)
~SubjectIterator ( )
inline

Destructor.

Member Function Documentation

void first ( )
inlinevirtual

Method that initializes the iteration.

Implements Iterator< Item >.

std::vector<Iterator<Item>*> getComposition ( )
inlinevirtual

Get a vector holding the composite structure of the iterator.

Reimplemented from Iterator< Item >.

bool isDone ( )
inlinevirtual

Method telling whether the iteration is finished or not.

Returns
true if iteration is finished, false otherwise.

Implements Iterator< Item >.

Item& item ( )
inlinevirtual

Method that returns the current iterated item. Note that the returned type is the template type.

Returns
the current item in the iteration.

Implements Iterator< Item >.

void next ( )
inlinevirtual

Method that goes to the next item in the iteration.

Returns
status of the iteration

Implements Iterator< Item >.

void reset ( )
inlinevirtual

Reset the iterator.

Reimplemented from Iterator< Item >.

void setItem ( Item &  i)
inlinevirtual

Get a reference on the object to be configured as the currently iterated item.

Parameters
[in]i: object to be referred.

Reimplemented from Iterator< Item >.


The documentation for this class was generated from the following file: