Compare commits
10 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 55ef99c848 | |||
| d17c80a195 | |||
| f994303cb5 | |||
| 0912566c5f | |||
| f5f78308bd | |||
| 3801a5dc0b | |||
| 44480a7128 | |||
| b2be9b51ca | |||
| 8b60fc0183 | |||
| fd34b335c1 |
@@ -0,0 +1,3 @@
|
|||||||
|
*.swp
|
||||||
|
build
|
||||||
|
.vscode
|
||||||
@@ -0,0 +1,23 @@
|
|||||||
|
# CMakeLists.txt
|
||||||
|
|
||||||
|
cmake_minimum_required(VERSION 3.10)
|
||||||
|
|
||||||
|
project(ModernTree LANGUAGES CXX)
|
||||||
|
|
||||||
|
set(CMAKE_CXX_STANDARD 17)
|
||||||
|
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
||||||
|
set(CMAKE_CXX_EXTENSIONS OFF)
|
||||||
|
|
||||||
|
# Находим все исходные файлы в директории src
|
||||||
|
file(GLOB_RECURSE SOURCES "src/*.cpp")
|
||||||
|
|
||||||
|
# Создаем исполняемый файл из исходников
|
||||||
|
add_executable(tree_app ${SOURCES})
|
||||||
|
|
||||||
|
# Указываем, что директория include является публичной для нашего проекта
|
||||||
|
# Это позволит использовать #include "ifaces/INode.h" и т.д.
|
||||||
|
target_include_directories(tree_app PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include)
|
||||||
|
|
||||||
|
# Сообщение для удобства
|
||||||
|
message(STATUS "Project configured. To build, run: cmake --build .")
|
||||||
|
message(STATUS "To run the application: ./tree_app")
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
all: build run
|
||||||
|
|
||||||
|
clean:
|
||||||
|
rm -rf build/
|
||||||
|
|
||||||
|
build:
|
||||||
|
cmake -B build
|
||||||
|
|
||||||
|
compile: build
|
||||||
|
cmake --build build
|
||||||
|
|
||||||
|
run: compile
|
||||||
|
./build/tree_app
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
#pragma once
|
||||||
|
class INode;
|
||||||
|
class ILink;
|
||||||
|
using NodePtr = std::shared_ptr<INode>;
|
||||||
|
using LinkPtr = std::shared_ptr<ILink>;
|
||||||
@@ -0,0 +1,16 @@
|
|||||||
|
#pragma once
|
||||||
|
#include <memory>
|
||||||
|
#include <vector>
|
||||||
|
#include "ifaces/ILinkMixin.h"
|
||||||
|
|
||||||
|
template <class TElem>
|
||||||
|
class ILink {
|
||||||
|
public:
|
||||||
|
virtual ~ILink() = default;
|
||||||
|
virtual std::shared_ptr<TElem> getNode() const = 0;
|
||||||
|
virtual std::shared_ptr<TElem> getParent() const = 0;
|
||||||
|
virtual void setParent(const std::shared_ptr<TElem>& parent) = 0;
|
||||||
|
virtual const std::vector<std::shared_ptr<TElem>>& getChildren() const = 0;
|
||||||
|
virtual void addChild(const std::shared_ptr<TElem>& child) = 0;
|
||||||
|
virtual void removeChild(const std::shared_ptr<TElem>& child) = 0;
|
||||||
|
};
|
||||||
@@ -0,0 +1,16 @@
|
|||||||
|
#pragma once
|
||||||
|
#include <memory>
|
||||||
|
#include "ifaces/ILink.h"
|
||||||
|
|
||||||
|
class ILinkMixin;
|
||||||
|
|
||||||
|
using MixinPtr = std::shared_ptr<ILinkMixin>;
|
||||||
|
using LinkPtr = std::shared_ptr<ILink<ILinkMixin>>;
|
||||||
|
|
||||||
|
class ILinkMixin {
|
||||||
|
public:
|
||||||
|
virtual ~ILinkMixin() = default;
|
||||||
|
virtual void linkChild(const MixinPtr& child) = 0;
|
||||||
|
virtual void unlinkParent() = 0;
|
||||||
|
virtual LinkPtr getLink() = 0;
|
||||||
|
};
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
#pragma once
|
||||||
|
#include "ifaces/ILinkMixin.h"
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
class INode;
|
||||||
|
|
||||||
|
using NodePtr = std::shared_ptr<INode>;
|
||||||
|
|
||||||
|
class INode : public virtual ILinkMixin {
|
||||||
|
public:
|
||||||
|
~INode() = default;
|
||||||
|
virtual const std::string& name() const = 0;
|
||||||
|
};
|
||||||
@@ -0,0 +1,26 @@
|
|||||||
|
#pragma once
|
||||||
|
#include <vector>
|
||||||
|
#include <algorithm>
|
||||||
|
|
||||||
|
#include "ifaces/ILink.h"
|
||||||
|
|
||||||
|
template <class TElem>
|
||||||
|
class BaseLink : public ILink<TElem> {
|
||||||
|
public:
|
||||||
|
BaseLink(std::shared_ptr<TElem> node) : owner_node_(node) {}
|
||||||
|
std::shared_ptr<TElem> getNode() const override { return owner_node_.lock(); }
|
||||||
|
std::shared_ptr<TElem> getParent() const override { return parent_.lock(); }
|
||||||
|
void setParent(const std::shared_ptr<TElem>& parent) override { parent_ = parent; }
|
||||||
|
const std::vector<std::shared_ptr<TElem>>& getChildren() const override { return children_; }
|
||||||
|
void removeChild(const std::shared_ptr<TElem>& child) override {
|
||||||
|
children_.erase(std::remove(children_.begin(), children_.end(), child), children_.end());
|
||||||
|
}
|
||||||
|
|
||||||
|
~BaseLink() override {
|
||||||
|
std::cout << "--- Destructor called for: " << "BaseLink" << "\n";
|
||||||
|
}
|
||||||
|
protected:
|
||||||
|
std::vector<std::shared_ptr<TElem>> children_;
|
||||||
|
std::weak_ptr<TElem> owner_node_;
|
||||||
|
std::weak_ptr<TElem> parent_;
|
||||||
|
};
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
#pragma once
|
||||||
|
#include "links/BaseLink.h"
|
||||||
|
#include <stdexcept>
|
||||||
|
|
||||||
|
class LeafLink : public BaseLink {
|
||||||
|
public:
|
||||||
|
using BaseLink::BaseLink;
|
||||||
|
void addChild(const NodePtr&) override { throw std::logic_error("LeafLink cannot have children"); }
|
||||||
|
};
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
#pragma once
|
||||||
|
#include "links/BaseLink.h"
|
||||||
|
|
||||||
|
template <class TElem>
|
||||||
|
class OneToManyLink : public BaseLink<TElem> {
|
||||||
|
public:
|
||||||
|
OneToManyLink(std::shared_ptr<TElem> e) : BaseLink<TElem>(e) {}
|
||||||
|
void addChild(const std::shared_ptr<TElem>& child) override { this->children_.push_back(child); }
|
||||||
|
};
|
||||||
@@ -0,0 +1,38 @@
|
|||||||
|
#pragma once
|
||||||
|
#include <iostream>
|
||||||
|
#include <memory>
|
||||||
|
|
||||||
|
#include "ifaces/ILinkMixin.h"
|
||||||
|
|
||||||
|
class BaseLinkMixin : public virtual ILinkMixin,
|
||||||
|
public std::enable_shared_from_this<ILinkMixin> {
|
||||||
|
public:
|
||||||
|
void linkChild(const MixinPtr& child) override {
|
||||||
|
LinkPtr childLink = child->getLink();
|
||||||
|
childLink->setParent(getNode());
|
||||||
|
|
||||||
|
getLink()->addChild(child);
|
||||||
|
}
|
||||||
|
|
||||||
|
void unlinkParent() override {
|
||||||
|
LinkPtr link = getLink();
|
||||||
|
|
||||||
|
MixinPtr 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:
|
||||||
|
MixinPtr getNode() {
|
||||||
|
return shared_from_this();
|
||||||
|
}
|
||||||
|
};
|
||||||
@@ -0,0 +1,36 @@
|
|||||||
|
#pragma once
|
||||||
|
#include <iostream>
|
||||||
|
#include "mixins/BaseLinkMixin.h"
|
||||||
|
#include <memory>
|
||||||
|
|
||||||
|
template <class TLink>
|
||||||
|
class LazyLinkMixin : public BaseLinkMixin {
|
||||||
|
public:
|
||||||
|
void unlinkParent() override {
|
||||||
|
/* No link -- no parent, who'll unlinked? */
|
||||||
|
if (!this->link_)
|
||||||
|
throw std::logic_error("Link isn't inited!");
|
||||||
|
|
||||||
|
BaseLinkMixin::unlinkParent();
|
||||||
|
}
|
||||||
|
|
||||||
|
LinkPtr getLink() override {
|
||||||
|
lazyInit();
|
||||||
|
return this->link_;
|
||||||
|
}
|
||||||
|
|
||||||
|
LazyLinkMixin() {}
|
||||||
|
|
||||||
|
~LazyLinkMixin() override {
|
||||||
|
std::cout << "--- Destructor called for: " << "LazyLinkMixin" << "\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
protected:
|
||||||
|
void lazyInit() {
|
||||||
|
if (!link_) {
|
||||||
|
link_ = std::make_shared<TLink>(
|
||||||
|
BaseLinkMixin::getNode());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
LinkPtr link_;
|
||||||
|
};
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
#pragma once
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
#include "ifaces/INode.h"
|
||||||
|
|
||||||
|
class BaseNode : public virtual INode {
|
||||||
|
public:
|
||||||
|
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:
|
||||||
|
std::string name_;
|
||||||
|
};
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
#pragma once
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
#include "nodes/BaseNode.h"
|
||||||
|
#include "mixins/LazyLinkMixin.h"
|
||||||
|
#include "links/OneToManyLink.h"
|
||||||
|
|
||||||
|
class SimpleNode : public BaseNode,
|
||||||
|
virtual public LazyLinkMixin<OneToManyLink<ILinkMixin>> {
|
||||||
|
public:
|
||||||
|
~SimpleNode() {
|
||||||
|
std::cout << "--- Simple destructor called for: " << name_ << "\n";
|
||||||
|
}
|
||||||
|
SimpleNode(std::string name) : BaseNode(std::move(name)) {
|
||||||
|
std::cout << "--- Simple constructor called for: " << name_ << "\n";
|
||||||
|
}
|
||||||
|
};
|
||||||
@@ -0,0 +1,63 @@
|
|||||||
|
#include "nodes/SimpleNode.h"
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
void printTree(const MixinPtr& start, int indent = 0) {
|
||||||
|
if (!start) return;
|
||||||
|
auto startNode = std::dynamic_pointer_cast<INode>(start);
|
||||||
|
if (!startNode) return;
|
||||||
|
for (int i = 0; i < indent; ++i) std::cout << " ";
|
||||||
|
std::cout << startNode->name() << "\n";
|
||||||
|
LinkPtr nodeLink = startNode->getLink();
|
||||||
|
for (const auto& child : nodeLink->getChildren()) {
|
||||||
|
printTree(child, indent + 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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>("Child1");
|
||||||
|
|
||||||
|
root->linkChild(child2);
|
||||||
|
std::cout << "\nInit tree:\n\n\n";
|
||||||
|
printTree(root);
|
||||||
|
|
||||||
|
|
||||||
|
{
|
||||||
|
auto child1 = std::make_shared<SimpleNode>("Child2");
|
||||||
|
root->linkChild(child1);
|
||||||
|
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>("SubChild3");
|
||||||
|
child3->linkChild(subchild2);
|
||||||
|
|
||||||
|
std::cout << "\nAnother tree:\n\n\n";
|
||||||
|
printTree(root);
|
||||||
|
|
||||||
|
std::cout << "\nUnlinking Child1...\n";
|
||||||
|
child1->unlinkParent();
|
||||||
|
std::cout << "\nTree after unlink:\n\n\n";
|
||||||
|
printTree(root);
|
||||||
|
std::cout << "\n\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";
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@@ -1,306 +0,0 @@
|
|||||||
#include <iostream>
|
|
||||||
#include <vector>
|
|
||||||
#include <memory>
|
|
||||||
#include <mutex>
|
|
||||||
#include <algorithm>
|
|
||||||
#include <stdexcept>
|
|
||||||
|
|
||||||
class INode;
|
|
||||||
class ILink;
|
|
||||||
class ILinkMixin;
|
|
||||||
|
|
||||||
using NodePtr = std::shared_ptr<INode>;
|
|
||||||
using LinkPtr = std::shared_ptr<ILink>;
|
|
||||||
using LinkMixinPtr = std::shared_ptr<ILinkMixin>;
|
|
||||||
|
|
||||||
class ILink {
|
|
||||||
public:
|
|
||||||
virtual ~ILink() = default;
|
|
||||||
|
|
||||||
virtual LinkPtr getParent() const = 0;
|
|
||||||
virtual void setParent(LinkPtr parent) = 0;
|
|
||||||
|
|
||||||
virtual const std::vector<LinkPtr>& getChildren() const = 0;
|
|
||||||
virtual void addChild(LinkPtr child) = 0;
|
|
||||||
virtual void removeChild(LinkPtr child) = 0;
|
|
||||||
|
|
||||||
virtual std::mutex& getLock() = 0;
|
|
||||||
virtual LinkMixinPtr getLinkMixin() const = 0;
|
|
||||||
};
|
|
||||||
|
|
||||||
class ILinkMixin {
|
|
||||||
public:
|
|
||||||
virtual ~ILinkMixin() = default;
|
|
||||||
virtual void linkChild(const NodePtr& child) = 0;
|
|
||||||
virtual void unlinkParent() = 0;
|
|
||||||
virtual void unlinkChild(const NodePtr& child) = 0;
|
|
||||||
virtual LinkPtr getLink() const = 0;
|
|
||||||
virtual NodePtr getNode() const = 0;
|
|
||||||
};
|
|
||||||
|
|
||||||
class INode {
|
|
||||||
public:
|
|
||||||
virtual ~INode() = default;
|
|
||||||
virtual LinkPtr getLink() const = 0;
|
|
||||||
virtual LinkMixinPtr getLinkMixin() const = 0;
|
|
||||||
};
|
|
||||||
|
|
||||||
class LeafLink : public ILink {
|
|
||||||
public:
|
|
||||||
explicit LeafLink(NodePtr node) : node_(std::move(node)) {}
|
|
||||||
|
|
||||||
LinkPtr getParent() const override { return parent_; }
|
|
||||||
void setParent(LinkPtr parent) override { parent_ = std::move(parent); }
|
|
||||||
|
|
||||||
const std::vector<LinkPtr>& getChildren() const override {
|
|
||||||
static std::vector<LinkPtr> empty;
|
|
||||||
return empty;
|
|
||||||
}
|
|
||||||
|
|
||||||
void addChild(LinkPtr) override {
|
|
||||||
throw std::logic_error("LeafLink cannot have children");
|
|
||||||
}
|
|
||||||
|
|
||||||
void removeChild(LinkPtr) override {}
|
|
||||||
|
|
||||||
std::mutex& getLock() override { return lock_; }
|
|
||||||
LinkMixinPtr getLinkMixin() const override { return node_->getLinkMixin(); }
|
|
||||||
|
|
||||||
private:
|
|
||||||
NodePtr node_;
|
|
||||||
LinkPtr parent_;
|
|
||||||
mutable std::mutex lock_;
|
|
||||||
};
|
|
||||||
|
|
||||||
class OneToManyLink : public ILink {
|
|
||||||
public:
|
|
||||||
explicit OneToManyLink(NodePtr node) : node_(std::move(node)) {}
|
|
||||||
|
|
||||||
LinkPtr getParent() const override { return parent_; }
|
|
||||||
void setParent(LinkPtr parent) override { parent_ = std::move(parent); }
|
|
||||||
|
|
||||||
const std::vector<LinkPtr>& getChildren() const override { return children_; }
|
|
||||||
|
|
||||||
void addChild(LinkPtr child) override { children_.push_back(std::move(child)); }
|
|
||||||
|
|
||||||
void removeChild(LinkPtr child) override {
|
|
||||||
children_.erase(std::remove(children_.begin(), children_.end(), child), children_.end());
|
|
||||||
}
|
|
||||||
|
|
||||||
std::mutex& getLock() override { return lock_; }
|
|
||||||
LinkMixinPtr getLinkMixin() const override { return node_->getLinkMixin(); }
|
|
||||||
|
|
||||||
private:
|
|
||||||
NodePtr node_;
|
|
||||||
LinkPtr parent_;
|
|
||||||
std::vector<LinkPtr> children_;
|
|
||||||
mutable std::mutex lock_;
|
|
||||||
};
|
|
||||||
|
|
||||||
class OneToOneLink : public ILink {
|
|
||||||
public:
|
|
||||||
explicit OneToOneLink(NodePtr node) : node_(std::move(node)) {}
|
|
||||||
|
|
||||||
LinkPtr getParent() const override { return parent_; }
|
|
||||||
void setParent(LinkPtr parent) override { parent_ = std::move(parent); }
|
|
||||||
|
|
||||||
const std::vector<LinkPtr>& getChildren() const override { return singleChildVec_; }
|
|
||||||
|
|
||||||
void addChild(LinkPtr child) override {
|
|
||||||
if (!singleChildVec_.empty()) {
|
|
||||||
throw std::logic_error("OneToOneLink already has a child");
|
|
||||||
}
|
|
||||||
singleChildVec_.push_back(std::move(child));
|
|
||||||
}
|
|
||||||
|
|
||||||
void removeChild(LinkPtr child) override {
|
|
||||||
if (!singleChildVec_.empty() && singleChildVec_.front() == child) {
|
|
||||||
singleChildVec_.clear();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
std::mutex& getLock() override { return lock_; }
|
|
||||||
LinkMixinPtr getLinkMixin() const override { return node_->getLinkMixin(); }
|
|
||||||
|
|
||||||
private:
|
|
||||||
NodePtr node_;
|
|
||||||
LinkPtr parent_;
|
|
||||||
std::vector<LinkPtr> singleChildVec_;
|
|
||||||
mutable std::mutex lock_;
|
|
||||||
};
|
|
||||||
|
|
||||||
// Note: TypedOneToManyLink ensures type-safety on child linkage
|
|
||||||
|
|
||||||
template <typename TChild>
|
|
||||||
class TypedOneToManyLink : public OneToManyLink {
|
|
||||||
public:
|
|
||||||
explicit TypedOneToManyLink(NodePtr node) : OneToManyLink(std::move(node)) {}
|
|
||||||
|
|
||||||
void addChild(LinkPtr child) override {
|
|
||||||
NodePtr n = child->getLinkMixin()->getNode();
|
|
||||||
if (!std::dynamic_pointer_cast<TChild>(n)) {
|
|
||||||
throw std::invalid_argument("TypedOneToManyLink: child is not of expected type");
|
|
||||||
}
|
|
||||||
OneToManyLink::addChild(std::move(child));
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
// Note: BaseLinkMixin is used for direct eager link composition (non-lazy)
|
|
||||||
|
|
||||||
class BaseLinkMixin : public ILinkMixin {
|
|
||||||
public:
|
|
||||||
explicit BaseLinkMixin(NodePtr self) {
|
|
||||||
link_ = std::make_shared<OneToManyLink>(self);
|
|
||||||
}
|
|
||||||
|
|
||||||
void linkChild(const NodePtr& child) override {
|
|
||||||
LinkPtr childLink = child->getLink();
|
|
||||||
childLink->setParent(link_);
|
|
||||||
link_->addChild(childLink);
|
|
||||||
}
|
|
||||||
|
|
||||||
void unlinkParent() override {
|
|
||||||
if (!link_ || !link_->getParent()) return;
|
|
||||||
auto parent = link_->getParent();
|
|
||||||
parent->getLinkMixin()->unlinkChild(getNode());
|
|
||||||
link_->setParent(nullptr);
|
|
||||||
}
|
|
||||||
|
|
||||||
void unlinkChild(const NodePtr& child) override {
|
|
||||||
LinkPtr childLink = child->getLink();
|
|
||||||
link_->removeChild(childLink);
|
|
||||||
childLink->setParent(nullptr);
|
|
||||||
}
|
|
||||||
|
|
||||||
LinkPtr getLink() const override { return link_; }
|
|
||||||
NodePtr getNode() const override { return self_; }
|
|
||||||
|
|
||||||
private:
|
|
||||||
LinkPtr link_;
|
|
||||||
NodePtr self_;
|
|
||||||
};
|
|
||||||
|
|
||||||
// Note: LazyLinkMixin starts with LeafLink and upgrades dynamically
|
|
||||||
|
|
||||||
template <typename TNode>
|
|
||||||
class LazyLinkMixin : public ILinkMixin {
|
|
||||||
public:
|
|
||||||
explicit LazyLinkMixin(NodePtr self) : self_(self) {
|
|
||||||
link_ = std::make_shared<LeafLink>(self);
|
|
||||||
}
|
|
||||||
|
|
||||||
void linkChild(const NodePtr& child) override {
|
|
||||||
ensureInitialized(child);
|
|
||||||
|
|
||||||
LinkPtr childLink = child->getLink();
|
|
||||||
childLink->setParent(link_);
|
|
||||||
link_->addChild(childLink);
|
|
||||||
}
|
|
||||||
|
|
||||||
void unlinkParent() override {
|
|
||||||
if (!link_ || !link_->getParent()) return;
|
|
||||||
auto parent = link_->getParent();
|
|
||||||
parent->getLinkMixin()->unlinkChild(getNode());
|
|
||||||
link_->setParent(nullptr);
|
|
||||||
|
|
||||||
if (link_->getChildren().empty()) {
|
|
||||||
link_ = std::make_shared<LeafLink>(getNode());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void unlinkChild(const NodePtr& child) override {
|
|
||||||
if (!link_) return;
|
|
||||||
LinkPtr childLink = child->getLink();
|
|
||||||
link_->removeChild(childLink);
|
|
||||||
childLink->setParent(nullptr);
|
|
||||||
|
|
||||||
if (link_->getChildren().empty()) {
|
|
||||||
link_ = std::make_shared<LeafLink>(getNode());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
LinkPtr getLink() const override { return link_; }
|
|
||||||
NodePtr getNode() const override { return self_; }
|
|
||||||
|
|
||||||
private:
|
|
||||||
void ensureInitialized(const NodePtr& child) {
|
|
||||||
if (link_ && !std::dynamic_pointer_cast<LeafLink>(link_)) return;
|
|
||||||
|
|
||||||
LinkPtr oldLink = link_;
|
|
||||||
LinkPtr newLink;
|
|
||||||
|
|
||||||
if (std::dynamic_pointer_cast<TNode>(child)) {
|
|
||||||
newLink = std::make_shared<TypedOneToManyLink<TNode>>(getNode());
|
|
||||||
} else {
|
|
||||||
newLink = std::make_shared<OneToOneLink>(getNode());
|
|
||||||
}
|
|
||||||
|
|
||||||
newLink->setParent(oldLink->getParent());
|
|
||||||
link_ = newLink;
|
|
||||||
}
|
|
||||||
|
|
||||||
LinkPtr link_;
|
|
||||||
NodePtr self_;
|
|
||||||
};
|
|
||||||
|
|
||||||
class SimpleNode : public INode, public std::enable_shared_from_this<SimpleNode> {
|
|
||||||
public:
|
|
||||||
static std::shared_ptr<SimpleNode> create(std::string name) {
|
|
||||||
struct EnableMakeShared : public SimpleNode {
|
|
||||||
EnableMakeShared(std::string n) : SimpleNode(std::move(n)) {}
|
|
||||||
};
|
|
||||||
auto ptr = std::make_shared<EnableMakeShared>(std::move(name));
|
|
||||||
ptr->initMixin();
|
|
||||||
return ptr;
|
|
||||||
}
|
|
||||||
|
|
||||||
const std::string& name() const { return name_; }
|
|
||||||
LinkPtr getLink() const override { return mixin_->getLink(); }
|
|
||||||
LinkMixinPtr getLinkMixin() const override { return mixin_; }
|
|
||||||
|
|
||||||
void linkChild(const std::shared_ptr<SimpleNode>& child) { mixin_->linkChild(child); }
|
|
||||||
void unlinkParent() { mixin_->unlinkParent(); }
|
|
||||||
|
|
||||||
private:
|
|
||||||
explicit SimpleNode(std::string name) : name_(std::move(name)) {}
|
|
||||||
|
|
||||||
void initMixin() {
|
|
||||||
mixin_ = std::make_shared<LazyLinkMixin<SimpleNode>>(shared_from_this());
|
|
||||||
}
|
|
||||||
|
|
||||||
std::string name_;
|
|
||||||
LinkMixinPtr mixin_;
|
|
||||||
};
|
|
||||||
|
|
||||||
void printTree(const LinkPtr& link, int indent = 0) {
|
|
||||||
for (int i = 0; i < indent; ++i) std::cout << " ";
|
|
||||||
auto node = link->getLinkMixin()->getNode();
|
|
||||||
auto simpleNode = std::dynamic_pointer_cast<SimpleNode>(node);
|
|
||||||
if (simpleNode) {
|
|
||||||
std::cout << simpleNode->name() << "\n";
|
|
||||||
}
|
|
||||||
for (const auto& child : link->getChildren()) {
|
|
||||||
printTree(child, 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);
|
|
||||||
|
|
||||||
printTree(root->getLink());
|
|
||||||
|
|
||||||
std::cout << "\nUnlinking Child2...\n";
|
|
||||||
child2->unlinkParent();
|
|
||||||
|
|
||||||
printTree(root->getLink());
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user