refactor repo
This commit is contained in:
@@ -16,7 +16,7 @@
|
|||||||
|
|
||||||
#include "SPITType.h"
|
#include "SPITType.h"
|
||||||
#include "spit_message.hpp"
|
#include "spit_message.hpp"
|
||||||
#include "SpitRepo.hpp"
|
#include "spit_repo.hpp"
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
using namespace spit;
|
using namespace spit;
|
||||||
|
|||||||
@@ -16,7 +16,7 @@
|
|||||||
|
|
||||||
#include "SPITSocket.h"
|
#include "SPITSocket.h"
|
||||||
#include "spit_message.hpp"
|
#include "spit_message.hpp"
|
||||||
#include "SpitRepo.hpp"
|
#include "spit_repo.hpp"
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
using namespace spit;
|
using namespace spit;
|
||||||
|
|||||||
@@ -20,7 +20,7 @@
|
|||||||
#include "SPITSocket.h"
|
#include "SPITSocket.h"
|
||||||
#include "SPITType.h"
|
#include "SPITType.h"
|
||||||
#include "spit_message.hpp"
|
#include "spit_message.hpp"
|
||||||
#include "SpitRepo.hpp"
|
#include "spit_repo.hpp"
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
|
|||||||
@@ -3,6 +3,10 @@
|
|||||||
#include <string>
|
#include <string>
|
||||||
#include <unordered_map>
|
#include <unordered_map>
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
namespace spit {
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
class SpitRepo {
|
class SpitRepo {
|
||||||
public:
|
public:
|
||||||
@@ -17,26 +21,10 @@ class SpitRepo {
|
|||||||
}
|
}
|
||||||
return i->second.get();
|
return i->second.get();
|
||||||
}
|
}
|
||||||
|
|
||||||
const T &getEntryRef(const string &id) {
|
const T &getEntryRef(const string &id) {
|
||||||
return *mEntries.at(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) {
|
void addEntry(unique_ptr<T> entry) {
|
||||||
mEntries[entry->getID()] = move(entry);
|
mEntries[entry->getID()] = move(entry);
|
||||||
}
|
}
|
||||||
@@ -51,7 +39,7 @@ class SpitRepo {
|
|||||||
mEntries.erase(i);
|
mEntries.erase(i);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
int getEntryCount() {
|
int getSize() {
|
||||||
return mEntries.size();
|
return mEntries.size();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -66,3 +54,5 @@ class SpitRepo {
|
|||||||
private:
|
private:
|
||||||
unordered_map<string, unique_ptr<T>> mEntries;
|
unordered_map<string, unique_ptr<T>> mEntries;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
}
|
||||||
@@ -82,13 +82,10 @@ void SPIT_Client::setActionSPITType(string typeId, bool p_ActionAssigned) {
|
|||||||
if (spitType->setActionAssigned(p_ActionAssigned)) {
|
if (spitType->setActionAssigned(p_ActionAssigned)) {
|
||||||
sendSPITTypeInternal(*spitType);
|
sendSPITTypeInternal(*spitType);
|
||||||
|
|
||||||
for (int i = 0; i < mSpitObjects.getEntryCount(); i++) {
|
for (auto& [key, value] : mSpitObjects.getRepo()) {
|
||||||
SPITObject *ptrSPITObject = mSpitObjects.getEntry(i);
|
if (value->getSPITTypeID() == spitType->getID()) {
|
||||||
if (ptrSPITObject == nullptr)
|
if (value->setActionAssignedType(p_ActionAssigned) == true) {
|
||||||
continue;
|
sendSPITObjectInternal(value.get());
|
||||||
if (ptrSPITObject->getSPITTypeID() == spitType->getID()) {
|
|
||||||
if (ptrSPITObject->setActionAssignedType(p_ActionAssigned) == true) {
|
|
||||||
sendSPITObjectInternal(ptrSPITObject);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -648,21 +645,12 @@ void SPIT_Client::parseRequestProjectTransfer(Message::RequestProjectTransfer &p
|
|||||||
}
|
}
|
||||||
|
|
||||||
// send all listes SPITTypes
|
// send all listes SPITTypes
|
||||||
int cntT = mSpitTypes.getEntryCount();
|
for (const auto& [key, value] : mSpitTypes.getRepo()) {
|
||||||
for (int i = 0; i < cntT; i++) {
|
sendSPITTypeInternal(*value);
|
||||||
SPITType *ptrSPITType = mSpitTypes.getEntry(i);
|
|
||||||
if (!ptrSPITType)
|
|
||||||
break;
|
|
||||||
sendSPITTypeInternal(*ptrSPITType);
|
|
||||||
fos::Task::delayMs(10);
|
fos::Task::delayMs(10);
|
||||||
}
|
}
|
||||||
|
for (auto& [key, value] : mSpitObjects.getRepo()) {
|
||||||
int cntO = mSpitObjects.getEntryCount();
|
sendSPITObjectInternal(value.get());
|
||||||
for (int i = 0; i < cntO; i++) {
|
|
||||||
SPITObject *ptrSPITObject = mSpitObjects.getEntry(i);
|
|
||||||
if (ptrSPITObject == nullptr)
|
|
||||||
break;
|
|
||||||
sendSPITObjectInternal(ptrSPITObject);
|
|
||||||
fos::Task::delayMs(10);
|
fos::Task::delayMs(10);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -932,6 +920,7 @@ bool SPIT_Client::changeSPITType(SPITType *p_SPITType) {
|
|||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* If a client has removed a SPITType where id == p_SPITTypeID, it should send it to the server.
|
* 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.
|
* The corresponding SPITType if listet in SPITType will be deleted.
|
||||||
|
|||||||
@@ -2,8 +2,6 @@
|
|||||||
* @file TestableUTest.cpp
|
* @file TestableUTest.cpp
|
||||||
* @author Gustice
|
* @author Gustice
|
||||||
* @brief
|
* @brief
|
||||||
*
|
|
||||||
* @copyright Copyright (c) 2024
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include "doctest/doctest.h"
|
#include "doctest/doctest.h"
|
||||||
|
|||||||
@@ -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);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user