]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
common: introduce std::optional-returning variants of cmd_getval(). 41434/head
authorRadoslaw Zarzynski <rzarzyns@redhat.com>
Wed, 19 May 2021 12:28:31 +0000 (12:28 +0000)
committerKefu Chai <kchai@redhat.com>
Sat, 22 May 2021 16:31:09 +0000 (00:31 +0800)
Using an output paramter instead of returning is confusing but
common in pre-C++11 code. Let's modernize `cmd_getval()`.

Signed-off-by: Radoslaw Zarzynski <rzarzyns@redhat.com>
src/common/cmdparse.h

index da3baaff79d91712011c5e0be847c2a2f6fa062a..0dec47773a3988b845a8e520830b3d421b5697a6 100644 (file)
@@ -5,6 +5,7 @@
 
 #include <vector>
 #include <stdexcept>
+#include <optional>
 #include <ostream>
 #include <boost/variant.hpp>
 #include "include/ceph_assert.h"       // boost clobbers this
@@ -77,6 +78,18 @@ bool cmd_getval(const cmdmap_t& cmdmap,
   }
 }
 
+template <typename T>
+std::optional<T> cmd_getval(const cmdmap_t& cmdmap,
+                           std::string_view k)
+{
+  T ret;
+  if (const bool found = cmd_getval(cmdmap, k, ret); found) {
+    return std::make_optional(std::move(ret));
+  } else {
+    return std::nullopt;
+  }
+}
+
 // with default
 
 template <typename T, typename V>