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>
|
template <class TElem>
|
||||||
class ILink {
|
class ILink {
|
||||||
public:
|
public:
|
||||||
|
using ElemPtr = std::shared_ptr<TElem>;
|
||||||
|
|
||||||
virtual ~ILink() = default;
|
virtual ~ILink() = default;
|
||||||
virtual std::shared_ptr<TElem> getNode() const = 0;
|
virtual ElemPtr getNode() const = 0;
|
||||||
virtual std::shared_ptr<TElem> getParent() const = 0;
|
virtual ElemPtr getParent() const = 0;
|
||||||
virtual void setParent(const std::shared_ptr<TElem>& parent) = 0;
|
virtual void setParent(const ElemPtr& parent) = 0;
|
||||||
virtual const std::vector<std::shared_ptr<TElem>>& getChildren() const = 0;
|
virtual const std::vector<ElemPtr>& getChildren() const = 0;
|
||||||
virtual void addChild(const std::shared_ptr<TElem>& child) = 0;
|
virtual void addChild(const ElemPtr& child) = 0;
|
||||||
virtual void removeChild(const std::shared_ptr<TElem>& child) = 0;
|
virtual void removeChild(const ElemPtr& child) = 0;
|
||||||
virtual void replaceChild(const std::shared_ptr<TElem>& oldChild, const std::shared_ptr<TElem>& newChild) = 0;
|
virtual void replaceChild(const ElemPtr& oldChild, const ElemPtr& newChild) = 0;
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -2,15 +2,15 @@
|
|||||||
#include <memory>
|
#include <memory>
|
||||||
#include "ifaces/ILink.h"
|
#include "ifaces/ILink.h"
|
||||||
|
|
||||||
class ILinkMixin;
|
template <class TElem>
|
||||||
|
|
||||||
using MixinPtr = std::shared_ptr<ILinkMixin>;
|
|
||||||
using LinkPtr = std::shared_ptr<ILink<ILinkMixin>>;
|
|
||||||
|
|
||||||
class ILinkMixin {
|
class ILinkMixin {
|
||||||
public:
|
public:
|
||||||
|
using LinkPtr = std::shared_ptr<ILink<TElem>>;
|
||||||
|
using ElemPtr = std::shared_ptr<TElem>;
|
||||||
|
|
||||||
virtual ~ILinkMixin() = default;
|
virtual ~ILinkMixin() = default;
|
||||||
virtual void linkChild(const MixinPtr& child) = 0;
|
virtual void linkChild(const ElemPtr& child) = 0;
|
||||||
virtual void unlinkParent() = 0;
|
virtual void unlinkParent() = 0;
|
||||||
|
virtual const std::vector<ElemPtr>& children() = 0;
|
||||||
virtual LinkPtr getLink() = 0;
|
virtual LinkPtr getLink() = 0;
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ class INode;
|
|||||||
|
|
||||||
using NodePtr = std::shared_ptr<INode>;
|
using NodePtr = std::shared_ptr<INode>;
|
||||||
|
|
||||||
class INode : public virtual ILinkMixin {
|
class INode : public virtual ILinkMixin<INode> {
|
||||||
public:
|
public:
|
||||||
~INode() = default;
|
~INode() = default;
|
||||||
virtual const std::string& name() const = 0;
|
virtual const std::string& name() const = 0;
|
||||||
|
|||||||
@@ -8,21 +8,23 @@
|
|||||||
template <class TElem>
|
template <class TElem>
|
||||||
class BaseLink : public ILink<TElem> {
|
class BaseLink : public ILink<TElem> {
|
||||||
public:
|
public:
|
||||||
BaseLink(std::shared_ptr<TElem> node) : owner_node_(node) {}
|
using ElemPtr = std::shared_ptr<TElem>;
|
||||||
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_; }
|
|
||||||
|
|
||||||
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);
|
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());
|
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);
|
auto it = std::find(children_.begin(), children_.end(), oldChild);
|
||||||
if (it != children_.end()) {
|
if (it != children_.end()) {
|
||||||
*it = newChild;
|
*it = newChild;
|
||||||
@@ -33,7 +35,7 @@ public:
|
|||||||
Logger::get("Link").dbg("--- Destructor called for: BaseLink");
|
Logger::get("Link").dbg("--- Destructor called for: BaseLink");
|
||||||
}
|
}
|
||||||
protected:
|
protected:
|
||||||
std::vector<std::shared_ptr<TElem>> children_;
|
std::vector<ElemPtr> children_;
|
||||||
std::weak_ptr<TElem> owner_node_;
|
std::weak_ptr<TElem> owner_node_;
|
||||||
std::weak_ptr<TElem> parent_;
|
std::weak_ptr<TElem> parent_;
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -5,10 +5,14 @@
|
|||||||
template <class TElem>
|
template <class TElem>
|
||||||
class OneToOneLink : public BaseLink<TElem> {
|
class OneToOneLink : public BaseLink<TElem> {
|
||||||
public:
|
public:
|
||||||
OneToOneLink(std::shared_ptr<TElem> e) : BaseLink<TElem>(e) {}
|
using ElemPtr = std::shared_ptr<TElem>;
|
||||||
void addChild(const std::shared_ptr<TElem>& child) override {
|
|
||||||
|
OneToOneLink(ElemPtr e) : BaseLink<TElem>(e) {}
|
||||||
|
|
||||||
|
void addChild(const ElemPtr& child) override {
|
||||||
if (!this->children_.empty())
|
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);
|
BaseLink<TElem>::addChild(child);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -3,37 +3,46 @@
|
|||||||
#include <memory>
|
#include <memory>
|
||||||
|
|
||||||
#include "ifaces/ILinkMixin.h"
|
#include "ifaces/ILinkMixin.h"
|
||||||
|
#include "ifaces/INode.h"
|
||||||
#include "Logger.h"
|
#include "Logger.h"
|
||||||
|
|
||||||
class BaseLinkMixin : public virtual ILinkMixin,
|
class BaseLinkMixin : public virtual ILinkMixin<INode>,
|
||||||
public std::enable_shared_from_this<ILinkMixin> {
|
public std::enable_shared_from_this<ILinkMixin<INode>> {
|
||||||
|
using ElemPtr = std::shared_ptr<INode>;
|
||||||
|
|
||||||
public:
|
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);
|
getLink()->addChild(child);
|
||||||
|
|
||||||
LinkPtr childLink = child->getLink();
|
auto childLink = child->getLink();
|
||||||
childLink->setParent(getNode());
|
childLink->setParent(getNode());
|
||||||
}
|
}
|
||||||
|
|
||||||
void unlinkParent() override {
|
void unlinkParent() override {
|
||||||
LinkPtr link = getLink();
|
auto link = getLink();
|
||||||
|
|
||||||
MixinPtr parent = link->getParent();
|
ElemPtr parent = link->getParent();
|
||||||
if (!parent)
|
if (!parent)
|
||||||
throw std::logic_error("Have no parent!");
|
throw std::logic_error("Have no parent!");
|
||||||
|
|
||||||
LinkPtr parentLink = parent->getLink();
|
auto parentLink = parent->getLink();
|
||||||
|
|
||||||
parentLink->removeChild(getNode());
|
parentLink->removeChild(getNode());
|
||||||
getLink()->setParent(nullptr);
|
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:
|
protected:
|
||||||
MixinPtr getNode() {
|
ElemPtr getNode() {
|
||||||
return shared_from_this();
|
return std::dynamic_pointer_cast<INode>(shared_from_this());
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -6,39 +6,35 @@
|
|||||||
#include "links/OneToOneLink.h"
|
#include "links/OneToOneLink.h"
|
||||||
#include "Logger.h"
|
#include "Logger.h"
|
||||||
|
|
||||||
class HierarchicalLinkMixin : public LazyLinkMixin<OneToOneLink<ILinkMixin>> {
|
template <class TElem>
|
||||||
public:
|
class HierarchicalLinkMixin : public LazyLinkMixin<OneToOneLink<TElem>> {
|
||||||
HierarchicalLinkMixin() {}
|
using LinkPtr = std::shared_ptr<ILink<TElem>>;
|
||||||
|
using ElemPtr = std::shared_ptr<TElem>;
|
||||||
|
|
||||||
|
public:
|
||||||
~HierarchicalLinkMixin() override {
|
~HierarchicalLinkMixin() override {
|
||||||
Logger::get("Mixin").dbg("--- Destructor called for: HierarchicalLinkMixin");
|
Logger::get("Mixin").dbg("--- Destructor called for: HierarchicalLinkMixin");
|
||||||
}
|
}
|
||||||
|
|
||||||
void linkChild(const MixinPtr& child) override {
|
void linkChild(const ElemPtr& child) override {
|
||||||
hierarchicalInit(child);
|
hierarchicalInit(child);
|
||||||
LazyLinkMixin<OneToOneLink<ILinkMixin>>::linkChild(child);
|
LazyLinkMixin<OneToOneLink<TElem>>::linkChild(child);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
void hierarchicalInit(const MixinPtr& child) {
|
void hierarchicalInit(const ElemPtr& child) {
|
||||||
Logger::get("Mixin").dbg("--- hierarchicalInit called");
|
Logger::get("Mixin").dbg("--- hierarchicalInit called");
|
||||||
if (this->link_ && !this->link_->getChildren().empty())
|
if (this->link_ && !this->link_->getChildren().empty())
|
||||||
return; // already have children, do nothing
|
return;
|
||||||
|
|
||||||
|
|
||||||
/* 1. Have no link_ —
|
|
||||||
* 2. Has OneToOne with parent or not
|
|
||||||
* 3. Has OneToOne with child
|
|
||||||
*/
|
|
||||||
|
|
||||||
LinkPtr newLink;
|
LinkPtr newLink;
|
||||||
|
|
||||||
if (typeid(*child) == typeid(*this)) {
|
if (typeid(*child) == typeid(*this)) {
|
||||||
Logger::get("Mixin").dbg("--- Mutate to OneToMany");
|
Logger::get("Mixin").dbg("--- Mutate to OneToMany");
|
||||||
newLink = std::make_shared<OneToManyLink<ILinkMixin>>(this->getNode());
|
newLink = std::make_shared<OneToManyLink<TElem>>(this->getNode());
|
||||||
} else {
|
} else {
|
||||||
Logger::get("Mixin").dbg("--- Mutate to OneToOne");
|
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_)
|
if (newLink && this->link_)
|
||||||
|
|||||||
@@ -6,7 +6,7 @@
|
|||||||
#include "Logger.h"
|
#include "Logger.h"
|
||||||
|
|
||||||
class ComplexNode : public BaseNode,
|
class ComplexNode : public BaseNode,
|
||||||
virtual public HierarchicalLinkMixin {
|
virtual public HierarchicalLinkMixin<INode> {
|
||||||
public:
|
public:
|
||||||
~ComplexNode() {
|
~ComplexNode() {
|
||||||
Logger::get("Node").dbg(std::string("--- Complex destructor called for: ") + name_);
|
Logger::get("Node").dbg(std::string("--- Complex destructor called for: ") + name_);
|
||||||
|
|||||||
@@ -7,7 +7,7 @@
|
|||||||
#include "Logger.h"
|
#include "Logger.h"
|
||||||
|
|
||||||
class SimpleNode : public BaseNode,
|
class SimpleNode : public BaseNode,
|
||||||
virtual public LazyLinkMixin<OneToOneLink<ILinkMixin>> {
|
virtual public LazyLinkMixin<OneToOneLink<INode>> {
|
||||||
public:
|
public:
|
||||||
~SimpleNode() {
|
~SimpleNode() {
|
||||||
Logger::get("Node").dbg(std::string("--- Simple destructor called for: ") + name_);
|
Logger::get("Node").dbg(std::string("--- Simple destructor called for: ") + name_);
|
||||||
|
|||||||
+9
-11
@@ -3,23 +3,22 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include "Logger.h"
|
#include "Logger.h"
|
||||||
|
|
||||||
void printTree(const NodePtr& startNode, int indent = 0) {
|
void printTree(const NodePtr& node, int indent = 0) {
|
||||||
if (!startNode) {
|
if (!node) {
|
||||||
std::cout << "No node" << "\n";
|
Logger::get("MAIN").err("No node");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (int i = 0; i < indent; ++i) std::cout << " ";
|
for (int i = 0; i < indent; ++i) std::cout << " ";
|
||||||
std::cout << startNode->name() << "\n";
|
std::cout << node->name() << "\n";
|
||||||
LinkPtr nodeLink = startNode->getLink();
|
|
||||||
for (const auto& child : nodeLink->getChildren()) {
|
for (const auto& child : node->children())
|
||||||
auto childNode = std::dynamic_pointer_cast<INode>(child);
|
printTree(child, indent + 1);
|
||||||
printTree(childNode, indent + 1);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
Logger::setMinSeverity("MAIN", Logger::Severity::Debug);
|
Logger::setMinSeverity("MAIN", Logger::Severity::Debug);
|
||||||
//Logger::suppressCategory("Node");
|
Logger::suppressCategory("Node");
|
||||||
Logger::suppressCategory("Link");
|
Logger::suppressCategory("Link");
|
||||||
Logger::suppressCategory("Mixin");
|
Logger::suppressCategory("Mixin");
|
||||||
auto& logger = Logger::get("MAIN");
|
auto& logger = Logger::get("MAIN");
|
||||||
@@ -35,7 +34,6 @@ int main() {
|
|||||||
printTree(root);
|
printTree(root);
|
||||||
std::cout << "\n";
|
std::cout << "\n";
|
||||||
|
|
||||||
|
|
||||||
{
|
{
|
||||||
auto child2 = std::make_shared<ComplexNode>("ComplexChild2");
|
auto child2 = std::make_shared<ComplexNode>("ComplexChild2");
|
||||||
root->linkChild(child2);
|
root->linkChild(child2);
|
||||||
|
|||||||
Reference in New Issue
Block a user