From 48655f30b12fe32d703e0c198422f4fa2957111b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=95=D0=BA=D0=B0=D1=82=D0=B5=D1=80=D0=B8=D0=BD=D0=B0=20?= =?UTF-8?q?=D0=92=D0=B0=D1=81=D0=B8=D0=BB=D1=8C=D0=B5=D0=B2=D0=B0?= <139324306+katvus@users.noreply.github.com> Date: Wed, 13 Dec 2023 21:01:23 +0300 Subject: [PATCH 1/3] Add files via upload --- node.cpp | 70 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ node.h | 25 ++++++++++++++++++++ 2 files changed, 95 insertions(+) create mode 100644 node.cpp create mode 100644 node.h diff --git a/node.cpp b/node.cpp new file mode 100644 index 00000000..bdef84cd --- /dev/null +++ b/node.cpp @@ -0,0 +1,70 @@ +#include "core/node.h" + +Node::Node(std::shared_ptr parent, std::string name) + : _name(name), _parent(parent) +{ + if (parent != nullptr) + { + parent->_child.push_back(shared_from_this()); + } +} + +Node::~Node() +{ + _child.clear(); + auto parent = _parent.lock(); + if (parent) { + parent->remove_child(shared_from_this()); + } +} + +std::shared_ptr Node::get_parent() const +{ + return _parent.lock(); +} + +std::vector> Node::get_children() const +{ + return _child; +} + +std::shared_ptr Node::add_child() +{ + Node child(*this); + std::shared_ptr child_ptr = std::make_shared(child); + _child.push_back(child_ptr); + return child_ptr; +} + +void Node::remove_child(std::shared_ptr del_child) +{ + std::vector>::iterator child_iter = _child.begin(); + auto del_child_iter = _child.end(); + while(child_iter != _child.end()) + { + if (*child_iter == del_child) + { + del_child_iter = child_iter; + break; + } + child_iter++; + } + if (del_child_iter == _child.end()) + { + //что нужно сделать в таком случае? + } + else + { + _child.erase(del_child_iter); + } +} + +std::string Node::get_name() const +{ + return _name; +} + +void Node::change_name(std::string new_name) +{ + _name = new_name; +} diff --git a/node.h b/node.h new file mode 100644 index 00000000..5c2bd39b --- /dev/null +++ b/node.h @@ -0,0 +1,25 @@ +#pragma once + +#include +#include +#include + +class Node: public std::enable_shared_from_this +{ +public: + explicit Node(std::shared_ptr parent = nullptr, std::string name = "without name"); + + ~Node(); + + std::shared_ptr get_parent() const; + std::vector> get_children() const; + std::shared_ptr add_child(); + void remove_child(std::shared_ptr child); + std::string get_name() const; + void change_name(std::string new_name); + +private: + const std::weak_ptr _parent; + std::vector> _child; + std::string _name; +}; From a2152a780a7092b438a9764655656cec230fe00d Mon Sep 17 00:00:00 2001 From: katvus Date: Wed, 13 Dec 2023 22:59:42 +0300 Subject: [PATCH 2/3] Changed location --- node.h => include/core/node.h | 50 ++++++------ node.cpp => src/core/node.cpp | 141 +++++++++++++++++----------------- 2 files changed, 96 insertions(+), 95 deletions(-) rename node.h => include/core/node.h (96%) rename node.cpp => src/core/node.cpp (91%) diff --git a/node.h b/include/core/node.h similarity index 96% rename from node.h rename to include/core/node.h index 5c2bd39b..3458d719 100644 --- a/node.h +++ b/include/core/node.h @@ -1,25 +1,25 @@ -#pragma once - -#include -#include -#include - -class Node: public std::enable_shared_from_this -{ -public: - explicit Node(std::shared_ptr parent = nullptr, std::string name = "without name"); - - ~Node(); - - std::shared_ptr get_parent() const; - std::vector> get_children() const; - std::shared_ptr add_child(); - void remove_child(std::shared_ptr child); - std::string get_name() const; - void change_name(std::string new_name); - -private: - const std::weak_ptr _parent; - std::vector> _child; - std::string _name; -}; +#pragma once + +#include +#include +#include + +class Node: public std::enable_shared_from_this +{ +public: + explicit Node(std::shared_ptr parent = nullptr, std::string name = "without name"); + + ~Node(); + + std::shared_ptr get_parent() const; + std::vector> get_children() const; + std::shared_ptr add_child(); + void remove_child(std::shared_ptr child); + std::string get_name() const; + void change_name(std::string new_name); + +private: + const std::weak_ptr _parent; + std::vector> _child; + std::string _name; +}; diff --git a/node.cpp b/src/core/node.cpp similarity index 91% rename from node.cpp rename to src/core/node.cpp index bdef84cd..17f2bb28 100644 --- a/node.cpp +++ b/src/core/node.cpp @@ -1,70 +1,71 @@ -#include "core/node.h" - -Node::Node(std::shared_ptr parent, std::string name) - : _name(name), _parent(parent) -{ - if (parent != nullptr) - { - parent->_child.push_back(shared_from_this()); - } -} - -Node::~Node() -{ - _child.clear(); - auto parent = _parent.lock(); - if (parent) { - parent->remove_child(shared_from_this()); - } -} - -std::shared_ptr Node::get_parent() const -{ - return _parent.lock(); -} - -std::vector> Node::get_children() const -{ - return _child; -} - -std::shared_ptr Node::add_child() -{ - Node child(*this); - std::shared_ptr child_ptr = std::make_shared(child); - _child.push_back(child_ptr); - return child_ptr; -} - -void Node::remove_child(std::shared_ptr del_child) -{ - std::vector>::iterator child_iter = _child.begin(); - auto del_child_iter = _child.end(); - while(child_iter != _child.end()) - { - if (*child_iter == del_child) - { - del_child_iter = child_iter; - break; - } - child_iter++; - } - if (del_child_iter == _child.end()) - { - //что нужно сделать в таком случае? - } - else - { - _child.erase(del_child_iter); - } -} - -std::string Node::get_name() const -{ - return _name; -} - -void Node::change_name(std::string new_name) -{ - _name = new_name; -} +#include "core/node.h" +#include "core/logging.h" + +Node::Node(std::shared_ptr parent, std::string name) + : _name(name), _parent(parent) +{ + if (parent != nullptr) + { + parent->_child.push_back(shared_from_this()); + } +} + +Node::~Node() +{ + _child.clear(); + auto parent = _parent.lock(); + if (parent) { + parent->remove_child(shared_from_this()); + } +} + +std::shared_ptr Node::get_parent() const +{ + return _parent.lock(); +} + +std::vector> Node::get_children() const +{ + return _child; +} + +std::shared_ptr Node::add_child() +{ + Node child(*this); + std::shared_ptr child_ptr = std::make_shared(child); + _child.push_back(child_ptr); + return child_ptr; +} + +void Node::remove_child(std::shared_ptr del_child) +{ + std::vector>::iterator child_iter = _child.begin(); + auto del_child_iter = _child.end(); + while(child_iter != _child.end()) + { + if (*child_iter == del_child) + { + del_child_iter = child_iter; + break; + } + child_iter++; + } + if (del_child_iter == _child.end()) + { + LOGW("Not possible to delete a non-existent child"); + } + else + { + _child.erase(del_child_iter); + } +} + +std::string Node::get_name() const +{ + return _name; +} + +void Node::change_name(std::string new_name) +{ + _name = new_name; +} From b3735cff2978b4a202ef3c3793e1facb0a279453 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=95=D0=BA=D0=B0=D1=82=D0=B5=D1=80=D0=B8=D0=BD=D0=B0=20?= =?UTF-8?q?=D0=92=D0=B0=D1=81=D0=B8=D0=BB=D1=8C=D0=B5=D0=B2=D0=B0?= <139324306+katvus@users.noreply.github.com> Date: Wed, 13 Dec 2023 21:01:23 +0300 Subject: [PATCH 3/3] Add class Node --- include/core/node.h | 25 ++++++++++++++++ src/core/node.cpp | 71 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 96 insertions(+) create mode 100644 include/core/node.h create mode 100644 src/core/node.cpp diff --git a/include/core/node.h b/include/core/node.h new file mode 100644 index 00000000..3458d719 --- /dev/null +++ b/include/core/node.h @@ -0,0 +1,25 @@ +#pragma once + +#include +#include +#include + +class Node: public std::enable_shared_from_this +{ +public: + explicit Node(std::shared_ptr parent = nullptr, std::string name = "without name"); + + ~Node(); + + std::shared_ptr get_parent() const; + std::vector> get_children() const; + std::shared_ptr add_child(); + void remove_child(std::shared_ptr child); + std::string get_name() const; + void change_name(std::string new_name); + +private: + const std::weak_ptr _parent; + std::vector> _child; + std::string _name; +}; diff --git a/src/core/node.cpp b/src/core/node.cpp new file mode 100644 index 00000000..17f2bb28 --- /dev/null +++ b/src/core/node.cpp @@ -0,0 +1,71 @@ +#include "core/node.h" +#include "core/logging.h" + +Node::Node(std::shared_ptr parent, std::string name) + : _name(name), _parent(parent) +{ + if (parent != nullptr) + { + parent->_child.push_back(shared_from_this()); + } +} + +Node::~Node() +{ + _child.clear(); + auto parent = _parent.lock(); + if (parent) { + parent->remove_child(shared_from_this()); + } +} + +std::shared_ptr Node::get_parent() const +{ + return _parent.lock(); +} + +std::vector> Node::get_children() const +{ + return _child; +} + +std::shared_ptr Node::add_child() +{ + Node child(*this); + std::shared_ptr child_ptr = std::make_shared(child); + _child.push_back(child_ptr); + return child_ptr; +} + +void Node::remove_child(std::shared_ptr del_child) +{ + std::vector>::iterator child_iter = _child.begin(); + auto del_child_iter = _child.end(); + while(child_iter != _child.end()) + { + if (*child_iter == del_child) + { + del_child_iter = child_iter; + break; + } + child_iter++; + } + if (del_child_iter == _child.end()) + { + LOGW("Not possible to delete a non-existent child"); + } + else + { + _child.erase(del_child_iter); + } +} + +std::string Node::get_name() const +{ + return _name; +} + +void Node::change_name(std::string new_name) +{ + _name = new_name; +}