RoboidControl-cpp/CoreThing.cpp
2024-12-16 12:47:47 +01:00

46 lines
1.1 KiB
C++

#include "CoreThing.h"
#include <iostream>
CoreThing::CoreThing(unsigned char networkId, unsigned char thingType) {
this->type = thingType;
this->networkId = networkId;
this->Init();
int thingId = CoreThing::Add(this);
if (thingId < 0) {
std::cout << "ERROR: Thing store is full\n";
this->id = 0; // what to do when we cannot store any more things?
} else
this->id = thingId;
}
void CoreThing::Init() {}
void CoreThing::SetName(const char *name) { this->name = name; }
// All things
CoreThing *CoreThing::allThings[256] = {nullptr};
CoreThing *CoreThing::Get(unsigned char networkId, unsigned char thingId) {
for (unsigned char ix = 0; ix < 256; ix++) {
CoreThing *thing = allThings[ix];
if (thing == nullptr)
continue;
if (thing->networkId == networkId && thing->id == thingId)
return thing;
}
return nullptr;
}
int CoreThing::Add(CoreThing *newThing) {
for (unsigned char ix = 0; ix < 256; ix++) {
CoreThing *thing = allThings[ix];
if (thing == nullptr) {
allThings[ix] = newThing;
return ix;
}
}
return -1;
}