This commit is contained in:
Сергей Маринкевич
2025-08-05 19:30:21 +07:00
parent f46e1a6d0b
commit d00bb90a38
11 changed files with 96 additions and 43 deletions
+2
View File
@@ -11,6 +11,8 @@ public:
using ElemPtr = std::shared_ptr<TElem>;
virtual ~ILinkMixin() = default;
virtual operator std::shared_ptr<TElem>() = 0;
virtual operator TElem&() = 0;
virtual void linkChild(const ElemPtr& child) = 0;
virtual void unlinkParent() = 0;
virtual const std::vector<ElemPtr>& children() = 0;
+1 -1
View File
@@ -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_;
+1 -1
View File
@@ -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_;
+18 -2
View File
@@ -14,7 +14,15 @@ 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();
}
operator INode&() override {
return static_cast<INode&>(*this);
}
void linkChild(const ElemPtr& child) override {
@@ -33,7 +41,15 @@ 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);
}
+11
View File
@@ -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)...));
}
};
+3 -3
View File
@@ -16,7 +16,7 @@ class HierarchicalLinkMixin : public LazyLinkMixin<OneToOneLink<TElem>> {
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 {
@@ -34,10 +34,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<TElem>>(*this);
} else {
Logger::get("Mixin").dbg("--- Mutate to OneToOne");
newLink = std::make_shared<OneToOneLink<TElem>>(this->getNode());
newLink = std::make_shared<OneToOneLink<TElem>>(*this);
}
if (newLink && this->link_)
+2 -3
View File
@@ -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_;
+2 -3
View File
@@ -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_;
+8 -5
View File
@@ -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<INode>,
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_);
}
};
+7 -4
View File
@@ -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_);
}
};
+41 -21
View File
@@ -31,12 +31,16 @@ 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);
@@ -45,23 +49,21 @@ int main() {
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);
child3->linkChild(SimpleNode::create("SimpleSubChild3"));
{
// Негативный сценарий 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());
@@ -69,14 +71,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());
@@ -84,8 +84,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());
@@ -120,16 +119,36 @@ int main() {
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";
printTreeLadder(*root);
@@ -137,6 +156,7 @@ int main() {
}
logger.info("Exited main scope. All smart pointers destroyed.");
logger.info("(It don't? Check ConDes logger)");
return 0;
}