#pragma once #include #include "mixins/LazyLinkMixin.h" #include #include "links/OneToManyLink.h" #include "links/OneToOneLink.h" #include "Logger.h" class HierarchicalLinkMixin : public LazyLinkMixin> { public: HierarchicalLinkMixin() {} ~HierarchicalLinkMixin() override { Logger::get("Mixin").dbg("--- Destructor called for: HierarchicalLinkMixin"); } void linkChild(const MixinPtr& child) override { hierarchicalInit(child); LazyLinkMixin>::linkChild(child); } protected: void hierarchicalInit(const MixinPtr& child) { Logger::get("Mixin").dbg("--- hierarchicalInit called"); if (this->link_ && !this->link_->getChildren().empty()) return; // already have children, do nothing /* 1. Have no link_ — * 2. Has OneToOne with parent or not * 3. Has OneToOne with child */ LinkPtr newLink; if (typeid(*child) == typeid(*this)) { Logger::get("Mixin").dbg("--- Mutate to OneToMany"); newLink = std::make_shared>(this->getNode()); } else { Logger::get("Mixin").dbg("--- Mutate to OneToOne"); newLink = std::make_shared>(this->getNode()); } if (newLink && this->link_) newLink->setParent(this->link_->getParent()); this->link_ = newLink; } };