Add getFeature function to Device

This commit is contained in:
pixl 2020-07-02 16:04:18 -04:00
parent b445b979d3
commit c382ba1c0b
No known key found for this signature in database
GPG Key ID: 1866C148CD593B6E
2 changed files with 24 additions and 6 deletions

View File

@ -44,11 +44,11 @@ void Device::_init()
logPrintf(INFO, "Device found: %s on %s:%d", name().c_str(), logPrintf(INFO, "Device found: %s on %s:%d", name().c_str(),
hidpp20().devicePath().c_str(), _index); hidpp20().devicePath().c_str(), _index);
_addFeature<features::DPI>(); _addFeature<features::DPI>("dpi");
_addFeature<features::SmartShift>(); _addFeature<features::SmartShift>("smartshift");
for(auto& feature: _features) for(auto& feature: _features)
feature->configure(); feature.second->configure();
} }
std::string Device::name() std::string Device::name()

View File

@ -24,6 +24,7 @@
#include "backend/hidpp20/Feature.h" #include "backend/hidpp20/Feature.h"
#include "features/DeviceFeature.h" #include "features/DeviceFeature.h"
#include "Configuration.h" #include "Configuration.h"
#include "util/log.h"
namespace logid namespace logid
{ {
@ -60,15 +61,31 @@ namespace logid
void wakeup(); void wakeup();
void sleep(); void sleep();
template<typename T>
std::shared_ptr<T> getFeature(std::string name) {
auto it = _features.find(name);
if(it == _features.end())
return nullptr;
try {
return std::dynamic_pointer_cast<std::shared_ptr<T>>
(it->second);
} catch(std::bad_cast& e) {
logPrintf(ERROR, "bad_cast while getting device feature %s: "
"%s", name.c_str(), e.what());
return nullptr;
}
}
private: private:
void _init(); void _init();
/* Adds a feature without calling an error if unsupported */ /* Adds a feature without calling an error if unsupported */
template<typename T> template<typename T>
void _addFeature() void _addFeature(std::string name)
{ {
try { try {
_features.push_back(std::make_shared<T>(this)); _features.emplace(name, std::make_shared<T>(this));
} catch (backend::hidpp20::UnsupportedFeature& e) { } catch (backend::hidpp20::UnsupportedFeature& e) {
} }
} }
@ -76,7 +93,8 @@ namespace logid
backend::hidpp20::Device _hidpp20; backend::hidpp20::Device _hidpp20;
std::string _path; std::string _path;
backend::hidpp::DeviceIndex _index; backend::hidpp::DeviceIndex _index;
std::vector<std::shared_ptr<features::DeviceFeature>> _features; std::map<std::string, std::shared_ptr<features::DeviceFeature>>
_features;
DeviceConfig _config; DeviceConfig _config;
}; };
} }