fup
This commit is contained in:
@@ -0,0 +1,5 @@
|
||||
#pragma once
|
||||
class INode;
|
||||
class ILink;
|
||||
using NodePtr = std::shared_ptr<INode>;
|
||||
using LinkPtr = std::shared_ptr<ILink>;
|
||||
@@ -1,11 +1,8 @@
|
||||
#pragma once
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
#include "ifaces/INode.h"
|
||||
|
||||
// Forward declarations
|
||||
class INode;
|
||||
class ILink;
|
||||
using NodePtr = std::shared_ptr<INode>;
|
||||
using LinkPtr = std::shared_ptr<ILink>;
|
||||
|
||||
class ILink {
|
||||
|
||||
@@ -1,11 +1,6 @@
|
||||
#pragma once
|
||||
#include <memory>
|
||||
|
||||
// Forward declarations для избежания циклических #include
|
||||
class INode;
|
||||
class ILink;
|
||||
using NodePtr = std::shared_ptr<INode>;
|
||||
using LinkPtr = std::shared_ptr<ILink>;
|
||||
#include "decls.h"
|
||||
|
||||
class ILinkMixin {
|
||||
public:
|
||||
@@ -13,5 +8,4 @@ public:
|
||||
virtual void linkChild(const NodePtr& child) = 0;
|
||||
virtual void unlinkParent() = 0;
|
||||
virtual LinkPtr getLink() = 0;
|
||||
virtual NodePtr getNode() = 0;
|
||||
};
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
#pragma once
|
||||
#include "ifaces/ILinkMixin.h"
|
||||
#include "ifaces/ILink.h"
|
||||
#include <string>
|
||||
|
||||
using NodePtr = std::shared_ptr<INode>;
|
||||
|
||||
class INode : public virtual ILinkMixin {
|
||||
public:
|
||||
~INode() = default;
|
||||
|
||||
@@ -0,0 +1,41 @@
|
||||
#pragma once
|
||||
#include <iostream>
|
||||
#include "ifaces/INode.h"
|
||||
#include "ifaces/ILinkMixin.h"
|
||||
#include "links/LeafLink.h"
|
||||
#include "links/OneToManyLink.h"
|
||||
#include <memory>
|
||||
|
||||
class BaseLinkMixin : public virtual ILinkMixin,
|
||||
public virtual INode,
|
||||
public std::enable_shared_from_this<INode> {
|
||||
public:
|
||||
void linkChild(const NodePtr& childNode) override {
|
||||
LinkPtr childLink = childNode->getLink();
|
||||
childLink->setParent(getNode());
|
||||
|
||||
getLink()->addChild(childNode);
|
||||
}
|
||||
|
||||
void unlinkParent() override {
|
||||
LinkPtr link = getLink();
|
||||
|
||||
NodePtr parent = link->getParent();
|
||||
if (!parent)
|
||||
throw std::logic_error("Have no parent!");
|
||||
|
||||
LinkPtr parentLink = parent->getLink();
|
||||
|
||||
parentLink->removeChild(getNode());
|
||||
getLink()->setParent(nullptr);
|
||||
}
|
||||
|
||||
~BaseLinkMixin() override {
|
||||
std::cout << "--- Destructor called for: " << "BaseLinkMixin" << "\n";
|
||||
}
|
||||
|
||||
protected:
|
||||
NodePtr getNode() {
|
||||
return shared_from_this();
|
||||
}
|
||||
};
|
||||
@@ -1,39 +1,22 @@
|
||||
#pragma once
|
||||
#include <iostream>
|
||||
#include "ifaces/INode.h"
|
||||
#include "ifaces/ILinkMixin.h"
|
||||
#include "links/LeafLink.h"
|
||||
#include "links/OneToManyLink.h"
|
||||
#include "mixins/BaseLinkMixin.h"
|
||||
#include <memory>
|
||||
|
||||
template <class TLink>
|
||||
class LazyLinkMixin : public virtual ILinkMixin {
|
||||
class LazyLinkMixin : public BaseLinkMixin {
|
||||
public:
|
||||
void linkChild(const NodePtr& childNode) override {
|
||||
LinkPtr childLink = childNode->getLink();
|
||||
childLink->setParent(getNode());
|
||||
|
||||
getLink()->addChild(childNode);
|
||||
}
|
||||
|
||||
void unlinkParent() override {
|
||||
/* No link -- no parent, who'll unlinked? */
|
||||
if (!link_)
|
||||
if (!this->link_)
|
||||
throw std::logic_error("Link isn't inited!");
|
||||
|
||||
NodePtr parent = link_->getParent();
|
||||
if (!parent)
|
||||
throw std::logic_error("Have no parent!");
|
||||
|
||||
LinkPtr parentLink = parent->getLink();
|
||||
|
||||
parentLink->removeChild(getNode());
|
||||
getLink()->setParent(nullptr);
|
||||
BaseLinkMixin::unlinkParent();
|
||||
}
|
||||
|
||||
LinkPtr getLink() override {
|
||||
lazyInit();
|
||||
return link_;
|
||||
return this->link_;
|
||||
}
|
||||
|
||||
~LazyLinkMixin() override {
|
||||
@@ -41,14 +24,11 @@ public:
|
||||
}
|
||||
|
||||
protected:
|
||||
virtual NodePtr getNode() = 0;
|
||||
|
||||
private:
|
||||
void lazyInit() {
|
||||
if (!link_) {
|
||||
link_ = std::make_shared<TLink>(getNode());
|
||||
if (!this->link_) {
|
||||
this->link_ = std::make_shared<TLink>(
|
||||
BaseLinkMixin::getNode());
|
||||
}
|
||||
}
|
||||
|
||||
LinkPtr link_;
|
||||
};
|
||||
|
||||
@@ -3,18 +3,15 @@
|
||||
#include "ifaces/INode.h"
|
||||
#include <iostream>
|
||||
|
||||
class BaseNode : public INode,
|
||||
public LazyLinkMixin<OneToManyLink>,
|
||||
public std::enable_shared_from_this<INode> {
|
||||
class BaseNode : public virtual INode {
|
||||
public:
|
||||
BaseNode(std::string name) : name_(std::move(name)) {}
|
||||
BaseNode(std::string name) : name_(std::move(name)) {
|
||||
std::cout << "--- Base constructor called for: " << name_ << "\n";
|
||||
}
|
||||
const std::string& name() const override { return name_; }
|
||||
~BaseNode() {
|
||||
std::cout << "--- Base destructor called for: " << name_ << "\n";
|
||||
}
|
||||
protected:
|
||||
NodePtr getNode() override {
|
||||
return shared_from_this();
|
||||
}
|
||||
std::string name_;
|
||||
};
|
||||
|
||||
@@ -2,10 +2,13 @@
|
||||
#include "nodes/BaseNode.h"
|
||||
#include <iostream>
|
||||
|
||||
class SimpleNode : public BaseNode {
|
||||
class SimpleNode : public BaseNode,
|
||||
public LazyLinkMixin<OneToManyLink> {
|
||||
public:
|
||||
~SimpleNode() {
|
||||
std::cout << "--- Simple destructor called for: " << name_ << "\n";
|
||||
}
|
||||
SimpleNode(std::string name) : BaseNode(std::move(name)) {}
|
||||
SimpleNode(std::string name) : BaseNode(std::move(name)) {
|
||||
std::cout << "--- Simple constructor called for: " << name_ << "\n";
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user