From 6ce7c38e247aa49a2c0500425a0499c469253655 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Wed, 14 Aug 2019 09:49:31 -0400 Subject: [PATCH] General: Make operator bool overloads and single-arg constructors explicit Prevents various error-prone cases of implicit conversion to bool, while retaining general conversions within conditional statements. We also disable implicit conversions with constructors that may be ambiguous to enforce being clear with intentions. --- include/jbus/Endpoint.hpp | 2 +- include/jbus/Listener.hpp | 4 ++-- include/jbus/Socket.hpp | 8 ++++---- lib/Listener.cpp | 4 ++-- 4 files changed, 9 insertions(+), 9 deletions(-) diff --git a/include/jbus/Endpoint.hpp b/include/jbus/Endpoint.hpp index bda3d96..f614a57 100644 --- a/include/jbus/Endpoint.hpp +++ b/include/jbus/Endpoint.hpp @@ -91,7 +91,7 @@ class Endpoint { return x34_bytesSent * 100 / x64_totalBytes; } bool isDone() const { return !x14_callback; } - operator bool() const { return m_initialized; } + explicit operator bool() const { return m_initialized; } }; friend class ThreadLocalEndpoint; diff --git a/include/jbus/Listener.hpp b/include/jbus/Listener.hpp index bbdee65..74efba1 100644 --- a/include/jbus/Listener.hpp +++ b/include/jbus/Listener.hpp @@ -10,8 +10,8 @@ namespace jbus { /** Server interface for accepting incoming connections from GBA emulator instances. */ class Listener { - net::Socket m_dataServer = {false}; - net::Socket m_clockServer = {false}; + net::Socket m_dataServer{false}; + net::Socket m_clockServer{false}; std::thread m_listenerThread; std::mutex m_queueLock; std::queue> m_endpointQueue; diff --git a/include/jbus/Socket.hpp b/include/jbus/Socket.hpp index 86df640..e349d6d 100644 --- a/include/jbus/Socket.hpp +++ b/include/jbus/Socket.hpp @@ -24,10 +24,10 @@ class IPAddress { void resolve(const std::string& address); public: - IPAddress(const std::string& address) { resolve(address); } + explicit IPAddress(const std::string& address) { resolve(address); } uint32_t toInteger() const; - operator bool() const { return m_valid; } + explicit operator bool() const { return m_valid; } }; /** Server-oriented TCP socket class derived from SFML */ @@ -50,7 +50,7 @@ public: static EResult LastWSAError(); #endif - Socket(bool blocking) : m_isBlocking(blocking) {} + explicit Socket(bool blocking) : m_isBlocking(blocking) {} ~Socket() { close(); } Socket(const Socket& other) = delete; @@ -76,7 +76,7 @@ public: EResult recv(void* buf, size_t len, size_t& transferred); EResult recv(void* buf, size_t len); - operator bool() const { return isOpen(); } + explicit operator bool() const { return isOpen(); } SocketTp GetInternalSocket() const { return m_socket; } }; diff --git a/lib/Listener.cpp b/lib/Listener.cpp index 49c3adb..d2c9562 100644 --- a/lib/Listener.cpp +++ b/lib/Listener.cpp @@ -43,8 +43,8 @@ void Listener::listenerProc() { } /* We use blocking I/O since we have a dedicated transfer thread */ - net::Socket acceptData = {true}; - net::Socket acceptClock = {true}; + net::Socket acceptData{true}; + net::Socket acceptClock{true}; std::string hostname; while (m_running) { if (m_dataServer.accept(acceptData, hostname) == net::Socket::EResult::OK) {