additional refactoring of object and type classes

This commit is contained in:
2025-04-03 21:31:08 +02:00
parent decaf80d1f
commit 1208bee756
5 changed files with 465 additions and 662 deletions
+143 -226
View File
@@ -15,197 +15,132 @@
#include "SPITObject.h"
#include "SPITSocket.h"
unordered_map<string, SPITObject *> SPITObject::s_SPITObjects;
unordered_map<string, SPITObject *> SPITObject::s_SPITObjectsRemote;
unordered_map<string, SPITObject *> SPITObject::sSpitObjects;
unordered_map<string, SPITObject *> SPITObject::sSpitObjectsRemote;
SPITObject *SPITObject::getSPITObject(string p_ID) {
{
auto i = s_SPITObjects.find(p_ID);
if (i == s_SPITObjects.end()) {
return NULL;
}
return i->second;
SPITObject *SPITObject::getSPITObject(const string &id) {
auto i = sSpitObjects.find(id);
if (i == sSpitObjects.end()) {
return nullptr;
}
return i->second;
}
/**
* Returns the reference of the SPITObject with ID == p_ID.<br>
* Throws an exception if no SPITObject is not found.<br>
* @param p_ID the ID
* @return reference of the SPITType with ID == p_ID
* 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(string p_ID) {
{
auto i = s_SPITObjects.find(p_ID);
if (i == s_SPITObjects.end()) {
throw "SPITObject no SPITObject with given id";
}
return *(i->second);
}
const SPITObject &SPITObject::getSPITObjectRef(const string &id) {
return *sSpitObjects.at(id);
}
SPITObject *SPITObject::getSPITObjectRemote(string p_ID) {
{
auto i = s_SPITObjectsRemote.find(p_ID);
if (i == s_SPITObjectsRemote.end()) {
return NULL;
}
return i->second;
SPITObject *SPITObject::getSPITObjectRemote(const string &id) {
auto i = sSpitObjectsRemote.find(id);
if (i == sSpitObjectsRemote.end()) {
return nullptr;
}
return i->second;
}
/**
* Returns the reference of the remote SPITObject with ID == p_ID.<br>
* Throws an exception if no SPITObject is not found.<br>
* @param p_ID the ID
* @return reference of the SPITType with ID == p_ID
* 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(string p_ID) {
{
auto i = s_SPITObjectsRemote.find(p_ID);
if (i == s_SPITObjectsRemote.end()) {
throw "Remote SPITObject no SPITObject with given id";
}
return *(i->second);
}
const SPITObject &SPITObject::getSPITObjectRemoteRef(const string &id) {
return *sSpitObjectsRemote.at(id);
}
bool SPITObject::addSPITObject(SPITObject *p_ptrSPITObject) {
if (p_ptrSPITObject == NULL)
void SPITObject::addSPITObject(SPITObject &spitObject) {
sSpitObjects[spitObject.getID()] = &spitObject;
}
void SPITObject::addSPITObjectRemote(SPITObject &spitObject) {
sSpitObjectsRemote[spitObject.getID()] = &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;
{
s_SPITObjects[p_ptrSPITObject->getID()] = p_ptrSPITObject;
return true;
}
i = sSpitObjects.erase(i);
return true;
}
bool SPITObject::addSPITObjectRemote(SPITObject *p_ptrSPITObject) {
if (p_ptrSPITObject == NULL)
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;
{
s_SPITObjectsRemote[p_ptrSPITObject->getID()] = p_ptrSPITObject;
return true;
}
}
bool SPITObject::removeSPITObject(SPITObject *p_ptrSPITObject) {
if (p_ptrSPITObject == NULL)
return false;
return removeSPITObject(p_ptrSPITObject->getID());
}
bool SPITObject::removeSPITObject(string p_SPITObjectID) {
{
auto i = s_SPITObjects.find(p_SPITObjectID);
if (i == s_SPITObjects.end()) {
return false;
}
i = s_SPITObjects.erase(i);
return true;
}
}
bool SPITObject::removeSPITObjectRemote(SPITObject *p_ptrSPITObject) {
if (p_ptrSPITObject == NULL)
return false;
return removeSPITObjectRemote(p_ptrSPITObject->getID());
}
bool SPITObject::removeSPITObjectRemote(string p_ptrSPITObject) {
{
auto i = s_SPITObjectsRemote.find(p_ptrSPITObject);
if (i == s_SPITObjectsRemote.end()) {
return false;
}
i = s_SPITObjectsRemote.erase(i);
return true;
}
i = sSpitObjectsRemote.erase(i);
return true;
}
int SPITObject::getSPITObjectCount() {
return s_SPITObjects.size();
}
int SPITObject::getSPITObjectRemoteCount() {
return s_SPITObjectsRemote.size();
}
SPITObject *SPITObject::getSPITObject(int p_Index) {
SPITObject *ptrSPITObject = NULL;
{
if (p_Index >= 0 && p_Index < s_SPITObjects.size()) {
unordered_map<string, SPITObject *>::iterator i = next(s_SPITObjects.begin(), p_Index);
ptrSPITObject = i->second;
return ptrSPITObject;
} else {
return NULL;
}
if (p_Index >= 0 && p_Index < sSpitObjects.size()) {
unordered_map<string, SPITObject *>::iterator i = next(sSpitObjects.begin(), p_Index);
return i->second;
}
return nullptr;
}
/**
* Returns a reference to the SPITObject at position p_Index.<br>
* Throws an exception if p_Index is out of range.<br>
* 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) {
SPITObject *ptrSPITObject = NULL;
{
if (p_Index >= 0 && p_Index < s_SPITObjects.size()) {
unordered_map<string, SPITObject *>::iterator i = next(s_SPITObjects.begin(), p_Index);
ptrSPITObject = i->second;
return *ptrSPITObject;
} else {
throw "SPITObject index out of range";
}
if (p_Index >= 0 && p_Index < sSpitObjects.size()) {
unordered_map<string, SPITObject *>::iterator i = next(sSpitObjects.begin(), p_Index);
return *i->second;
} else {
throw "SPITObject index out of range";
}
}
SPITObject *SPITObject::getSPITObjectRemote(int p_Index) {
SPITObject *ptrSPITObject = NULL;
{
if (p_Index >= 0 && p_Index < s_SPITObjectsRemote.size()) {
unordered_map<string, SPITObject *>::iterator i = next(s_SPITObjectsRemote.begin(), p_Index);
ptrSPITObject = i->second;
return ptrSPITObject;
} else {
return NULL;
}
if (p_Index >= 0 && p_Index < sSpitObjectsRemote.size()) {
unordered_map<string, SPITObject *>::iterator i = next(sSpitObjectsRemote.begin(), p_Index);
return i->second;
} else {
return nullptr;
}
}
/**
* Returns a reference to the remote SPITObject at position p_Index.<br>
* Throws an exception if p_Index is out of range.<br>
* 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) {
SPITObject *ptrSPITObject = NULL;
{
if (p_Index >= 0 && p_Index < s_SPITObjectsRemote.size()) {
unordered_map<string, SPITObject *>::iterator i = next(s_SPITObjectsRemote.begin(), p_Index);
ptrSPITObject = i->second;
return *ptrSPITObject;
} else {
throw "Remote SPITObject index out of range";
}
if (p_Index >= 0 && p_Index < sSpitObjectsRemote.size()) {
unordered_map<string, SPITObject *>::iterator i = next(sSpitObjectsRemote.begin(), p_Index);
return *i->second;
} else {
throw "Remote SPITObject index out of range";
}
}
void SPITObject::clearSPITObjects() {
SPITObject *ptrSPITObject;
{
for (auto i = s_SPITObjects.begin(); i != s_SPITObjects.end(); i++) {
ptrSPITObject = i->second;
i = s_SPITObjects.erase(i);
delete ptrSPITObject;
if (i == s_SPITObjects.end()) {
break;
}
}
for (auto i = sSpitObjects.begin(); i != sSpitObjects.end(); i++) {
ptrSPITObject = i->second;
i = sSpitObjects.erase(i);
delete ptrSPITObject;
}
}
void SPITObject::clearSPITObjectsRemote() {
SPITObject *ptrSPITObject;
{
for (auto i = s_SPITObjectsRemote.begin(); i != s_SPITObjectsRemote.end(); i++) {
ptrSPITObject = i->second;
i = s_SPITObjectsRemote.erase(i);
delete ptrSPITObject;
if (i == s_SPITObjectsRemote.end()) {
break;
}
}
for (auto i = sSpitObjectsRemote.begin(); i != sSpitObjectsRemote.end(); i++) {
ptrSPITObject = i->second;
i = sSpitObjectsRemote.erase(i);
delete ptrSPITObject;
}
}
@@ -213,82 +148,48 @@ void SPITObject::clearSPITObjectsRemote() {
* clears all unconfirmed SPITObjects not created by this SPIT_Client.
*/
void SPITObject::clearUnconfirmedSPITObjects() {
for (auto i = s_SPITObjects.begin(); i != s_SPITObjects.end(); i++) {
for (auto i = sSpitObjects.begin(); i != sSpitObjects.end(); i++) {
SPITObject *ptrSPITObject = i->second;
if (ptrSPITObject->getCreatedByClient() == false && ptrSPITObject->getConfirmed() == false) {
i = s_SPITObjects.erase(i);
i = sSpitObjects.erase(i);
delete ptrSPITObject;
}
if (i == s_SPITObjects.end()) {
break;
}
}
}
void SPITObject::eventProjectClosed() {
for (auto i = s_SPITObjects.begin(); i != s_SPITObjects.end(); i++) {
for (auto i = sSpitObjects.begin(); i != sSpitObjects.end(); i++) {
SPITObject *ptrSPITObject = i->second;
if (ptrSPITObject->getCreatedByClient() == true) {
continue;
}
i = s_SPITObjects.erase(i);
i = sSpitObjects.erase(i);
delete ptrSPITObject;
if (i == s_SPITObjects.end()) {
break;
}
}
// Remote
for (auto i = s_SPITObjectsRemote.begin(); i != s_SPITObjectsRemote.end(); i++) {
for (auto i = sSpitObjectsRemote.begin(); i != sSpitObjectsRemote.end(); i++) {
SPITObject *ptrSPITObject = i->second;
i = s_SPITObjectsRemote.erase(i);
i = sSpitObjectsRemote.erase(i);
delete ptrSPITObject;
if (i == s_SPITObjectsRemote.end()) {
break;
}
}
}
/**
* sets confirmed = false for all SPITObjects.<br>
* This should be called when the client has loaded a project.<br>
* sets confirmed = false for all SPITObjects.
* This should be called when the client has loaded a project.
*/
void SPITObject::setConfirmedFalse() {
SPITObject *ptrSPITObject;
{
for (auto i = s_SPITObjects.begin(); i != s_SPITObjects.end(); i++) {
ptrSPITObject = i->second;
ptrSPITObject->setConfirmed(false);
}
for (auto i = sSpitObjects.begin(); i != sSpitObjects.end(); i++) {
ptrSPITObject = i->second;
ptrSPITObject->setConfirmed(false);
}
}
SPITObject::SPITObject() {
initMembers();
}
void SPITObject::initMembers() {
m_ActionAssigned = false;
m_ActionAssignedType = false;
m_Confirmed = false;
m_Delay = 0;
m_Duration = 0;
m_Fixed = false;
m_CreatedByClient = false;
m_FrameFadeInLength = 0;
m_FrameFadeOutLength = 0;
m_FrameLength = 0;
m_FrameStart = 0;
m_Remark = "";
m_ID = createGUID();
m_Name = "";
m_SPITTypeID = "";
m_ServerParams = "";
m_ServerRemote = false;
m_Track = 0;
m_ValueFactor = 1000000; // Values are given in ppm -> 1000000 = 1/1;
m_ValueUse = 0x01;
}
SPITObject::~SPITObject() {}
bool SPITObject::setMessageOP(SPIT_Message::ReportSPITObject *p_ReportSPITObject) {
bool boolChangeAllowed;
bool boolChanged = false;
@@ -309,98 +210,114 @@ bool SPITObject::setMessageOP(SPIT_Message::ReportSPITObject *p_ReportSPITObject
}
if (m_Delay != p_ReportSPITObject->getDelay()) {
if (boolChangeAllowed == false)
throw new exception();
if (boolChangeAllowed == false) {
return false;
}
m_Delay = p_ReportSPITObject->getDelay();
boolChanged = true;
}
if (m_Duration != p_ReportSPITObject->getDuration()) {
if (boolChangeAllowed == false)
throw new exception();
if (boolChangeAllowed == false) {
return false;
}
m_Duration = p_ReportSPITObject->getDuration();
boolChanged = true;
}
if (m_Fixed != p_ReportSPITObject->getFixed()) {
if (boolChangeAllowed == false)
throw new exception();
if (boolChangeAllowed == false) {
return false;
}
m_Fixed = p_ReportSPITObject->getFixed();
boolChanged = true;
}
if (m_FrameFadeInLength != p_ReportSPITObject->getFrameFadeInLength()) {
if (boolChangeAllowed == false)
throw new exception();
if (boolChangeAllowed == false) {
return false;
}
m_FrameFadeInLength = p_ReportSPITObject->getFrameFadeInLength();
boolChanged = true;
}
if (m_FrameFadeOutLength != p_ReportSPITObject->getFrameFadeOutLength()) {
if (boolChangeAllowed == false)
throw new exception();
if (boolChangeAllowed == false) {
return false;
}
m_FrameFadeOutLength = p_ReportSPITObject->getFrameFadeOutLength();
boolChanged = true;
}
if (m_FrameLength != p_ReportSPITObject->getFrameLength()) {
if (boolChangeAllowed == false)
throw new exception();
if (boolChangeAllowed == false) {
return false;
}
m_FrameLength = p_ReportSPITObject->getFrameLength();
boolChanged = true;
}
if (m_FrameStart != p_ReportSPITObject->getFrameStart()) {
if (boolChangeAllowed == false)
throw new exception();
if (boolChangeAllowed == false) {
return false;
}
m_FrameStart = p_ReportSPITObject->getFrameStart();
boolChanged = true;
}
if (m_Remark != p_ReportSPITObject->getRemark()) {
if (boolChangeAllowed == false)
throw new exception();
if (boolChangeAllowed == false) {
return false;
}
m_Remark = p_ReportSPITObject->getRemark();
boolChanged = true;
}
if (m_ID != p_ReportSPITObject->getID()) {
if (boolChangeAllowed == false)
throw new exception();
if (boolChangeAllowed == false) {
return false;
}
m_ID = p_ReportSPITObject->getID();
boolChanged = true;
}
if (m_Name != p_ReportSPITObject->getName()) {
if (boolChangeAllowed == false)
throw new exception();
if (boolChangeAllowed == false) {
return false;
}
m_Name = p_ReportSPITObject->getName();
boolChanged = true;
}
if (m_SPITTypeID != p_ReportSPITObject->getSPITTypeID()) {
if (boolChangeAllowed == false)
throw new exception();
if (boolChangeAllowed == false) {
return false;
}
m_SPITTypeID = p_ReportSPITObject->getSPITTypeID();
boolChanged = true;
}
if (m_ServerParams != p_ReportSPITObject->getServerParams()) {
if (boolChangeAllowed == false)
throw new exception();
if (boolChangeAllowed == false) {
return false;
}
m_ServerParams = p_ReportSPITObject->getServerParams();
boolChanged = true;
}
if (m_ServerRemote != p_ReportSPITObject->getServerRemote()) {
if (boolChangeAllowed == false)
throw new exception();
if (boolChangeAllowed == false) {
return false;
}
m_ServerRemote = p_ReportSPITObject->getServerRemote();
boolChanged = true;
}
if (m_Track != p_ReportSPITObject->getTrack()) {
if (boolChangeAllowed == false)
throw new exception();
if (boolChangeAllowed == false) {
return false;
}
m_Track = p_ReportSPITObject->getTrack();
boolChanged = true;
}
if (m_ValueFactor != p_ReportSPITObject->getValueFactor()) {
if (boolChangeAllowed == false)
throw new exception();
if (boolChangeAllowed == false) {
return false;
}
m_ValueFactor = p_ReportSPITObject->getValueFactor();
boolChanged = true;
}
if (m_ValueUse != p_ReportSPITObject->getValueUse()) {
if (boolChangeAllowed == false)
throw new exception();
if (boolChangeAllowed == false) {
return false;
}
m_ValueUse = p_ReportSPITObject->getValueUse();
boolChanged = true;
}
@@ -431,8 +348,8 @@ void SPITObject::fillMessageOP(SPIT_Message::RemoveSPITObject *p_RemoveSPITObjec
p_RemoveSPITObject->setID(m_ID);
}
void SPITObject::setID(string p_ID) {
m_ID = p_ID;
void SPITObject::setID(const string &id) {
m_ID = id;
}
void SPITObject::setCreatedByClient(bool p_CreatedByClient) {
m_CreatedByClient = p_CreatedByClient;
@@ -486,8 +403,8 @@ bool SPITObject::setServerRemote(bool p_BelongstoServer) {
return true;
}
/**
* This is a virtual construct - ActionAssignedType is not implemented in the SPITProtocol.<br>
* This is used when an action is associated with a SPITType. <br>
* This is a virtual construct - ActionAssignedType is not implemented in the SPITProtocol.
* This is used when an action is associated with a SPITType.
* Then this action is done for every SPITObject with the SPITType.
* @return true = an action is assigned for the SPITType.
*/