blah
This commit is contained in:
@@ -11,6 +11,8 @@ public:
|
||||
using ElemPtr = std::shared_ptr<TElem>;
|
||||
|
||||
virtual ~ILinkMixin() = default;
|
||||
virtual operator std::shared_ptr<TElem>() = 0;
|
||||
virtual operator TElem&() = 0;
|
||||
virtual void linkChild(const ElemPtr& child) = 0;
|
||||
virtual void unlinkParent() = 0;
|
||||
virtual const std::vector<ElemPtr>& children() = 0;
|
||||
|
||||
@@ -34,7 +34,7 @@ public:
|
||||
}
|
||||
|
||||
~BaseLink() override {
|
||||
Logger::get("Link").dbg("--- Destructor called for: BaseLink");
|
||||
Logger::get("ConDes").dbg("--- Destructor called for: BaseLink");
|
||||
}
|
||||
protected:
|
||||
std::vector<ElemPtr> children_;
|
||||
|
||||
@@ -20,7 +20,7 @@ public:
|
||||
void removeChild(const ElemPtr& child) override { }
|
||||
|
||||
~NotImplementedLink() override {
|
||||
Logger::get("Link").dbg("--- Destructor called for: NotImplementedLink");
|
||||
Logger::get("ConDes").dbg("--- Destructor called for: NotImplementedLink");
|
||||
}
|
||||
private:
|
||||
std::vector<ElemPtr> empty_;
|
||||
|
||||
@@ -14,7 +14,15 @@ class BaseLinkMixin : public virtual ILinkMixin<INode>,
|
||||
|
||||
public:
|
||||
~BaseLinkMixin() override {
|
||||
Logger::get("Mixin").dbg("--- Destructor called for: BaseLinkMixin");
|
||||
Logger::get("ConDes").dbg("--- Destructor called for: BaseLinkMixin");
|
||||
}
|
||||
|
||||
operator std::shared_ptr<INode>() override {
|
||||
return this->getNode();
|
||||
}
|
||||
|
||||
operator INode&() override {
|
||||
return static_cast<INode&>(*this);
|
||||
}
|
||||
|
||||
void linkChild(const ElemPtr& child) override {
|
||||
@@ -33,7 +41,15 @@ public:
|
||||
|
||||
auto parentLink = parent->getLink();
|
||||
|
||||
parentLink->removeChild(getNode());
|
||||
/* NOTE:
|
||||
*
|
||||
* Keep a reference to the node we gonna to unlink.
|
||||
* Otherwise, we'll disappear between `removeChild` and
|
||||
* `setParent`. Do not rearrange these calls, because
|
||||
* we want to modify the tree top down.
|
||||
*/
|
||||
auto node = getNode();
|
||||
parentLink->removeChild(node);
|
||||
getLink()->setParent(nullptr);
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
#pragma once
|
||||
#include <memory>
|
||||
|
||||
template <typename T>
|
||||
class FabricMixin {
|
||||
public:
|
||||
template <typename... Args>
|
||||
static std::shared_ptr<T> create(Args&&... args) {
|
||||
return std::shared_ptr<T>(new T(std::forward<Args>(args)...));
|
||||
}
|
||||
};
|
||||
@@ -16,7 +16,7 @@ class HierarchicalLinkMixin : public LazyLinkMixin<OneToOneLink<TElem>> {
|
||||
|
||||
public:
|
||||
~HierarchicalLinkMixin() override {
|
||||
Logger::get("Mixin").dbg("--- Destructor called for: HierarchicalLinkMixin");
|
||||
Logger::get("ConDes").dbg("--- Destructor called for: HierarchicalLinkMixin");
|
||||
}
|
||||
|
||||
void linkChild(const ElemPtr& child) override {
|
||||
@@ -34,10 +34,10 @@ protected:
|
||||
|
||||
if (typeid(*child) == typeid(*this)) {
|
||||
Logger::get("Mixin").dbg("--- Mutate to OneToMany");
|
||||
newLink = std::make_shared<OneToManyLink<TElem>>(this->getNode());
|
||||
newLink = std::make_shared<OneToManyLink<TElem>>(*this);
|
||||
} else {
|
||||
Logger::get("Mixin").dbg("--- Mutate to OneToOne");
|
||||
newLink = std::make_shared<OneToOneLink<TElem>>(this->getNode());
|
||||
newLink = std::make_shared<OneToOneLink<TElem>>(*this);
|
||||
}
|
||||
|
||||
if (newLink && this->link_)
|
||||
|
||||
@@ -25,14 +25,13 @@ public:
|
||||
LazyLinkMixin() {}
|
||||
|
||||
~LazyLinkMixin() override {
|
||||
Logger::get("Mixin").dbg("--- Destructor called for: LazyLinkMixin");
|
||||
Logger::get("ConDes").info("--- Destructor called for: LazyLinkMixin");
|
||||
}
|
||||
|
||||
protected:
|
||||
void lazyInit() {
|
||||
if (!link_) {
|
||||
link_ = std::make_shared<TLink>(
|
||||
BaseLinkMixin::getNode());
|
||||
link_ = std::make_shared<TLink>(*this);
|
||||
}
|
||||
}
|
||||
LinkPtr link_;
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
#pragma once
|
||||
#include <iostream>
|
||||
|
||||
#include "ifaces/INode.h"
|
||||
#include "Logger.h"
|
||||
@@ -9,12 +8,12 @@
|
||||
class BaseNode : public virtual INode {
|
||||
public:
|
||||
BaseNode(std::string name) : name_(std::move(name)) {
|
||||
Logger::get("Node").dbg(std::string("--- Base constructor called for: ") + name_);
|
||||
Logger::get("ConDes").dbg(std::string("--- Base constructor called for: ") + name_);
|
||||
}
|
||||
const std::string& name() const override { return name_; }
|
||||
const std::string& kind() const override { return kind_; }
|
||||
~BaseNode() {
|
||||
Logger::get("Node").dbg(std::string("--- Base destructor called for: ") + name_);
|
||||
Logger::get("ConDes").info(std::string("--- Base destructor called for: ") + name_);
|
||||
}
|
||||
protected:
|
||||
std::string name_;
|
||||
|
||||
@@ -1,19 +1,22 @@
|
||||
#pragma once
|
||||
#include <iostream>
|
||||
|
||||
#include "nodes/BaseNode.h"
|
||||
#include "mixins/HierarchicalLinkMixin.h"
|
||||
#include "mixins/FabricMixin.h"
|
||||
#include "Logger.h"
|
||||
|
||||
/// \brief Класс сложного (составного) узла дерева.
|
||||
/// Может содержать несколько дочерних ComplexNode и один SimpleNode.
|
||||
/// Может содержать несколько дочерних ComplexNode или один SimpleNode.
|
||||
class ComplexNode : public BaseNode,
|
||||
virtual public HierarchicalLinkMixin<INode> {
|
||||
virtual public HierarchicalLinkMixin<INode>,
|
||||
public FabricMixin<ComplexNode> {
|
||||
public:
|
||||
~ComplexNode() {
|
||||
Logger::get("Node").dbg(std::string("--- Complex destructor called for: ") + name_);
|
||||
Logger::get("ConDes").dbg(std::string("--- Complex destructor called for: ") + name_);
|
||||
}
|
||||
private:
|
||||
friend class FabricMixin<ComplexNode>;
|
||||
ComplexNode(std::string name) : BaseNode(std::move(name)) {
|
||||
Logger::get("Node").dbg(std::string("--- Complex constructor called for: ") + name_);
|
||||
Logger::get("ConDes").dbg(std::string("--- Complex constructor called for: ") + name_);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -1,20 +1,23 @@
|
||||
#pragma once
|
||||
#include <iostream>
|
||||
|
||||
#include "nodes/BaseNode.h"
|
||||
#include "mixins/LazyLinkMixin.h"
|
||||
#include "mixins/FabricMixin.h"
|
||||
#include "links/OneToOneLink.h"
|
||||
#include "Logger.h"
|
||||
|
||||
/// \brief Класс простого (листового) узла дерева.
|
||||
/// Может содержать только одного дочернего ComplexNode.
|
||||
class SimpleNode : public BaseNode,
|
||||
virtual public LazyLinkMixin<OneToOneLink<INode>> {
|
||||
virtual public LazyLinkMixin<OneToOneLink<INode>>,
|
||||
public FabricMixin<SimpleNode> {
|
||||
public:
|
||||
~SimpleNode() {
|
||||
Logger::get("Node").dbg(std::string("--- Simple destructor called for: ") + name_);
|
||||
Logger::get("ConDes").dbg(std::string("--- Simple destructor called for: ") + name_);
|
||||
}
|
||||
private:
|
||||
friend class FabricMixin<SimpleNode>;
|
||||
SimpleNode(std::string name) : BaseNode(std::move(name)) {
|
||||
Logger::get("Node").dbg(std::string("--- Simple constructor called for: ") + name_);
|
||||
Logger::get("ConDes").dbg(std::string("--- Simple constructor called for: ") + name_);
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user