From 1fd1e4b5f2418e3ac2909658993bfedb615537ec Mon Sep 17 00:00:00 2001 From: rtk0c Date: Thu, 6 May 2021 21:56:40 -0700 Subject: Change brace style to on new line, add initial deliveries view when an order entry is selected --- core/src/Model/Items.hpp | 69 ++++++++++++++++++++++++++++++++---------------- 1 file changed, 46 insertions(+), 23 deletions(-) (limited to 'core/src/Model/Items.hpp') 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 template -class ItemList { +class ItemList +{ private: std::vector mStorage; tsl::array_map mNameLookup; public: template - 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 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 ItemBase { +class ItemBase +{ private: ItemList* mList; size_t mId; @@ -147,32 +161,39 @@ public: ItemBase(ItemList& list, size_t id = std::numeric_limits::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::max(); } - ItemList& GetList() const { + ItemList& 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(*this), name); mName = std::move(name); } }; -class ProductItem : public ItemBase { +class ProductItem : public ItemBase +{ private: std::string mDescription; int mPrice = 0; @@ -195,7 +216,8 @@ public: void Deserialize(const Json::Value& elm); }; -class FactoryItem : public ItemBase { +class FactoryItem : public ItemBase +{ private: std::string mDescription; std::string mEmail; @@ -212,7 +234,8 @@ public: void Deserialize(const Json::Value& elm); }; -class CustomerItem : public ItemBase { +class CustomerItem : public ItemBase +{ private: std::string mDescription; std::string mEmail; -- cgit v1.2.3-70-g09d2