Skip to content

Commit

Permalink
NITWORK: Remove copy constructor and operator= because singleton
Browse files Browse the repository at this point in the history
PATCH
  • Loading branch information
Saverio976 committed Oct 2, 2023
1 parent 1e2c549 commit e456d85
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 24 deletions.
9 changes: 7 additions & 2 deletions src/Nitwork/ANitwork.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,11 @@ namespace Nitwork {

class ANitwork : public INitwork {
public:
ANitwork();
virtual ~ANitwork() = default;
ANitwork(const ANitwork &) = delete;
ANitwork(const ANitwork &&) = delete;
void operator=(const ANitwork &) = delete;
void operator=(const ANitwork &&) = delete;

// start the NitworkServer
bool start(int port, int threadNb, int tick, const std::string &ip = "") override;
Expand Down Expand Up @@ -62,8 +65,10 @@ namespace Nitwork {


protected:
ANitwork();

/* Getters / Setters */
n_id_t getPacketID() const;
[[nodiscard]] n_id_t getPacketID() const;
void addPacketToSend(const boost::asio::ip::udp::endpoint &, const struct packet_s &);

void startReceiveHandler() final;
Expand Down
14 changes: 10 additions & 4 deletions src/Nitwork/INitwork.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ extern "C"
}

namespace Nitwork {
typedef std::function<void(std::any &, boost::asio::ip::udp::endpoint &)> actionHandler;
typedef std::function<void(
using actionHandler = std::function<void(std::any &, boost::asio::ip::udp::endpoint &)>;
using handleBodyT = std::function<void(
const struct header_s &,
actionHandler &
)> handleBodyT;
)>;

class SenderData {
public:
Expand All @@ -39,15 +39,21 @@ namespace Nitwork {
n_actionType_t action;
std::any body;
};

class INitwork {
public:
virtual ~INitwork() = default;
INitwork(const INitwork &) = delete;
INitwork(const INitwork &&) = delete;
void operator=(const INitwork &) = delete;
void operator=(const INitwork &&) = delete;

// start the NitworkServer
virtual bool start(int port, int threadNb, int tick, const std::string &ip = "") = 0;

virtual void stop() = 0;
protected:
INitwork() = default;
// start the NitworkServer config
virtual bool startNitworkConfig(int port, const std::string &ip) = 0;
// start the NitworkServer threads (context threads, clock thread, input thread and output thread)
Expand All @@ -73,7 +79,7 @@ namespace Nitwork {
const boost::asio::ip::udp::endpoint &endpoint) = 0;

// getters
virtual const std::map<
[[nodiscard]] virtual const std::map<
enum n_actionType_t,
actionHandler
>& getActionToSendHandlers() const = 0;
Expand Down
6 changes: 3 additions & 3 deletions src/Nitwork/NitworkClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

namespace Nitwork {
NitworkClient::NitworkClient()
: ANitwork(), _resolver(_context) {}
: _resolver(_context) {}

// NOLINTBEGIN(cppcoreguidelines-avoid-non-const-global-variables)
NitworkClient NitworkClient::_instance = NitworkClient();
Expand Down Expand Up @@ -96,7 +96,7 @@ namespace Nitwork {
.header = {
.magick1 = HEADER_CODE1,
.ids_received = getIdsReceived(),
.last_id_received = (_receivedPacketsIds.size() > 0) ? _receivedPacketsIds.back() : 0,
.last_id_received = (!_receivedPacketsIds.empty()) ? _receivedPacketsIds.back() : 0,
.id = getPacketID(),
.nb_action = 1,
.magick2 = HEADER_CODE2,
Expand All @@ -114,4 +114,4 @@ namespace Nitwork {
};
addPacketToSend(_endpoint, packetData);
}
} // namespace Nitwork
} // namespace Nitwork
15 changes: 10 additions & 5 deletions src/Nitwork/NitworkClient.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,11 @@
namespace Nitwork {
class NitworkClient : public ANitwork {
public:
NitworkClient();
~NitworkClient() = default;
~NitworkClient() override = default;
NitworkClient(const NitworkClient &) = delete;
NitworkClient(const NitworkClient &&) = delete;
void operator=(const NitworkClient &) = delete;
void operator=(const NitworkClient &&) = delete;

static NitworkClient &getInstance();

Expand All @@ -23,7 +26,7 @@ namespace Nitwork {
bool startNitworkConfig(int port, const std::string &ip) final;

void handleBodyAction(
const struct header_s header,
struct header_s header,
const boost::asio::ip::udp::endpoint &endpoint) final;


Expand All @@ -32,6 +35,7 @@ namespace Nitwork {
// Messages creation methods
void addInitMsg();
private:
NitworkClient();
[[nodiscard]] const std::map<
enum n_actionType_t,
actionHandler
Expand All @@ -46,8 +50,9 @@ namespace Nitwork {
boost::asio::ip::udp::endpoint &endpoint);
protected:
private:
static NitworkClient
_instance; // instance of the NitworkClient (singleton)
// NOLINTBEGIN(cppcoreguidelines-avoid-non-const-global-variables)
static NitworkClient _instance; // instance of the NitworkClient (singleton)
// NOLINTEND(cppcoreguidelines-avoid-non-const-global-variables)
boost::asio::ip::udp::resolver _resolver; // resolver used to find the server

// maps that will be used to handle the actions, in order to send or receive them
Expand Down
5 changes: 1 addition & 4 deletions src/Nitwork/NitworkServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@
#include "NitworkServer.hpp"

namespace Nitwork {
NitworkServer::NitworkServer()
: ANitwork() {}

// NOLINTBEGIN(cppcoreguidelines-avoid-non-const-global-variables)
NitworkServer NitworkServer::_instance = NitworkServer();
// NOLINTEND(cppcoreguidelines-avoid-non-const-global-variables)
Expand Down Expand Up @@ -109,4 +106,4 @@ namespace Nitwork {
std::cout << "ready" << std::endl;
}
/* End Handle packet (msg) Section */
}
}
14 changes: 8 additions & 6 deletions src/Nitwork/NitworkServer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,11 @@
namespace Nitwork {
class NitworkServer : public ANitwork {
public:
NitworkServer();
~NitworkServer() = default;
~NitworkServer() override = default;
NitworkServer(const NitworkServer &) = delete;
NitworkServer(const NitworkServer &&) = delete;
void operator=(const NitworkServer &) = delete;
void operator=(const NitworkServer &&) = delete;

static NitworkServer &getInstance();

Expand All @@ -22,9 +25,10 @@ namespace Nitwork {
bool startNitworkConfig(int port, const std::string &ip) final;

void handleBodyAction(
const struct header_s header,
struct header_s header,
const boost::asio::ip::udp::endpoint &endpoint) final;
private:
NitworkServer() = default;
[[nodiscard]] const std::map<
enum n_actionType_t,
actionHandler
Expand All @@ -37,8 +41,6 @@ namespace Nitwork {
void handleReadyMsg(
const std::any &msg,
boost::asio::ip::udp::endpoint &endpoint);
protected:
private:
// NOLINTBEGIN(cppcoreguidelines-avoid-non-const-global-variables)
static NitworkServer
_instance; // instance of the NitworkServer (singleton)
Expand Down Expand Up @@ -115,4 +117,4 @@ namespace Nitwork {
}
};
};
}
}

0 comments on commit e456d85

Please sign in to comment.