aboutsummaryrefslogtreecommitdiff
path: root/app/source/Cplt/Model/Items.hpp
diff options
context:
space:
mode:
Diffstat (limited to 'app/source/Cplt/Model/Items.hpp')
-rw-r--r--app/source/Cplt/Model/Items.hpp69
1 files changed, 23 insertions, 46 deletions
diff --git a/app/source/Cplt/Model/Items.hpp b/app/source/Cplt/Model/Items.hpp
index c00ee59..f3254e2 100644
--- a/app/source/Cplt/Model/Items.hpp
+++ b/app/source/Cplt/Model/Items.hpp
@@ -15,16 +15,14 @@
#include <vector>
template <class T>
-class ItemList
-{
+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)
- {
+ T& Insert(std::string name, Args... args) {
auto iter = mNameLookup.find(name);
if (iter != mNameLookup.end()) {
throw std::runtime_error("Duplicate key.");
@@ -44,25 +42,21 @@ public:
return mStorage[id];
}
- void Remove(size_t index)
- {
+ void Remove(size_t index) {
auto& item = mStorage[index];
mNameLookup.erase(item.GetName());
mStorage[index] = T(*this);
}
- T* Find(size_t id)
- {
+ T* Find(size_t id) {
return &mStorage[id];
}
- const T* Find(size_t id) const
- {
+ const T* Find(size_t id) const {
return &mStorage[id];
}
- const T* Find(std::string_view name) const
- {
+ const T* Find(std::string_view name) const {
auto iter = mNameLookup.find(name);
if (iter != mNameLookup.end()) {
return &mStorage[iter.value()];
@@ -71,8 +65,7 @@ public:
}
}
- Json::Value Serialize() const
- {
+ Json::Value Serialize() const {
Json::Value items(Json::arrayValue);
for (auto& item : mStorage) {
if (!item.IsInvalid()) {
@@ -92,8 +85,7 @@ public:
ItemList() = default;
- ItemList(const Json::Value& root)
- {
+ ItemList(const Json::Value& root) {
constexpr const char* kMessage = "Failed to load item list from JSON.";
auto& itemCount = root["MaxItemId"];
@@ -118,23 +110,19 @@ public:
}
}
- typename decltype(mStorage)::iterator begin()
- {
+ typename decltype(mStorage)::iterator begin() {
return mStorage.begin();
}
- typename decltype(mStorage)::iterator end()
- {
+ typename decltype(mStorage)::iterator end() {
return mStorage.end();
}
- typename decltype(mStorage)::const_iterator begin() const
- {
+ typename decltype(mStorage)::const_iterator begin() const {
return mStorage.begin();
}
- typename decltype(mStorage)::const_iterator end() const
- {
+ typename decltype(mStorage)::const_iterator end() const {
return mStorage.end();
}
@@ -142,16 +130,14 @@ private:
template <class TSelf>
friend class ItemBase;
- void UpdateItemName(const T& item, const std::string& newName)
- {
+ void UpdateItemName(const T& item, const std::string& newName) {
mNameLookup.erase(item.GetName());
mNameLookup.insert(newName, item.GetId());
}
};
template <class TSelf>
-class ItemBase
-{
+class ItemBase {
private:
ItemList<TSelf>* mList;
size_t mId;
@@ -161,39 +147,32 @@ 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) }
- {
+ , mName{ std::move(name) } {
}
- bool IsInvalid() const
- {
+ bool IsInvalid() const {
return mId == std::numeric_limits<size_t>::max();
}
- ItemList<TSelf>& GetList() const
- {
+ ItemList<TSelf>& GetList() const {
return *mList;
}
- size_t GetId() const
- {
+ size_t GetId() const {
return mId;
}
- const std::string& GetName() const
- {
+ const std::string& GetName() const {
return mName;
}
- void SetName(std::string name)
- {
+ void SetName(std::string name) {
mList->UpdateItemName(static_cast<TSelf&>(*this), name);
mName = std::move(name);
}
};
-class ProductItem : public ItemBase<ProductItem>
-{
+class ProductItem : public ItemBase<ProductItem> {
private:
std::string mDescription;
int mPrice = 0;
@@ -216,8 +195,7 @@ public:
void Deserialize(const Json::Value& elm);
};
-class FactoryItem : public ItemBase<FactoryItem>
-{
+class FactoryItem : public ItemBase<FactoryItem> {
private:
std::string mDescription;
std::string mEmail;
@@ -234,8 +212,7 @@ public:
void Deserialize(const Json::Value& elm);
};
-class CustomerItem : public ItemBase<CustomerItem>
-{
+class CustomerItem : public ItemBase<CustomerItem> {
private:
std::string mDescription;
std::string mEmail;