tmp
This commit is contained in:
@@ -0,0 +1,3 @@
|
|||||||
|
*.swp
|
||||||
|
build
|
||||||
|
.vscode
|
||||||
@@ -10,11 +10,11 @@ using LinkPtr = std::shared_ptr<ILink>;
|
|||||||
|
|
||||||
class ILink {
|
class ILink {
|
||||||
public:
|
public:
|
||||||
virtual ~ILink() = default;
|
virtual ~ILink() = default;
|
||||||
virtual NodePtr getNode() const = 0;
|
virtual NodePtr getNode() const = 0;
|
||||||
virtual LinkPtr getParent() const = 0;
|
virtual NodePtr getParent() const = 0;
|
||||||
virtual void setParent(const LinkPtr& parent) = 0;
|
virtual void setParent(const NodePtr& parent) = 0;
|
||||||
virtual const std::vector<LinkPtr>& getChildren() const = 0;
|
virtual const std::vector<NodePtr>& getChildren() const = 0;
|
||||||
virtual void addChild(const LinkPtr& child) = 0;
|
virtual void addChild(const NodePtr& child) = 0;
|
||||||
virtual void removeChild(const LinkPtr& child) = 0;
|
virtual void removeChild(const NodePtr& child) = 0;
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -9,8 +9,9 @@ using LinkPtr = std::shared_ptr<ILink>;
|
|||||||
|
|
||||||
class ILinkMixin {
|
class ILinkMixin {
|
||||||
public:
|
public:
|
||||||
virtual ~ILinkMixin() = default;
|
virtual ~ILinkMixin() = default;
|
||||||
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 getSelf() = 0;
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -5,7 +5,7 @@
|
|||||||
// INode является ILinkMixin, поэтому полный #include здесь оправдан
|
// INode является ILinkMixin, поэтому полный #include здесь оправдан
|
||||||
class INode : public ILinkMixin {
|
class INode : public ILinkMixin {
|
||||||
public:
|
public:
|
||||||
~INode() override = default;
|
~INode() override = default;
|
||||||
virtual const std::string& name() const = 0;
|
virtual const std::string& name() const = 0;
|
||||||
virtual NodePtr getSelf() = 0;
|
virtual NodePtr getSelf() = 0;
|
||||||
};
|
};
|
||||||
|
|||||||
+14
-14
@@ -5,20 +5,20 @@
|
|||||||
|
|
||||||
class BaseLink : public ILink {
|
class BaseLink : public ILink {
|
||||||
public:
|
public:
|
||||||
explicit BaseLink(NodePtr node) : owner_node_(node) {}
|
explicit BaseLink(NodePtr node) : owner_node_(node) {}
|
||||||
NodePtr getNode() const override { return owner_node_.lock(); }
|
NodePtr getNode() const override { return owner_node_.lock(); }
|
||||||
LinkPtr getParent() const override { return parent_.lock(); }
|
NodePtr getParent() const override { return parent_.lock(); }
|
||||||
void setParent(const LinkPtr& parent) override { parent_ = parent; }
|
void setParent(const NodePtr& parent) override { parent_ = parent; }
|
||||||
const std::vector<LinkPtr>& getChildren() const override { return children_; }
|
const std::vector<NodePtr>& getChildren() const override { return children_; }
|
||||||
void removeChild(const LinkPtr& child) override {
|
void removeChild(const NodePtr& child) override {
|
||||||
children_.erase(std::remove(children_.begin(), children_.end(), child), children_.end());
|
children_.erase(std::remove(children_.begin(), children_.end(), child), children_.end());
|
||||||
}
|
}
|
||||||
|
|
||||||
~BaseLink() override {
|
~BaseLink() override {
|
||||||
std::cout << "--- Destructor called for: " << "BaseLink" << "\n";
|
std::cout << "--- Destructor called for: " << "BaseLink" << "\n";
|
||||||
}
|
}
|
||||||
protected:
|
protected:
|
||||||
std::vector<LinkPtr> children_;
|
std::vector<NodePtr> children_;
|
||||||
std::weak_ptr<INode> owner_node_;
|
std::weak_ptr<INode> owner_node_;
|
||||||
std::weak_ptr<ILink> parent_;
|
std::weak_ptr<INode> parent_;
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -4,6 +4,6 @@
|
|||||||
|
|
||||||
class LeafLink : public BaseLink {
|
class LeafLink : public BaseLink {
|
||||||
public:
|
public:
|
||||||
using BaseLink::BaseLink;
|
using BaseLink::BaseLink;
|
||||||
void addChild(const LinkPtr&) override { throw std::logic_error("LeafLink cannot have children"); }
|
void addChild(const NodePtr&) override { throw std::logic_error("LeafLink cannot have children"); }
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -3,6 +3,6 @@
|
|||||||
|
|
||||||
class OneToManyLink : public BaseLink {
|
class OneToManyLink : public BaseLink {
|
||||||
public:
|
public:
|
||||||
using BaseLink::BaseLink;
|
using BaseLink::BaseLink;
|
||||||
void addChild(const LinkPtr& child) override { children_.push_back(child); }
|
void addChild(const NodePtr& child) override { children_.push_back(child); }
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -5,67 +5,50 @@
|
|||||||
#include "links/OneToManyLink.h"
|
#include "links/OneToManyLink.h"
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
|
||||||
template <class TFinalNode>
|
template <class TLink>
|
||||||
class LazyLinkMixin : public INode {
|
class LazyLinkMixin : public ILinkMixin {
|
||||||
public:
|
public:
|
||||||
void linkChild(const NodePtr& childNode) override {
|
void linkChild(const NodePtr& childNode) override {
|
||||||
lazyInit();
|
LinkPtr childLink = childNode->getLink();
|
||||||
upgradeLinkIfNeeded();
|
childLink->setParent(getSelf());
|
||||||
LinkPtr childLink = childNode->getLink();
|
|
||||||
childLink->setParent(link_);
|
|
||||||
link_->addChild(childLink);
|
|
||||||
}
|
|
||||||
|
|
||||||
void unlinkParent() override {
|
getLink()->addChild(childNode);
|
||||||
lazyInit();
|
}
|
||||||
LinkPtr parentLink = link_->getParent();
|
|
||||||
if (!parentLink) return;
|
|
||||||
parentLink->removeChild(this->getLink());
|
|
||||||
link_->setParent(nullptr);
|
|
||||||
}
|
|
||||||
|
|
||||||
LinkPtr getLink() override {
|
void unlinkParent() override {
|
||||||
lazyInit();
|
/* No link -- no parent, who'll unlinked? */
|
||||||
return link_;
|
if (!link_)
|
||||||
}
|
throw std::logic_error("Link isn't inited!");
|
||||||
|
|
||||||
~LazyLinkMixin() override {
|
NodePtr parent = link_->getParent();
|
||||||
std::cout << "--- Destructor called for: " << "LazyLinkMixin" << "\n";
|
if (!parent)
|
||||||
}
|
throw std::logic_error("Have no parent!");
|
||||||
|
|
||||||
|
LinkPtr parentLink = parent->getLink();
|
||||||
|
|
||||||
|
parentLink->removeChild(getSelf());
|
||||||
|
getLink()->setParent(nullptr);
|
||||||
|
}
|
||||||
|
|
||||||
|
LinkPtr getLink() override {
|
||||||
|
lazyInit();
|
||||||
|
return link_;
|
||||||
|
}
|
||||||
|
|
||||||
|
~LazyLinkMixin() override {
|
||||||
|
std::cout << "--- Destructor called for: " << "LazyLinkMixin" << "\n";
|
||||||
|
}
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
virtual std::shared_ptr<TFinalNode> getShared() = 0;
|
virtual NodePtr getShared() = 0;
|
||||||
NodePtr getSelf() override { return getShared(); }
|
NodePtr getSelf() override { return getShared(); }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void lazyInit() {
|
void lazyInit() {
|
||||||
if (!link_) {
|
if (!link_) {
|
||||||
link_ = std::make_shared<LeafLink>(getSelf());
|
link_ = std::make_shared<TLink>(getSelf());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void upgradeLinkIfNeeded() {
|
LinkPtr link_;
|
||||||
if (!std::dynamic_pointer_cast<LeafLink>(link_)) return;
|
|
||||||
auto newLink = std::make_shared<OneToManyLink>(getSelf());
|
|
||||||
if (auto parentLink = link_->getParent()) {
|
|
||||||
parentLink->removeChild(link_);
|
|
||||||
parentLink->addChild(newLink);
|
|
||||||
}
|
|
||||||
newLink->setParent(link_->getParent());
|
|
||||||
link_ = newLink;
|
|
||||||
}
|
|
||||||
|
|
||||||
void downgradeLinkIfPossible() {
|
|
||||||
if (link_->getChildren().empty() && std::dynamic_pointer_cast<OneToManyLink>(link_)) {
|
|
||||||
auto newLink = std::make_shared<LeafLink>(getSelf());
|
|
||||||
if (auto parentLink = link_->getParent()) {
|
|
||||||
parentLink->removeChild(link_);
|
|
||||||
parentLink->addChild(newLink);
|
|
||||||
}
|
|
||||||
newLink->setParent(link_->getParent());
|
|
||||||
link_ = newLink;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
LinkPtr link_;
|
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -0,0 +1,20 @@
|
|||||||
|
#pragma once
|
||||||
|
#include "mixins/LazyLinkMixin.h"
|
||||||
|
#include "ifaces/INode.h"
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
class BaseNode : public INode,
|
||||||
|
public LazyLinkMixin<OneToManyLink>,
|
||||||
|
public std::enable_shared_from_this<BaseNode> {
|
||||||
|
public:
|
||||||
|
BaseNode(std::string name) : name_(std::move(name)) {}
|
||||||
|
const std::string& name() const override { return name_; }
|
||||||
|
~BaseNode() {
|
||||||
|
std::cout << "--- Destructor called for: " << name_ << "\n";
|
||||||
|
}
|
||||||
|
protected:
|
||||||
|
NodePtr getShared() override {
|
||||||
|
return shared_from_this();
|
||||||
|
}
|
||||||
|
std::string name_;
|
||||||
|
};
|
||||||
@@ -1,26 +1,12 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#include "mixins/LazyLinkMixin.h"
|
#include "nodes/BaseNode.h"
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
class SimpleNode : public LazyLinkMixin<SimpleNode>,
|
class SimpleNode :
|
||||||
public std::enable_shared_from_this<SimpleNode> {
|
public BaseNode {
|
||||||
public:
|
public:
|
||||||
static NodePtr create(std::string name) {
|
~SimpleNode() {
|
||||||
struct EnableMakeShared : public SimpleNode {
|
std::cout << "--- Destructor called for: " << name_ << "\n";
|
||||||
EnableMakeShared(std::string n) : SimpleNode(std::move(n)) {}
|
}
|
||||||
};
|
SimpleNode(std::string name) : BaseNode(std::move(name)) {}
|
||||||
return std::make_shared<EnableMakeShared>(std::move(name));
|
|
||||||
}
|
|
||||||
const std::string& name() const override { return name_; }
|
|
||||||
~SimpleNode() {
|
|
||||||
std::cout << "--- Destructor called for: " << name_ << "\n";
|
|
||||||
}
|
|
||||||
protected:
|
|
||||||
std::shared_ptr<SimpleNode> getShared() override {
|
|
||||||
return shared_from_this();
|
|
||||||
}
|
|
||||||
private:
|
|
||||||
friend class LazyLinkMixin<SimpleNode>;
|
|
||||||
explicit SimpleNode(std::string name) : name_(std::move(name)) {}
|
|
||||||
std::string name_;
|
|
||||||
};
|
};
|
||||||
|
|||||||
+36
-36
@@ -2,50 +2,50 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
void printTree(const NodePtr& startNode, int indent = 0) {
|
void printTree(const NodePtr& startNode, int indent = 0) {
|
||||||
if (!startNode) return;
|
if (!startNode) 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 << startNode->name() << "\n";
|
||||||
LinkPtr nodeLink = startNode->getLink();
|
LinkPtr nodeLink = startNode->getLink();
|
||||||
for (const auto& childLink : nodeLink->getChildren()) {
|
for (const auto& child : nodeLink->getChildren()) {
|
||||||
printTree(childLink->getNode(), indent + 1);
|
printTree(child, indent + 1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
std::cout << "Entering main scope...\n\n";
|
std::cout << "Entering main scope...\n\n";
|
||||||
|
|
||||||
// Начало новой области видимости
|
|
||||||
{
|
|
||||||
auto root = SimpleNode::create("Root");
|
|
||||||
auto child2 = SimpleNode::create("Child2");
|
|
||||||
|
|
||||||
root->linkChild(child2);
|
|
||||||
|
|
||||||
|
// Начало новой области видимости
|
||||||
{
|
{
|
||||||
auto child1 = SimpleNode::create("Child1");
|
auto root = std::make_shared<SimpleNode>("Root");
|
||||||
root->linkChild(child1);
|
auto child2 = std::make_shared<SimpleNode>("Child2");
|
||||||
auto subchild = SimpleNode::create("SubChild");
|
|
||||||
child1->linkChild(subchild);
|
|
||||||
auto child3 = SimpleNode::create("Child3");
|
|
||||||
root->linkChild(child3);
|
|
||||||
auto subchild2 = SimpleNode::create("SubChild2");
|
|
||||||
child3->linkChild(subchild2);
|
|
||||||
|
|
||||||
std::cout << "Initial tree:\n";
|
root->linkChild(child2);
|
||||||
|
|
||||||
|
{
|
||||||
|
auto child1 = std::make_shared<SimpleNode>("Child1");
|
||||||
|
root->linkChild(child1);
|
||||||
|
auto subchild = std::make_shared<SimpleNode>("SubChild");
|
||||||
|
child1->linkChild(subchild);
|
||||||
|
auto child3 = std::make_shared<SimpleNode>("Child3");
|
||||||
|
root->linkChild(child3);
|
||||||
|
auto subchild2 = std::make_shared<SimpleNode>("SubChild2");
|
||||||
|
child3->linkChild(subchild2);
|
||||||
|
|
||||||
|
std::cout << "Initial tree:\n";
|
||||||
|
printTree(root);
|
||||||
|
|
||||||
|
std::cout << "\nUnlinking Child1...\n";
|
||||||
|
child1->unlinkParent();
|
||||||
|
std::cout << "\nTree after unlink:\n";
|
||||||
|
printTree(root);
|
||||||
|
}
|
||||||
|
|
||||||
|
std::cout << "\nTree after scope out:\n";
|
||||||
printTree(root);
|
printTree(root);
|
||||||
|
|
||||||
std::cout << "\nUnlinking Child1...\n";
|
} // <--- КОНЕЦ ОБЛАСТИ ВИДИМОСТИ
|
||||||
child1->unlinkParent();
|
|
||||||
std::cout << "\nTree after unlink:\n";
|
|
||||||
printTree(root);
|
|
||||||
}
|
|
||||||
|
|
||||||
std::cout << "\nTree after scope out:\n";
|
std::cout << "\nExited main scope. All smart pointers destroyed.\n";
|
||||||
printTree(root);
|
|
||||||
|
|
||||||
} // <--- КОНЕЦ ОБЛАСТИ ВИДИМОСТИ
|
return 0;
|
||||||
|
|
||||||
std::cout << "\nExited main scope. All smart pointers destroyed.\n";
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user