From 5945b5500e30c52067d9aa4d8da26565c57972fb Mon Sep 17 00:00:00 2001 From: Sage Weil Date: Thu, 2 Aug 2018 14:35:16 -0500 Subject: [PATCH] common/cmdparse: add cmd_getval_throws variants that throw bad_cmd_get If input is bad (not missing). Signed-off-by: Sage Weil --- src/common/cmdparse.h | 41 +++++++++++++++++++++++++++++++++++++---- 1 file changed, 37 insertions(+), 4 deletions(-) diff --git a/src/common/cmdparse.h b/src/common/cmdparse.h index 81a46e4fb2dad..1bfc1bb4a7b09 100644 --- a/src/common/cmdparse.h +++ b/src/common/cmdparse.h @@ -57,8 +57,8 @@ struct bad_cmd_get : public std::exception { }; template -bool -cmd_getval(CephContext *cct, const cmdmap_t& cmdmap, const std::string& k, T& val) +bool cmd_getval(CephContext *cct, const cmdmap_t& cmdmap, const std::string& k, + T& val) { if (cmdmap.count(k)) { try { @@ -71,16 +71,49 @@ cmd_getval(CephContext *cct, const cmdmap_t& cmdmap, const std::string& k, T& va return false; } +template +bool cmd_getval_throws(CephContext *cct, const cmdmap_t& cmdmap, + const std::string& k, T& val) +{ + if (cmdmap.count(k)) { + try { + val = boost::get(cmdmap.find(k)->second); + return true; + } catch (boost::bad_get&) { + throw bad_cmd_get(k, cmdmap); + } + } + return false; +} + // with default template -void -cmd_getval(CephContext *cct, const cmdmap_t& cmdmap, const std::string& k, T& val, const T& defval) +void cmd_getval(CephContext *cct, const cmdmap_t& cmdmap, const std::string& k, + T& val, const T& defval) { if (!cmd_getval(cct, cmdmap, k, val)) val = defval; } +template +bool cmd_getval_throws( + CephContext *cct, const cmdmap_t& cmdmap, const std::string& k, + T& val, const T& defval) +{ + if (cmdmap.count(k)) { + try { + val = boost::get(cmdmap.find(k)->second); + return true; + } catch (boost::bad_get&) { + throw bad_cmd_get(k, cmdmap); + } + } else { + val = defval; + return true; + } +} + template void cmd_putval(CephContext *cct, cmdmap_t& cmdmap, const std::string& k, const T& val) -- 2.39.5