make nodes to provide nodes instead of mixins

This commit is contained in:
Сергей Маринкевич
2025-07-25 15:02:15 +07:00
parent c228caaa45
commit e5dc6f7502
11 changed files with 76 additions and 70 deletions
+20 -11
View File
@@ -3,37 +3,46 @@
#include <memory>
#include "ifaces/ILinkMixin.h"
#include "ifaces/INode.h"
#include "Logger.h"
class BaseLinkMixin : public virtual ILinkMixin,
public std::enable_shared_from_this<ILinkMixin> {
class BaseLinkMixin : public virtual ILinkMixin<INode>,
public std::enable_shared_from_this<ILinkMixin<INode>> {
using ElemPtr = std::shared_ptr<INode>;
public:
void linkChild(const MixinPtr& child) override {
~BaseLinkMixin() override {
Logger::get("Mixin").dbg("--- Destructor called for: BaseLinkMixin");
}
void linkChild(const ElemPtr& child) override {
getLink()->addChild(child);
LinkPtr childLink = child->getLink();
auto childLink = child->getLink();
childLink->setParent(getNode());
}
void unlinkParent() override {
LinkPtr link = getLink();
auto link = getLink();
MixinPtr parent = link->getParent();
ElemPtr parent = link->getParent();
if (!parent)
throw std::logic_error("Have no parent!");
LinkPtr parentLink = parent->getLink();
auto parentLink = parent->getLink();
parentLink->removeChild(getNode());
getLink()->setParent(nullptr);
}
~BaseLinkMixin() override {
Logger::get("Mixin").dbg("--- Destructor called for: BaseLinkMixin");
const std::vector<ElemPtr>& children() override {
auto link = getLink();
return link->getChildren();
}
protected:
MixinPtr getNode() {
return shared_from_this();
ElemPtr getNode() {
return std::dynamic_pointer_cast<INode>(shared_from_this());
}
};
+11 -15
View File
@@ -6,39 +6,35 @@
#include "links/OneToOneLink.h"
#include "Logger.h"
class HierarchicalLinkMixin : public LazyLinkMixin<OneToOneLink<ILinkMixin>> {
public:
HierarchicalLinkMixin() {}
template <class TElem>
class HierarchicalLinkMixin : public LazyLinkMixin<OneToOneLink<TElem>> {
using LinkPtr = std::shared_ptr<ILink<TElem>>;
using ElemPtr = std::shared_ptr<TElem>;
public:
~HierarchicalLinkMixin() override {
Logger::get("Mixin").dbg("--- Destructor called for: HierarchicalLinkMixin");
}
void linkChild(const MixinPtr& child) override {
void linkChild(const ElemPtr& child) override {
hierarchicalInit(child);
LazyLinkMixin<OneToOneLink<ILinkMixin>>::linkChild(child);
LazyLinkMixin<OneToOneLink<TElem>>::linkChild(child);
}
protected:
void hierarchicalInit(const MixinPtr& child) {
void hierarchicalInit(const ElemPtr& 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
*/
return;
LinkPtr newLink;
if (typeid(*child) == typeid(*this)) {
Logger::get("Mixin").dbg("--- Mutate to OneToMany");
newLink = std::make_shared<OneToManyLink<ILinkMixin>>(this->getNode());
newLink = std::make_shared<OneToManyLink<TElem>>(this->getNode());
} else {
Logger::get("Mixin").dbg("--- Mutate to OneToOne");
newLink = std::make_shared<OneToOneLink<ILinkMixin>>(this->getNode());
newLink = std::make_shared<OneToOneLink<TElem>>(this->getNode());
}
if (newLink && this->link_)