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) {
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<DeviceBase> 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<DeviceBase> dev = tok.m_connectedDev;
tok._deviceClose();
deviceDisconnected(tok, dev.get());
m_tokensLock.lock();
m_tokens.erase(preCheck);
m_tokensLock.unlock();
}
bool DeviceFinder::startScanning() {