Move getRequiredParamAsInt into deviceAttachmentSpec.h

Make it accessible for all senseApis.
This commit is contained in:
2025-09-06 21:12:43 -04:00
parent 38b29ddfc0
commit 3b07a15e11
3 changed files with 42 additions and 32 deletions
+36
View File
@@ -5,6 +5,8 @@
#include <string>
#include <iostream>
#include <sstream>
#include <algorithm>
#include <stdexcept>
namespace smo {
namespace device {
@@ -65,6 +67,40 @@ public:
return os.str();
}
/**
* @brief Parse a required integer parameter from provider parameters
* @param spec The device attachment specification
* @param paramName The name of the parameter to parse
* @return The parsed integer value
* @throws std::runtime_error if parameter is not found or cannot be parsed
*/
static int parseRequiredParamAsInt(
const DeviceAttachmentSpec& spec, const std::string& paramName
)
{
auto it = std::find_if(
spec.providerParams.begin(),
spec.providerParams.end(),
[&paramName](const auto& param) {
return param.first == paramName;
}
);
if (it == spec.providerParams.end())
{
throw std::runtime_error(
"No " + paramName + " specified in provider params");
}
try {
return std::stoi(it->second);
} catch (const std::exception& e) {
throw std::runtime_error(
"Failed to parse '" + paramName + "' param value '"
+ it->second + "' as integer: " + e.what());
}
}
};
class InteroceptorDevAttachmentSpec : public DeviceAttachmentSpec
+5 -28
View File
@@ -69,8 +69,11 @@ AttachedWindow::AttachedWindow(
": Required xcbXorg function pointers not available");
}
windowSelector.display = getRequiredParamAsInt(*spec, "display");
windowSelector.screen = getRequiredParamAsInt(*spec, "screen");
windowSelector.display = smo::device::DeviceAttachmentSpec
::parseRequiredParamAsInt(*spec, "display");
windowSelector.screen = smo::device::DeviceAttachmentSpec
::parseRequiredParamAsInt(*spec, "screen");
parseWindowSelector(*spec);
// Get connection from libxcbXorg
@@ -149,32 +152,6 @@ void AttachedWindow::parseWindowSelector(
}
}
int AttachedWindow::getRequiredParamAsInt(const smo::device::DeviceAttachmentSpec& spec,
const std::string& paramName)
{
auto it = std::find_if(
spec.providerParams.begin(),
spec.providerParams.end(),
[&paramName](const auto& param) {
return param.first == paramName;
}
);
if (it == spec.providerParams.end())
{
throw std::runtime_error(
"No " + paramName + " specified in provider params");
}
try {
return std::stoi(it->second);
} catch (const std::exception& e) {
throw std::runtime_error(
"Failed to parse '" + paramName + "' param value '"
+ it->second + "' as integer: " + e.what());
}
}
std::string AttachedWindow::stringify() const {
std::ostringstream os;
-3
View File
@@ -44,9 +44,6 @@ public:
private:
void parseWindowSelector(const smo::device::DeviceAttachmentSpec& spec);
int getRequiredParamAsInt(
const smo::device::DeviceAttachmentSpec& spec,
const std::string& paramName);
std::shared_ptr<smo::device::DeviceAttachmentSpec> deviceAttachmentSpec;
WindowSelector windowSelector;