Design Pattern Examples
Overview of object-oriented design patterns
Iterator_Class.cpp
Go to the documentation of this file.
1
5#include "helpers/_countof.h"
6
7#include "Iterator_Class.h"
8
9namespace // Anonymous
10{
11 // Hardcoded data to be iterated over.
12 // The number of keys must match the number of values.
13
14 std::string _keys[] = { std::string("One"), std::string("Two"), std::string("Three") };
15 std::string _values[] = { std::string("Value 1"), std::string("Value 2"), std::string("Value 3") };
16
17} // end anonymous namespace
18
19
21{
22
23 std::shared_ptr<IIterator<ItemPair>> IteratorContainer_Class::GetItems()
24 {
25 std::vector<ItemPair> items;
26
27 size_t numItems = _countof(_keys);
28 for (size_t index = 0; index < numItems; ++index)
29 {
30 items.push_back(ItemPair(_keys[index], _values[index]));
31 }
32
33 return std::make_shared<Iterator<ItemPair>>(&items[0], items.size());
34 }
35
36
37 std::shared_ptr<IIterator<std::string>> IteratorContainer_Class::GetKeys()
38 {
39 return std::make_shared<Iterator<std::string>>(_keys, _countof(_keys));
40 }
41
42
43 std::shared_ptr < IIterator<std::string>> IteratorContainer_Class::GetValues()
44 {
45 return std::make_shared<Iterator<std::string>>(_values, _countof(_values));
46 }
47
48} // end namespace
Declaration of the IteratorContainer_Class class, the IIterator interface, along with the implementat...
static const char * _values[]
A list of values as example data. The number of values must match the number of keys in the _keys lis...
static const char * _keys[]
A list of keys as example data. The number of keys must match the number of values in the _values lis...
Represents a key/value pair where the key and value are strings.
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 ...
#define _countof(w)
The namespace containing all Design Pattern Examples implemented in C++.