blob: 0c7be416d08353c1eb29bff81ce9441b2914a29a (
plain)
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
|
#pragma once
#include <tsl/array_map.h>
#include <cstddef>
#include <stdexcept>
#include <string>
#include <string_view>
#include <vector>
/// Pointers and references returned by accessors are valid as long as no non-const functions have been called.
template <class T>
class ItemList {
public:
class Iterator {
private:
typename std::vector<T>::const_iterator mBackingIter;
public:
Iterator(typename std::vector<T>::const_iterator it)
: mBackingIter{ it } {
}
Iterator& operator++() {
++mBackingIter;
return *this;
}
Iterator& operator++(int) {
auto tmp = *this;
++mBackingIter;
return tmp;
}
Iterator& operator--() {
--mBackingIter;
return *this;
}
Iterator& operator--(int) {
auto tmp = *this;
--mBackingIter;
return tmp;
}
const T& operator*() const {
return *mBackingIter;
}
friend bool operator==(const Iterator&, const Iterator&) = default;
};
private:
std::vector<T> mStorage;
tsl::array_map<char, size_t> mNameLookup;
public:
template <class... Args>
T& Insert(std::string name, Args... args) {
auto iter = mNameLookup.find(name);
if (iter != mNameLookup.end()) {
throw std::runtime_error("Duplicate key.");
}
size_t id = mStorage.size();
mNameLookup.insert(name, id);
return mStorage.emplace_back(id, std::move(name), std::forward<Args>(args)...);
}
const T* Find(size_t id) const {
return &mStorage[id];
}
const T* Find(std::string_view name) const {
auto iter = mNameLookup.find(name);
if (iter != mNameLookup.end()) {
return &mStorage[iter.value()];
} else {
return nullptr;
}
}
Iterator begin() const {
return Iterator(mStorage.begin());
}
Iterator end() const {
return Iterator(mStorage.end());
}
};
class ItemBase {
private:
size_t mId;
public:
ItemBase();
ItemBase(size_t id);
bool IsInvalid() const;
size_t GetId() const;
};
class ProductItem : public ItemBase {
private:
std::string mName;
std::string mDescription;
public:
ProductItem() {}
ProductItem(size_t id, std::string name);
const std::string& GetName() const;
void SetName(std::string mName);
const std::string& GetDescription() const;
void SetDescription(std::string description);
};
class FactoryItem : public ItemBase {
private:
std::string mName;
std::string mDescription;
public:
FactoryItem() {}
FactoryItem(size_t id, std::string name);
const std::string& GetName() const;
void SetName(std::string name);
const std::string& GetDescription() const;
void SetDescription(std::string description);
};
class CustomerItem : public ItemBase {
private:
std::string mName;
std::string mDescription;
public:
CustomerItem() {}
CustomerItem(size_t id, std::string name);
const std::string& GetName() const;
void SetName(std::string name);
const std::string& GetDescription() const;
void SetDescription(std::string description);
};
|