You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
poc-links/include/mixins/HierarchicalLinkMixin.h

50 lines
1.3 KiB
C++

#pragma once
#include <iostream>
#include "mixins/LazyLinkMixin.h"
#include <memory>
#include "links/OneToManyLink.h"
#include "links/OneToOneLink.h"
#include "Logger.h"
class HierarchicalLinkMixin : public LazyLinkMixin<OneToOneLink<ILinkMixin>> {
public:
HierarchicalLinkMixin() {}
~HierarchicalLinkMixin() override {
Logger::get("Mixin").dbg("--- Destructor called for: HierarchicalLinkMixin");
}
void linkChild(const MixinPtr& child) override {
hierarchicalInit(child);
LazyLinkMixin<OneToOneLink<ILinkMixin>>::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<OneToManyLink<ILinkMixin>>(this->getNode());
} else {
Logger::get("Mixin").dbg("--- Mutate to OneToOne");
newLink = std::make_shared<OneToOneLink<ILinkMixin>>(this->getNode());
}
if (newLink && this->link_)
newLink->setParent(this->link_->getParent());
this->link_ = newLink;
}
};