make nodes to provide nodes instead of mixins
This commit is contained in:
@@ -1,5 +0,0 @@
|
||||
#pragma once
|
||||
class INode;
|
||||
class ILink;
|
||||
using NodePtr = std::shared_ptr<INode>;
|
||||
using LinkPtr = std::shared_ptr<ILink>;
|
||||
@@ -6,12 +6,14 @@
|
||||
template <class TElem>
|
||||
class ILink {
|
||||
public:
|
||||
using ElemPtr = std::shared_ptr<TElem>;
|
||||
|
||||
virtual ~ILink() = default;
|
||||
virtual std::shared_ptr<TElem> getNode() const = 0;
|
||||
virtual std::shared_ptr<TElem> getParent() const = 0;
|
||||
virtual void setParent(const std::shared_ptr<TElem>& parent) = 0;
|
||||
virtual const std::vector<std::shared_ptr<TElem>>& getChildren() const = 0;
|
||||
virtual void addChild(const std::shared_ptr<TElem>& child) = 0;
|
||||
virtual void removeChild(const std::shared_ptr<TElem>& child) = 0;
|
||||
virtual void replaceChild(const std::shared_ptr<TElem>& oldChild, const std::shared_ptr<TElem>& newChild) = 0;
|
||||
virtual ElemPtr getNode() const = 0;
|
||||
virtual ElemPtr getParent() const = 0;
|
||||
virtual void setParent(const ElemPtr& parent) = 0;
|
||||
virtual const std::vector<ElemPtr>& getChildren() const = 0;
|
||||
virtual void addChild(const ElemPtr& child) = 0;
|
||||
virtual void removeChild(const ElemPtr& child) = 0;
|
||||
virtual void replaceChild(const ElemPtr& oldChild, const ElemPtr& newChild) = 0;
|
||||
};
|
||||
|
||||
@@ -2,15 +2,15 @@
|
||||
#include <memory>
|
||||
#include "ifaces/ILink.h"
|
||||
|
||||
class ILinkMixin;
|
||||
|
||||
using MixinPtr = std::shared_ptr<ILinkMixin>;
|
||||
using LinkPtr = std::shared_ptr<ILink<ILinkMixin>>;
|
||||
|
||||
template <class TElem>
|
||||
class ILinkMixin {
|
||||
public:
|
||||
using LinkPtr = std::shared_ptr<ILink<TElem>>;
|
||||
using ElemPtr = std::shared_ptr<TElem>;
|
||||
|
||||
virtual ~ILinkMixin() = default;
|
||||
virtual void linkChild(const MixinPtr& child) = 0;
|
||||
virtual void linkChild(const ElemPtr& child) = 0;
|
||||
virtual void unlinkParent() = 0;
|
||||
virtual const std::vector<ElemPtr>& children() = 0;
|
||||
virtual LinkPtr getLink() = 0;
|
||||
};
|
||||
|
||||
@@ -6,7 +6,7 @@ class INode;
|
||||
|
||||
using NodePtr = std::shared_ptr<INode>;
|
||||
|
||||
class INode : public virtual ILinkMixin {
|
||||
class INode : public virtual ILinkMixin<INode> {
|
||||
public:
|
||||
~INode() = default;
|
||||
virtual const std::string& name() const = 0;
|
||||
|
||||
@@ -8,21 +8,23 @@
|
||||
template <class TElem>
|
||||
class BaseLink : public ILink<TElem> {
|
||||
public:
|
||||
BaseLink(std::shared_ptr<TElem> node) : owner_node_(node) {}
|
||||
std::shared_ptr<TElem> getNode() const override { return owner_node_.lock(); }
|
||||
std::shared_ptr<TElem> getParent() const override { return parent_.lock(); }
|
||||
void setParent(const std::shared_ptr<TElem>& parent) override { parent_ = parent; }
|
||||
const std::vector<std::shared_ptr<TElem>>& getChildren() const override { return children_; }
|
||||
using ElemPtr = std::shared_ptr<TElem>;
|
||||
|
||||
void addChild(const std::shared_ptr<TElem>& child) override {
|
||||
BaseLink(ElemPtr node) : owner_node_(node) {}
|
||||
ElemPtr getNode() const override { return owner_node_.lock(); }
|
||||
ElemPtr getParent() const override { return parent_.lock(); }
|
||||
void setParent(const ElemPtr& parent) override { parent_ = parent; }
|
||||
const std::vector<ElemPtr>& getChildren() const override { return children_; }
|
||||
|
||||
void addChild(const ElemPtr& child) override {
|
||||
this->children_.push_back(child);
|
||||
}
|
||||
|
||||
void removeChild(const std::shared_ptr<TElem>& child) override {
|
||||
void removeChild(const ElemPtr& child) override {
|
||||
children_.erase(std::remove(children_.begin(), children_.end(), child), children_.end());
|
||||
}
|
||||
|
||||
void replaceChild(const std::shared_ptr<TElem>& oldChild, const std::shared_ptr<TElem>& newChild) override {
|
||||
void replaceChild(const ElemPtr& oldChild, const ElemPtr& newChild) override {
|
||||
auto it = std::find(children_.begin(), children_.end(), oldChild);
|
||||
if (it != children_.end()) {
|
||||
*it = newChild;
|
||||
@@ -33,7 +35,7 @@ public:
|
||||
Logger::get("Link").dbg("--- Destructor called for: BaseLink");
|
||||
}
|
||||
protected:
|
||||
std::vector<std::shared_ptr<TElem>> children_;
|
||||
std::vector<ElemPtr> children_;
|
||||
std::weak_ptr<TElem> owner_node_;
|
||||
std::weak_ptr<TElem> parent_;
|
||||
};
|
||||
|
||||
@@ -5,10 +5,14 @@
|
||||
template <class TElem>
|
||||
class OneToOneLink : public BaseLink<TElem> {
|
||||
public:
|
||||
OneToOneLink(std::shared_ptr<TElem> e) : BaseLink<TElem>(e) {}
|
||||
void addChild(const std::shared_ptr<TElem>& child) override {
|
||||
using ElemPtr = std::shared_ptr<TElem>;
|
||||
|
||||
OneToOneLink(ElemPtr e) : BaseLink<TElem>(e) {}
|
||||
|
||||
void addChild(const ElemPtr& child) override {
|
||||
if (!this->children_.empty())
|
||||
throw std::logic_error("OneToOneLink cannot have more than one child");
|
||||
throw std::logic_error("Too many children");
|
||||
|
||||
BaseLink<TElem>::addChild(child);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -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());
|
||||
}
|
||||
};
|
||||
|
||||
@@ -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_)
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
#include "Logger.h"
|
||||
|
||||
class ComplexNode : public BaseNode,
|
||||
virtual public HierarchicalLinkMixin {
|
||||
virtual public HierarchicalLinkMixin<INode> {
|
||||
public:
|
||||
~ComplexNode() {
|
||||
Logger::get("Node").dbg(std::string("--- Complex destructor called for: ") + name_);
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
#include "Logger.h"
|
||||
|
||||
class SimpleNode : public BaseNode,
|
||||
virtual public LazyLinkMixin<OneToOneLink<ILinkMixin>> {
|
||||
virtual public LazyLinkMixin<OneToOneLink<INode>> {
|
||||
public:
|
||||
~SimpleNode() {
|
||||
Logger::get("Node").dbg(std::string("--- Simple destructor called for: ") + name_);
|
||||
|
||||
+9
-11
@@ -3,23 +3,22 @@
|
||||
#include <iostream>
|
||||
#include "Logger.h"
|
||||
|
||||
void printTree(const NodePtr& startNode, int indent = 0) {
|
||||
if (!startNode) {
|
||||
std::cout << "No node" << "\n";
|
||||
void printTree(const NodePtr& node, int indent = 0) {
|
||||
if (!node) {
|
||||
Logger::get("MAIN").err("No node");
|
||||
return;
|
||||
}
|
||||
|
||||
for (int i = 0; i < indent; ++i) std::cout << " ";
|
||||
std::cout << startNode->name() << "\n";
|
||||
LinkPtr nodeLink = startNode->getLink();
|
||||
for (const auto& child : nodeLink->getChildren()) {
|
||||
auto childNode = std::dynamic_pointer_cast<INode>(child);
|
||||
printTree(childNode, indent + 1);
|
||||
}
|
||||
std::cout << node->name() << "\n";
|
||||
|
||||
for (const auto& child : node->children())
|
||||
printTree(child, indent + 1);
|
||||
}
|
||||
|
||||
int main() {
|
||||
Logger::setMinSeverity("MAIN", Logger::Severity::Debug);
|
||||
//Logger::suppressCategory("Node");
|
||||
Logger::suppressCategory("Node");
|
||||
Logger::suppressCategory("Link");
|
||||
Logger::suppressCategory("Mixin");
|
||||
auto& logger = Logger::get("MAIN");
|
||||
@@ -35,7 +34,6 @@ int main() {
|
||||
printTree(root);
|
||||
std::cout << "\n";
|
||||
|
||||
|
||||
{
|
||||
auto child2 = std::make_shared<ComplexNode>("ComplexChild2");
|
||||
root->linkChild(child2);
|
||||
|
||||
Reference in New Issue
Block a user