DeviceFinder: Invert conditionals within _insertToken and _removeToken

By converting the conditions into guard clauses, we can eliminate some
code nesting.
This commit is contained in:
Lioncash 2019-08-18 05:48:54 -04:00
parent 9853a97dd2
commit 6cc5b30127
1 changed files with 19 additions and 16 deletions

View File

@ -36,27 +36,30 @@ DeviceFinder::~DeviceFinder() {
} }
bool DeviceFinder::_insertToken(std::unique_ptr<DeviceToken>&& token) { bool DeviceFinder::_insertToken(std::unique_ptr<DeviceToken>&& token) {
if (DeviceSignature::DeviceMatchToken(*token, m_types)) { if (!DeviceSignature::DeviceMatchToken(*token, m_types)) {
m_tokensLock.lock(); return false;
TInsertedDeviceToken insertedTok = m_tokens.insert(std::make_pair(token->getDevicePath(), std::move(token)));
m_tokensLock.unlock();
deviceConnected(*insertedTok.first->second);
return true;
} }
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) { void DeviceFinder::_removeToken(const std::string& path) {
auto preCheck = m_tokens.find(path); const auto preCheck = m_tokens.find(path);
if (preCheck != m_tokens.end()) { if (preCheck == m_tokens.end()) {
DeviceToken& tok = *preCheck->second; return;
std::shared_ptr<DeviceBase> dev = tok.m_connectedDev;
tok._deviceClose();
deviceDisconnected(tok, dev.get());
m_tokensLock.lock();
m_tokens.erase(preCheck);
m_tokensLock.unlock();
} }
DeviceToken& tok = *preCheck->second;
std::shared_ptr<DeviceBase> dev = tok.m_connectedDev;
tok._deviceClose();
deviceDisconnected(tok, dev.get());
m_tokensLock.lock();
m_tokens.erase(preCheck);
m_tokensLock.unlock();
} }
bool DeviceFinder::startScanning() { bool DeviceFinder::startScanning() {