iters
Сергей Маринкевич 5 months ago
parent 0912566c5f
commit f994303cb5

@ -13,5 +13,5 @@ public:
virtual void linkChild(const NodePtr& child) = 0; virtual void linkChild(const NodePtr& child) = 0;
virtual void unlinkParent() = 0; virtual void unlinkParent() = 0;
virtual LinkPtr getLink() = 0; virtual LinkPtr getLink() = 0;
virtual NodePtr getSelf() = 0; virtual NodePtr getNode() = 0;
}; };

@ -11,7 +11,7 @@ class LazyLinkMixin : public virtual ILinkMixin {
public: public:
void linkChild(const NodePtr& childNode) override { void linkChild(const NodePtr& childNode) override {
LinkPtr childLink = childNode->getLink(); LinkPtr childLink = childNode->getLink();
childLink->setParent(getSelf()); childLink->setParent(getNode());
getLink()->addChild(childNode); getLink()->addChild(childNode);
} }
@ -27,7 +27,7 @@ public:
LinkPtr parentLink = parent->getLink(); LinkPtr parentLink = parent->getLink();
parentLink->removeChild(getSelf()); parentLink->removeChild(getNode());
getLink()->setParent(nullptr); getLink()->setParent(nullptr);
} }
@ -41,13 +41,12 @@ public:
} }
protected: protected:
virtual NodePtr getShared() = 0; virtual NodePtr getNode() = 0;
NodePtr getSelf() override { return getShared(); }
private: private:
void lazyInit() { void lazyInit() {
if (!link_) { if (!link_) {
link_ = std::make_shared<TLink>(getSelf()); link_ = std::make_shared<TLink>(getNode());
} }
} }

@ -5,18 +5,15 @@
class BaseNode : public INode, class BaseNode : public INode,
public LazyLinkMixin<OneToManyLink>, public LazyLinkMixin<OneToManyLink>,
public std::enable_shared_from_this<BaseNode> { public std::enable_shared_from_this<INode> {
public: public:
//using LazyLinkMixin<OneToManyLink>::linkChild;
//using LazyLinkMixin<OneToManyLink>::unlinkParent;
//using LazyLinkMixin<OneToManyLink>::getLink;
BaseNode(std::string name) : name_(std::move(name)) {} BaseNode(std::string name) : name_(std::move(name)) {}
const std::string& name() const override { return name_; } const std::string& name() const override { return name_; }
~BaseNode() { ~BaseNode() {
std::cout << "--- Destructor called for: " << name_ << "\n"; std::cout << "--- Base destructor called for: " << name_ << "\n";
} }
protected: protected:
NodePtr getShared() override { NodePtr getNode() override {
return shared_from_this(); return shared_from_this();
} }
std::string name_; std::string name_;

@ -2,11 +2,10 @@
#include "nodes/BaseNode.h" #include "nodes/BaseNode.h"
#include <iostream> #include <iostream>
class SimpleNode : class SimpleNode : public BaseNode {
public BaseNode {
public: public:
~SimpleNode() { ~SimpleNode() {
std::cout << "--- Destructor called for: " << name_ << "\n"; std::cout << "--- Simple destructor called for: " << name_ << "\n";
} }
SimpleNode(std::string name) : BaseNode(std::move(name)) {} SimpleNode(std::string name) : BaseNode(std::move(name)) {}
}; };

@ -1,4 +1,4 @@
#include "nodes/SimpleNode.h" // Путь изменен #include "nodes/SimpleNode.h"
#include <iostream> #include <iostream>
void printTree(const NodePtr& startNode, int indent = 0) { void printTree(const NodePtr& startNode, int indent = 0) {
@ -14,7 +14,6 @@ void printTree(const NodePtr& startNode, int indent = 0) {
int main() { int main() {
std::cout << "Entering main scope...\n\n"; std::cout << "Entering main scope...\n\n";
// Начало новой области видимости
{ {
auto root = std::make_shared<SimpleNode>("Root"); auto root = std::make_shared<SimpleNode>("Root");
auto child2 = std::make_shared<SimpleNode>("Child2"); auto child2 = std::make_shared<SimpleNode>("Child2");
@ -43,7 +42,7 @@ int main() {
std::cout << "\nTree after scope out:\n"; std::cout << "\nTree after scope out:\n";
printTree(root); printTree(root);
} // <--- КОНЕЦ ОБЛАСТИ ВИДИМОСТИ }
std::cout << "\nExited main scope. All smart pointers destroyed.\n"; std::cout << "\nExited main scope. All smart pointers destroyed.\n";

Loading…
Cancel
Save