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
+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;
};