poperdolilo
parent
8b60fc0183
commit
b2be9b51ca
@ -0,0 +1,20 @@
|
||||
#pragma once
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
|
||||
// Forward declarations
|
||||
class INode;
|
||||
class ILink;
|
||||
using NodePtr = std::shared_ptr<INode>;
|
||||
using LinkPtr = std::shared_ptr<ILink>;
|
||||
|
||||
class ILink {
|
||||
public:
|
||||
virtual ~ILink() = default;
|
||||
virtual NodePtr getNode() const = 0;
|
||||
virtual LinkPtr getParent() const = 0;
|
||||
virtual void setParent(const LinkPtr& parent) = 0;
|
||||
virtual const std::vector<LinkPtr>& getChildren() const = 0;
|
||||
virtual void addChild(const LinkPtr& child) = 0;
|
||||
virtual void removeChild(const LinkPtr& child) = 0;
|
||||
};
|
||||
@ -0,0 +1,16 @@
|
||||
#pragma once
|
||||
#include <memory>
|
||||
|
||||
// Forward declarations для избежания циклических #include
|
||||
class INode;
|
||||
class ILink;
|
||||
using NodePtr = std::shared_ptr<INode>;
|
||||
using LinkPtr = std::shared_ptr<ILink>;
|
||||
|
||||
class ILinkMixin {
|
||||
public:
|
||||
virtual ~ILinkMixin() = default;
|
||||
virtual void linkChild(const NodePtr& child) = 0;
|
||||
virtual void unlinkParent() = 0;
|
||||
virtual LinkPtr getLink() = 0;
|
||||
};
|
||||
@ -0,0 +1,11 @@
|
||||
#pragma once
|
||||
#include "ifaces/ILinkMixin.h"
|
||||
#include <string>
|
||||
|
||||
// INode является ILinkMixin, поэтому полный #include здесь оправдан
|
||||
class INode : public ILinkMixin {
|
||||
public:
|
||||
~INode() override = default;
|
||||
virtual const std::string& name() const = 0;
|
||||
virtual NodePtr getSelf() = 0;
|
||||
};
|
||||
@ -0,0 +1,20 @@
|
||||
#pragma once
|
||||
#include "ifaces/ILink.h"
|
||||
#include "ifaces/INode.h"
|
||||
#include <algorithm>
|
||||
|
||||
class BaseLink : public ILink {
|
||||
public:
|
||||
explicit BaseLink(NodePtr node) : owner_node_(node) {}
|
||||
NodePtr getNode() const override { return owner_node_.lock(); }
|
||||
LinkPtr getParent() const override { return parent_.lock(); }
|
||||
void setParent(const LinkPtr& parent) override { parent_ = parent; }
|
||||
const std::vector<LinkPtr>& getChildren() const override { return children_; }
|
||||
void removeChild(const LinkPtr& child) override {
|
||||
children_.erase(std::remove(children_.begin(), children_.end(), child), children_.end());
|
||||
}
|
||||
protected:
|
||||
std::vector<LinkPtr> children_;
|
||||
std::weak_ptr<INode> owner_node_;
|
||||
std::weak_ptr<ILink> parent_;
|
||||
};
|
||||
@ -0,0 +1,9 @@
|
||||
#pragma once
|
||||
#include "links/BaseLink.h"
|
||||
#include <stdexcept>
|
||||
|
||||
class LeafLink : public BaseLink {
|
||||
public:
|
||||
using BaseLink::BaseLink;
|
||||
void addChild(const LinkPtr&) override { throw std::logic_error("LeafLink cannot have children"); }
|
||||
};
|
||||
@ -0,0 +1,8 @@
|
||||
#pragma once
|
||||
#include "links/BaseLink.h"
|
||||
|
||||
class OneToManyLink : public BaseLink {
|
||||
public:
|
||||
using BaseLink::BaseLink;
|
||||
void addChild(const LinkPtr& child) override { children_.push_back(child); }
|
||||
};
|
||||
@ -0,0 +1,66 @@
|
||||
#pragma once
|
||||
#include "ifaces/INode.h"
|
||||
#include "links/LeafLink.h"
|
||||
#include "links/OneToManyLink.h"
|
||||
#include <memory>
|
||||
|
||||
template <class TFinalNode>
|
||||
class LazyLinkMixin : public INode {
|
||||
public:
|
||||
void linkChild(const NodePtr& childNode) override {
|
||||
lazyInit();
|
||||
upgradeLinkIfNeeded();
|
||||
LinkPtr childLink = childNode->getLink();
|
||||
childLink->setParent(link_);
|
||||
link_->addChild(childLink);
|
||||
}
|
||||
|
||||
void unlinkParent() override {
|
||||
lazyInit();
|
||||
LinkPtr parentLink = link_->getParent();
|
||||
if (!parentLink) return;
|
||||
parentLink->removeChild(this->getLink());
|
||||
link_->setParent(nullptr);
|
||||
}
|
||||
|
||||
LinkPtr getLink() override {
|
||||
lazyInit();
|
||||
return link_;
|
||||
}
|
||||
|
||||
protected:
|
||||
virtual std::shared_ptr<TFinalNode> getShared() = 0;
|
||||
NodePtr getSelf() override { return getShared(); }
|
||||
|
||||
private:
|
||||
void lazyInit() {
|
||||
if (!link_) {
|
||||
link_ = std::make_shared<LeafLink>(getSelf());
|
||||
}
|
||||
}
|
||||
|
||||
void upgradeLinkIfNeeded() {
|
||||
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,22 @@
|
||||
#pragma once
|
||||
#include "mixins/LazyLinkMixin.h"
|
||||
|
||||
class SimpleNode : public LazyLinkMixin<SimpleNode>,
|
||||
public std::enable_shared_from_this<SimpleNode> {
|
||||
public:
|
||||
static NodePtr create(std::string name) {
|
||||
struct EnableMakeShared : public SimpleNode {
|
||||
EnableMakeShared(std::string n) : SimpleNode(std::move(n)) {}
|
||||
};
|
||||
return std::make_shared<EnableMakeShared>(std::move(name));
|
||||
}
|
||||
const std::string& name() const override { return name_; }
|
||||
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_;
|
||||
};
|
||||
@ -0,0 +1,35 @@
|
||||
#include "nodes/SimpleNode.h"
|
||||
#include <iostream>
|
||||
|
||||
void printTree(const NodePtr& startNode, int indent = 0) {
|
||||
if (!startNode) return;
|
||||
for (int i = 0; i < indent; ++i) std::cout << " ";
|
||||
std::cout << startNode->name() << "\n";
|
||||
LinkPtr nodeLink = startNode->getLink();
|
||||
for (const auto& childLink : nodeLink->getChildren()) {
|
||||
printTree(childLink->getNode(), indent + 1);
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
auto root = SimpleNode::create("Root");
|
||||
auto child1 = SimpleNode::create("Child1");
|
||||
auto child2 = SimpleNode::create("Child2");
|
||||
|
||||
root->linkChild(child1);
|
||||
root->linkChild(child2);
|
||||
|
||||
auto subchild = SimpleNode::create("SubChild");
|
||||
child1->linkChild(subchild);
|
||||
|
||||
std::cout << "Initial tree:\n";
|
||||
printTree(root);
|
||||
|
||||
std::cout << "\nUnlinking Child1...\n";
|
||||
child1->unlinkParent();
|
||||
|
||||
std::cout << "\nFinal tree:\n";
|
||||
printTree(root);
|
||||
|
||||
return 0;
|
||||
}
|
||||
Loading…
Reference in New Issue