Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use more of C++11 and later features #31

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 16 additions & 25 deletions src/PacketSerial.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
///
Expand All @@ -47,22 +47,12 @@ 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_():
_receiveBufferIndex(0),
_stream(nullptr),
_onPacketFunction(nullptr),
_onPacketFunctionWithSender(nullptr),
_senderPtr(nullptr)
{
}

PacketSerial_() = default;
/// \brief Destroy the PacketSerial_ device.
~PacketSerial_()
{
}
~PacketSerial_() = default;

/// \brief Begin a default serial connection with the given speed.
///
Expand Down Expand Up @@ -167,7 +157,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;
}
Expand All @@ -179,7 +169,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;
}
Expand All @@ -191,7 +181,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;
}
Expand All @@ -207,7 +197,7 @@ class PacketSerial_
/// myPacketSerial.update();
/// }
///
void update()
void update() noexcept
{
if (_stream == nullptr) return;

Expand Down Expand Up @@ -402,14 +392,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;

Expand All @@ -425,10 +416,10 @@ class PacketSerial_


/// \brief A typedef for the default COBS PacketSerial class.
typedef PacketSerial_<COBS> PacketSerial;
using PacketSerial = PacketSerial_<COBS>;

/// \brief A typedef for a PacketSerial type with COBS encoding.
typedef PacketSerial_<COBS> COBSPacketSerial;
using COBSPacketSerial = PacketSerial_<COBS>;

/// \brief A typedef for a PacketSerial type with SLIP encoding.
typedef PacketSerial_<SLIP, SLIP::END> SLIPPacketSerial;
using SLIPPacketSerial = PacketSerial_<SLIP, SLIP::END>;