diff --git a/components/spit/include/SPITObject.h b/components/spit/include/SPITObject.h index 09c32af..dc5904b 100644 --- a/components/spit/include/SPITObject.h +++ b/components/spit/include/SPITObject.h @@ -16,7 +16,7 @@ #include "SPITType.h" #include "spit_message.hpp" -#include "SpitRepo.hpp" +#include "spit_repo.hpp" using namespace std; using namespace spit; diff --git a/components/spit/include/SPITType.h b/components/spit/include/SPITType.h index 5870097..e0b182b 100644 --- a/components/spit/include/SPITType.h +++ b/components/spit/include/SPITType.h @@ -16,7 +16,7 @@ #include "SPITSocket.h" #include "spit_message.hpp" -#include "SpitRepo.hpp" +#include "spit_repo.hpp" using namespace std; using namespace spit; diff --git a/components/spit/include/SPIT_Client.h b/components/spit/include/SPIT_Client.h index 4a66edb..25ee0f6 100644 --- a/components/spit/include/SPIT_Client.h +++ b/components/spit/include/SPIT_Client.h @@ -20,7 +20,7 @@ #include "SPITSocket.h" #include "SPITType.h" #include "spit_message.hpp" -#include "SpitRepo.hpp" +#include "spit_repo.hpp" #include #include diff --git a/components/spit/include/SpitRepo.hpp b/components/spit/include/spit_repo.hpp similarity index 68% rename from components/spit/include/SpitRepo.hpp rename to components/spit/include/spit_repo.hpp index 0b3432e..c896f28 100644 --- a/components/spit/include/SpitRepo.hpp +++ b/components/spit/include/spit_repo.hpp @@ -3,6 +3,10 @@ #include #include +using namespace std; + +namespace spit { + template class SpitRepo { public: @@ -10,33 +14,17 @@ class SpitRepo { return mEntries.contains(id); } - T *getEntry(const string &id) { + T *getEntry(const string &id) { auto i = mEntries.find(id); if (i == mEntries.end()) { return nullptr; } return i->second.get(); } - const T &getEntryRef(const string &id) { return *mEntries.at(id); } - T *getEntry(int index) { - if (index >= 0 && index < mEntries.size()) { - return next(mEntries.begin(), index)->second.get(); - } - return nullptr; - } - const T &getEntryRef(int index) { - if (index >= 0 && index < mEntries.size()) { - auto i = next(mEntries.begin(), index); - return *i->second; - } else { - throw "SPITObject index out of range"; - } - } - void addEntry(unique_ptr entry) { mEntries[entry->getID()] = move(entry); } @@ -51,7 +39,7 @@ class SpitRepo { mEntries.erase(i); return true; } - int getEntryCount() { + int getSize() { return mEntries.size(); } @@ -66,3 +54,5 @@ class SpitRepo { private: unordered_map> mEntries; }; + +} \ No newline at end of file diff --git a/components/spit/src/SPIT_Client.cpp b/components/spit/src/SPIT_Client.cpp index 9e5c164..41bb50f 100644 --- a/components/spit/src/SPIT_Client.cpp +++ b/components/spit/src/SPIT_Client.cpp @@ -82,13 +82,10 @@ void SPIT_Client::setActionSPITType(string typeId, bool p_ActionAssigned) { if (spitType->setActionAssigned(p_ActionAssigned)) { sendSPITTypeInternal(*spitType); - for (int i = 0; i < mSpitObjects.getEntryCount(); i++) { - SPITObject *ptrSPITObject = mSpitObjects.getEntry(i); - if (ptrSPITObject == nullptr) - continue; - if (ptrSPITObject->getSPITTypeID() == spitType->getID()) { - if (ptrSPITObject->setActionAssignedType(p_ActionAssigned) == true) { - sendSPITObjectInternal(ptrSPITObject); + for (auto& [key, value] : mSpitObjects.getRepo()) { + if (value->getSPITTypeID() == spitType->getID()) { + if (value->setActionAssignedType(p_ActionAssigned) == true) { + sendSPITObjectInternal(value.get()); } } } @@ -648,21 +645,12 @@ void SPIT_Client::parseRequestProjectTransfer(Message::RequestProjectTransfer &p } // send all listes SPITTypes - int cntT = mSpitTypes.getEntryCount(); - for (int i = 0; i < cntT; i++) { - SPITType *ptrSPITType = mSpitTypes.getEntry(i); - if (!ptrSPITType) - break; - sendSPITTypeInternal(*ptrSPITType); + for (const auto& [key, value] : mSpitTypes.getRepo()) { + sendSPITTypeInternal(*value); fos::Task::delayMs(10); } - - int cntO = mSpitObjects.getEntryCount(); - for (int i = 0; i < cntO; i++) { - SPITObject *ptrSPITObject = mSpitObjects.getEntry(i); - if (ptrSPITObject == nullptr) - break; - sendSPITObjectInternal(ptrSPITObject); + for (auto& [key, value] : mSpitObjects.getRepo()) { + sendSPITObjectInternal(value.get()); fos::Task::delayMs(10); } @@ -932,6 +920,7 @@ bool SPIT_Client::changeSPITType(SPITType *p_SPITType) { return true; } + /** * If a client has removed a SPITType where id == p_SPITTypeID, it should send it to the server. * The corresponding SPITType if listet in SPITType will be deleted. diff --git a/components/spit/uTest/messageTest.cpp b/components/spit/uTest/messageTest.cpp index bf721db..d371d76 100644 --- a/components/spit/uTest/messageTest.cpp +++ b/components/spit/uTest/messageTest.cpp @@ -2,8 +2,6 @@ * @file TestableUTest.cpp * @author Gustice * @brief - * - * @copyright Copyright (c) 2024 */ #include "doctest/doctest.h" diff --git a/components/spit/uTest/spitRepoTest.cpp b/components/spit/uTest/spitRepoTest.cpp new file mode 100644 index 0000000..9f96692 --- /dev/null +++ b/components/spit/uTest/spitRepoTest.cpp @@ -0,0 +1,128 @@ +/** + * @file spitRepoTest.cpp + * @author Gustice + * @brief + */ + +#include "doctest/doctest.h" +#include +#include + +#include "spit_repo.hpp" + +using namespace spit; +using namespace std; + +struct SomeType { + string id; + int val{}; + int tst{}; + string getID() { return id; } +}; + +class SpitRepoFix { + protected: + SpitRepo sut; +}; + +TEST_CASE_FIXTURE(SpitRepoFix, "Typical use case") { + CHECK_EQ(sut.getSize(), 0); + CHECK_EQ(sut.hasEntry("obj1"), false); + CHECK_EQ(sut.getEntry("obj1"), nullptr); + CHECK_THROWS(sut.getEntryRef("obj1")); + CHECK_EQ(sut.removeEntry("obj1"), false); + // CHECK_EQ(sut.removeEntry(1), false); + + auto obj1 = make_unique("obj1", 1, 0); + sut.addEntry(move(obj1)); + { + CHECK_EQ(sut.getSize(), 1); + CHECK_EQ(sut.hasEntry("obj1"), true); + CHECK_EQ(sut.getEntry("obj1")->val, 1); + CHECK_EQ(sut.getEntryRef("obj1").val, 1); + } + + auto obj2 = make_unique("obj2", 2, 0); + sut.addEntry(move(obj2)); + { + CHECK_EQ(sut.getSize(), 2); + CHECK_EQ(sut.hasEntry("obj2"), true); + CHECK_EQ(sut.getEntry("obj2")->val, 2); + CHECK_EQ(sut.getEntryRef("obj2").val, 2); + } + + auto obj3 = make_unique("obj3", 3, 0); + sut.addEntry(move(obj3)); + { + CHECK_EQ(sut.getSize(), 3); + CHECK_EQ(sut.hasEntry("obj3"), true); + CHECK_EQ(sut.getEntry("obj3")->val, 3); + CHECK_EQ(sut.getEntryRef("obj3").val, 3); + } + + auto obj4 = make_unique("obj4", 4, 0); + sut.addEntry(move(obj4)); + { + CHECK_EQ(sut.getSize(), 4); + CHECK_EQ(sut.hasEntry("obj4"), true); + CHECK_EQ(sut.getEntry("obj4")->val, 4); + CHECK_EQ(sut.getEntryRef("obj4").val, 4); + } + + { + auto *p = const_cast(sut.getEntry("obj3")); + auto &r = sut.getEntryRef("obj3"); + CHECK_EQ(p->val, 3); + CHECK_EQ(r.val, 3); + + CHECK_EQ(r.tst, 0); + p->tst = 20; + CHECK_EQ(r.tst, 20); + } + + { + const auto & repo = sut.getRepo(); + CHECK_EQ(repo.size(), 4); + CHECK_EQ(repo.at("obj1")->val, 1); + CHECK_EQ(repo.at("obj2")->val, 2); + CHECK_EQ(repo.at("obj3")->val, 3); + CHECK_EQ(repo.at("obj4")->val, 4); + } + + { + CHECK_EQ(sut.removeEntry("obj1"), true); + CHECK_EQ(sut.removeEntry("obj3"), true); + + const auto & repo = sut.getRepo(); + CHECK_EQ(sut.getSize(), 2); + CHECK_EQ(repo.size(), 2); + CHECK_EQ(repo.at("obj2")->val, 2); + CHECK_EQ(repo.at("obj4")->val, 4); + } + + auto obj5 = make_unique("obj5", 5, 0); + sut.addEntry(move(obj5)); + { + const auto & repo = sut.getRepo(); + CHECK_EQ(sut.getSize(), 3); + CHECK_EQ(repo.size(), 3); + CHECK_EQ(repo.at("obj2")->val, 2); + CHECK_EQ(repo.at("obj4")->val, 4); + CHECK_EQ(repo.at("obj5")->val, 5); + } + + { + sut.clearEntries(); + CHECK_EQ(sut.getSize(), 0); + } + + auto obj6 = make_unique("obj6", 6, 0); + sut.addEntry(move(obj6)); + { // Still works after clear + CHECK_EQ(sut.getSize(), 1); + CHECK_EQ(sut.hasEntry("obj6"), true); + CHECK_EQ(sut.getEntry("obj6")->val, 6); + CHECK_EQ(sut.getEntryRef("obj6").val, 6); + } + +}