Iterator Header
-
namespace iterator_pattern
Namespace for the Iterator pattern.
-
template<typename T>
class Aggregate - #include <iterator.hpp>
Aggregate interface for creating iterators.
Provides a method to retrieve an iterator for a collection.
Subclassed by iterator_pattern::ConcreteAggregate< T >
-
template<typename T>
class ConcreteAggregate : public iterator_pattern::Aggregate<T> - #include <iterator.hpp>
Concrete Aggregate that holds a collection of items.
Implements the Aggregate interface and provides a method to create iterators.
Public Functions
-
inline void addItem(const T &item)
Add an item to the collection.
- Parameters:
item – The item to add.
-
inline virtual std::unique_ptr<Iterator<T>> createIterator() const override
Retrieve the collection’s iterator.
- Returns:
A unique pointer to a ConcreteIterator.
Private Members
-
std::vector<T> items
Collection of items.
-
inline void addItem(const T &item)
-
template<typename T>
class ConcreteIterator : public iterator_pattern::Iterator<T> - #include <iterator.hpp>
Concrete Iterator for traversing a collection.
Implements the Iterator interface and provides traversal logic.
Public Functions
-
inline explicit ConcreteIterator(const std::vector<T> &collection)
Constructor initializes the iterator with a collection.
- Parameters:
collection – The collection to iterate over.
-
inline virtual bool hasNext() const override
Check if there are more elements in the collection.
- Returns:
True if there are more elements, otherwise false.
-
inline virtual T next() override
Retrieve the next element in the collection.
- Throws:
std::out_of_range – if there are no more elements.
- Returns:
The next element.
Private Members
-
const std::vector<T> &collection
Reference to the collection being iterated.
-
size_t position
Current position in the collection.
-
inline explicit ConcreteIterator(const std::vector<T> &collection)
-
template<typename T>
class Iterator - #include <iterator.hpp>
Iterator interface for traversing collections.
Defines the methods for sequentially accessing elements.
Subclassed by iterator_pattern::ConcreteIterator< T >
Public Functions
-
virtual ~Iterator() = default
-
virtual bool hasNext() const = 0
Check if there are more elements in the collection.
- Returns:
True if there are more elements, otherwise false.
-
virtual T next() = 0
Retrieve the next element in the collection.
- Throws:
std::out_of_range – if there are no more elements.
- Returns:
The next element.
-
virtual ~Iterator() = default
-
template<typename T>