refactor: concentrating repo functions into repo class

This commit is contained in:
2025-04-07 00:12:29 +02:00
parent 3231120a1e
commit 407c155c4d
8 changed files with 197 additions and 392 deletions
+7 -37
View File
@@ -16,47 +16,17 @@
#include "SPITType.h"
#include "SPIT_Message.h"
#include <string>
#include <unordered_map>
#include <memory>
#include "SpitRepo.hpp"
using namespace std;
class SPITObjectListener;
class SPITObject {
private:
static unordered_map<string, unique_ptr<SPITObject>> sSpitObjects;
static unordered_map<string, unique_ptr<SPITObject>> sSpitObjectsRemote;
public:
static SPITObject *getSPITObject(const string &id);
static const SPITObject &getSPITObjectRef(const string &id);
static void addSPITObject(unique_ptr<SPITObject> spitObject);
static bool removeSPITObject(const SPITObject &spitObject);
static bool removeSPITObject(const string & spitObjectID);
static int getSPITObjectCount() {
return sSpitObjects.size();
}
static SPITObject *getSPITObject(int p_Index);
static const SPITObject &getSPITObjectRef(int p_Index);
static void clearSPITObjects() { sSpitObjects.clear(); }
static void clearUnconfirmedSPITObjects();
static void setConfirmedFalse();
static SPITObject *getSPITObjectRemote(const string &id);
static const SPITObject &getSPITObjectRemoteRef(const string &id);
static void addSPITObjectRemote(unique_ptr<SPITObject> spitObject);
static bool removeSPITObjectRemote(const SPITObject &spitObject);
static bool removeSPITObjectRemote(const string & spitObjectID);
static int getSPITObjectRemoteCount() {
return sSpitObjectsRemote.size();
}
static SPITObject *getSPITObjectRemote(int p_Index);
static const SPITObject &getSPITObjectRemoteRef(int p_Index);
static void clearSPITObjectsRemote(){ sSpitObjectsRemote.clear(); }
static void eventProjectClosed();
static void clearUnconfirmedSPITObjects(SpitRepo<SPITObject> &repo);
static void setConfirmedFalse(SpitRepo<SPITObject> &repo);
static void eventProjectClosed(SpitRepo<SPITObject> &repo, SpitRepo<SPITObject> &remoteRepo);
SPITObject(string p_ID);
@@ -133,15 +103,15 @@ class SPITObject {
long long getDelay() const { return m_Delay; }
long long getDuration() const { return m_Duration; }
bool getProjectIndepended() const {
bool getProjectIndepended(SpitRepo<SPITType> &repo, SpitRepo<SPITType> &remoteRepo) const {
SPITType *ptrSPITType;
if (m_ServerRemote == false) {
ptrSPITType = SPITType::getSPITType(m_SPITTypeID);
ptrSPITType = repo.getEntry(m_SPITTypeID);
if (ptrSPITType == nullptr)
return true;
return ptrSPITType->getProjectIndepended();
} else {
ptrSPITType = SPITType::getSPITTypeRemote(m_SPITTypeID);
ptrSPITType = remoteRepo.getEntry(m_SPITTypeID);
if (ptrSPITType == nullptr)
return true;
return ptrSPITType->getProjectIndepended();
+3 -27
View File
@@ -16,40 +16,16 @@
#include "SPITSocket.h"
#include "SPIT_Message.h"
#include <string>
#include <unordered_map>
#include <memory>
#include "SpitRepo.hpp"
using namespace std;
class SPITTypeListener;
class SPITType {
private:
static unordered_map<string, unique_ptr<SPITType>> sSpitTypes;
static unordered_map<string, unique_ptr<SPITType>> sSpitTypesRemote;
public:
static SPITType *getSPITType(const string &id);
static const SPITType &getSPITTypeRef(const string &id);
static void addSPITType( unique_ptr<SPITType> spitType);
static bool removeSPITType(const SPITType &spitType);
static int getSPITTypeCount() { return sSpitTypes.size(); }
static SPITType *getSPITType(int p_Index);
static const SPITType &getSPITTypeRef(int p_Index);
static void clearSPITTypes() { sSpitTypes.clear(); }
static void setConfirmedFalse();
static SPITType *getSPITTypeRemote(const string &id);
static const SPITType &getSPITTypeRemoteRef(const string &id);
static void addSPITTypeRemote(unique_ptr<SPITType> spitType);
static bool removeSPITTypeRemote(const SPITType &spitType);
static int getSPITTypeRemoteCount() { return sSpitTypesRemote.size(); }
static SPITType *getSPITTypeRemote(int p_Index);
static const SPITType &getSPITTypeRemoteRef(int p_Index);
static void clearSPITTypesRemote() { sSpitTypesRemote.clear(); }
static void eventProjectClosed();
static void setConfirmedFalse(SpitRepo<SPITType> &repo);
static void eventProjectClosed(SpitRepo<SPITType> &repo, SpitRepo<SPITType> &remoteRepo);
SPITType(string p_ID);
SPITType(string p_ID, string p_GroupName, string p_Name, bool p_Fixed, bool p_ProjectIndepended, long long p_DefaultDelay, long long p_DefaultDuration);
+7
View File
@@ -20,6 +20,7 @@
#include "SPITSocket.h"
#include "SPITType.h"
#include "SPIT_Message.h"
#include "SpitRepo.hpp"
#include <memory>
#include <vector>
@@ -345,6 +346,12 @@ class SPIT_Client : I_SAD_Client {
void answerWatchdog() {
sAnswered = true;
}
SpitRepo<SPITObject> mSpitObjects;
SpitRepo<SPITObject> mRemoteSpitObjects;
SpitRepo<SPITType> mSpitTypes;
SpitRepo<SPITType> mRemoteSpitTypes;
};
#endif /* SPIT_CLIENT_H */
+64
View File
@@ -0,0 +1,64 @@
#pragma once
#include <memory>
#include <string>
#include <unordered_map>
template <typename T>
class SpitRepo {
public:
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);
}
bool removeEntry(const T &entry) {
return removeEntry(entry.getID());
}
bool removeEntry(const string &id) {
auto i = mEntries.find(id);
if (i == mEntries.end()) {
return false;
}
i = mEntries.erase(i);
return true;
}
int getEntryCount() {
return mEntries.size();
}
unordered_map<string, unique_ptr<T>> &getRepo() {
return mEntries;
}
void clearEntries() {
mEntries.clear();
}
private:
unordered_map<string, unique_ptr<T>> mEntries;
};