Use unique_ptr for increased ownership clarity of communication components

pull/4/head
Paul Hollinsky 2018-10-04 13:13:44 -04:00
parent 214acf4c8f
commit 21f5badb0c
4 changed files with 18 additions and 18 deletions

View File

@ -22,10 +22,10 @@ namespace icsneo {
class Communication { class Communication {
public: public:
Communication( Communication(
std::shared_ptr<ICommunication> com, std::unique_ptr<ICommunication> com,
std::shared_ptr<Packetizer> p, std::shared_ptr<Packetizer> p,
std::shared_ptr<Encoder> e, std::unique_ptr<Encoder> e,
std::shared_ptr<Decoder> md) : packetizer(p), encoder(e), decoder(md), impl(com) {} std::unique_ptr<Decoder> md) : packetizer(p), encoder(std::move(e)), decoder(std::move(md)), impl(std::move(com)) {}
virtual ~Communication() { close(); } virtual ~Communication() { close(); }
bool open(); bool open();
@ -47,12 +47,12 @@ public:
} }
std::shared_ptr<Message> waitForMessageSync(std::shared_ptr<MessageFilter> f, std::chrono::milliseconds timeout = std::chrono::milliseconds(50)); std::shared_ptr<Message> waitForMessageSync(std::shared_ptr<MessageFilter> f, std::chrono::milliseconds timeout = std::chrono::milliseconds(50));
std::shared_ptr<Packetizer> packetizer; std::shared_ptr<Packetizer> packetizer; // Ownership is shared with the encoder
std::shared_ptr<Encoder> encoder; std::unique_ptr<Encoder> encoder;
std::shared_ptr<Decoder> decoder; std::unique_ptr<Decoder> decoder;
protected: protected:
std::shared_ptr<ICommunication> impl; std::unique_ptr<ICommunication> impl;
static int messageCallbackIDCounter; static int messageCallbackIDCounter;
std::map<int, MessageCallback> messageCallbacks; std::map<int, MessageCallback> messageCallbacks;
std::atomic<bool> closing{false}; std::atomic<bool> closing{false};

View File

@ -11,10 +11,10 @@ namespace icsneo {
class MultiChannelCommunication : public Communication { class MultiChannelCommunication : public Communication {
public: public:
MultiChannelCommunication( MultiChannelCommunication(
std::shared_ptr<ICommunication> com, std::unique_ptr<ICommunication> com,
std::shared_ptr<Packetizer> p, std::shared_ptr<Packetizer> p,
std::shared_ptr<Encoder> e, std::unique_ptr<Encoder> e,
std::shared_ptr<Decoder> md) : Communication(com, p, e, md) {} std::unique_ptr<Decoder> md) : Communication(std::move(com), p, std::move(e), std::move(md)) {}
void spawnThreads() override; void spawnThreads() override;
void joinThreads() override; void joinThreads() override;
bool sendPacket(std::vector<uint8_t>& bytes) override; bool sendPacket(std::vector<uint8_t>& bytes) override;

View File

@ -12,11 +12,11 @@ class NeoVIFIRE2ETH : public NeoVIFIRE2 {
public: public:
static constexpr const uint16_t PRODUCT_ID = 0x0004; static constexpr const uint16_t PRODUCT_ID = 0x0004;
NeoVIFIRE2ETH(neodevice_t neodevice) : NeoVIFIRE2(neodevice) { NeoVIFIRE2ETH(neodevice_t neodevice) : NeoVIFIRE2(neodevice) {
auto transport = std::make_shared<PCAP>(getWritableNeoDevice()); auto transport = std::unique_ptr<ICommunication>(new PCAP(getWritableNeoDevice()));
auto packetizer = std::make_shared<Packetizer>(); auto packetizer = std::make_shared<Packetizer>();
auto encoder = std::make_shared<Encoder>(packetizer); auto encoder = std::unique_ptr<Encoder>(new Encoder(packetizer));
auto decoder = std::make_shared<Decoder>(); auto decoder = std::unique_ptr<Decoder>(new Decoder());
com = std::make_shared<Communication>(transport, packetizer, encoder, decoder); com = std::make_shared<Communication>(std::move(transport), packetizer, std::move(encoder), std::move(decoder));
settings = std::make_shared<NeoVIFIRE2Settings>(com); settings = std::make_shared<NeoVIFIRE2Settings>(com);
productId = PRODUCT_ID; productId = PRODUCT_ID;
} }

View File

@ -11,11 +11,11 @@ class NeoVIFIRE2USB : public NeoVIFIRE2 {
public: public:
static constexpr const uint16_t PRODUCT_ID = 0x1000; static constexpr const uint16_t PRODUCT_ID = 0x1000;
NeoVIFIRE2USB(neodevice_t neodevice) : NeoVIFIRE2(neodevice) { NeoVIFIRE2USB(neodevice_t neodevice) : NeoVIFIRE2(neodevice) {
auto transport = std::make_shared<FTDI>(getWritableNeoDevice()); auto transport = std::unique_ptr<ICommunication>(new FTDI(getWritableNeoDevice()));
auto packetizer = std::make_shared<Packetizer>(); auto packetizer = std::make_shared<Packetizer>();
auto encoder = std::make_shared<Encoder>(packetizer); auto encoder = std::unique_ptr<Encoder>(new Encoder(packetizer));
auto decoder = std::make_shared<Decoder>(); auto decoder = std::unique_ptr<Decoder>(new Decoder());
com = std::make_shared<Communication>(transport, packetizer, encoder, decoder); com = std::make_shared<Communication>(std::move(transport), packetizer, std::move(encoder), std::move(decoder));
settings = std::make_shared<NeoVIFIRE2Settings>(com); settings = std::make_shared<NeoVIFIRE2Settings>(com);
productId = PRODUCT_ID; productId = PRODUCT_ID;
} }