Skip to content

Commit

Permalink
adding missing noexept specifiers
Browse files Browse the repository at this point in the history
  • Loading branch information
noah1510 committed Dec 14, 2023
1 parent 93967e3 commit 567559e
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 28 deletions.
4 changes: 2 additions & 2 deletions include/rs232_native.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ namespace sakurajin {
#endif
};

RS232_EXPORT_MACRO std::vector<std::string> getAvailablePorts();
RS232_EXPORT_MACRO std::vector<std::string> getMatchingPorts(const std::regex& pattern);
RS232_EXPORT_MACRO std::vector<std::string> getAvailablePorts() noexcept;
RS232_EXPORT_MACRO std::vector<std::string> getMatchingPorts(const std::regex& pattern) noexcept;

/**
* @brief The native implementation of RS232
Expand Down
8 changes: 4 additions & 4 deletions src/rs232_native_common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,23 @@ sakurajin::RS232_native::~RS232_native() {
disconnect();
}

sakurajin::connectionStatus sakurajin::RS232_native::getConnectionStatus() {
sakurajin::connectionStatus sakurajin::RS232_native::getConnectionStatus() noexcept {
return connStatus;
}

std::string_view sakurajin::RS232_native::getDeviceName() const {
std::string_view sakurajin::RS232_native::getDeviceName() const noexcept {
return devname;
}

bool sakurajin::RS232_native::checkForFlag(sakurajin::portStatusFlags flag, bool block) {
bool sakurajin::RS232_native::checkForFlag(sakurajin::portStatusFlags flag, bool block) noexcept {
auto flags = retrieveFlags(block);
if (flags < 0) {
return false;
}
return (flags & static_cast<ssize_t>(flag)) != 0;
}

std::vector<std::string> sakurajin::getAvailablePorts() {
std::vector<std::string> sakurajin::getAvailablePorts() noexcept {
std::vector<std::string> allPorts;

// All the regex patterns to check for available ports
Expand Down
28 changes: 14 additions & 14 deletions src/rs232_native_linux.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@
#include <fcntl.h>
#include <unistd.h>

inline int& getPort(void* portHandle) {
inline int& getPort(void* portHandle) noexcept {
return *static_cast<int*>(portHandle);
}

inline struct termios& getTermios(void* termiosHandle) {
inline struct termios& getTermios(void* termiosHandle) noexcept {
return *static_cast<termios*>(termiosHandle);
}

std::vector<std::string> sakurajin::getMatchingPorts(const std::regex& pattern) {
std::vector<std::string> sakurajin::getMatchingPorts(const std::regex& pattern) noexcept {

std::vector<std::string> allPorts;

Expand All @@ -30,12 +30,12 @@ std::vector<std::string> sakurajin::getMatchingPorts(const std::regex& pattern)
return allPorts;
}

sakurajin::connectionStatus sakurajin::RS232_native::connect(Baudrate baudrate, std::ostream& error_stream) {
sakurajin::connectionStatus sakurajin::RS232_native::connect(Baudrate baudrate, std::ostream& error_stream) noexcept {
if (connStatus == connectionStatus::connected) {
return connStatus;
}

//make sure no read or write operation is performed while the port is being opened
// make sure no read or write operation is performed while the port is being opened
std::scoped_lock lock{dataAccessMutex};

// convert the baudrate to int
Expand All @@ -47,14 +47,14 @@ sakurajin::connectionStatus sakurajin::RS232_native::connect(Baudrate baudrate,
devicePath = "/dev" / devicePath;
}

//if the file does not exist, return an error
// if the file does not exist, return an error
if (!std::filesystem::exists(devicePath)) {
error_stream << "device " << devicePath << " does not exist" << std::endl;
connStatus = connectionStatus::portNotFound;
return connStatus;
}

//open the port and return an error if that fails
// open the port and return an error if that fails
portHandle = static_cast<void*>(new int{});
getPort(portHandle) = open(devicePath.string().c_str(), O_RDWR | O_NOCTTY | O_NDELAY);
if (getPort(portHandle) < 0) {
Expand All @@ -63,7 +63,7 @@ sakurajin::connectionStatus sakurajin::RS232_native::connect(Baudrate baudrate,
return connStatus;
}

//get the current port settings and return an error if that fails
// get the current port settings and return an error if that fails
portConfig = static_cast<void*>(new termios{});
int error = tcgetattr(getPort(portHandle), &getTermios(portConfig));
if (error < 0) {
Expand Down Expand Up @@ -94,7 +94,7 @@ sakurajin::connectionStatus sakurajin::RS232_native::connect(Baudrate baudrate,
return connStatus;
}

ssize_t sakurajin::RS232_native::readRawData(char* data_location, int length, bool lock) {
ssize_t sakurajin::RS232_native::readRawData(char* data_location, int length, bool lock) noexcept {
if (connStatus != connectionStatus::connected) {
return -1;
}
Expand All @@ -111,7 +111,7 @@ ssize_t sakurajin::RS232_native::readRawData(char* data_location, int length, bo
return callWithOptionalLock(lock, [this, data_location, length]() { return read(getPort(portHandle), data_location, length); });
}

ssize_t sakurajin::RS232_native::writeRawData(char* data_location, int length, bool lock) {
ssize_t sakurajin::RS232_native::writeRawData(char* data_location, int length, bool lock) noexcept {
if (connStatus != connectionStatus::connected) {
return -1;
}
Expand All @@ -124,23 +124,23 @@ void sakurajin::RS232_native::disconnect() noexcept {
return;
}

//lock the mutex to make sure the port is not accessed while it is being closed
// lock the mutex to make sure the port is not accessed while it is being closed
std::scoped_lock lock(dataAccessMutex);

connStatus = connectionStatus::disconnected;

//close the port handles
// close the port handles
close(getPort(portHandle));
tcsetattr(getPort(portHandle), TCSANOW, &getTermios(portConfig));

//free the memory and set the pointers to nullptr
// free the memory and set the pointers to nullptr
delete &getPort(portHandle);
delete &getTermios(portConfig);
portHandle = nullptr;
portConfig = nullptr;
}

ssize_t sakurajin::RS232_native::retrieveFlags(bool block) {
ssize_t sakurajin::RS232_native::retrieveFlags(bool block) noexcept {
if (connStatus != connectionStatus::connected) {
return -1;
}
Expand Down
16 changes: 8 additions & 8 deletions src/rs232_native_win.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include <codecvt>
#include <locale>

static inline std::string wstrToStr(const std::wstring& wstr) {
static inline std::string wstrToStr(const std::wstring& wstr) noexcept {
if (wstr.empty()) {
return "";
}
Expand All @@ -15,15 +15,15 @@ static inline std::string wstrToStr(const std::wstring& wstr) {
return strTo;
}

inline HANDLE& getCport(void* portHandle) {
inline HANDLE& getCport(void* portHandle) noexcept {
return *static_cast<HANDLE*>(portHandle);
}

inline DCB& getDCB(void* DCBHandle) {
inline DCB& getDCB(void* DCBHandle) noexcept {
return *static_cast<DCB*>(DCBHandle);
}

std::vector<std::string> sakurajin::getMatchingPorts(std::regex pattern) {
std::vector<std::string> sakurajin::getMatchingPorts(std::regex pattern) noexcept {
std::vector<std::string> allPorts;
wchar_t lpTargetPath[5000];

Expand All @@ -46,7 +46,7 @@ std::vector<std::string> sakurajin::getMatchingPorts(std::regex pattern) {
return allPorts;
}

sakurajin::connectionStatus sakurajin::RS232_native::connect(sakurajin::Baudrate baudrate, std::ostream& error_stream) {
sakurajin::connectionStatus sakurajin::RS232_native::connect(sakurajin::Baudrate baudrate, std::ostream& error_stream) noexcept {
if (connStatus == connectionStatus::connected) {
return connStatus;
}
Expand Down Expand Up @@ -125,7 +125,7 @@ void sakurajin::RS232_native::disconnect() noexcept {
connStatus = connectionStatus::disconnected;
}

ssize_t sakurajin::RS232_native::readRawData(char* data_location, int length, bool block) {
ssize_t sakurajin::RS232_native::readRawData(char* data_location, int length, bool block) noexcept {
if (connStatus != connectionStatus::connected) {
return -1;
}
Expand All @@ -139,7 +139,7 @@ ssize_t sakurajin::RS232_native::readRawData(char* data_location, int length, bo
});
}

ssize_t sakurajin::RS232_native::writeRawData(char* data_location, int length, bool block) {
ssize_t sakurajin::RS232_native::writeRawData(char* data_location, int length, bool block) noexcept {
if (connStatus != connectionStatus::connected) {
return -1;
}
Expand All @@ -153,7 +153,7 @@ ssize_t sakurajin::RS232_native::writeRawData(char* data_location, int length, b
});
}

ssize_t sakurajin::RS232_native::retrieveFlags(bool block) {
ssize_t sakurajin::RS232_native::retrieveFlags(bool block) noexcept {
if (connStatus != connectionStatus::connected) {
return -1;
}
Expand Down

0 comments on commit 567559e

Please sign in to comment.