]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
common/cmdparse: add cmd_getval_throws variants that throw bad_cmd_get
authorSage Weil <sage@redhat.com>
Thu, 2 Aug 2018 19:35:16 +0000 (14:35 -0500)
committerSage Weil <sage@redhat.com>
Sun, 5 Aug 2018 02:54:24 +0000 (21:54 -0500)
If input is bad (not missing).

Signed-off-by: Sage Weil <sage@redhat.com>
src/common/cmdparse.h

index 81a46e4fb2dad593f4fed8a7b74a6737cd361572..1bfc1bb4a7b094e67d14e33fb14e22fb6c642745 100644 (file)
@@ -57,8 +57,8 @@ struct bad_cmd_get : public std::exception {
 };
 
 template <typename T>
-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 <typename T>
+bool cmd_getval_throws(CephContext *cct, const cmdmap_t& cmdmap,
+                      const std::string& k, T& val)
+{
+  if (cmdmap.count(k)) {
+    try {
+      val = boost::get<T>(cmdmap.find(k)->second);
+      return true;
+    } catch (boost::bad_get&) {
+      throw bad_cmd_get(k, cmdmap);
+    }
+  }
+  return false;
+}
+
 // with default
 
 template <typename T>
-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 <typename T>
+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<T>(cmdmap.find(k)->second);
+      return true;
+    } catch (boost::bad_get&) {
+      throw bad_cmd_get(k, cmdmap);
+    }
+  } else {
+    val = defval;
+    return true;
+  }
+}
+
 template <typename T>
 void
 cmd_putval(CephContext *cct, cmdmap_t& cmdmap, const std::string& k, const T& val)