refactor: concentrating repo functions into repo class
This commit is contained in:
@@ -16,147 +16,39 @@
|
||||
#include "SPITSocket.h"
|
||||
#include "esp_log.h"
|
||||
|
||||
unordered_map<string, unique_ptr<SPITObject>> SPITObject::sSpitObjects;
|
||||
unordered_map<string, unique_ptr<SPITObject>> SPITObject::sSpitObjectsRemote;
|
||||
|
||||
SPITObject *SPITObject::getSPITObject(const string &id) {
|
||||
auto i = sSpitObjects.find(id);
|
||||
if (i == sSpitObjects.end()) {
|
||||
return nullptr;
|
||||
}
|
||||
return i->second.get();
|
||||
}
|
||||
/**
|
||||
* Returns the reference of the SPITObject with ID == id.
|
||||
* Throws an exception if no SPITObject is not found.
|
||||
* @param id the ID
|
||||
* @return reference of the SPITType with ID == id
|
||||
*/
|
||||
const SPITObject &SPITObject::getSPITObjectRef(const string &id) {
|
||||
return *sSpitObjects.at(id);
|
||||
}
|
||||
|
||||
SPITObject *SPITObject::getSPITObjectRemote(const string &id) {
|
||||
auto i = sSpitObjectsRemote.find(id);
|
||||
if (i == sSpitObjectsRemote.end()) {
|
||||
return nullptr;
|
||||
}
|
||||
return i->second.get();
|
||||
}
|
||||
/**
|
||||
* Returns the reference of the remote SPITObject with ID == id.
|
||||
* Throws an exception if no SPITObject is not found.
|
||||
* @param id the ID
|
||||
* @return reference of the SPITType with ID == id
|
||||
*/
|
||||
const SPITObject &SPITObject::getSPITObjectRemoteRef(const string &id) {
|
||||
return *sSpitObjectsRemote.at(id);
|
||||
}
|
||||
|
||||
void SPITObject::addSPITObject(unique_ptr<SPITObject> spitObject) {
|
||||
ESP_LOGD("OBJ", "adding spitObject %s/%s", spitObject->getID().c_str(), spitObject->getName().c_str());
|
||||
sSpitObjects[spitObject->getID()] = move(spitObject);
|
||||
}
|
||||
void SPITObject::addSPITObjectRemote(unique_ptr<SPITObject> spitObject) {
|
||||
ESP_LOGD("OBJ", "adding spitObject %s/%s", spitObject->getID().c_str(), spitObject->getName().c_str());
|
||||
sSpitObjectsRemote[spitObject->getID()] = move(spitObject);
|
||||
}
|
||||
bool SPITObject::removeSPITObject(const SPITObject &spitObject) {
|
||||
return removeSPITObject(spitObject.getID());
|
||||
}
|
||||
bool SPITObject::removeSPITObject(const string &spitObjectID) {
|
||||
auto i = sSpitObjects.find(spitObjectID);
|
||||
if (i == sSpitObjects.end()) {
|
||||
return false;
|
||||
}
|
||||
i = sSpitObjects.erase(i);
|
||||
return true;
|
||||
}
|
||||
bool SPITObject::removeSPITObjectRemote(const SPITObject &spitObject) {
|
||||
return removeSPITObjectRemote(spitObject.getID());
|
||||
}
|
||||
bool SPITObject::removeSPITObjectRemote(const string &spitObjectID) {
|
||||
auto i = sSpitObjectsRemote.find(spitObjectID);
|
||||
if (i == sSpitObjectsRemote.end()) {
|
||||
return false;
|
||||
}
|
||||
i = sSpitObjectsRemote.erase(i);
|
||||
return true;
|
||||
}
|
||||
|
||||
SPITObject *SPITObject::getSPITObject(int p_Index) {
|
||||
if (p_Index >= 0 && p_Index < sSpitObjects.size()) {
|
||||
auto i = next(sSpitObjects.begin(), p_Index);
|
||||
return i->second.get();
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
/**
|
||||
* Returns a reference to the SPITObject at position p_Index.
|
||||
* Throws an exception if p_Index is out of range.
|
||||
* @param p_Index position
|
||||
* @return the reference of the SPITObject at position.
|
||||
*/
|
||||
const SPITObject &SPITObject::getSPITObjectRef(int p_Index) {
|
||||
if (p_Index >= 0 && p_Index < sSpitObjects.size()) {
|
||||
auto i = next(sSpitObjects.begin(), p_Index);
|
||||
return *i->second;
|
||||
} else {
|
||||
throw "SPITObject index out of range";
|
||||
}
|
||||
}
|
||||
SPITObject *SPITObject::getSPITObjectRemote(int p_Index) {
|
||||
if (p_Index >= 0 && p_Index < sSpitObjectsRemote.size()) {
|
||||
auto i = next(sSpitObjectsRemote.begin(), p_Index);
|
||||
return i->second.get();
|
||||
} else {
|
||||
return nullptr;
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Returns a reference to the remote SPITObject at position p_Index.
|
||||
* Throws an exception if p_Index is out of range.
|
||||
* @param p_Index position
|
||||
* @return the reference of the SPITObject at position.
|
||||
*/
|
||||
const SPITObject &SPITObject::getSPITObjectRemoteRef(int p_Index) {
|
||||
if (p_Index >= 0 && p_Index < sSpitObjectsRemote.size()) {
|
||||
auto i = next(sSpitObjectsRemote.begin(), p_Index);
|
||||
return *i->second;
|
||||
} else {
|
||||
throw "Remote SPITObject index out of range";
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* clears all unconfirmed SPITObjects not created by this SPIT_Client.
|
||||
*/
|
||||
void SPITObject::clearUnconfirmedSPITObjects() {
|
||||
for (auto i = sSpitObjects.begin(); i != sSpitObjects.end(); i++) {
|
||||
void SPITObject::clearUnconfirmedSPITObjects(SpitRepo<SPITObject>& repo) {
|
||||
auto& entries = repo.getRepo();
|
||||
for (auto i = entries.begin(); i != entries.end(); i++) {
|
||||
SPITObject *ptrSPITObject = i->second.get();
|
||||
if (ptrSPITObject->getCreatedByClient() == false && ptrSPITObject->getConfirmed() == false) {
|
||||
i = sSpitObjects.erase(i);
|
||||
i = entries.erase(i);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void SPITObject::eventProjectClosed() {
|
||||
for (auto i = sSpitObjects.begin(); i != sSpitObjects.end(); i++) {
|
||||
void SPITObject::eventProjectClosed(SpitRepo<SPITObject>& repo, SpitRepo<SPITObject>& remoteRepo) {
|
||||
auto& entries = repo.getRepo();
|
||||
for (auto i = entries.begin(); i != entries.end(); i++) {
|
||||
SPITObject *ptrSPITObject = i->second.get();
|
||||
if (ptrSPITObject->getCreatedByClient() == true) {
|
||||
continue;
|
||||
}
|
||||
i = sSpitObjects.erase(i);
|
||||
i = entries.erase(i);
|
||||
}
|
||||
// Remote
|
||||
sSpitObjectsRemote.clear();
|
||||
remoteRepo.clearEntries();
|
||||
}
|
||||
/**
|
||||
* sets confirmed = false for all SPITObjects.
|
||||
* This should be called when the client has loaded a project.
|
||||
*/
|
||||
void SPITObject::setConfirmedFalse() {
|
||||
for (auto i = sSpitObjects.begin(); i != sSpitObjects.end(); i++) {
|
||||
void SPITObject::setConfirmedFalse(SpitRepo<SPITObject>& repo) {
|
||||
auto& entries = repo.getRepo();
|
||||
for (auto i = entries.begin(); i != entries.end(); i++) {
|
||||
i->second->setConfirmed(false);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user