]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph-ci.git/commitdiff
common/cmdparse: add another template cmd_getval_or helper
authorPatrick Donnelly <pdonnell@ibm.com>
Fri, 21 Mar 2025 16:57:25 +0000 (12:57 -0400)
committerPatrick Donnelly <pdonnell@ibm.com>
Wed, 1 Oct 2025 18:46:56 +0000 (14:46 -0400)
To mimic the conventional signature where you pass the lvalue you want to set.

Signed-off-by: Patrick Donnelly <pdonnell@ibm.com>
src/common/cmdparse.h

index 70c6045c3bb16b32225f1a9f94d41158bf053e33..94f5576ffbd9212b63e500608f42e22efc71442c 100644 (file)
@@ -135,6 +135,23 @@ T cmd_getval_cast_or(const cmdmap_t& cmdmap, std::string_view k, T defval)
   }
 }
 
+template <typename T, typename V>
+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<T>(cmdmap.find(k)->second);
+    } catch (boost::bad_get&) {
+      throw bad_cmd_get(k, cmdmap);
+    }
+  }
+}
+
+
 template <typename T>
 void
 cmd_putval(CephContext *cct, cmdmap_t& cmdmap, std::string_view k, const T& val)