add hierarchical node
This commit is contained in:
@@ -0,0 +1,89 @@
|
|||||||
|
#pragma once
|
||||||
|
#include <string>
|
||||||
|
#include <iostream>
|
||||||
|
#include <unordered_map>
|
||||||
|
#include <unordered_set>
|
||||||
|
#include <memory>
|
||||||
|
#include <mutex>
|
||||||
|
|
||||||
|
class Logger {
|
||||||
|
public:
|
||||||
|
enum class Severity {
|
||||||
|
Debug,
|
||||||
|
Info,
|
||||||
|
Warning,
|
||||||
|
Error
|
||||||
|
};
|
||||||
|
|
||||||
|
// Получить логгер по категории (создаёт, если нет)
|
||||||
|
static Logger& get(const std::string& category) {
|
||||||
|
std::lock_guard<std::mutex> lock(registry_mutex_);
|
||||||
|
auto it = registry_.find(category);
|
||||||
|
if (it == registry_.end()) {
|
||||||
|
auto logger = std::unique_ptr<Logger>(new Logger(category));
|
||||||
|
auto [inserted_it, _] = registry_.emplace(category, std::move(logger));
|
||||||
|
return *(inserted_it->second);
|
||||||
|
}
|
||||||
|
return *(it->second);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Настроить suppression для категории
|
||||||
|
static void suppressCategory(const std::string& category) {
|
||||||
|
get(category).suppressed_ = true;
|
||||||
|
}
|
||||||
|
static void unsuppressCategory(const std::string& category) {
|
||||||
|
get(category).suppressed_ = false;
|
||||||
|
}
|
||||||
|
static void setMinSeverity(const std::string& category, Severity level) {
|
||||||
|
get(category).min_severity_ = level;
|
||||||
|
}
|
||||||
|
static Severity getMinSeverity(const std::string& category) {
|
||||||
|
return get(category).min_severity_;
|
||||||
|
}
|
||||||
|
static bool isSuppressed(const std::string& category) {
|
||||||
|
return get(category).suppressed_;
|
||||||
|
}
|
||||||
|
|
||||||
|
void log(const std::string& message, Severity severity = Severity::Info) const {
|
||||||
|
if (suppressed_) return;
|
||||||
|
if (severity < min_severity_) return;
|
||||||
|
std::cout << "[" << category_ << "] " << severityToString(severity) << ": " << message << std::endl << std::flush;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Удобные методы для логирования по уровню
|
||||||
|
void dbg(const std::string& message) const { log(message, Severity::Debug); }
|
||||||
|
void info(const std::string& message) const { log(message, Severity::Info); }
|
||||||
|
void warn(const std::string& message) const { log(message, Severity::Warning); }
|
||||||
|
void err(const std::string& message) const { log(message, Severity::Error); }
|
||||||
|
|
||||||
|
static std::string severityToString(Severity severity) {
|
||||||
|
switch (severity) {
|
||||||
|
case Severity::Debug: return "DEBUG";
|
||||||
|
case Severity::Info: return "INFO";
|
||||||
|
case Severity::Warning: return "WARNING";
|
||||||
|
case Severity::Error: return "ERROR";
|
||||||
|
default: return "UNKNOWN";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const std::string& category() const { return category_; }
|
||||||
|
Severity minSeverity() const { return min_severity_; }
|
||||||
|
void setMinSeverity(Severity s) { min_severity_ = s; }
|
||||||
|
bool suppressed() const { return suppressed_; }
|
||||||
|
void setSuppressed(bool s) { suppressed_ = s; }
|
||||||
|
|
||||||
|
private:
|
||||||
|
explicit Logger(std::string category)
|
||||||
|
: category_(std::move(category)), min_severity_(Severity::Debug), suppressed_(false) {}
|
||||||
|
|
||||||
|
std::string category_;
|
||||||
|
Severity min_severity_;
|
||||||
|
bool suppressed_;
|
||||||
|
|
||||||
|
static std::unordered_map<std::string, std::unique_ptr<Logger>> registry_;
|
||||||
|
static std::mutex registry_mutex_;
|
||||||
|
};
|
||||||
|
|
||||||
|
// Определения статических членов
|
||||||
|
inline std::unordered_map<std::string, std::unique_ptr<Logger>> Logger::registry_{};
|
||||||
|
inline std::mutex Logger::registry_mutex_;
|
||||||
@@ -13,4 +13,5 @@ public:
|
|||||||
virtual const std::vector<std::shared_ptr<TElem>>& getChildren() const = 0;
|
virtual const std::vector<std::shared_ptr<TElem>>& getChildren() const = 0;
|
||||||
virtual void addChild(const std::shared_ptr<TElem>& child) = 0;
|
virtual void addChild(const std::shared_ptr<TElem>& child) = 0;
|
||||||
virtual void removeChild(const std::shared_ptr<TElem>& child) = 0;
|
virtual void removeChild(const std::shared_ptr<TElem>& child) = 0;
|
||||||
|
virtual void replaceChild(const std::shared_ptr<TElem>& oldChild, const std::shared_ptr<TElem>& newChild) = 0;
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -10,4 +10,5 @@ class INode : public virtual ILinkMixin {
|
|||||||
public:
|
public:
|
||||||
~INode() = default;
|
~INode() = default;
|
||||||
virtual const std::string& name() const = 0;
|
virtual const std::string& name() const = 0;
|
||||||
|
virtual const std::string& kind() const = 0;
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -3,6 +3,7 @@
|
|||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
|
|
||||||
#include "ifaces/ILink.h"
|
#include "ifaces/ILink.h"
|
||||||
|
#include "Logger.h"
|
||||||
|
|
||||||
template <class TElem>
|
template <class TElem>
|
||||||
class BaseLink : public ILink<TElem> {
|
class BaseLink : public ILink<TElem> {
|
||||||
@@ -12,12 +13,24 @@ public:
|
|||||||
std::shared_ptr<TElem> getParent() const override { return parent_.lock(); }
|
std::shared_ptr<TElem> getParent() const override { return parent_.lock(); }
|
||||||
void setParent(const std::shared_ptr<TElem>& parent) override { parent_ = parent; }
|
void setParent(const std::shared_ptr<TElem>& parent) override { parent_ = parent; }
|
||||||
const std::vector<std::shared_ptr<TElem>>& getChildren() const override { return children_; }
|
const std::vector<std::shared_ptr<TElem>>& getChildren() const override { return children_; }
|
||||||
|
|
||||||
|
void addChild(const std::shared_ptr<TElem>& child) override {
|
||||||
|
this->children_.push_back(child);
|
||||||
|
}
|
||||||
|
|
||||||
void removeChild(const std::shared_ptr<TElem>& child) override {
|
void removeChild(const std::shared_ptr<TElem>& child) override {
|
||||||
children_.erase(std::remove(children_.begin(), children_.end(), child), children_.end());
|
children_.erase(std::remove(children_.begin(), children_.end(), child), children_.end());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void replaceChild(const std::shared_ptr<TElem>& oldChild, const std::shared_ptr<TElem>& newChild) override {
|
||||||
|
auto it = std::find(children_.begin(), children_.end(), oldChild);
|
||||||
|
if (it != children_.end()) {
|
||||||
|
*it = newChild;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
~BaseLink() override {
|
~BaseLink() override {
|
||||||
std::cout << "--- Destructor called for: " << "BaseLink" << "\n";
|
Logger::get("Link").dbg("--- Destructor called for: BaseLink");
|
||||||
}
|
}
|
||||||
protected:
|
protected:
|
||||||
std::vector<std::shared_ptr<TElem>> children_;
|
std::vector<std::shared_ptr<TElem>> children_;
|
||||||
|
|||||||
@@ -5,5 +5,7 @@
|
|||||||
class LeafLink : public BaseLink {
|
class LeafLink : public BaseLink {
|
||||||
public:
|
public:
|
||||||
using BaseLink::BaseLink;
|
using BaseLink::BaseLink;
|
||||||
void addChild(const NodePtr&) override { throw std::logic_error("LeafLink cannot have children"); }
|
void addChild(const NodePtr&) override {
|
||||||
|
throw std::logic_error("LeafLink cannot have children");
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -0,0 +1,24 @@
|
|||||||
|
#pragma once
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
#include "ifaces/ILink.h"
|
||||||
|
#include "Logger.h"
|
||||||
|
|
||||||
|
template <class TElem>
|
||||||
|
class NotImplementedLink : public ILink<TElem> {
|
||||||
|
using ElemPtr = std::shared_ptr<TElem>>;
|
||||||
|
public:
|
||||||
|
NotImplementedLink(ElemPtr node) {}
|
||||||
|
ElemPtr getNode() const override { return nullptr; }
|
||||||
|
ElemPtr getParent() const override { return nullptr; }
|
||||||
|
void setParent(const ElemPtr& parent) override { }
|
||||||
|
const std::vector<ElemPtr>& getChildren() const override { return empty_; }
|
||||||
|
void addChild(const ElemPtr& child) override { }
|
||||||
|
void removeChild(const ElemPtr& child) override { }
|
||||||
|
|
||||||
|
~NotImplementedLink() override {
|
||||||
|
Logger::get("Link").dbg("--- Destructor called for: NotImplementedLink");
|
||||||
|
}
|
||||||
|
private:
|
||||||
|
std::vector<ElemPtr> empty_;
|
||||||
|
};
|
||||||
@@ -5,5 +5,10 @@ template <class TElem>
|
|||||||
class OneToManyLink : public BaseLink<TElem> {
|
class OneToManyLink : public BaseLink<TElem> {
|
||||||
public:
|
public:
|
||||||
OneToManyLink(std::shared_ptr<TElem> e) : BaseLink<TElem>(e) {}
|
OneToManyLink(std::shared_ptr<TElem> e) : BaseLink<TElem>(e) {}
|
||||||
void addChild(const std::shared_ptr<TElem>& child) override { this->children_.push_back(child); }
|
void addChild(const std::shared_ptr<TElem>& child) override {
|
||||||
|
/* Each child must be exactly the same type as parent */
|
||||||
|
if (typeid(*child) != typeid(*this->owner_node_.lock()))
|
||||||
|
throw std::logic_error("Foundling child");
|
||||||
|
BaseLink<TElem>::addChild(child);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -0,0 +1,14 @@
|
|||||||
|
#pragma once
|
||||||
|
#include "links/BaseLink.h"
|
||||||
|
#include <stdexcept>
|
||||||
|
|
||||||
|
template <class TElem>
|
||||||
|
class OneToOneLink : public BaseLink<TElem> {
|
||||||
|
public:
|
||||||
|
OneToOneLink(std::shared_ptr<TElem> e) : BaseLink<TElem>(e) {}
|
||||||
|
void addChild(const std::shared_ptr<TElem>& child) override {
|
||||||
|
if (!this->children_.empty())
|
||||||
|
throw std::logic_error("OneToOneLink cannot have more than one child");
|
||||||
|
BaseLink<TElem>::addChild(child);
|
||||||
|
}
|
||||||
|
};
|
||||||
@@ -3,15 +3,16 @@
|
|||||||
#include <memory>
|
#include <memory>
|
||||||
|
|
||||||
#include "ifaces/ILinkMixin.h"
|
#include "ifaces/ILinkMixin.h"
|
||||||
|
#include "Logger.h"
|
||||||
|
|
||||||
class BaseLinkMixin : public virtual ILinkMixin,
|
class BaseLinkMixin : public virtual ILinkMixin,
|
||||||
public std::enable_shared_from_this<ILinkMixin> {
|
public std::enable_shared_from_this<ILinkMixin> {
|
||||||
public:
|
public:
|
||||||
void linkChild(const MixinPtr& child) override {
|
void linkChild(const MixinPtr& child) override {
|
||||||
|
getLink()->addChild(child);
|
||||||
|
|
||||||
LinkPtr childLink = child->getLink();
|
LinkPtr childLink = child->getLink();
|
||||||
childLink->setParent(getNode());
|
childLink->setParent(getNode());
|
||||||
|
|
||||||
getLink()->addChild(child);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void unlinkParent() override {
|
void unlinkParent() override {
|
||||||
@@ -28,7 +29,7 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
~BaseLinkMixin() override {
|
~BaseLinkMixin() override {
|
||||||
std::cout << "--- Destructor called for: " << "BaseLinkMixin" << "\n";
|
Logger::get("Mixin").dbg("--- Destructor called for: BaseLinkMixin");
|
||||||
}
|
}
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
|||||||
@@ -0,0 +1,49 @@
|
|||||||
|
#pragma once
|
||||||
|
#include <iostream>
|
||||||
|
#include "mixins/LazyLinkMixin.h"
|
||||||
|
#include <memory>
|
||||||
|
#include "links/OneToManyLink.h"
|
||||||
|
#include "links/OneToOneLink.h"
|
||||||
|
#include "Logger.h"
|
||||||
|
|
||||||
|
class HierarchicalLinkMixin : public LazyLinkMixin<OneToOneLink<ILinkMixin>> {
|
||||||
|
public:
|
||||||
|
HierarchicalLinkMixin() {}
|
||||||
|
|
||||||
|
~HierarchicalLinkMixin() override {
|
||||||
|
Logger::get("Mixin").dbg("--- Destructor called for: HierarchicalLinkMixin");
|
||||||
|
}
|
||||||
|
|
||||||
|
void linkChild(const MixinPtr& child) override {
|
||||||
|
hierarchicalInit(child);
|
||||||
|
LazyLinkMixin<OneToOneLink<ILinkMixin>>::linkChild(child);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected:
|
||||||
|
void hierarchicalInit(const MixinPtr& child) {
|
||||||
|
Logger::get("Mixin").dbg("--- hierarchicalInit called");
|
||||||
|
if (this->link_ && !this->link_->getChildren().empty())
|
||||||
|
return; // already have children, do nothing
|
||||||
|
|
||||||
|
|
||||||
|
/* 1. Have no link_ —
|
||||||
|
* 2. Has OneToOne with parent or not
|
||||||
|
* 3. Has OneToOne with child
|
||||||
|
*/
|
||||||
|
|
||||||
|
LinkPtr newLink;
|
||||||
|
|
||||||
|
if (typeid(*child) == typeid(*this)) {
|
||||||
|
Logger::get("Mixin").dbg("--- Mutate to OneToMany");
|
||||||
|
newLink = std::make_shared<OneToManyLink<ILinkMixin>>(this->getNode());
|
||||||
|
} else {
|
||||||
|
Logger::get("Mixin").dbg("--- Mutate to OneToOne");
|
||||||
|
newLink = std::make_shared<OneToOneLink<ILinkMixin>>(this->getNode());
|
||||||
|
}
|
||||||
|
|
||||||
|
if (newLink && this->link_)
|
||||||
|
newLink->setParent(this->link_->getParent());
|
||||||
|
|
||||||
|
this->link_ = newLink;
|
||||||
|
}
|
||||||
|
};
|
||||||
@@ -2,6 +2,7 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include "mixins/BaseLinkMixin.h"
|
#include "mixins/BaseLinkMixin.h"
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
#include "Logger.h"
|
||||||
|
|
||||||
template <class TLink>
|
template <class TLink>
|
||||||
class LazyLinkMixin : public BaseLinkMixin {
|
class LazyLinkMixin : public BaseLinkMixin {
|
||||||
@@ -22,7 +23,7 @@ public:
|
|||||||
LazyLinkMixin() {}
|
LazyLinkMixin() {}
|
||||||
|
|
||||||
~LazyLinkMixin() override {
|
~LazyLinkMixin() override {
|
||||||
std::cout << "--- Destructor called for: " << "LazyLinkMixin" << "\n";
|
Logger::get("Mixin").dbg("--- Destructor called for: LazyLinkMixin");
|
||||||
}
|
}
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
|||||||
@@ -2,16 +2,19 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
#include "ifaces/INode.h"
|
#include "ifaces/INode.h"
|
||||||
|
#include "Logger.h"
|
||||||
|
|
||||||
class BaseNode : public virtual INode {
|
class BaseNode : public virtual INode {
|
||||||
public:
|
public:
|
||||||
BaseNode(std::string name) : name_(std::move(name)) {
|
BaseNode(std::string name) : name_(std::move(name)) {
|
||||||
std::cout << "--- Base constructor called for: " << name_ << "\n";
|
Logger::get("Node").dbg(std::string("--- Base constructor called for: ") + name_);
|
||||||
}
|
}
|
||||||
const std::string& name() const override { return name_; }
|
const std::string& name() const override { return name_; }
|
||||||
|
const std::string& kind() const override { return kind_; }
|
||||||
~BaseNode() {
|
~BaseNode() {
|
||||||
std::cout << "--- Base destructor called for: " << name_ << "\n";
|
Logger::get("Node").dbg(std::string("--- Base destructor called for: ") + name_);
|
||||||
}
|
}
|
||||||
protected:
|
protected:
|
||||||
std::string name_;
|
std::string name_;
|
||||||
|
std::string kind_;
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -0,0 +1,17 @@
|
|||||||
|
#pragma once
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
#include "nodes/BaseNode.h"
|
||||||
|
#include "mixins/HierarchicalLinkMixin.h"
|
||||||
|
#include "Logger.h"
|
||||||
|
|
||||||
|
class ComplexNode : public BaseNode,
|
||||||
|
virtual public HierarchicalLinkMixin {
|
||||||
|
public:
|
||||||
|
~ComplexNode() {
|
||||||
|
Logger::get("Node").dbg(std::string("--- Complex destructor called for: ") + name_);
|
||||||
|
}
|
||||||
|
ComplexNode(std::string name) : BaseNode(std::move(name)) {
|
||||||
|
Logger::get("Node").dbg(std::string("--- Complex constructor called for: ") + name_);
|
||||||
|
}
|
||||||
|
};
|
||||||
@@ -3,15 +3,16 @@
|
|||||||
|
|
||||||
#include "nodes/BaseNode.h"
|
#include "nodes/BaseNode.h"
|
||||||
#include "mixins/LazyLinkMixin.h"
|
#include "mixins/LazyLinkMixin.h"
|
||||||
#include "links/OneToManyLink.h"
|
#include "links/OneToOneLink.h"
|
||||||
|
#include "Logger.h"
|
||||||
|
|
||||||
class SimpleNode : public BaseNode,
|
class SimpleNode : public BaseNode,
|
||||||
virtual public LazyLinkMixin<OneToManyLink<ILinkMixin>> {
|
virtual public LazyLinkMixin<OneToOneLink<ILinkMixin>> {
|
||||||
public:
|
public:
|
||||||
~SimpleNode() {
|
~SimpleNode() {
|
||||||
std::cout << "--- Simple destructor called for: " << name_ << "\n";
|
Logger::get("Node").dbg(std::string("--- Simple destructor called for: ") + name_);
|
||||||
}
|
}
|
||||||
SimpleNode(std::string name) : BaseNode(std::move(name)) {
|
SimpleNode(std::string name) : BaseNode(std::move(name)) {
|
||||||
std::cout << "--- Simple constructor called for: " << name_ << "\n";
|
Logger::get("Node").dbg(std::string("--- Simple constructor called for: ") + name_);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
+96
-33
@@ -1,63 +1,126 @@
|
|||||||
#include "nodes/SimpleNode.h"
|
#include "nodes/SimpleNode.h"
|
||||||
|
#include "nodes/ComplexNode.h"
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
#include "Logger.h"
|
||||||
|
|
||||||
void printTree(const MixinPtr& start, int indent = 0) {
|
void printTree(const NodePtr& startNode, int indent = 0) {
|
||||||
if (!start) return;
|
if (!startNode) {
|
||||||
auto startNode = std::dynamic_pointer_cast<INode>(start);
|
std::cout << "No node" << "\n";
|
||||||
if (!startNode) return;
|
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& child : nodeLink->getChildren()) {
|
for (const auto& child : nodeLink->getChildren()) {
|
||||||
printTree(child, indent + 1);
|
auto childNode = std::dynamic_pointer_cast<INode>(child);
|
||||||
|
printTree(childNode, 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() {
|
int main() {
|
||||||
std::cout << "Entering main scope...\n\n";
|
Logger::setMinSeverity("MAIN", Logger::Severity::Debug);
|
||||||
|
//Logger::suppressCategory("Node");
|
||||||
|
Logger::suppressCategory("Link");
|
||||||
|
Logger::suppressCategory("Mixin");
|
||||||
|
auto& logger = Logger::get("MAIN");
|
||||||
|
logger.info("Entering main scope...");
|
||||||
|
|
||||||
{
|
{
|
||||||
auto root = std::make_shared<SimpleNode>("Root");
|
auto root = std::make_shared<ComplexNode>("ComplexRoot");
|
||||||
auto child2 = std::make_shared<SimpleNode>("Child1");
|
auto child1 = std::make_shared<ComplexNode>("ComplexChild1");
|
||||||
|
|
||||||
root->linkChild(child2);
|
root->linkChild(child1);
|
||||||
std::cout << "\nInit tree:\n\n\n";
|
|
||||||
|
std::cout << "\nInit tree:\n";
|
||||||
printTree(root);
|
printTree(root);
|
||||||
|
std::cout << "\n";
|
||||||
|
|
||||||
|
|
||||||
{
|
{
|
||||||
auto child1 = std::make_shared<SimpleNode>("Child2");
|
auto child2 = std::make_shared<ComplexNode>("ComplexChild2");
|
||||||
root->linkChild(child1);
|
root->linkChild(child2);
|
||||||
auto subchild = std::make_shared<SimpleNode>("SubChild2");
|
|
||||||
child1->linkChild(subchild);
|
auto subchild2 = std::make_shared<SimpleNode>("SimpleSubChild2");
|
||||||
auto child3 = std::make_shared<SimpleNode>("Child3");
|
child2->linkChild(subchild2);
|
||||||
|
|
||||||
|
auto child3 = std::make_shared<ComplexNode>("ComplexChild3");
|
||||||
root->linkChild(child3);
|
root->linkChild(child3);
|
||||||
auto subchild2 = std::make_shared<SimpleNode>("SubChild3");
|
|
||||||
child3->linkChild(subchild2);
|
|
||||||
|
|
||||||
std::cout << "\nAnother tree:\n\n\n";
|
auto subchild3 = std::make_shared<SimpleNode>("SimpleSubChild3");
|
||||||
printTree(root);
|
child3->linkChild(subchild3);
|
||||||
|
|
||||||
std::cout << "\nUnlinking Child1...\n";
|
{
|
||||||
child1->unlinkParent();
|
// Негативный сценарий 1: попытка добавить второй SimpleNode к ComplexNode
|
||||||
std::cout << "\nTree after unlink:\n\n\n";
|
try {
|
||||||
|
auto anotherSimple = std::make_shared<SimpleNode>("ShouldFail");
|
||||||
|
child2->linkChild(anotherSimple);
|
||||||
|
logger.err("[ERROR] Не должно было получиться добавить второй SimpleNode к ComplexNode!");
|
||||||
|
} catch (const std::logic_error& e) {
|
||||||
|
logger.warn(std::string("[Ожидаемое исключение] ") + e.what());
|
||||||
|
}
|
||||||
|
|
||||||
|
// Негативный сценарий 2: попытка добавить ComplexNode к SimpleNode
|
||||||
|
// Это допустимо: SimpleNode может иметь ComplexNode в качестве единственного ребёнка
|
||||||
|
auto goodComplex = std::make_shared<ComplexNode>("GoodComplex");
|
||||||
|
subchild2->linkChild(goodComplex);
|
||||||
|
logger.info("[OK] ComplexNode успешно добавлен к SimpleNode как единственный ребёнок.");
|
||||||
|
|
||||||
|
// Негативный сценарий: попытка добавить второго ребёнка к SimpleNode
|
||||||
|
try {
|
||||||
|
auto anotherSimple = std::make_shared<SimpleNode>("ShouldFail2");
|
||||||
|
subchild2->linkChild(anotherSimple);
|
||||||
|
logger.err("[ERROR] Не должно было получиться добавить второго ребёнка к SimpleNode!");
|
||||||
|
} catch (const std::logic_error& e) {
|
||||||
|
logger.warn(std::string("[Ожидаемое исключение] ") + e.what());
|
||||||
|
}
|
||||||
|
|
||||||
|
// Негативный сценарий: попытка добавить SimpleNode в ComplexNode, который уже содержит несколько ComplexNode-дочерних
|
||||||
|
try {
|
||||||
|
auto badSimple = std::make_shared<SimpleNode>("BadSimple");
|
||||||
|
root->linkChild(badSimple);
|
||||||
|
logger.err("[ERROR] Не должно было получиться добавить SimpleNode в ComplexNode с несколькими ComplexNode-дочерними!");
|
||||||
|
} catch (const std::logic_error& e) {
|
||||||
|
logger.warn(std::string("[Ожидаемое исключение] ") + e.what());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
std::cout << "\nAnother tree:\n";
|
||||||
printTree(root);
|
printTree(root);
|
||||||
std::cout << "\n\n";
|
std::cout << "\n";
|
||||||
|
|
||||||
|
logger.info("Unlinking ComplexChild2...\n");
|
||||||
|
child2->unlinkParent();
|
||||||
|
|
||||||
|
std::cout << "\nTree after unlink:\n";
|
||||||
|
printTree(root);
|
||||||
|
std::cout << "\n";
|
||||||
|
|
||||||
|
logger.info("Put refs of ComplexChild2, ComplexChild3 and its children");
|
||||||
}
|
}
|
||||||
|
|
||||||
std::cout << "\nTree after scope out:\n\n\n";
|
std::cout << "\nTree after scope out:\n";
|
||||||
printTree(root);
|
printTree(root);
|
||||||
std::cout << "\n\n";
|
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();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
auto child4 = std::make_shared<SimpleNode>("SimpleChild4");
|
||||||
|
root->linkChild(child4);
|
||||||
|
|
||||||
|
std::cout << "\nTree flush and link SimpleChild4:\n";
|
||||||
|
printTree(root);
|
||||||
|
std::cout << "\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
std::cout << "\nExited main scope. All smart pointers destroyed.\n";
|
logger.info("Exited main scope. All smart pointers destroyed.");
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user