1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
|
/**
* @file iterator.h
* @brief Iterators implementation.
* @author Cesar Mauri Loba (cesar at crea-si dot com)
*
* -------------------------------------------------------------------------
*
* Copyright: (C) 2010 Cesar Mauri Loba - CREA Software Systems
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef SPCORE_ITERATOR_H
#define SPCORE_ITERATOR_H
#include "include/spcore/baseobj.h"
#include <vector>
#include <map>
namespace spcore {
/**
@brief Interface for iterators
Based on GOF iterator pattern
**/
template<class T>
class IIterator : public IBaseObject {
protected:
virtual ~IIterator() {}
public:
virtual void First() = 0;
virtual void Next() = 0;
virtual bool IsDone() const = 0;
virtual T CurrentItem() const = 0;
};
/**
@brief Iterator implementation for std::vector
**/
template<class T>
class CIteratorVector : public IIterator<T> {
public:
CIteratorVector(const std::vector<T> & vector) {
m_vector= &vector;
m_iterator= vector.begin();
}
virtual ~CIteratorVector() { m_vector= NULL; }
virtual void First() { m_iterator= m_vector->begin(); }
virtual void Next() { ++m_iterator; }
virtual bool IsDone() const { return (m_iterator== m_vector->end()); }
virtual T CurrentItem() const { return (*m_iterator); }
private:
const std::vector<T>* m_vector;
typename std::vector<T>::const_iterator m_iterator;
};
/**
@brief Iterator implementation for std::map
**/
template<class KEY, class VALUE>
class CIteratorMap : public IIterator<VALUE> {
public:
CIteratorMap(const std::map<KEY,VALUE> & map) {
m_map= ↦
m_iterator= map.begin();
}
virtual ~CIteratorMap() { m_map= NULL; }
virtual void First() { m_iterator= m_map->begin(); }
virtual void Next() { ++m_iterator; }
virtual bool IsDone() const { return (m_iterator== m_map->end()); }
virtual VALUE CurrentItem() const { return (m_iterator->second); }
private:
const std::map<KEY,VALUE>* m_map;
typename std::map<KEY,VALUE>::const_iterator m_iterator;
};
} // namespace spcore
#endif
|