Design Pattern Examples
Overview of object-oriented design patterns
Iterator_Class.h
Go to the documentation of this file.
1
9
10#pragma once
11#ifndef __ITERATOR_CLASS_H__
12#define __ITERATOR_CLASS_H__
13
14#include <algorithm>
15#include <memory>
16
17#include "helpers/stringlist.h"
18
20{
21
26 {
27 public:
28 std::string Key;
29 std::string Value;
30
35 {
36 }
37
38
44 ItemPair(std::string key, std::string value)
45 : Key(key)
46 , Value(value)
47 {
48 }
49 };
50
51
71 template <typename TItemType>
72 struct IIterator
73 {
77 virtual ~IIterator() {}
78
82 virtual void Reset() = 0;
83
89 virtual bool Next(TItemType& item) = 0;
90 };
91
92
116 template <typename TItemType>
117 class Iterator : public IIterator<TItemType>
118 {
119 private:
123 TItemType* _items;
124
128 size_t _numItems;
129
133 size_t _index;
134
135 public:
141 Iterator(TItemType* items, size_t numItems)
142 : _items(nullptr)
143 , _numItems(numItems)
144 , _index(0)
145 {
146 _items = new TItemType[numItems];
147 std::copy(items, items + numItems, _items);
148 }
149
154 {
155 delete[] _items;
156 }
157
164 bool Next(TItemType& item)
165 {
166 bool retrieved_item = true;
167 if (_index < _numItems)
168 {
169 item = _items[_index];
170 ++_index;
171 }
172 else
173 {
174 retrieved_item = false;
175 }
176 return retrieved_item;
177 }
178
182 void Reset() override
183 {
184 _index = 0;
185 }
186 };
187
188
198 {
199 public:
205 std::shared_ptr<IIterator<ItemPair>> GetItems();
206
212 std::shared_ptr<IIterator<std::string>> GetKeys();
213
219 std::shared_ptr<IIterator<std::string>> GetValues();
220 };
221
222} // end namespace
223
224#endif // __ITERATOR_CLASS_H__
Represents a key/value pair where the key and value are strings.
ItemPair(std::string key, std::string value)
Constructor.
Represents a container that offers up two kinds of iterators for the hardcoded contents,...
std::shared_ptr< IIterator< std::string > > GetKeys()
Retrieve an iterator over the "key" part of the data, where the data returned from the iterator is a ...
std::shared_ptr< IIterator< ItemPair > > GetItems()
Retrieve an iterator over the data that returns an ItemPair object containing both key and value for ...
std::shared_ptr< IIterator< std::string > > GetValues()
Retrieve an iterator over the "value" part of the data, where the data returned from the iterator is ...
Represents an iterator for a container by implementing the IIterator interface.
size_t _numItems
The number of items in the array of items.
TItemType * _items
The array of items to iterate over.
bool Next(TItemType &item)
Fetches the next item in the iteration, if any.
size_t _index
The index into the _items array to the next item.
Iterator(TItemType *items, size_t numItems)
Constructor.
void Reset() override
Reset the iterator to the beginning.
The namespace containing all Design Pattern Examples implemented in C++.
Represents an iterator for some type. This is a forward-only iterator in that it can only start at 0 ...
virtual void Reset()=0
Start iteration from beginning of container.
virtual ~IIterator()
Virtual destructor so this struct can be used as an interface.
virtual bool Next(TItemType &item)=0
Retrieve the next item from the container.