From 6cc5b3012719daf70c65ccfbb8305b220220641b Mon Sep 17 00:00:00 2001 From: Lioncash Date: Sun, 18 Aug 2019 05:48:54 -0400 Subject: [PATCH] DeviceFinder: Invert conditionals within _insertToken and _removeToken By converting the conditions into guard clauses, we can eliminate some code nesting. --- lib/inputdev/DeviceFinder.cpp | 35 +++++++++++++++++++---------------- 1 file changed, 19 insertions(+), 16 deletions(-) diff --git a/lib/inputdev/DeviceFinder.cpp b/lib/inputdev/DeviceFinder.cpp index e1d9c03..0598f94 100644 --- a/lib/inputdev/DeviceFinder.cpp +++ b/lib/inputdev/DeviceFinder.cpp @@ -36,27 +36,30 @@ DeviceFinder::~DeviceFinder() { } bool DeviceFinder::_insertToken(std::unique_ptr&& token) { - if (DeviceSignature::DeviceMatchToken(*token, m_types)) { - m_tokensLock.lock(); - TInsertedDeviceToken insertedTok = m_tokens.insert(std::make_pair(token->getDevicePath(), std::move(token))); - m_tokensLock.unlock(); - deviceConnected(*insertedTok.first->second); - return true; + if (!DeviceSignature::DeviceMatchToken(*token, m_types)) { + return false; } - return false; + + m_tokensLock.lock(); + TInsertedDeviceToken insertedTok = m_tokens.insert(std::make_pair(token->getDevicePath(), std::move(token))); + m_tokensLock.unlock(); + deviceConnected(*insertedTok.first->second); + return true; } void DeviceFinder::_removeToken(const std::string& path) { - auto preCheck = m_tokens.find(path); - if (preCheck != m_tokens.end()) { - DeviceToken& tok = *preCheck->second; - std::shared_ptr dev = tok.m_connectedDev; - tok._deviceClose(); - deviceDisconnected(tok, dev.get()); - m_tokensLock.lock(); - m_tokens.erase(preCheck); - m_tokensLock.unlock(); + const auto preCheck = m_tokens.find(path); + if (preCheck == m_tokens.end()) { + return; } + + DeviceToken& tok = *preCheck->second; + std::shared_ptr dev = tok.m_connectedDev; + tok._deviceClose(); + deviceDisconnected(tok, dev.get()); + m_tokensLock.lock(); + m_tokens.erase(preCheck); + m_tokensLock.unlock(); } bool DeviceFinder::startScanning() {