From 8fb1a08ce5a214195a92935eafc5a686d92e4d02 Mon Sep 17 00:00:00 2001 From: faraphel Date: Mon, 23 Dec 2024 13:12:46 +0100 Subject: [PATCH] use smart pointer when creating an object with an instance of the creator --- source/gns3/GnsNode.cpp | 2 +- source/gns3/GnsNode.hpp | 6 ++++-- source/gns3/GnsProject.cpp | 4 ++-- source/gns3/GnsProject.hpp | 6 +++--- source/gns3/GnsServer.cpp | 2 +- source/gns3/GnsServer.hpp | 2 +- 6 files changed, 12 insertions(+), 10 deletions(-) diff --git a/source/gns3/GnsNode.cpp b/source/gns3/GnsNode.cpp index f12bb33..d25d231 100644 --- a/source/gns3/GnsNode.cpp +++ b/source/gns3/GnsNode.cpp @@ -9,7 +9,7 @@ namespace gns { -GnsNode::GnsNode(const GnsProject *project, const std::string& uuid, const std::vector& ports) { +GnsNode::GnsNode(const std::shared_ptr& project, const std::string& uuid, const std::vector& ports) { this->project = project; this->uuid = uuid; this->ports = ports; diff --git a/source/gns3/GnsNode.hpp b/source/gns3/GnsNode.hpp index 68a75d0..a5d95ec 100644 --- a/source/gns3/GnsNode.hpp +++ b/source/gns3/GnsNode.hpp @@ -1,5 +1,7 @@ #pragma once +#include + #include "GnsPort.hpp" #include @@ -18,7 +20,7 @@ class GnsProject; */ class GnsNode { public: - GnsNode(const GnsProject* project, const std::string& uuid, const std::vector& ports); + GnsNode(const std::shared_ptr& project, const std::string& uuid, const std::vector& ports); /** * Get the base URL for the node API @@ -44,7 +46,7 @@ public: void start() const; private: - const GnsProject* project; + std::shared_ptr project; std::string uuid; std::vector ports; }; diff --git a/source/gns3/GnsProject.cpp b/source/gns3/GnsProject.cpp index 7bd7531..09dfcd1 100644 --- a/source/gns3/GnsProject.cpp +++ b/source/gns3/GnsProject.cpp @@ -10,7 +10,7 @@ using json = nlohmann::json; namespace gns { -GnsProject::GnsProject(const GnsServer* server, const std::string& uuid) { +GnsProject::GnsProject(const std::shared_ptr& server, const std::string& uuid) { this->server = server; this->uuid = uuid; } @@ -60,7 +60,7 @@ std::vector> GnsProject::getNodes() const { // save the node nodes.emplace_back(std::make_shared( - this, + shared_from_this(), node_data["node_id"], ports )); diff --git a/source/gns3/GnsProject.hpp b/source/gns3/GnsProject.hpp index 963b08e..07a7983 100644 --- a/source/gns3/GnsProject.hpp +++ b/source/gns3/GnsProject.hpp @@ -15,9 +15,9 @@ class GnsServer; /** * Represent a GNS3 project. */ -class GnsProject { +class GnsProject : public std::enable_shared_from_this { public: - GnsProject(const GnsServer* server, const std::string& uuid); + GnsProject(const std::shared_ptr& server, const std::string& uuid); /** * Get the base prefix for an API request @@ -38,7 +38,7 @@ public: [[nodiscard]] std::vector> getNodes() const; private: - const GnsServer* server; + std::shared_ptr server; std::string uuid; }; diff --git a/source/gns3/GnsServer.cpp b/source/gns3/GnsServer.cpp index 6b1161b..e6fb9c4 100644 --- a/source/gns3/GnsServer.cpp +++ b/source/gns3/GnsServer.cpp @@ -39,7 +39,7 @@ std::vector> GnsServer::getProjects() const { projects.reserve(data.size()); for (const auto& project_data : data) projects.emplace_back(std::make_shared( - this, + shared_from_this(), project_data["project_id"] )); diff --git a/source/gns3/GnsServer.hpp b/source/gns3/GnsServer.hpp index f90894a..637f3f4 100644 --- a/source/gns3/GnsServer.hpp +++ b/source/gns3/GnsServer.hpp @@ -14,7 +14,7 @@ namespace gns { * Represent a GNS3 server * Wrapper around the GNS3 API (https://gns3-server.readthedocs.io/en/latest/endpoints.html) */ -class GnsServer { +class GnsServer : public std::enable_shared_from_this { public: GnsServer(const std::string& host, std::uint16_t port);