KVIrc  4.9.2
DeveloperAPIs
Public Member Functions | Protected Attributes | List of all members
KviPointerListIterator< T > Class Template Reference

A fast KviPointerList iterator. More...

#include <KviPointerList.h>

Public Member Functions

T * current ()
 Returs the value pointed by the iterator. More...
 
bool isValid ()
 Returns true if this iterator points to a valid element. More...
 
 KviPointerListIterator (const KviPointerListIterator< T > &src)
 Creates an iterator copy. More...
 
 KviPointerListIterator (KviPointerList< T > &l)
 Creates an iterator for the list l. More...
 
 KviPointerListIterator (KviPointerList< T > &l, KviPointerListNode *pNode)
 Creates an iterator for the list l. More...
 
bool moveFirst ()
 Moves the iterator to the first element of the list. More...
 
bool moveLast ()
 Moves the iterator to the last element of the list. More...
 
bool moveNext ()
 Moves the iterator to the next element of the list. More...
 
bool movePrev ()
 Moves the iterator to the previous element of the list. More...
 
T * operator* ()
 Returs the value pointed by the iterator. More...
 
bool operator++ ()
 Moves the iterator to the next element of the list. More...
 
bool operator-- ()
 Moves the iterator to the previous element of the list. More...
 
void operator= (const KviPointerListIterator< T > &src)
 Creates an iterator copy. More...
 

Protected Attributes

KviPointerList< T > * m_pList
 
KviPointerListNodem_pNode
 

Detailed Description

template<typename T>
class KviPointerListIterator< T >

A fast KviPointerList iterator.

This class allows traversing the list sequentially. Multilpe iterators can traverse the list at the same time.

Iteration example 1:

* KviPointerListIterator<T> it(list);
* for(bool b = it.moveFirst(); b; b = it.moveNext())
* {
*     T * pData = it.data();
*     doSomethingWithData(pData);
* }
* 

Iteration example 2:

* KviPointerListIterator<T> it(list);
* if(it.moveFirst())
* {
*     do {
*         T * pData = it.data();
*         doSomethingWithData(pData);
*     } while(it.moveNext());
* }
* 

Iteration example 3:

* KviPointerListIterator<T> it(list.iteratorAt(10));
* if(it.isValid())
* {
*    do {
*         T * pData = it.data();
*         doSomethingWithData(pData);
*    while(it.movePrev());
* }
* 

Please note that you must NOT remove any item from the list when using the iterators. An iterator pointing to a removed item will crash your application if you use it. The following code will NOT work (and crash):

* KviPointerList<T> l;
* l.append(new KviCString("x"));
* l.append(new KviCString("y"));
* KviPointerListIterator<T> it(l);
* it.moveFirst();
* l.removeFirst();
* KviCString * tmp = it.data(); <-- this will crash
* 

In the rare cases in that you need to remove items while traversing the list you should put them in a temporary list and remove them after the iteration.

I've chosen this way because usually you don't modify the list while traversing it and a fix for this would add a constant overhead to several list operation. You just must take care of it yourself.

Warning
This class is not thread safe by itself.

Constructor & Destructor Documentation

template<typename T>
KviPointerListIterator< T >::KviPointerListIterator ( const KviPointerListIterator< T > &  src)
inline

Creates an iterator copy.

The new iterator points exactly to the item pointed by src.

Parameters
srcThe source item to point to
Returns
KviPointerListIterator
template<typename T>
KviPointerListIterator< T >::KviPointerListIterator ( KviPointerList< T > &  l)
inline

Creates an iterator for the list l.

The iterator points to the first list item, if any.

Parameters
lThe source list to point to
Returns
KviPointerListIterator
template<typename T>
KviPointerListIterator< T >::KviPointerListIterator ( KviPointerList< T > &  l,
KviPointerListNode pNode 
)
inline

Creates an iterator for the list l.

The iterator points to the specified list node.

Parameters
lThe source list
pNodeThe list node to point to
Returns
KviPointerListIterator

Member Function Documentation

template<typename T>
T* KviPointerListIterator< T >::current ( )
inline
template<typename T>
bool KviPointerListIterator< T >::isValid ( )
inline

Returns true if this iterator points to a valid element.

Returns false otherwise.

Returns
bool
template<typename T>
bool KviPointerListIterator< T >::moveFirst ( )
inline

Moves the iterator to the first element of the list.

Returns true in case of success or false if the list is empty.

Returns
bool

Referenced by KviPointerHashTableIterator< Key, T >::moveFirst(), KviPointerHashTableIterator< Key, T >::moveNext(), spellchecker_kvs_check(), and spellchecker_kvs_suggestions().

template<typename T>
bool KviPointerListIterator< T >::moveLast ( )
inline

Moves the iterator to the last element of the list.

Returns true in case of success or false if the list is empty.

Returns
bool

Referenced by KviPointerHashTableIterator< Key, T >::moveLast(), and KviPointerHashTableIterator< Key, T >::movePrev().

template<typename T>
bool KviPointerListIterator< T >::moveNext ( )
inline

Moves the iterator to the next element of the list.

The iterator must be actually valid for this function to work. Returns true in case of success or false if there is no next item.

Returns
bool

Referenced by KviPointerHashTableIterator< Key, T >::moveNext(), spellchecker_kvs_check(), and spellchecker_kvs_suggestions().

template<typename T>
bool KviPointerListIterator< T >::movePrev ( )
inline

Moves the iterator to the previous element of the list.

The iterator must be actually valid for this function to work. Returns true in case of success or false if there is no previous item.

Returns
bool

Referenced by KviPointerHashTableIterator< Key, T >::movePrev().

template<typename T>
T* KviPointerListIterator< T >::operator* ( )
inline

Returs the value pointed by the iterator.

If the iterator is not valid, returns NULL. This is just an alias to current().

Returns
T *
template<typename T>
bool KviPointerListIterator< T >::operator++ ( )
inline

Moves the iterator to the next element of the list.

The iterator must be actually valid for this operator to work. Returns true in case of success or false if there is no next item. This is just a convenient alias to moveNext().

Returns
bool
template<typename T>
bool KviPointerListIterator< T >::operator-- ( )
inline

Moves the iterator to the previous element of the list.

The iterator must be actually valid for this operator to work. Returns true in case of success or false if there is no previous item. This is just a convenient alias to movePrev().

Returns
bool
template<typename T>
void KviPointerListIterator< T >::operator= ( const KviPointerListIterator< T > &  src)
inline

Creates an iterator copy.

The new iterator points exactly to the item pointed by src.

Parameters
srcThe source item to copy
Returns
void

Member Data Documentation

template<typename T>
KviPointerList<T>* KviPointerListIterator< T >::m_pList
protected
template<typename T>
KviPointerListNode* KviPointerListIterator< T >::m_pNode
protected

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