From a90d0fce2bfb9be2efe9c27a697df9e63f69f49d Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Sun, 18 Feb 2024 18:48:27 -0600 Subject: [PATCH] refactor: inline simple getters in qt client (#6621) * refactor: make RpcClient::url() constexpr * refactor: make Session::getRemoteUrl() constexpr * refactor: make RpcQueue::setTolerateErrors() constexpr * refactor: make RpcClient::isLocal() constexpr * refactor: make Session::isLocal() constexpr * refactor: make Session::isServer() constexpr * refactor: make PathButton::label() constexpr * chore: remove accidentally-committed makelog file --- libtransmission/makelog | 1 - qt/PathButton.cc | 5 ----- qt/PathButton.h | 6 +++++- qt/RpcClient.cc | 21 +-------------------- qt/RpcClient.h | 14 +++++++++++--- qt/RpcQueue.h | 2 +- qt/Session.cc | 19 +------------------ qt/Session.h | 12 +++++++++--- 8 files changed, 28 insertions(+), 52 deletions(-) delete mode 100644 libtransmission/makelog diff --git a/libtransmission/makelog b/libtransmission/makelog deleted file mode 100644 index cf407e178..000000000 --- a/libtransmission/makelog +++ /dev/null @@ -1 +0,0 @@ -ninja: error: loading 'build.ninja': No such file or directory diff --git a/qt/PathButton.cc b/qt/PathButton.cc index 5a87c4504..a4a3a04fa 100644 --- a/qt/PathButton.cc +++ b/qt/PathButton.cc @@ -63,11 +63,6 @@ void PathButton::setPath(QString const& path) emit pathChanged(path_); } -QString const& PathButton::path() const -{ - return path_; -} - QSize PathButton::sizeHint() const { auto const sh = QToolButton::sizeHint(); diff --git a/qt/PathButton.h b/qt/PathButton.h index df26e8af2..398cf1e54 100644 --- a/qt/PathButton.h +++ b/qt/PathButton.h @@ -28,7 +28,11 @@ public: void setNameFilter(QString const& name_filter); void setPath(QString const& path); - QString const& path() const; + + [[nodiscard]] constexpr auto const& path() const noexcept + { + return path_; + } // QWidget QSize sizeHint() const override; diff --git a/qt/RpcClient.cc b/qt/RpcClient.cc index 4a12e8d83..46f8d87d2 100644 --- a/qt/RpcClient.cc +++ b/qt/RpcClient.cc @@ -67,29 +67,10 @@ void RpcClient::start(tr_session* session) void RpcClient::start(QUrl const& url) { url_ = url; + url_is_loopback_ = QHostAddress{ url_.host() }.isLoopback(); request_.reset(); } -bool RpcClient::isLocal() const -{ - if (session_ != nullptr) - { - return true; - } - - if (QHostAddress{ url_.host() }.isLoopback()) - { - return true; - } - - return false; -} - -QUrl const& RpcClient::url() const -{ - return url_; -} - RpcResponseFuture RpcClient::exec(tr_quark method, tr_variant* args) { return exec(tr_quark_get_string_view(method), args); diff --git a/qt/RpcClient.h b/qt/RpcClient.h index 62acb02e7..a12d30cbe 100644 --- a/qt/RpcClient.h +++ b/qt/RpcClient.h @@ -55,13 +55,20 @@ class RpcClient : public QObject public: explicit RpcClient(QObject* parent = nullptr); + [[nodiscard]] constexpr auto const& url() const noexcept + { + return url_; + } + + [[nodiscard]] constexpr auto isLocal() const noexcept + { + return session_ != nullptr || url_is_loopback_; + } + void stop(); void start(tr_session* session); void start(QUrl const& url); - bool isLocal() const; - QUrl const& url() const; - RpcResponseFuture exec(tr_quark method, tr_variant* args); RpcResponseFuture exec(std::string_view method, tr_variant* args); @@ -94,4 +101,5 @@ private: std::unordered_map> local_requests_; int64_t next_tag_ = {}; bool const verbose_ = qEnvironmentVariableIsSet("TR_RPC_VERBOSE"); + bool url_is_loopback_ = false; }; diff --git a/qt/RpcQueue.h b/qt/RpcQueue.h index c98f16014..a798ae196 100644 --- a/qt/RpcQueue.h +++ b/qt/RpcQueue.h @@ -27,7 +27,7 @@ class RpcQueue : public QObject public: explicit RpcQueue(QObject* parent = nullptr); - void setTolerateErrors(bool tolerate_errors = true) + constexpr void setTolerateErrors(bool tolerate_errors = true) { tolerate_errors_ = tolerate_errors; } diff --git a/qt/Session.cc b/qt/Session.cc index 34398e62c..8646013da 100644 --- a/qt/Session.cc +++ b/qt/Session.cc @@ -386,24 +386,7 @@ void Session::start() emit sourceChanged(); } -bool Session::isServer() const -{ - return session_ != nullptr; -} - -bool Session::isLocal() const -{ - if (!session_id_.isEmpty()) - { - return is_definitely_local_session_; - } - - return rpc_.isLocal(); -} - -/*** -**** -***/ +// --- void Session::addOptionalIds(tr_variant* args_dict, torrent_ids_t const& torrent_ids) const { diff --git a/qt/Session.h b/qt/Session.h index 70e36d4af..7ac6982e9 100644 --- a/qt/Session.h +++ b/qt/Session.h @@ -46,7 +46,7 @@ public: void stop(); void restart(); - QUrl const& getRemoteUrl() const + [[nodiscard]] constexpr auto const& getRemoteUrl() const noexcept { return rpc_.url(); } @@ -86,10 +86,16 @@ public: bool portTestPending(PortTestIpProtocol ip_protocol) const noexcept; /** returns true if the transmission session is being run inside this client */ - bool isServer() const; + [[nodiscard]] constexpr auto isServer() const noexcept + { + return session_ != nullptr; + } /** returns true if isServer() is true or if the remote address is the localhost */ - bool isLocal() const; + [[nodiscard]] auto isLocal() const noexcept + { + return !session_id_.isEmpty() ? is_definitely_local_session_ : rpc_.isLocal(); + } RpcResponseFuture exec(tr_quark method, tr_variant* args); RpcResponseFuture exec(std::string_view method, tr_variant* args);