refactor repo

This commit is contained in:
2025-11-10 15:34:07 +01:00
parent f064794499
commit de9ff6f807
7 changed files with 148 additions and 43 deletions
+1 -1
View File
@@ -16,7 +16,7 @@
#include "SPITType.h"
#include "spit_message.hpp"
#include "SpitRepo.hpp"
#include "spit_repo.hpp"
using namespace std;
using namespace spit;
+1 -1
View File
@@ -16,7 +16,7 @@
#include "SPITSocket.h"
#include "spit_message.hpp"
#include "SpitRepo.hpp"
#include "spit_repo.hpp"
using namespace std;
using namespace spit;
+1 -1
View File
@@ -20,7 +20,7 @@
#include "SPITSocket.h"
#include "SPITType.h"
#include "spit_message.hpp"
#include "SpitRepo.hpp"
#include "spit_repo.hpp"
#include <memory>
#include <vector>
@@ -3,6 +3,10 @@
#include <string>
#include <unordered_map>
using namespace std;
namespace spit {
template <typename T>
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<T> 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<string, unique_ptr<T>> mEntries;
};
}
+9 -20
View File
@@ -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.
-2
View File
@@ -2,8 +2,6 @@
* @file TestableUTest.cpp
* @author Gustice
* @brief
*
* @copyright Copyright (c) 2024
*/
#include "doctest/doctest.h"
+128
View File
@@ -0,0 +1,128 @@
/**
* @file spitRepoTest.cpp
* @author Gustice
* @brief
*/
#include "doctest/doctest.h"
#include <fmt/core.h>
#include <iostream>
#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<SomeType> 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<SomeType>("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<SomeType>("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<SomeType>("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<SomeType>("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<SomeType *>(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<SomeType>("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<SomeType>("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);
}
}