use smart pointer when creating an object with an instance of the creator

This commit is contained in:
faraphel 2024-12-23 13:12:46 +01:00
parent a0060c8fa8
commit 8fb1a08ce5
6 changed files with 12 additions and 10 deletions

View file

@ -9,7 +9,7 @@
namespace gns { namespace gns {
GnsNode::GnsNode(const GnsProject *project, const std::string& uuid, const std::vector<GnsPort>& ports) { GnsNode::GnsNode(const std::shared_ptr<const GnsProject>& project, const std::string& uuid, const std::vector<GnsPort>& ports) {
this->project = project; this->project = project;
this->uuid = uuid; this->uuid = uuid;
this->ports = ports; this->ports = ports;

View file

@ -1,5 +1,7 @@
#pragma once #pragma once
#include <memory>
#include "GnsPort.hpp" #include "GnsPort.hpp"
#include <string> #include <string>
@ -18,7 +20,7 @@ class GnsProject;
*/ */
class GnsNode { class GnsNode {
public: public:
GnsNode(const GnsProject* project, const std::string& uuid, const std::vector<GnsPort>& ports); GnsNode(const std::shared_ptr<const GnsProject>& project, const std::string& uuid, const std::vector<GnsPort>& ports);
/** /**
* Get the base URL for the node API * Get the base URL for the node API
@ -44,7 +46,7 @@ public:
void start() const; void start() const;
private: private:
const GnsProject* project; std::shared_ptr<const GnsProject> project;
std::string uuid; std::string uuid;
std::vector<GnsPort> ports; std::vector<GnsPort> ports;
}; };

View file

@ -10,7 +10,7 @@ using json = nlohmann::json;
namespace gns { namespace gns {
GnsProject::GnsProject(const GnsServer* server, const std::string& uuid) { GnsProject::GnsProject(const std::shared_ptr<const GnsServer>& server, const std::string& uuid) {
this->server = server; this->server = server;
this->uuid = uuid; this->uuid = uuid;
} }
@ -60,7 +60,7 @@ std::vector<std::shared_ptr<GnsNode>> GnsProject::getNodes() const {
// save the node // save the node
nodes.emplace_back(std::make_shared<GnsNode>( nodes.emplace_back(std::make_shared<GnsNode>(
this, shared_from_this(),
node_data["node_id"], node_data["node_id"],
ports ports
)); ));

View file

@ -15,9 +15,9 @@ class GnsServer;
/** /**
* Represent a GNS3 project. * Represent a GNS3 project.
*/ */
class GnsProject { class GnsProject : public std::enable_shared_from_this<GnsProject> {
public: public:
GnsProject(const GnsServer* server, const std::string& uuid); GnsProject(const std::shared_ptr<const GnsServer>& server, const std::string& uuid);
/** /**
* Get the base prefix for an API request * Get the base prefix for an API request
@ -38,7 +38,7 @@ public:
[[nodiscard]] std::vector<std::shared_ptr<GnsNode>> getNodes() const; [[nodiscard]] std::vector<std::shared_ptr<GnsNode>> getNodes() const;
private: private:
const GnsServer* server; std::shared_ptr<const GnsServer> server;
std::string uuid; std::string uuid;
}; };

View file

@ -39,7 +39,7 @@ std::vector<std::shared_ptr<GnsProject>> GnsServer::getProjects() const {
projects.reserve(data.size()); projects.reserve(data.size());
for (const auto& project_data : data) for (const auto& project_data : data)
projects.emplace_back(std::make_shared<GnsProject>( projects.emplace_back(std::make_shared<GnsProject>(
this, shared_from_this(),
project_data["project_id"] project_data["project_id"]
)); ));

View file

@ -14,7 +14,7 @@ namespace gns {
* Represent a GNS3 server * Represent a GNS3 server
* Wrapper around the GNS3 API (https://gns3-server.readthedocs.io/en/latest/endpoints.html) * Wrapper around the GNS3 API (https://gns3-server.readthedocs.io/en/latest/endpoints.html)
*/ */
class GnsServer { class GnsServer : public std::enable_shared_from_this<GnsServer> {
public: public:
GnsServer(const std::string& host, std::uint16_t port); GnsServer(const std::string& host, std::uint16_t port);