Compare commits
8 Commits
0f9d73366b
..
master
| Author | SHA1 | Date | |
|---|---|---|---|
| 79caaad58d | |||
| df1a728f56 | |||
| 10b79b8af6 | |||
| dedd7df19c | |||
| 5b515eaf19 | |||
| 784594faa6 | |||
| 6c5d9d7992 | |||
| f46e1a6d0b |
@@ -0,0 +1,21 @@
|
||||
#pragma once
|
||||
|
||||
#include "iterators/BaseIterator.h"
|
||||
|
||||
template <typename T>
|
||||
struct IteratorForward : public BaseIterator<T> {
|
||||
explicit IteratorForward(T* ptr) : Iterator<T>(first(ptr)) {}
|
||||
|
||||
static T* first(T* node) {
|
||||
return node;
|
||||
}
|
||||
|
||||
static T* next(T* node) {
|
||||
return node ? node->get_next() : nullptr;
|
||||
}
|
||||
|
||||
Iterator<T>& operator++() override {
|
||||
this->current = next(this->current);
|
||||
return *this;
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,14 @@
|
||||
#pragma once
|
||||
|
||||
/// \brief Интерфейс для классов-итераторов.
|
||||
/// \tparam T Тип итерируемого элемента.
|
||||
template <typename T>
|
||||
class IIterator {
|
||||
public:
|
||||
virtual T* operator->() const = 0;
|
||||
virtual operator const T*() const = 0;
|
||||
|
||||
virtual bool operator!=(const IIterator& other) const = 0;
|
||||
|
||||
virtual IIterator& operator++() = 0;
|
||||
};
|
||||
@@ -11,8 +11,10 @@ public:
|
||||
using ElemPtr = std::shared_ptr<TElem>;
|
||||
|
||||
virtual ~ILinkMixin() = default;
|
||||
virtual operator std::shared_ptr<TElem>() = 0;
|
||||
virtual void linkChild(const ElemPtr& child) = 0;
|
||||
virtual void unlinkParent() = 0;
|
||||
virtual const std::vector<ElemPtr>& children() = 0;
|
||||
virtual ElemPtr parent() = 0;
|
||||
virtual LinkPtr getLink() = 0;
|
||||
};
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
#pragma once
|
||||
#include <queue>
|
||||
|
||||
#include "iterators/BaseIterator.h"
|
||||
|
||||
template <typename T>
|
||||
class BFSIterator : public BaseIterator<T> {
|
||||
public:
|
||||
explicit BFSIterator(T* ptr) : BaseIterator<T>(ptr) {
|
||||
if (!this->current())
|
||||
return;
|
||||
|
||||
for (auto& it : this->current()->children())
|
||||
q.push({it.get(), 1});
|
||||
}
|
||||
|
||||
IIterator<T>& operator++() override {
|
||||
advance();
|
||||
return *this;
|
||||
}
|
||||
|
||||
protected:
|
||||
std::queue<std::pair<T*, size_t>> q;
|
||||
|
||||
void advance() {
|
||||
if (q.empty()) {
|
||||
this->set_current(nullptr);
|
||||
return;
|
||||
}
|
||||
|
||||
auto& [node, lvl] = q.front();
|
||||
q.pop();
|
||||
|
||||
this->set_current(node);
|
||||
this->set_level(lvl);
|
||||
|
||||
for (auto& it : this->current()->children())
|
||||
q.push({it.get(), lvl + 1});
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,42 @@
|
||||
#pragma once
|
||||
|
||||
#include "ifaces/IIterator.h"
|
||||
|
||||
/// \brief Базовый итератор.
|
||||
/// \tparam T Тип итерируемого элемента.
|
||||
/// Предоставляет базовую реализацию методов для работы с итерируемыми объектами.
|
||||
/// Возвращает объект-прокси, мимикрирующий под итерируемый элемент. Позволяет использовать
|
||||
/// распаковку структуры для получения уровня элемента в поддереве.
|
||||
template <typename T>
|
||||
class BaseIterator : public IIterator<T> {
|
||||
public:
|
||||
explicit BaseIterator(T* ptr) : proxy{ptr, 0} {}
|
||||
|
||||
struct Proxy {
|
||||
T* e;
|
||||
size_t level;
|
||||
|
||||
T& operator*() const { return *e; }
|
||||
T* operator->() const { return e; }
|
||||
};
|
||||
Proxy& operator*() { return proxy; }
|
||||
|
||||
T* operator->() const override { return current(); }
|
||||
operator const T*() const { return current(); }
|
||||
operator T&() const { return *current(); }
|
||||
operator T*() const { return current(); }
|
||||
|
||||
bool operator!=(const IIterator<T>& other) const override {
|
||||
const T* other_current = other;
|
||||
return current() != other_current;
|
||||
}
|
||||
|
||||
protected:
|
||||
T* current() const { return proxy.e; }
|
||||
void set_current(T* e) { proxy.e = e; }
|
||||
|
||||
size_t level() const { return proxy.level; }
|
||||
void set_level(size_t level) { proxy.level = level; }
|
||||
private:
|
||||
Proxy proxy;
|
||||
};
|
||||
@@ -0,0 +1,43 @@
|
||||
#pragma once
|
||||
#include <stack>
|
||||
#include <iterator>
|
||||
|
||||
#include "iterators/BaseIterator.h"
|
||||
|
||||
template <typename T>
|
||||
class DFSIterator : public BaseIterator<T> {
|
||||
public:
|
||||
explicit DFSIterator(T* ptr) : BaseIterator<T>(ptr) {
|
||||
if (!this->current())
|
||||
return;
|
||||
|
||||
for (auto it = this->current()->children().rbegin();
|
||||
it != this->current()->children().rend(); ++it)
|
||||
s.push({it->get(), 1});
|
||||
}
|
||||
|
||||
IIterator<T>& operator++() override {
|
||||
advance();
|
||||
return *this;
|
||||
}
|
||||
|
||||
protected:
|
||||
std::stack<std::pair<T*, size_t>> s;
|
||||
|
||||
void advance() {
|
||||
if (s.empty()) {
|
||||
this->set_current(nullptr);
|
||||
return;
|
||||
}
|
||||
|
||||
auto [node, lvl] = s.top();
|
||||
s.pop();
|
||||
|
||||
this->set_current(node);
|
||||
this->set_level(lvl);
|
||||
|
||||
for (auto it = this->current()->children().rbegin();
|
||||
it != this->current()->children().rend(); ++it)
|
||||
s.push({it->get(), lvl + 1});
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,27 @@
|
||||
#pragma once
|
||||
|
||||
#include "ifaces/INode.h"
|
||||
|
||||
#include "iterators/DFSIterator.h"
|
||||
#include "iterators/BFSIterator.h"
|
||||
|
||||
namespace traversal {
|
||||
/// \brief Фабрика итераторов узлов дерева.
|
||||
/// \tparam T Тип алгоритма для обхода дерева.
|
||||
/// Конструирует итерируемый объект с заданным алгоритмом обхода. Например, в ширину
|
||||
/// или глубину.
|
||||
template<typename Policy>
|
||||
class Traversal {
|
||||
INode* start;
|
||||
|
||||
public:
|
||||
explicit Traversal(INode* start) : start(start) {}
|
||||
explicit Traversal(NodePtr start) : start(start.get()) {}
|
||||
|
||||
Policy begin() const { return Policy(start); }
|
||||
Policy end() const { return Policy(nullptr); }
|
||||
};
|
||||
|
||||
using DFS = Traversal<DFSIterator<INode>>;
|
||||
using BFS = Traversal<BFSIterator<INode>>;
|
||||
}
|
||||
@@ -34,7 +34,7 @@ public:
|
||||
}
|
||||
|
||||
~BaseLink() override {
|
||||
Logger::get("Link").dbg("--- Destructor called for: BaseLink");
|
||||
Logger::get("ConDes").dbg("--- Destructor called for: BaseLink");
|
||||
}
|
||||
protected:
|
||||
std::vector<ElemPtr> children_;
|
||||
|
||||
@@ -3,10 +3,14 @@
|
||||
#include <stdexcept>
|
||||
|
||||
/// \brief Связь для листового узла, не допускающая дочерних элементов.
|
||||
class LeafLink : public BaseLink {
|
||||
template <class TElem>
|
||||
class LeafLink : public BaseLink<TElem> {
|
||||
public:
|
||||
using BaseLink::BaseLink;
|
||||
void addChild(const NodePtr&) override {
|
||||
throw std::logic_error("LeafLink cannot have children");
|
||||
using ElemPtr = std::shared_ptr<TElem>;
|
||||
|
||||
LeafLink(std::shared_ptr<TElem> e) : BaseLink<TElem>(e) {}
|
||||
|
||||
void addChild(const ElemPtr&) override {
|
||||
throw std::logic_error("Leaf cannot have children");
|
||||
}
|
||||
};
|
||||
|
||||
@@ -20,7 +20,7 @@ public:
|
||||
void removeChild(const ElemPtr& child) override { }
|
||||
|
||||
~NotImplementedLink() override {
|
||||
Logger::get("Link").dbg("--- Destructor called for: NotImplementedLink");
|
||||
Logger::get("ConDes").dbg("--- Destructor called for: NotImplementedLink");
|
||||
}
|
||||
private:
|
||||
std::vector<ElemPtr> empty_;
|
||||
|
||||
@@ -14,7 +14,11 @@ class BaseLinkMixin : public virtual ILinkMixin<INode>,
|
||||
|
||||
public:
|
||||
~BaseLinkMixin() override {
|
||||
Logger::get("Mixin").dbg("--- Destructor called for: BaseLinkMixin");
|
||||
Logger::get("ConDes").dbg("--- Destructor called for: BaseLinkMixin");
|
||||
}
|
||||
|
||||
operator std::shared_ptr<INode>() override {
|
||||
return this->getNode();
|
||||
}
|
||||
|
||||
void linkChild(const ElemPtr& child) override {
|
||||
@@ -33,10 +37,25 @@ public:
|
||||
|
||||
auto parentLink = parent->getLink();
|
||||
|
||||
parentLink->removeChild(getNode());
|
||||
/* NOTE:
|
||||
*
|
||||
* Keep a reference to the node we gonna to unlink.
|
||||
* Otherwise, we'll disappear between `removeChild` and
|
||||
* `setParent`. Do not rearrange these calls, because
|
||||
* we want to modify the tree top down.
|
||||
*/
|
||||
auto node = getNode();
|
||||
parentLink->removeChild(node);
|
||||
getLink()->setParent(nullptr);
|
||||
}
|
||||
|
||||
ElemPtr parent() override {
|
||||
auto link = getLink();
|
||||
|
||||
ElemPtr parent = link->getParent();
|
||||
|
||||
return parent;
|
||||
}
|
||||
|
||||
const std::vector<ElemPtr>& children() override {
|
||||
auto link = getLink();
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
#pragma once
|
||||
#include <memory>
|
||||
|
||||
template <typename T>
|
||||
class FabricMixin {
|
||||
public:
|
||||
template <typename... Args>
|
||||
static std::shared_ptr<T> create(Args&&... args) {
|
||||
return std::shared_ptr<T>(new T(std::forward<Args>(args)...));
|
||||
}
|
||||
};
|
||||
@@ -1,27 +1,30 @@
|
||||
#pragma once
|
||||
#include <iostream>
|
||||
#include "mixins/LazyLinkMixin.h"
|
||||
#include <memory>
|
||||
|
||||
#include "ifaces/INode.h"
|
||||
#include "mixins/LazyLinkMixin.h"
|
||||
#include "links/OneToManyLink.h"
|
||||
#include "links/OneToOneLink.h"
|
||||
#include "Logger.h"
|
||||
|
||||
/// \brief Миксин для иерархических связей между элементами.
|
||||
/// Автоматически выбирает тип связи (один-ко-многим или один-к-одному) в зависимости от типа дочернего элемента.
|
||||
/// \tparam TElem Тип дочернего элемента.
|
||||
template <class TElem>
|
||||
class HierarchicalLinkMixin : public LazyLinkMixin<OneToOneLink<TElem>> {
|
||||
using LinkPtr = std::shared_ptr<ILink<TElem>>;
|
||||
using ElemPtr = std::shared_ptr<TElem>;
|
||||
/// \tparam INode Тип дочернего элемента.
|
||||
/// Автоматически выбирает тип связи (один-ко-многим или один-к-одному) в зависимости от типа
|
||||
/// дочернего узла. Если тип дочернего узла совпадает с родителем, то используется связь
|
||||
/// один-ко-многим. При попытке подключить узел отличного типа выбирается связь один-к-одному.
|
||||
class HierarchicalLinkMixin : public LazyLinkMixin<OneToOneLink<INode>> {
|
||||
using LinkPtr = std::shared_ptr<ILink<INode>>;
|
||||
using ElemPtr = std::shared_ptr<INode>;
|
||||
|
||||
public:
|
||||
~HierarchicalLinkMixin() override {
|
||||
Logger::get("Mixin").dbg("--- Destructor called for: HierarchicalLinkMixin");
|
||||
Logger::get("ConDes").dbg("--- Destructor called for: HierarchicalLinkMixin");
|
||||
}
|
||||
|
||||
void linkChild(const ElemPtr& child) override {
|
||||
hierarchicalInit(child);
|
||||
LazyLinkMixin<OneToOneLink<TElem>>::linkChild(child);
|
||||
LazyLinkMixin<OneToOneLink<INode>>::linkChild(child);
|
||||
}
|
||||
|
||||
protected:
|
||||
@@ -34,10 +37,10 @@ protected:
|
||||
|
||||
if (typeid(*child) == typeid(*this)) {
|
||||
Logger::get("Mixin").dbg("--- Mutate to OneToMany");
|
||||
newLink = std::make_shared<OneToManyLink<TElem>>(this->getNode());
|
||||
newLink = std::make_shared<OneToManyLink<INode>>(*this);
|
||||
} else {
|
||||
Logger::get("Mixin").dbg("--- Mutate to OneToOne");
|
||||
newLink = std::make_shared<OneToOneLink<TElem>>(this->getNode());
|
||||
newLink = std::make_shared<OneToOneLink<INode>>(*this);
|
||||
}
|
||||
|
||||
if (newLink && this->link_)
|
||||
|
||||
@@ -25,14 +25,13 @@ public:
|
||||
LazyLinkMixin() {}
|
||||
|
||||
~LazyLinkMixin() override {
|
||||
Logger::get("Mixin").dbg("--- Destructor called for: LazyLinkMixin");
|
||||
Logger::get("ConDes").info("--- Destructor called for: LazyLinkMixin");
|
||||
}
|
||||
|
||||
protected:
|
||||
void lazyInit() {
|
||||
if (!link_) {
|
||||
link_ = std::make_shared<TLink>(
|
||||
BaseLinkMixin::getNode());
|
||||
link_ = std::make_shared<TLink>(*this);
|
||||
}
|
||||
}
|
||||
LinkPtr link_;
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
#pragma once
|
||||
#include <iostream>
|
||||
|
||||
#include "ifaces/INode.h"
|
||||
#include "Logger.h"
|
||||
@@ -9,12 +8,12 @@
|
||||
class BaseNode : public virtual INode {
|
||||
public:
|
||||
BaseNode(std::string name) : name_(std::move(name)) {
|
||||
Logger::get("Node").dbg(std::string("--- Base constructor called for: ") + name_);
|
||||
Logger::get("ConDes").dbg(std::string("--- Base constructor called for: ") + name_);
|
||||
}
|
||||
const std::string& name() const override { return name_; }
|
||||
const std::string& kind() const override { return kind_; }
|
||||
~BaseNode() {
|
||||
Logger::get("Node").dbg(std::string("--- Base destructor called for: ") + name_);
|
||||
Logger::get("ConDes").info(std::string("--- Base destructor called for: ") + name_);
|
||||
}
|
||||
protected:
|
||||
std::string name_;
|
||||
|
||||
@@ -1,19 +1,22 @@
|
||||
#pragma once
|
||||
#include <iostream>
|
||||
|
||||
#include "nodes/BaseNode.h"
|
||||
#include "mixins/HierarchicalLinkMixin.h"
|
||||
#include "mixins/FabricMixin.h"
|
||||
#include "Logger.h"
|
||||
|
||||
/// \brief Класс сложного (составного) узла дерева.
|
||||
/// Может содержать несколько дочерних ComplexNode и один SimpleNode.
|
||||
/// Может содержать несколько дочерних ComplexNode или один SimpleNode.
|
||||
class ComplexNode : public BaseNode,
|
||||
virtual public HierarchicalLinkMixin<INode> {
|
||||
virtual public HierarchicalLinkMixin,
|
||||
public FabricMixin<ComplexNode> {
|
||||
public:
|
||||
~ComplexNode() {
|
||||
Logger::get("Node").dbg(std::string("--- Complex destructor called for: ") + name_);
|
||||
Logger::get("ConDes").dbg(std::string("--- Complex destructor called for: ") + name_);
|
||||
}
|
||||
private:
|
||||
friend class FabricMixin<ComplexNode>;
|
||||
ComplexNode(std::string name) : BaseNode(std::move(name)) {
|
||||
Logger::get("Node").dbg(std::string("--- Complex constructor called for: ") + name_);
|
||||
Logger::get("ConDes").dbg(std::string("--- Complex constructor called for: ") + name_);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
#pragma once
|
||||
|
||||
#include "nodes/BaseNode.h"
|
||||
#include "mixins/LazyLinkMixin.h"
|
||||
#include "mixins/FabricMixin.h"
|
||||
#include "links/LeafLink.h"
|
||||
#include "Logger.h"
|
||||
|
||||
/// \brief Класс простого (листового) узла дерева.
|
||||
/// Может содержать только одного дочернего ComplexNode.
|
||||
class LeafNode : public BaseNode,
|
||||
virtual public LazyLinkMixin<LeafLink<INode>>,
|
||||
public FabricMixin<LeafNode> {
|
||||
public:
|
||||
~LeafNode() {
|
||||
Logger::get("ConDes").dbg(std::string("--- Leaf destructor called for: ") + name_);
|
||||
}
|
||||
private:
|
||||
friend class FabricMixin<LeafNode>;
|
||||
LeafNode(std::string name) : BaseNode(std::move(name)) {
|
||||
Logger::get("ConDes").dbg(std::string("--- Leaf constructor called for: ") + name_);
|
||||
}
|
||||
};
|
||||
@@ -1,20 +1,23 @@
|
||||
#pragma once
|
||||
#include <iostream>
|
||||
|
||||
#include "nodes/BaseNode.h"
|
||||
#include "mixins/LazyLinkMixin.h"
|
||||
#include "mixins/FabricMixin.h"
|
||||
#include "links/OneToOneLink.h"
|
||||
#include "Logger.h"
|
||||
|
||||
/// \brief Класс простого (листового) узла дерева.
|
||||
/// Может содержать только одного дочернего ComplexNode.
|
||||
class SimpleNode : public BaseNode,
|
||||
virtual public LazyLinkMixin<OneToOneLink<INode>> {
|
||||
virtual public LazyLinkMixin<OneToOneLink<INode>>,
|
||||
public FabricMixin<SimpleNode> {
|
||||
public:
|
||||
~SimpleNode() {
|
||||
Logger::get("Node").dbg(std::string("--- Simple destructor called for: ") + name_);
|
||||
Logger::get("ConDes").dbg(std::string("--- Simple destructor called for: ") + name_);
|
||||
}
|
||||
private:
|
||||
friend class FabricMixin<SimpleNode>;
|
||||
SimpleNode(std::string name) : BaseNode(std::move(name)) {
|
||||
Logger::get("Node").dbg(std::string("--- Simple constructor called for: ") + name_);
|
||||
Logger::get("ConDes").dbg(std::string("--- Simple constructor called for: ") + name_);
|
||||
}
|
||||
};
|
||||
|
||||
+85
-37
@@ -1,19 +1,30 @@
|
||||
#include "nodes/SimpleNode.h"
|
||||
#include "nodes/ComplexNode.h"
|
||||
#include <iostream>
|
||||
|
||||
#include "Logger.h"
|
||||
|
||||
void printTree(const NodePtr& node, int indent = 0) {
|
||||
if (!node) {
|
||||
Logger::get("MAIN").err("No node");
|
||||
return;
|
||||
}
|
||||
#include "nodes/SimpleNode.h"
|
||||
#include "nodes/ComplexNode.h"
|
||||
#include "nodes/LeafNode.h"
|
||||
#include "iterators/Traversal.h"
|
||||
|
||||
for (int i = 0; i < indent; ++i) std::cout << " ";
|
||||
std::cout << node->name() << "\n";
|
||||
|
||||
for (const auto& child : node->children())
|
||||
printTree(child, indent + 1);
|
||||
void printNode(INode& node, size_t level) {
|
||||
for (size_t i = level; i > 0; i--)
|
||||
std::cout << " ";
|
||||
std::cout << node.name() << "\n";
|
||||
}
|
||||
|
||||
void printTreeBFS(INode& node) {
|
||||
for (auto& child : traversal::BFS(&node))
|
||||
printNode(*child, 0);
|
||||
}
|
||||
void printTreeList(INode& node) {
|
||||
for (auto& child : traversal::DFS(&node))
|
||||
printNode(*child, 0);
|
||||
}
|
||||
void printTreeLadder(INode& node) {
|
||||
for (auto& [child, level] : traversal::DFS(&node))
|
||||
printNode(*child, level);
|
||||
}
|
||||
|
||||
int main() {
|
||||
@@ -21,37 +32,48 @@ int main() {
|
||||
Logger::suppressCategory("Node");
|
||||
Logger::suppressCategory("Link");
|
||||
Logger::suppressCategory("Mixin");
|
||||
|
||||
Logger::suppressCategory("ConDes");
|
||||
Logger::setMinSeverity("ConDes", Logger::Severity::Info);
|
||||
|
||||
auto& logger = Logger::get("MAIN");
|
||||
logger.info("Entering main scope...");
|
||||
|
||||
{
|
||||
auto root = std::make_shared<ComplexNode>("ComplexRoot");
|
||||
auto child1 = std::make_shared<ComplexNode>("ComplexChild1");
|
||||
auto root = ComplexNode::create("ComplexRoot");
|
||||
auto child1 = ComplexNode::create("ComplexChild1");
|
||||
|
||||
root->linkChild(child1);
|
||||
|
||||
std::cout << "\nInit tree:\n";
|
||||
printTree(root);
|
||||
printTreeLadder(*root);
|
||||
std::cout << "\n";
|
||||
|
||||
{
|
||||
auto child2 = std::make_shared<ComplexNode>("ComplexChild2");
|
||||
auto child2 = ComplexNode::create("ComplexChild2");
|
||||
root->linkChild(child2);
|
||||
|
||||
auto subchild2 = std::make_shared<SimpleNode>("SimpleSubChild2");
|
||||
auto subchild2 = SimpleNode::create("SimpleSubChild2");
|
||||
child2->linkChild(subchild2);
|
||||
|
||||
auto child3 = std::make_shared<ComplexNode>("ComplexChild3");
|
||||
auto child3 = ComplexNode::create("ComplexChild3");
|
||||
root->linkChild(child3);
|
||||
|
||||
auto subchild3 = std::make_shared<SimpleNode>("SimpleSubChild3");
|
||||
child3->linkChild(subchild3);
|
||||
auto leaf3 = LeafNode::create("LeafSubChild3");
|
||||
child3->linkChild(leaf3);
|
||||
|
||||
{
|
||||
// Негативный сценарий 0: попытка добавить SimpleNode к LeafNode
|
||||
try {
|
||||
leaf3->linkChild(SimpleNode::create("ShouldFail"));
|
||||
logger.err("[ERROR] Не должно было получиться добавить SimpleNode к LeafNode!");
|
||||
} catch (const std::logic_error& e) {
|
||||
logger.warn(std::string("[Ожидаемое исключение] ") + e.what());
|
||||
}
|
||||
|
||||
// Негативный сценарий 1: попытка добавить второй SimpleNode к ComplexNode
|
||||
try {
|
||||
auto anotherSimple = std::make_shared<SimpleNode>("ShouldFail");
|
||||
child2->linkChild(anotherSimple);
|
||||
child2->linkChild(SimpleNode::create("ShouldFail"));
|
||||
logger.err("[ERROR] Не должно было получиться добавить второй SimpleNode к ComplexNode!");
|
||||
} catch (const std::logic_error& e) {
|
||||
logger.warn(std::string("[Ожидаемое исключение] ") + e.what());
|
||||
@@ -59,14 +81,12 @@ int main() {
|
||||
|
||||
// Негативный сценарий 2: попытка добавить ComplexNode к SimpleNode
|
||||
// Это допустимо: SimpleNode может иметь ComplexNode в качестве единственного ребёнка
|
||||
auto goodComplex = std::make_shared<ComplexNode>("GoodComplex");
|
||||
subchild2->linkChild(goodComplex);
|
||||
subchild2->linkChild(ComplexNode::create("GoodComplex"));
|
||||
logger.info("[OK] ComplexNode успешно добавлен к SimpleNode как единственный ребёнок.");
|
||||
|
||||
// Негативный сценарий: попытка добавить второго ребёнка к SimpleNode
|
||||
try {
|
||||
auto anotherSimple = std::make_shared<SimpleNode>("ShouldFail2");
|
||||
subchild2->linkChild(anotherSimple);
|
||||
subchild2->linkChild(SimpleNode::create("ShouldFail2"));
|
||||
logger.err("[ERROR] Не должно было получиться добавить второго ребёнка к SimpleNode!");
|
||||
} catch (const std::logic_error& e) {
|
||||
logger.warn(std::string("[Ожидаемое исключение] ") + e.what());
|
||||
@@ -74,8 +94,7 @@ int main() {
|
||||
|
||||
// Негативный сценарий: попытка добавить SimpleNode в ComplexNode, который уже содержит несколько ComplexNode-дочерних
|
||||
try {
|
||||
auto badSimple = std::make_shared<SimpleNode>("BadSimple");
|
||||
root->linkChild(badSimple);
|
||||
root->linkChild(SimpleNode::create("BadSimple"));
|
||||
logger.err("[ERROR] Не должно было получиться добавить SimpleNode в ComplexNode с несколькими ComplexNode-дочерними!");
|
||||
} catch (const std::logic_error& e) {
|
||||
logger.warn(std::string("[Ожидаемое исключение] ") + e.what());
|
||||
@@ -83,42 +102,71 @@ int main() {
|
||||
}
|
||||
|
||||
std::cout << "\nAnother tree:\n";
|
||||
printTree(root);
|
||||
printTreeLadder(*root);
|
||||
std::cout << "\n";
|
||||
|
||||
std::cout << "\nList:\n";
|
||||
printTreeList(*root);
|
||||
std::cout << "\n";
|
||||
|
||||
std::cout << "\nBFS:\n";
|
||||
printTreeBFS(*root);
|
||||
std::cout << "\n";
|
||||
|
||||
logger.info("Unlinking ComplexChild2...\n");
|
||||
child2->unlinkParent();
|
||||
|
||||
std::cout << "\nTree after unlink:\n";
|
||||
printTree(root);
|
||||
printTreeLadder(*root);
|
||||
std::cout << "\n";
|
||||
|
||||
logger.info("Put refs of ComplexChild2, ComplexChild3 and its children");
|
||||
}
|
||||
|
||||
std::cout << "\nTree after scope out:\n";
|
||||
printTree(root);
|
||||
printTreeLadder(*root);
|
||||
std::cout << "\n";
|
||||
|
||||
logger.info("Unlinking ComplexChild1 and ComplexChild3...\n");
|
||||
child1->unlinkParent();
|
||||
for (auto child : root->getLink()->getChildren()) {
|
||||
NodePtr childNode = std::dynamic_pointer_cast<INode>(child);
|
||||
if (childNode->name() == "ComplexChild3") {
|
||||
childNode->unlinkParent();
|
||||
for (auto child : traversal::BFS(root)) {
|
||||
if (child->name() == "ComplexChild3") {
|
||||
/* Avoid of disconnecting a node of the tree
|
||||
* we're traversing. Method holds reference
|
||||
* to it's object itself. But iterator ignores
|
||||
* smart pointers, feeling free to dereference
|
||||
* the destroyed object.
|
||||
*/
|
||||
#if 1
|
||||
/* Keep in mind to keep your reference until
|
||||
* the cycle ends, if you want more than just
|
||||
* remove subtree.
|
||||
*/
|
||||
NodePtr node = *child;
|
||||
node->unlinkParent();
|
||||
std::cout << "Unlinked: "
|
||||
<< node->name() << std::endl;
|
||||
break;
|
||||
#else
|
||||
/* Otherwise, just break immediately you
|
||||
* unlinked a node. The current pointer is not
|
||||
* a valid pointer anymore.
|
||||
*/
|
||||
child->unlinkParent();
|
||||
break;
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
auto child4 = std::make_shared<SimpleNode>("SimpleChild4");
|
||||
root->linkChild(child4);
|
||||
root->linkChild(SimpleNode::create("SimpleChild4"));
|
||||
|
||||
std::cout << "\nTree flush and link SimpleChild4:\n";
|
||||
printTree(root);
|
||||
printTreeLadder(*root);
|
||||
std::cout << "\n";
|
||||
}
|
||||
|
||||
logger.info("Exited main scope. All smart pointers destroyed.");
|
||||
logger.info("(It don't? Check ConDes logger)");
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user