diff --git a/src/datamodel/instance.cpp b/src/datamodel/instance.cpp new file mode 100644 index 0000000..7b5b5ea --- /dev/null +++ b/src/datamodel/instance.cpp @@ -0,0 +1,46 @@ +#include "instance.h" +#include +#include +#include +#include + +std::optional> Instance::GetParent() { + if (!this->Parent.has_value() || this->Parent.value().expired()) + return std::nullopt; + return this->Parent.value().lock(); +} + +void Instance::SetParent(std::optional> newParent) { + // Remove this instance from the existing parent + if (this->Parent.has_value() && !this->Parent.value().expired()) { + std::shared_ptr parent = this->Parent.value().lock(); + parent->children.erase(std::remove_if(parent->children.begin(), parent->children.end(), [this](std::shared_ptr ptr) { + return ptr.get() == this; + })); + } + + // Add self to + if (newParent.has_value()) { + newParent.value()->children.push_back(this->shared_from_this()); + } + + this->Parent = newParent; +} + +std::vector> Instance::GetChildren() { + return this->children; +} + +Instance Instance::CloneInternal() { + // TODO: Implement refs + + Instance clone = *this; + clone.children = std::vector>(); + clone.children.reserve(this->children.size()); + + for (std::shared_ptr child : this->children) { + clone.children.push_back(std::make_shared(child->CloneInternal())); + } + + return clone; +} \ No newline at end of file diff --git a/src/datamodel/instance.h b/src/datamodel/instance.h new file mode 100644 index 0000000..7175f85 --- /dev/null +++ b/src/datamodel/instance.h @@ -0,0 +1,20 @@ +#pragma once +#include +#include +#include +#include + +// The base class for all objects in the data model +class Instance : std::enable_shared_from_this { +private: + std::optional> Parent; + std::vector> children; +public: + std::string Name; + std::optional> GetParent(); + void SetParent(std::optional> newParent); + std::vector> GetChildren(); + + virtual void Init(); + Instance CloneInternal(); +}; \ No newline at end of file