diff options
Diffstat (limited to 'core/src/Model/Items.hpp')
-rw-r--r-- | core/src/Model/Items.hpp | 69 |
1 files changed, 46 insertions, 23 deletions
diff --git a/core/src/Model/Items.hpp b/core/src/Model/Items.hpp index 1289be6..859aedf 100644 --- a/core/src/Model/Items.hpp +++ b/core/src/Model/Items.hpp @@ -15,14 +15,16 @@ #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."); @@ -42,21 +44,25 @@ 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()]; @@ -65,7 +71,8 @@ public: } } - Json::Value Serialize() const { + Json::Value Serialize() const + { Json::Value items(Json::arrayValue); for (auto& item : mStorage) { if (!item.IsInvalid()) { @@ -85,7 +92,8 @@ 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"]; @@ -110,19 +118,23 @@ 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(); } @@ -130,14 +142,16 @@ 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; @@ -147,32 +161,39 @@ 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; @@ -195,7 +216,8 @@ public: void Deserialize(const Json::Value& elm); }; -class FactoryItem : public ItemBase<FactoryItem> { +class FactoryItem : public ItemBase<FactoryItem> +{ private: std::string mDescription; std::string mEmail; @@ -212,7 +234,8 @@ public: void Deserialize(const Json::Value& elm); }; -class CustomerItem : public ItemBase<CustomerItem> { +class CustomerItem : public ItemBase<CustomerItem> +{ private: std::string mDescription; std::string mEmail; |