Skip to content

Commit

Permalink
Codechange: do not use unique_ptr, but embed std::string directly in …
Browse files Browse the repository at this point in the history
…NetworkError
  • Loading branch information
rubidium42 committed May 1, 2021
1 parent a050b24 commit 8ccbd97
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 6 deletions.
8 changes: 4 additions & 4 deletions src/network/core/os_abstraction.cpp
Expand Up @@ -78,14 +78,14 @@ bool NetworkError::IsConnectInProgress() const
*/
const char *NetworkError::AsString() const
{
if (this->message.get() == nullptr) {
if (this->message.empty()) {
#if defined(_WIN32)
char buffer[512];
if (FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, this->error,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), buffer, sizeof(buffer), NULL) == 0) {
seprintf(buffer, lastof(buffer), "Unknown error %d", this->error);
}
this->message.reset(new std::string(buffer));
this->message.assign(buffer);
#else
/* Make strerror thread safe by locking access to it. There is a thread safe strerror_r, however
* the non-POSIX variant is available due to defining _GNU_SOURCE meaning it is not portable.
Expand All @@ -94,10 +94,10 @@ const char *NetworkError::AsString() const
* variant always fills the buffer. This makes the behaviour too erratic to work with. */
static std::mutex mutex;
std::lock_guard<std::mutex> guard(mutex);
this->message.reset(new std::string(strerror(this->error)));
this->message.assign(strerror(this->error));
#endif
}
return this->message.get()->c_str();
return this->message.c_str();
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/network/core/os_abstraction.h
Expand Up @@ -20,8 +20,8 @@
*/
class NetworkError {
private:
int error; ///< The underlying error number from errno or WSAGetLastError.
mutable std::unique_ptr<std::string> message; ///< The string representation of the error.
int error; ///< The underlying error number from errno or WSAGetLastError.
mutable std::string message; ///< The string representation of the error (set on first call to #AsString).
public:
NetworkError(int error);

Expand Down

0 comments on commit 8ccbd97

Please sign in to comment.