diff --git a/Sensor.cpp b/Sensor.cpp index daa996d..ff61c27 100644 --- a/Sensor.cpp +++ b/Sensor.cpp @@ -14,20 +14,27 @@ void Sensor::SetParent(Thing *parent) { } } -void Sensor::ConnectTo(Thing *thing) { - this->name = thing->name; - this->id = thing->id; +void Sensor::ConnectTo(Thing *oldThing) { + this->name = oldThing->name; + this->id = oldThing->id; - Thing *thingParent = thing->GetParent(); - thingParent->RemoveChild(thing); - thingParent->AddChild(this); - this->children = thing->GetChildren(); - this->position = thing->position; - this->orientation = thing->orientation; + Thing *oldParent = oldThing->GetParent(); + + oldParent->RemoveChild(oldThing); + oldParent->AddChild(this); + + for (int childIx = 0; childIx < oldThing->childCount; childIx++) { + Thing *child = oldThing->GetChild(childIx); + this->AddChild(child); + } + + this->position = oldThing->position; + this->orientation = oldThing->orientation; // delete (thing); // can we do this? } void Sensor::ConnectTo(Thing *rootThing, const char *thingName) { Thing *thing = rootThing->FindChild(thingName); - this->ConnectTo(thing); + if (thing != nullptr) + this->ConnectTo(thing); } diff --git a/Thing.cpp b/Thing.cpp index 8e2bd64..a673f41 100644 --- a/Thing.cpp +++ b/Thing.cpp @@ -65,12 +65,13 @@ void Thing::AddChild(Thing *child) { this->childCount++; } -Thing *Passer::RoboidControl::Thing::RemoveChild(Thing *child) { - Thing **newChildren = new Thing *[this->childCount - 1]; +Thing *Thing::RemoveChild(Thing *child) { + int newChildCount = this->childCount - 1; + Thing **newChildren = new Thing *[newChildCount]; unsigned char newChildIx = 0; for (unsigned char childIx = 0; childIx < this->childCount; childIx++) { if (this->children[childIx] != child) { - if (newChildIx == this->childCount - 1) { // We did not find the child + if (newChildIx == newChildCount) { // We did not find the child // stop copying and return nothing delete[] newChildren; return nullptr; @@ -83,7 +84,7 @@ Thing *Passer::RoboidControl::Thing::RemoveChild(Thing *child) { delete[] this->children; this->children = newChildren; - this->childCount--; + this->childCount = newChildCount; return child; } diff --git a/Thing.h b/Thing.h index dea7cd9..39641c6 100644 --- a/Thing.h +++ b/Thing.h @@ -117,6 +117,7 @@ protected: static const unsigned int SensorType = 0x4000; /// @brief Bitmap for Roboid type static const unsigned int RoboidType = 0x2000; + static const unsigned char CustomType = 0x80; /// @brief Basic Thing types enum class Type {