From 73936eda967d3fa0d8a2bb9d5dfb31c506079b6b Mon Sep 17 00:00:00 2001 From: Patrick Donnelly Date: Fri, 21 Mar 2025 12:57:25 -0400 Subject: [PATCH] common/cmdparse: add another template cmd_getval_or helper To mimic the conventional signature where you pass the lvalue you want to set. Signed-off-by: Patrick Donnelly (cherry picked from commit ba57b3b5479dc238b4d041a6f82eaf2c38a97ea1) --- src/common/cmdparse.h | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/common/cmdparse.h b/src/common/cmdparse.h index 70c6045c3bb..94f5576ffbd 100644 --- a/src/common/cmdparse.h +++ b/src/common/cmdparse.h @@ -135,6 +135,23 @@ T cmd_getval_cast_or(const cmdmap_t& cmdmap, std::string_view k, T defval) } } +template +void cmd_getval_or(const cmdmap_t& cmdmap, std::string_view k, + V& val, const V& defval) +{ + auto found = cmdmap.find(k); + if (found == cmdmap.end()) { + val = T(defval); + } else { + try { + val = boost::get(cmdmap.find(k)->second); + } catch (boost::bad_get&) { + throw bad_cmd_get(k, cmdmap); + } + } +} + + template void cmd_putval(CephContext *cct, cmdmap_t& cmdmap, std::string_view k, const T& val) -- 2.39.5