aboutsummaryrefslogtreecommitdiff
path: root/core/src/Model/Items.hpp
blob: 14b62f3c6ec99dd6e2f35e6c4d1e0af3702da70d (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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
#pragma once

#include "cplt_fwd.hpp"

#include <json/reader.h>
#include <json/value.h>
#include <json/writer.h>
#include <tsl/array_map.h>
#include <cstddef>
#include <limits>
#include <stdexcept>
#include <string>
#include <string_view>
#include <utility>
#include <vector>

template <class T>
class ItemList {
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.");
		}

		for (size_t i = 0; i < mStorage.size(); ++i) {
			if (mStorage[i].IsInvalid()) {
				mStorage[i] = T(*this, i, std::move(name), std::forward<Args>(args)...);
				mNameLookup.insert(name, i);
				return mStorage[i];
			}
		}

		size_t id = mStorage.size();
		mNameLookup.insert(name, id);
		mStorage.emplace_back(*this, id, std::move(name), std::forward<Args>(args)...);
		return mStorage[id];
	}

	void Remove(size_t index) {
		auto& item = mStorage[index];
		mNameLookup.erase(item.GetName());
		mStorage[index] = T(*this);
	}

	T* Find(size_t id) {
		return &mStorage[id];
	}

	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;
		}
	}

	Json::Value Serialize() const {
		Json::Value items(Json::arrayValue);
		for (auto& item : mStorage) {
			if (!item.IsInvalid()) {
				auto elm = item.Serialize();
				elm["Id"] = item.GetId();
				elm["Name"] = item.GetName();
				items.append(elm);
			}
		}

		Json::Value root;
		root["MaxItemId"] = mStorage.size();
		root["Items"] = std::move(items);

		return root;
	}

	ItemList() = default;

	ItemList(const Json::Value& root) {
		constexpr const char* kMessage = "Failed to load item list from JSON.";

		auto& itemCount = root["MaxItemId"];
		if (!itemCount.isIntegral()) throw std::runtime_error(kMessage);

		mStorage.resize(itemCount.asInt64(), T(*this));

		auto& items = root["Items"];
		if (!items.isArray()) throw std::runtime_error(kMessage);

		for (auto& elm : items) {
			if (!elm.isObject()) throw std::runtime_error(kMessage);

			auto& id = elm["Id"];
			if (!id.isIntegral()) throw std::runtime_error(kMessage);
			auto& name = elm["Name"];
			if (!name.isString()) throw std::runtime_error(kMessage);

			size_t iid = id.asInt64();
			mStorage[iid] = T(*this, iid, name.asString());
			mStorage[iid].Deserialize(elm);
		}
	}

	typename decltype(mStorage)::iterator begin() {
		return mStorage.begin();
	}

	typename decltype(mStorage)::iterator end() {
		return mStorage.end();
	}

	typename decltype(mStorage)::const_iterator begin() const {
		return mStorage.begin();
	}

	typename decltype(mStorage)::const_iterator end() const {
		return mStorage.end();
	}

private:
	template <class TSelf>
	friend class ItemBase;

	void UpdateItemName(const T& item, const std::string& newName) {
		mNameLookup.erase(item.GetName());
		mNameLookup.insert(newName, item.GetId());
	}
};

template <class TSelf>
class ItemBase {
private:
	ItemList<TSelf>* mList;
	size_t mId;
	std::string mName;

public:
	ItemBase(ItemList<TSelf>& list, size_t id = std::numeric_limits<size_t>::max(), std::string name = "")
		: mList{ &list }
		, mId{ id }
		, mName{ std::move(name) } {
	}

	bool IsInvalid() const {
		return mId == std::numeric_limits<size_t>::max();
	}

	ItemList<TSelf>& GetList() const {
		return *mList;
	}

	size_t GetId() const {
		return mId;
	}

	const std::string& GetName() const {
		return mName;
	}

	void SetName(std::string name) {
		mList->UpdateItemName(static_cast<TSelf&>(*this), name);
		mName = std::move(name);
	}
};

class ProductItem : public ItemBase<ProductItem> {
private:
	std::string mDescription;
	int mStock = 0;

public:
	using ItemBase::ItemBase;

	const std::string& GetDescription() const;
	void SetDescription(std::string description);
	int GetStock() const;
	void SetStock(int stock);

	Json::Value Serialize() const;
	void Deserialize(const Json::Value& elm);
};

class FactoryItem : public ItemBase<FactoryItem> {
private:
	std::string mDescription;
	std::string mEmail;

public:
	using ItemBase::ItemBase;

	const std::string& GetDescription() const;
	void SetDescription(std::string description);
	const std::string& GetEmail() const;
	void SetEmail(std::string email);

	Json::Value Serialize() const;
	void Deserialize(const Json::Value& elm);
};

class CustomerItem : public ItemBase<CustomerItem> {
private:
	std::string mDescription;
	std::string mEmail;

public:
	using ItemBase::ItemBase;

	const std::string& GetDescription() const;
	void SetDescription(std::string description);
	const std::string& GetEmail() const;
	void SetEmail(std::string email);

	Json::Value Serialize() const;
	void Deserialize(const Json::Value& elm);
};