From 4336b0b93083fa9ee0a9c260c86d7af734a1658a Mon Sep 17 00:00:00 2001 From: Joshua Scoggins Date: Sun, 15 Oct 2023 12:26:35 -0700 Subject: [PATCH 1/3] Do not private constructors, use delete instead Default the destructor and migrate typedef to using declaration Delete move constructor and move assignment operator as well. --- src/PacketSerial.h | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/src/PacketSerial.h b/src/PacketSerial.h index 67de570..449102b 100644 --- a/src/PacketSerial.h +++ b/src/PacketSerial.h @@ -36,7 +36,7 @@ class PacketSerial_ /// /// where buffer is a pointer to the incoming buffer array, and size is the /// number of bytes in the incoming buffer. - typedef void (*PacketHandlerFunction)(const uint8_t* buffer, size_t size); + using PacketHandlerFunction = void(*)(const uint8_t* buffer, size_t size); /// \brief A typedef describing the packet handler method. /// @@ -47,7 +47,7 @@ class PacketSerial_ /// where sender is a pointer to the PacketSerial_ instance that recieved /// the buffer, buffer is a pointer to the incoming buffer array, and size /// is the number of bytes in the incoming buffer. - typedef void (*PacketHandlerFunctionWithSender)(const void* sender, const uint8_t* buffer, size_t size); + using PacketHandlerFunctionWithSender = void(*)(const void* sender, const uint8_t* buffer, size_t size); /// \brief Construct a default PacketSerial_ device. PacketSerial_(): @@ -58,11 +58,8 @@ class PacketSerial_ _senderPtr(nullptr) { } - /// \brief Destroy the PacketSerial_ device. - ~PacketSerial_() - { - } + ~PacketSerial_() = default; /// \brief Begin a default serial connection with the given speed. /// @@ -167,7 +164,7 @@ class PacketSerial_ /// includes some network objects. /// /// \param stream A pointer to an Arduino `Stream`. - void setStream(Stream* stream) + void setStream(Stream* stream) noexcept { _stream = stream; } @@ -179,7 +176,7 @@ class PacketSerial_ /// takes ownership of the stream and thus does not have exclusive /// access to the stream anyway. /// \returns a non-const pointer to the stream, or nullptr if unset. - Stream* getStream() + Stream* getStream() noexcept { return _stream; } @@ -191,7 +188,7 @@ class PacketSerial_ /// takes ownership of the stream and thus does not have exclusive /// access to the stream anyway. /// \returns a const pointer to the stream, or nullptr if unset. - const Stream* getStream() const + const Stream* getStream() const noexcept { return _stream; } @@ -402,14 +399,15 @@ class PacketSerial_ /// overflow() method is called. /// /// \returns true if the receive buffer overflowed. - bool overflow() const + constexpr bool overflow() const noexcept { return _recieveBufferOverflow; } - + PacketSerial_(const PacketSerial_&) = delete; + PacketSerial_(PacketSerial_&&) = delete; + PacketSerial_& operator = (const PacketSerial_&) = delete; + PacketSerial_& operator = (PacketSerial_&&) = delete; private: - PacketSerial_(const PacketSerial_&); - PacketSerial_& operator = (const PacketSerial_&); bool _recieveBufferOverflow = false; From 59490c09b5248a83f51e6c0b62d9e6e7536835c4 Mon Sep 17 00:00:00 2001 From: Joshua Scoggins Date: Sun, 15 Oct 2023 12:29:39 -0700 Subject: [PATCH 2/3] Because of pre-existing NSDMI no need for an explicit constructor so mark it as default --- src/PacketSerial.h | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/src/PacketSerial.h b/src/PacketSerial.h index 449102b..eca785b 100644 --- a/src/PacketSerial.h +++ b/src/PacketSerial.h @@ -50,14 +50,7 @@ class PacketSerial_ using PacketHandlerFunctionWithSender = void(*)(const void* sender, const uint8_t* buffer, size_t size); /// \brief Construct a default PacketSerial_ device. - PacketSerial_(): - _receiveBufferIndex(0), - _stream(nullptr), - _onPacketFunction(nullptr), - _onPacketFunctionWithSender(nullptr), - _senderPtr(nullptr) - { - } + PacketSerial_() = default; /// \brief Destroy the PacketSerial_ device. ~PacketSerial_() = default; From 64a6db1416ad80ec8f6de150ed4de01c04cc3b4a Mon Sep 17 00:00:00 2001 From: Joshua Scoggins Date: Sun, 15 Oct 2023 12:35:34 -0700 Subject: [PATCH 3/3] Mark more using declarations --- src/PacketSerial.h | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/PacketSerial.h b/src/PacketSerial.h index eca785b..4765d17 100644 --- a/src/PacketSerial.h +++ b/src/PacketSerial.h @@ -197,7 +197,7 @@ class PacketSerial_ /// myPacketSerial.update(); /// } /// - void update() + void update() noexcept { if (_stream == nullptr) return; @@ -416,10 +416,10 @@ class PacketSerial_ /// \brief A typedef for the default COBS PacketSerial class. -typedef PacketSerial_ PacketSerial; +using PacketSerial = PacketSerial_; /// \brief A typedef for a PacketSerial type with COBS encoding. -typedef PacketSerial_ COBSPacketSerial; +using COBSPacketSerial = PacketSerial_; /// \brief A typedef for a PacketSerial type with SLIP encoding. -typedef PacketSerial_ SLIPPacketSerial; +using SLIPPacketSerial = PacketSerial_;