From 9853a97dd2632093cd4dcc0b146b574a151d9f25 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Sun, 18 Aug 2019 05:38:18 -0400 Subject: [PATCH 1/3] DeviceFinder: Move includes into cpp file where applicable Avoids over-exposing inclusions that don't need to be propagated across headers. --- include/boo/inputdev/DeviceFinder.hpp | 83 +++++---------------------- lib/inputdev/DeviceFinder.cpp | 73 +++++++++++++++++++++++ 2 files changed, 86 insertions(+), 70 deletions(-) diff --git a/include/boo/inputdev/DeviceFinder.hpp b/include/boo/inputdev/DeviceFinder.hpp index 3acb551..ede8b11 100644 --- a/include/boo/inputdev/DeviceFinder.hpp +++ b/include/boo/inputdev/DeviceFinder.hpp @@ -1,13 +1,12 @@ #pragma once -#include -#include +#include #include -#include "DeviceToken.hpp" -#include "IHIDListener.hpp" -#include "DeviceSignature.hpp" -#include -#include +#include +#include +#include "boo/inputdev/DeviceSignature.hpp" +#include "boo/inputdev/DeviceToken.hpp" +#include "boo/inputdev/IHIDListener.hpp" #ifdef _WIN32 #ifndef WIN32_LEAN_AND_MEAN @@ -44,28 +43,8 @@ private: bool _hasToken(const std::string& path) const { return m_tokens.find(path) != m_tokens.end(); } - bool _insertToken(std::unique_ptr&& token) { - if (DeviceSignature::DeviceMatchToken(*token, m_types)) { - m_tokensLock.lock(); - TInsertedDeviceToken inseredTok = m_tokens.insert(std::make_pair(token->getDevicePath(), std::move(token))); - m_tokensLock.unlock(); - deviceConnected(*inseredTok.first->second); - return true; - } - return false; - } - void _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(); - } - } + bool _insertToken(std::unique_ptr&& token); + void _removeToken(const std::string& path); public: class CDeviceTokensHandle { @@ -83,26 +62,8 @@ public: }; /* Application must specify its interested device-types */ - DeviceFinder(std::unordered_set types) { - if (skDevFinder) { - fmt::print(stderr, fmt("only one instance of CDeviceFinder may be constructed")); - abort(); - } - skDevFinder = this; - for (const uint64_t& typeHash : types) { - const DeviceSignature* sigIter = BOO_DEVICE_SIGS; - while (sigIter->m_name) { - if (sigIter->m_typeHash == typeHash) - m_types.push_back(sigIter); - ++sigIter; - } - } - } - virtual ~DeviceFinder() { - if (m_listener) - m_listener->stopScanning(); - skDevFinder = NULL; - } + DeviceFinder(std::unordered_set types); + virtual ~DeviceFinder(); /* Get interested device-type mask */ const DeviceSignature::TDeviceSignatureSet& getTypes() const { return m_types; } @@ -111,29 +72,11 @@ public: CDeviceTokensHandle getTokens() { return CDeviceTokensHandle(*this); } /* Automatic device scanning */ - bool startScanning() { - if (!m_listener) - m_listener = IHIDListenerNew(*this); - if (m_listener) - return m_listener->startScanning(); - return false; - } - bool stopScanning() { - if (!m_listener) - m_listener = IHIDListenerNew(*this); - if (m_listener) - return m_listener->stopScanning(); - return false; - } + bool startScanning(); + bool stopScanning(); /* Manual device scanning */ - bool scanNow() { - if (!m_listener) - m_listener = IHIDListenerNew(*this); - if (m_listener) - return m_listener->scanNow(); - return false; - } + bool scanNow(); virtual void deviceConnected(DeviceToken&) {} virtual void deviceDisconnected(DeviceToken&, DeviceBase*) {} diff --git a/lib/inputdev/DeviceFinder.cpp b/lib/inputdev/DeviceFinder.cpp index 3d0889a..e1d9c03 100644 --- a/lib/inputdev/DeviceFinder.cpp +++ b/lib/inputdev/DeviceFinder.cpp @@ -1,5 +1,8 @@ #include "boo/inputdev/DeviceFinder.hpp" +#include +#include + #if _WIN32 #include #include @@ -10,6 +13,76 @@ namespace boo { DeviceFinder* DeviceFinder::skDevFinder = nullptr; +DeviceFinder::DeviceFinder(std::unordered_set types) { + if (skDevFinder) { + fmt::print(stderr, fmt("only one instance of CDeviceFinder may be constructed")); + std::abort(); + } + skDevFinder = this; + for (const uint64_t& typeHash : types) { + const DeviceSignature* sigIter = BOO_DEVICE_SIGS; + while (sigIter->m_name) { + if (sigIter->m_typeHash == typeHash) + m_types.push_back(sigIter); + ++sigIter; + } + } +} + +DeviceFinder::~DeviceFinder() { + if (m_listener) + m_listener->stopScanning(); + skDevFinder = nullptr; +} + +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; + } + return false; +} + +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(); + } +} + +bool DeviceFinder::startScanning() { + if (!m_listener) + m_listener = IHIDListenerNew(*this); + if (m_listener) + return m_listener->startScanning(); + return false; +} + +bool DeviceFinder::stopScanning() { + if (!m_listener) + m_listener = IHIDListenerNew(*this); + if (m_listener) + return m_listener->stopScanning(); + return false; +} + +bool DeviceFinder::scanNow() { + if (!m_listener) + m_listener = IHIDListenerNew(*this); + if (m_listener) + return m_listener->scanNow(); + return false; +} + #if _WIN32 && !WINDOWS_STORE /* Windows-specific WM_DEVICECHANGED handler */ LRESULT DeviceFinder::winDevChangedHandler(WPARAM wParam, LPARAM lParam) { From 6cc5b3012719daf70c65ccfbb8305b220220641b Mon Sep 17 00:00:00 2001 From: Lioncash Date: Sun, 18 Aug 2019 05:48:54 -0400 Subject: [PATCH 2/3] 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() { From 90485ac1b9c45a3a296d13710788c360d7a93200 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Sun, 18 Aug 2019 05:50:35 -0400 Subject: [PATCH 3/3] DeviceFinder: Make use of unordered_map's emplace within _insertToken Same thing, but less reading. --- lib/inputdev/DeviceFinder.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/inputdev/DeviceFinder.cpp b/lib/inputdev/DeviceFinder.cpp index 0598f94..dfd30da 100644 --- a/lib/inputdev/DeviceFinder.cpp +++ b/lib/inputdev/DeviceFinder.cpp @@ -41,7 +41,7 @@ bool DeviceFinder::_insertToken(std::unique_ptr&& token) { } m_tokensLock.lock(); - TInsertedDeviceToken insertedTok = m_tokens.insert(std::make_pair(token->getDevicePath(), std::move(token))); + const TInsertedDeviceToken insertedTok = m_tokens.emplace(token->getDevicePath(), std::move(token)); m_tokensLock.unlock(); deviceConnected(*insertedTok.first->second); return true;