iters
Сергей Маринкевич 5 months ago
parent f994303cb5
commit d17c80a195

@ -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";
}
};

@ -11,37 +11,47 @@ void printTree(const NodePtr& startNode, int indent = 0) {
}
}
int main2() {
std::cout << "Creatin...\n\n";
auto root = std::make_shared<SimpleNode>("Root");
std::cout << "Nice!\n\n";
return 0;
}
int main() {
std::cout << "Entering main scope...\n\n";
{
auto root = std::make_shared<SimpleNode>("Root");
auto child2 = std::make_shared<SimpleNode>("Child2");
auto child2 = std::make_shared<SimpleNode>("Child1");
root->linkChild(child2);
std::cout << "\nInit tree:\n\n\n";
printTree(root);
{
auto child1 = std::make_shared<SimpleNode>("Child1");
auto child1 = std::make_shared<SimpleNode>("Child2");
root->linkChild(child1);
auto subchild = std::make_shared<SimpleNode>("SubChild");
auto subchild = std::make_shared<SimpleNode>("SubChild2");
child1->linkChild(subchild);
auto child3 = std::make_shared<SimpleNode>("Child3");
root->linkChild(child3);
auto subchild2 = std::make_shared<SimpleNode>("SubChild2");
auto subchild2 = std::make_shared<SimpleNode>("SubChild3");
child3->linkChild(subchild2);
std::cout << "Initial tree:\n";
std::cout << "\nAnother tree:\n\n\n";
printTree(root);
std::cout << "\nUnlinking Child1...\n";
child1->unlinkParent();
std::cout << "\nTree after unlink:\n";
std::cout << "\nTree after unlink:\n\n\n";
printTree(root);
}
std::cout << "\nTree after scope out:\n";
std::cout << "\nTree after scope out:\n\n\n";
printTree(root);
std::cout << "\n\n";
}
std::cout << "\nExited main scope. All smart pointers destroyed.\n";

Loading…
Cancel
Save