From 3b07a15e11f8c1b5e231d984e80f68c995d95216 Mon Sep 17 00:00:00 2001 From: Hayodea Hakol Date: Sat, 6 Sep 2025 21:12:43 -0400 Subject: [PATCH] Move getRequiredParamAsInt into deviceAttachmentSpec.h Make it accessible for all senseApis. --- include/user/deviceAttachmentSpec.h | 36 +++++++++++++++++++++++++++++ senseApis/xcbWindow/xcbWindow.cpp | 35 +++++----------------------- senseApis/xcbWindow/xcbWindow.h | 3 --- 3 files changed, 42 insertions(+), 32 deletions(-) diff --git a/include/user/deviceAttachmentSpec.h b/include/user/deviceAttachmentSpec.h index aa12028..451d76e 100644 --- a/include/user/deviceAttachmentSpec.h +++ b/include/user/deviceAttachmentSpec.h @@ -5,6 +5,8 @@ #include #include #include +#include +#include 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(), + [¶mName](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 diff --git a/senseApis/xcbWindow/xcbWindow.cpp b/senseApis/xcbWindow/xcbWindow.cpp index 083dcd0..5542be5 100644 --- a/senseApis/xcbWindow/xcbWindow.cpp +++ b/senseApis/xcbWindow/xcbWindow.cpp @@ -69,9 +69,12 @@ AttachedWindow::AttachedWindow( ": Required xcbXorg function pointers not available"); } - windowSelector.display = getRequiredParamAsInt(*spec, "display"); - windowSelector.screen = getRequiredParamAsInt(*spec, "screen"); - parseWindowSelector(*spec); + windowSelector.display = smo::device::DeviceAttachmentSpec + ::parseRequiredParamAsInt(*spec, "display"); + windowSelector.screen = smo::device::DeviceAttachmentSpec + ::parseRequiredParamAsInt(*spec, "screen"); + + parseWindowSelector(*spec); // Get connection from libxcbXorg std::shared_ptr 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(), - [¶mName](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; diff --git a/senseApis/xcbWindow/xcbWindow.h b/senseApis/xcbWindow/xcbWindow.h index 7093a11..f5e4679 100644 --- a/senseApis/xcbWindow/xcbWindow.h +++ b/senseApis/xcbWindow/xcbWindow.h @@ -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 deviceAttachmentSpec; WindowSelector windowSelector;