From 65efde32c99b8119d3c55729508b92801b60cc13 Mon Sep 17 00:00:00 2001 From: Vaxry Date: Sun, 12 Nov 2023 13:40:02 +0000 Subject: internal: make getPlusMinusKeywordResult return optional --- src/helpers/MiscFunctions.cpp | 29 ++++++++++++++++++++++------- src/helpers/MiscFunctions.hpp | 2 +- 2 files changed, 23 insertions(+), 8 deletions(-) (limited to 'src/helpers') diff --git a/src/helpers/MiscFunctions.cpp b/src/helpers/MiscFunctions.cpp index 7a9c5e5e..85a81657 100644 --- a/src/helpers/MiscFunctions.cpp +++ b/src/helpers/MiscFunctions.cpp @@ -204,12 +204,12 @@ std::string removeBeginEndSpacesTabs(std::string str) { return str; } -float getPlusMinusKeywordResult(std::string source, float relative) { +std::optional getPlusMinusKeywordResult(std::string source, float relative) { try { return relative + stof(source); } catch (...) { Debug::log(ERR, "Invalid arg \"{}\" in getPlusMinusKeywordResult!", source); - return INT_MAX; + return {}; } } @@ -300,7 +300,13 @@ int getWorkspaceIDFromString(const std::string& in, std::string& outName) { Debug::log(ERR, "Relative monitor workspace on monitor null!"); return WORKSPACE_INVALID; } - result = (int)getPlusMinusKeywordResult(in.substr(1), 0); + + const auto PLUSMINUSRESULT = getPlusMinusKeywordResult(in.substr(1), 0); + + if (!PLUSMINUSRESULT.has_value()) + return WORKSPACE_INVALID; + + result = (int)PLUSMINUSRESULT.value(); int remains = (int)result; @@ -436,7 +442,12 @@ int getWorkspaceIDFromString(const std::string& in, std::string& outName) { } // monitor relative - result = (int)getPlusMinusKeywordResult(in.substr(1), 0); + const auto PLUSMINUSRESULT = getPlusMinusKeywordResult(in.substr(1), 0); + + if (!PLUSMINUSRESULT.has_value()) + return WORKSPACE_INVALID; + + result = (int)PLUSMINUSRESULT.value(); // result now has +/- what we should move on mon int remains = (int)result; @@ -477,9 +488,13 @@ int getWorkspaceIDFromString(const std::string& in, std::string& outName) { outName = g_pCompositor->getWorkspaceByID(validWSes[currentItem])->m_szName; } else { if (in[0] == '+' || in[0] == '-') { - if (g_pCompositor->m_pLastMonitor) - result = std::max((int)getPlusMinusKeywordResult(in, g_pCompositor->m_pLastMonitor->activeWorkspace), 1); - else { + if (g_pCompositor->m_pLastMonitor) { + const auto PLUSMINUSRESULT = getPlusMinusKeywordResult(in, g_pCompositor->m_pLastMonitor->activeWorkspace); + if (!PLUSMINUSRESULT.has_value()) + return WORKSPACE_INVALID; + + result = std::max((int)PLUSMINUSRESULT.value(), 1); + } else { Debug::log(ERR, "Relative workspace on no mon!"); return WORKSPACE_INVALID; } diff --git a/src/helpers/MiscFunctions.hpp b/src/helpers/MiscFunctions.hpp index b2fe5858..90479121 100644 --- a/src/helpers/MiscFunctions.hpp +++ b/src/helpers/MiscFunctions.hpp @@ -27,7 +27,7 @@ void logSystemInfo(); std::string execAndGet(const char*); int64_t getPPIDof(int64_t pid); int64_t configStringToInt(const std::string&); -float getPlusMinusKeywordResult(std::string in, float relative); +std::optional getPlusMinusKeywordResult(std::string in, float relative); void matrixProjection(float mat[9], int w, int h, wl_output_transform tr); double normalizeAngleRad(double ang); std::string replaceInString(std::string subject, const std::string& search, const std::string& replace); -- cgit v1.2.3