-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCafeMenuIterator.h
More file actions
31 lines (27 loc) · 768 Bytes
/
CafeMenuIterator.h
File metadata and controls
31 lines (27 loc) · 768 Bytes
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
#ifndef CAFE_MENU_ITERATOR_H
#define CAFE_MENU_ITERATOR_H
#include "Iterator.h"
#include "MenuItem.h"
#include <map>
#include <stdexcept>
class CafeMenuIterator : public Iterator<MenuItem> {
public:
CafeMenuIterator(std::map<std::string, MenuItem> &i) :
items(i), iter(items.begin()) {}
MenuItem* next() override { return &iter++->second; }
bool hasNext() const override { return iter != items.end(); }
void remove() override;
private:
std::map<std::string, MenuItem> &items;
std::map<std::string, MenuItem>::iterator iter;
};
inline
void
CafeMenuIterator::remove()
{
if (items.empty())
throw std::invalid_argument(
"You can't remove an item until you've done at least one next()");
items.erase(iter);
}
#endif /* CAFE_MENU_ITERATOR_H */