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 #pragma once
#include <memory> #include <memory>
#include <vector> #include <vector>
#include "ifaces/INode.h"
// Forward declarations
class INode;
class ILink;
using NodePtr = std::shared_ptr<INode>;
using LinkPtr = std::shared_ptr<ILink>; using LinkPtr = std::shared_ptr<ILink>;
class ILink { class ILink {

@ -1,11 +1,6 @@
#pragma once #pragma once
#include <memory> #include <memory>
#include "decls.h"
// Forward declarations для избежания циклических #include
class INode;
class ILink;
using NodePtr = std::shared_ptr<INode>;
using LinkPtr = std::shared_ptr<ILink>;
class ILinkMixin { class ILinkMixin {
public: public:
@ -13,5 +8,4 @@ public:
virtual void linkChild(const NodePtr& child) = 0; virtual void linkChild(const NodePtr& child) = 0;
virtual void unlinkParent() = 0; virtual void unlinkParent() = 0;
virtual LinkPtr getLink() = 0; virtual LinkPtr getLink() = 0;
virtual NodePtr getNode() = 0;
}; };

@ -1,8 +1,9 @@
#pragma once #pragma once
#include "ifaces/ILinkMixin.h" #include "ifaces/ILinkMixin.h"
#include "ifaces/ILink.h"
#include <string> #include <string>
using NodePtr = std::shared_ptr<INode>;
class INode : public virtual ILinkMixin { class INode : public virtual ILinkMixin {
public: public:
~INode() = default; ~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 #pragma once
#include <iostream> #include <iostream>
#include "ifaces/INode.h" #include "mixins/BaseLinkMixin.h"
#include "ifaces/ILinkMixin.h"
#include "links/LeafLink.h"
#include "links/OneToManyLink.h"
#include <memory> #include <memory>
template <class TLink> template <class TLink>
class LazyLinkMixin : public virtual ILinkMixin { class LazyLinkMixin : public BaseLinkMixin {
public: public:
void linkChild(const NodePtr& childNode) override {
LinkPtr childLink = childNode->getLink();
childLink->setParent(getNode());
getLink()->addChild(childNode);
}
void unlinkParent() override { void unlinkParent() override {
/* No link -- no parent, who'll unlinked? */ /* No link -- no parent, who'll unlinked? */
if (!link_) if (!this->link_)
throw std::logic_error("Link isn't inited!"); throw std::logic_error("Link isn't inited!");
NodePtr parent = link_->getParent(); BaseLinkMixin::unlinkParent();
if (!parent)
throw std::logic_error("Have no parent!");
LinkPtr parentLink = parent->getLink();
parentLink->removeChild(getNode());
getLink()->setParent(nullptr);
} }
LinkPtr getLink() override { LinkPtr getLink() override {
lazyInit(); lazyInit();
return link_; return this->link_;
} }
~LazyLinkMixin() override { ~LazyLinkMixin() override {
@ -41,14 +24,11 @@ public:
} }
protected: protected:
virtual NodePtr getNode() = 0;
private:
void lazyInit() { void lazyInit() {
if (!link_) { if (!this->link_) {
link_ = std::make_shared<TLink>(getNode()); this->link_ = std::make_shared<TLink>(
BaseLinkMixin::getNode());
} }
} }
LinkPtr link_; LinkPtr link_;
}; };

@ -3,18 +3,15 @@
#include "ifaces/INode.h" #include "ifaces/INode.h"
#include <iostream> #include <iostream>
class BaseNode : public INode, class BaseNode : public virtual INode {
public LazyLinkMixin<OneToManyLink>,
public std::enable_shared_from_this<INode> {
public: 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_; } const std::string& name() const override { return name_; }
~BaseNode() { ~BaseNode() {
std::cout << "--- Base destructor called for: " << name_ << "\n"; std::cout << "--- Base destructor called for: " << name_ << "\n";
} }
protected: protected:
NodePtr getNode() override {
return shared_from_this();
}
std::string name_; std::string name_;
}; };

@ -2,10 +2,13 @@
#include "nodes/BaseNode.h" #include "nodes/BaseNode.h"
#include <iostream> #include <iostream>
class SimpleNode : public BaseNode { class SimpleNode : public BaseNode,
public LazyLinkMixin<OneToManyLink> {
public: public:
~SimpleNode() { ~SimpleNode() {
std::cout << "--- Simple destructor called for: " << name_ << "\n"; 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() { int main() {
std::cout << "Entering main scope...\n\n"; std::cout << "Entering main scope...\n\n";
{ {
auto root = std::make_shared<SimpleNode>("Root"); auto root = std::make_shared<SimpleNode>("Root");
auto child2 = std::make_shared<SimpleNode>("Child2"); auto child2 = std::make_shared<SimpleNode>("Child1");
root->linkChild(child2); 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); root->linkChild(child1);
auto subchild = std::make_shared<SimpleNode>("SubChild"); auto subchild = std::make_shared<SimpleNode>("SubChild2");
child1->linkChild(subchild); child1->linkChild(subchild);
auto child3 = std::make_shared<SimpleNode>("Child3"); auto child3 = std::make_shared<SimpleNode>("Child3");
root->linkChild(child3); root->linkChild(child3);
auto subchild2 = std::make_shared<SimpleNode>("SubChild2"); auto subchild2 = std::make_shared<SimpleNode>("SubChild3");
child3->linkChild(subchild2); child3->linkChild(subchild2);
std::cout << "Initial tree:\n"; std::cout << "\nAnother tree:\n\n\n";
printTree(root); printTree(root);
std::cout << "\nUnlinking Child1...\n"; std::cout << "\nUnlinking Child1...\n";
child1->unlinkParent(); child1->unlinkParent();
std::cout << "\nTree after unlink:\n"; std::cout << "\nTree after unlink:\n\n\n";
printTree(root); printTree(root);
} }
std::cout << "\nTree after scope out:\n"; std::cout << "\nTree after scope out:\n\n\n";
printTree(root); printTree(root);
std::cout << "\n\n";
} }
std::cout << "\nExited main scope. All smart pointers destroyed.\n"; std::cout << "\nExited main scope. All smart pointers destroyed.\n";

Loading…
Cancel
Save