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 <string>
#include <iostream> #include <iostream>
#include <sstream> #include <sstream>
#include <algorithm>
#include <stdexcept>
namespace smo { namespace smo {
namespace device { namespace device {
@@ -65,6 +67,40 @@ public:
return os.str(); 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 class InteroceptorDevAttachmentSpec : public DeviceAttachmentSpec
+6 -29
View File
@@ -69,9 +69,12 @@ AttachedWindow::AttachedWindow(
": Required xcbXorg function pointers not available"); ": Required xcbXorg function pointers not available");
} }
windowSelector.display = getRequiredParamAsInt(*spec, "display"); windowSelector.display = smo::device::DeviceAttachmentSpec
windowSelector.screen = getRequiredParamAsInt(*spec, "screen"); ::parseRequiredParamAsInt(*spec, "display");
parseWindowSelector(*spec); windowSelector.screen = smo::device::DeviceAttachmentSpec
::parseRequiredParamAsInt(*spec, "screen");
parseWindowSelector(*spec);
// Get connection from libxcbXorg // Get connection from libxcbXorg
std::shared_ptr<xcb_xorg::XcbConnection> conn = std::shared_ptr<xcb_xorg::XcbConnection> conn =
@@ -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::string AttachedWindow::stringify() const {
std::ostringstream os; std::ostringstream os;
-3
View File
@@ -44,9 +44,6 @@ public:
private: private:
void parseWindowSelector(const smo::device::DeviceAttachmentSpec& spec); 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; std::shared_ptr<smo::device::DeviceAttachmentSpec> deviceAttachmentSpec;
WindowSelector windowSelector; WindowSelector windowSelector;