add hierarchical node
This commit is contained in:
@@ -3,15 +3,16 @@
|
||||
#include <memory>
|
||||
|
||||
#include "ifaces/ILinkMixin.h"
|
||||
#include "Logger.h"
|
||||
|
||||
class BaseLinkMixin : public virtual ILinkMixin,
|
||||
public std::enable_shared_from_this<ILinkMixin> {
|
||||
public:
|
||||
void linkChild(const MixinPtr& child) override {
|
||||
getLink()->addChild(child);
|
||||
|
||||
LinkPtr childLink = child->getLink();
|
||||
childLink->setParent(getNode());
|
||||
|
||||
getLink()->addChild(child);
|
||||
}
|
||||
|
||||
void unlinkParent() override {
|
||||
@@ -28,7 +29,7 @@ public:
|
||||
}
|
||||
|
||||
~BaseLinkMixin() override {
|
||||
std::cout << "--- Destructor called for: " << "BaseLinkMixin" << "\n";
|
||||
Logger::get("Mixin").dbg("--- Destructor called for: BaseLinkMixin");
|
||||
}
|
||||
|
||||
protected:
|
||||
|
||||
@@ -0,0 +1,49 @@
|
||||
#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;
|
||||
}
|
||||
};
|
||||
@@ -2,6 +2,7 @@
|
||||
#include <iostream>
|
||||
#include "mixins/BaseLinkMixin.h"
|
||||
#include <memory>
|
||||
#include "Logger.h"
|
||||
|
||||
template <class TLink>
|
||||
class LazyLinkMixin : public BaseLinkMixin {
|
||||
@@ -22,7 +23,7 @@ public:
|
||||
LazyLinkMixin() {}
|
||||
|
||||
~LazyLinkMixin() override {
|
||||
std::cout << "--- Destructor called for: " << "LazyLinkMixin" << "\n";
|
||||
Logger::get("Mixin").dbg("--- Destructor called for: LazyLinkMixin");
|
||||
}
|
||||
|
||||
protected:
|
||||
|
||||
Reference in New Issue
Block a user