From e06b6b0be40e8d21180030a748cb809f4fa8ab3a Mon Sep 17 00:00:00 2001 From: sithlord48 Date: Sun, 29 Jun 2025 04:25:40 -0400 Subject: [PATCH] refactor: XSocket, new XSocketWithWhat subclass used to reduce duplication in XSocket subclasses that have a common with method --- src/lib/net/XSocket.cpp | 9 ++++ src/lib/net/XSocket.h | 106 ++++++++++------------------------------ 2 files changed, 35 insertions(+), 80 deletions(-) diff --git a/src/lib/net/XSocket.cpp b/src/lib/net/XSocket.cpp index 02c9ae17d..28d0e3a6e 100644 --- a/src/lib/net/XSocket.cpp +++ b/src/lib/net/XSocket.cpp @@ -69,6 +69,15 @@ std::string XSocketBind::getWhat() const throw() return format("XSocketBind", "cannot bind address: %{1}", what()); } +// +// XSocketAddressInUse +// + +std::string XSocketAddressInUse::getWhat() const throw() +{ + return format("XSocketAddressInUse", "cannot bind address: %{1}", what()); +} + // // XSocketConnect // diff --git a/src/lib/net/XSocket.h b/src/lib/net/XSocket.h index 318dcbb76..d3bc699e8 100644 --- a/src/lib/net/XSocket.h +++ b/src/lib/net/XSocket.h @@ -106,20 +106,20 @@ private: }; /** - * @brief XSocketBind - Thrown when a socket cannot be bound to an address. + * @brief XSocketWithWhat - generic XSocket Exception with a generic `what` method impl */ -class XSocketBind : public XSocket +class XSocketWithWhat : public XSocket { public: - XSocketBind() : XSocket(), m_state(kDone) + XSocketWithWhat() : XSocket(), m_state(kDone) { // do nothing } - explicit XSocketBind(const std::string &msg) : XSocket(msg), m_state(kFirst) + explicit XSocketWithWhat(const std::string &msg) : XSocket(msg), m_state(kFirst) { // do nothing } - ~XSocketBind() throw() override = default; + ~XSocketWithWhat() throw() override = default; const char *what() const throw() override { @@ -135,9 +135,6 @@ public: } } -protected: - std::string getWhat() const throw() override; - private: enum EState { @@ -148,99 +145,48 @@ private: mutable EState m_state; mutable std::string m_formatted; }; + +/** + * @brief XSocketBind - Thrown when a socket cannot be bound to an address. + */ +class XSocketBind : public XSocketWithWhat +{ + using XSocketWithWhat::XSocketWithWhat; + +protected: + std::string getWhat() const throw() override; +}; + /** * @brief XSocketAddressInUse * Thrown when a socket cannot be bound to an address because the address is already in use. */ -class XSocketAddressInUse : public XSocketBind +class XSocketAddressInUse : public XSocketWithWhat { - using XSocketBind::XSocketBind; + using XSocketWithWhat::XSocketWithWhat; + +protected: + std::string getWhat() const throw() override; }; /** * @brief XSocketConnect - Thrown when a socket cannot connect to a remote endpoint. */ -class XSocketConnect : public XSocket +class XSocketConnect : public XSocketWithWhat { -public: - XSocketConnect() : XSocket(), m_state(kDone) - { - // do nothing - } - explicit XSocketConnect(const std::string &msg) : XSocket(msg), m_state(kFirst) - { - // do nothing - } - ~XSocketConnect() throw() override = default; - - const char *what() const throw() override - { - if (m_state == kFirst) { - m_state = kFormat; - m_formatted = getWhat(); - m_state = kDone; - } - if (m_state == kDone) { - return m_formatted.c_str(); - } else { - return XSocket::what(); - } - } + using XSocketWithWhat::XSocketWithWhat; protected: std::string getWhat() const throw() override; - -private: - enum EState - { - kFirst, - kFormat, - kDone - }; - mutable EState m_state; - mutable std::string m_formatted; }; /** * @brief XSocketConnect - Thrown when a socket cannot be created (by the operating system). */ -class XSocketCreate : public XSocket +class XSocketCreate : public XSocketWithWhat { -public: - XSocketCreate() : XSocket(), m_state(kDone) - { - // do nothing - } - explicit XSocketCreate(const std::string &msg) : XSocket(msg), m_state(kFirst) - { - // do nothing - } - ~XSocketCreate() throw() override = default; - - const char *what() const throw() override - { - if (m_state == kFirst) { - m_state = kFormat; - m_formatted = getWhat(); - m_state = kDone; - } - if (m_state == kDone) { - return m_formatted.c_str(); - } else { - return XSocket::what(); - } - } + using XSocketWithWhat::XSocketWithWhat; protected: std::string getWhat() const throw() override; - -private: - enum EState - { - kFirst, - kFormat, - kDone - }; - mutable EState m_state; - mutable std::string m_formatted; };