43 lines
1.0 KiB
C++
43 lines
1.0 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() {}
|
|
|
|
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;
|
|
} |