]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
common/options: s/boost::variant/std::variant/
authorKefu Chai <kchai@redhat.com>
Sun, 27 Jun 2021 02:38:39 +0000 (10:38 +0800)
committerKefu Chai <kchai@redhat.com>
Tue, 29 Jun 2021 08:56:58 +0000 (16:56 +0800)
we should use standard library for more well defined behavior, and
less dependencies on 3rd party libraries.

Signed-off-by: Kefu Chai <kchai@redhat.com>
src/common/config.cc
src/common/config.h
src/common/options.cc
src/common/options.h
src/mgr/ActivePyModules.cc
src/mon/ConfigMonitor.cc

index 855a284b719d60907b8b73987eb94a961356f648..ca9f194a125b791359674b775b22690489a087a4 100644 (file)
@@ -106,7 +106,7 @@ int ceph_resolve_file_search(const std::string& filename_list,
 
 static int conf_stringify(const Option::value_t& v, string *out)
 {
-  if (boost::get<boost::blank>(&v)) {
+  if (v == Option::value_t{}) {
     return -ENOENT;
   }
   *out = Option::to_str(v);
@@ -193,7 +193,7 @@ md_config_t::md_config_t(ConfigValues& values,
   for (const auto &i : schema) {
     const Option &opt = i.second;
     if (opt.type == Option::TYPE_STR) {
-      bool has_daemon_default = !boost::get<boost::blank>(&opt.daemon_value);
+      bool has_daemon_default = (opt.daemon_value != Option::value_t{});
       Option::value_t default_val;
       if (is_daemon && has_daemon_default) {
        default_val = opt.daemon_value;
@@ -202,7 +202,7 @@ md_config_t::md_config_t(ConfigValues& values,
       }
       // We call pre_validate as a sanity check, but also to get any
       // side effect (value modification) from the validator.
-      std::string *def_str = boost::get<std::string>(&default_val);
+      auto* def_str = std::get_if<std::string>(&default_val);
       std::string val = *def_str;
       std::string err;
       if (opt.pre_validate(&val, &err) != 0) {
@@ -1055,7 +1055,7 @@ Option::value_t md_config_t::_get_val(
   std::ostream *err) const
 {
   if (key.empty()) {
-    return Option::value_t(boost::blank());
+    return {};
   }
 
   // In key names, leading and trailing whitespace are not significant.
@@ -1064,7 +1064,7 @@ Option::value_t md_config_t::_get_val(
   const Option *o = find_option(k);
   if (!o) {
     // not a valid config option
-    return Option::value_t(boost::blank());
+    return {};
   }
 
   return _get_val(values, *o, stack, err);
@@ -1097,7 +1097,7 @@ Option::value_t md_config_t::_get_val_nometa(const ConfigValues& values,
 
 const Option::value_t& md_config_t::_get_val_default(const Option& o) const
 {
-  bool has_daemon_default = !boost::get<boost::blank>(&o.daemon_value);
+  bool has_daemon_default = (o.daemon_value != Option::value_t{});
   if (is_daemon && has_daemon_default) {
     return o.daemon_value;
   } else {
@@ -1146,7 +1146,7 @@ Option::value_t md_config_t::_expand_meta(
   if (!stack) {
     return in;
   }
-  const std::string *str = boost::get<const std::string>(&in);
+  const auto str = std::get_if<std::string>(&in);
   if (!str) {
     // strings only!
     return in;
@@ -1490,19 +1490,19 @@ class assign_visitor : public boost::static_visitor<>
   {
     T *member = const_cast<T *>(&(conf->*(boost::get<const T Config::*>(ptr))));
 
-    *member = boost::get<T>(val);
+    *member = std::get<T>(val);
   }
   void operator()(uint64_t Config::* ptr) const
   {
     using T = uint64_t;
     auto member = const_cast<T*>(&(conf->*(boost::get<const T Config::*>(ptr))));
-    *member = boost::apply_visitor(get_size_visitor<T>{}, val);
+    *member = std::visit(get_size_visitor<T>{}, val);
   }
   void operator()(int64_t Config::* ptr) const
   {
     using T = int64_t;
     auto member = const_cast<T*>(&(conf->*(boost::get<const T Config::*>(ptr))));
-    *member = boost::apply_visitor(get_size_visitor<T>{}, val);
+    *member = std::visit(get_size_visitor<T>{}, val);
   }
 };
 } // anonymous namespace
@@ -1527,13 +1527,13 @@ void md_config_t::update_legacy_val(ConfigValues& values,
 
 static void dump(Formatter *f, int level, Option::value_t in)
 {
-  if (const bool *v = boost::get<const bool>(&in)) {
+  if (const auto v = std::get_if<bool>(&in)) {
     f->dump_bool(ceph_conf_level_name(level), *v);
-  } else if (const int64_t *v = boost::get<const int64_t>(&in)) {
+  } else if (const auto v = std::get_if<int64_t>(&in)) {
     f->dump_int(ceph_conf_level_name(level), *v);
-  } else if (const uint64_t *v = boost::get<const uint64_t>(&in)) {
+  } else if (const auto v = std::get_if<uint64_t>(&in)) {
     f->dump_unsigned(ceph_conf_level_name(level), *v);
-  } else if (const double *v = boost::get<const double>(&in)) {
+  } else if (const auto v = std::get_if<double>(&in)) {
     f->dump_float(ceph_conf_level_name(level), *v);
   } else {
     f->dump_stream(ceph_conf_level_name(level)) << Option::to_str(in);
index ef7d5b34fdc6b3f19dd82c913d766620a6cdc14b..8b5402b1095b7bba844cc0b5659dd335e7796049 100644 (file)
@@ -17,6 +17,7 @@
 
 #include <map>
 #include <boost/container/small_vector.hpp>
+#include <boost/variant.hpp>
 #include "common/ConfUtils.h"
 #include "common/code_environment.h"
 #include "log/SubsystemMap.h"
@@ -201,7 +202,7 @@ public:
                Callback&& cb, Args&&... args) const ->
     std::result_of_t<Callback(const T&, Args...)> {
     return std::forward<Callback>(cb)(
-      boost::get<T>(this->get_val_generic(values, key)),
+      std::get<T>(this->get_val_generic(values, key)),
       std::forward<Args>(args)...);
   }
 
@@ -357,7 +358,7 @@ public:
 template<typename T>
 const T md_config_t::get_val(const ConfigValues& values,
                             const std::string_view key) const {
-  return boost::get<T>(this->get_val_generic(values, key));
+  return std::get<T>(this->get_val_generic(values, key));
 }
 
 inline std::ostream& operator<<(std::ostream& o, const boost::blank& ) {
index ac760e3491d6c63c14100bfa04f22736bb9fb70d..33fd6032b46b06950e88c0b097192c91c95e8800 100644 (file)
@@ -27,7 +27,7 @@ using ceph::Formatter;
 using ceph::parse_timespan;
 
 namespace {
-class printer : public boost::static_visitor<> {
+class printer {
   ostream& out;
 public:
   explicit printer(ostream& os)
@@ -36,7 +36,7 @@ public:
   void operator()(const T& v) const {
     out << v;
   }
-  void operator()(boost::blank blank) const {
+  void operator()(std::monostate) const {
     return;
   }
   void operator()(bool v) const {
@@ -59,29 +59,29 @@ public:
 
 ostream& operator<<(ostream& os, const Option::value_t& v) {
   printer p{os};
-  v.apply_visitor(p);
+  std::visit(p, v);
   return os;
 }
 
 void Option::dump_value(const char *field_name,
     const Option::value_t &v, Formatter *f) const
 {
-  if (boost::get<boost::blank>(&v)) {
+  if (v == value_t{}) {
     // This should be nil but Formatter doesn't allow it.
     f->dump_string(field_name, "");
     return;
   }
   switch (type) {
   case TYPE_INT:
-    f->dump_int(field_name, boost::get<int64_t>(v)); break;
+    f->dump_int(field_name, std::get<int64_t>(v)); break;
   case TYPE_UINT:
-    f->dump_unsigned(field_name, boost::get<uint64_t>(v)); break;
+    f->dump_unsigned(field_name, std::get<uint64_t>(v)); break;
   case TYPE_STR:
-    f->dump_string(field_name, boost::get<std::string>(v)); break;
+    f->dump_string(field_name, std::get<std::string>(v)); break;
   case TYPE_FLOAT:
-    f->dump_float(field_name, boost::get<double>(v)); break;
+    f->dump_float(field_name, std::get<double>(v)); break;
   case TYPE_BOOL:
-    f->dump_bool(field_name, boost::get<bool>(v)); break;
+    f->dump_bool(field_name, std::get<bool>(v)); break;
   default:
     f->dump_stream(field_name) << v; break;
   }
@@ -99,7 +99,7 @@ int Option::pre_validate(std::string *new_value, std::string *err) const
 int Option::validate(const Option::value_t &new_value, std::string *err) const
 {
   // Generic validation: min
-  if (!boost::get<boost::blank>(&(min))) {
+  if (min != value_t{}) {
     if (new_value < min) {
       std::ostringstream oss;
       oss << "Value '" << new_value << "' is below minimum " << min;
@@ -109,7 +109,7 @@ int Option::validate(const Option::value_t &new_value, std::string *err) const
   }
 
   // Generic validation: max
-  if (!boost::get<boost::blank>(&(max))) {
+  if (max != value_t{}) {
     if (new_value > max) {
       std::ostringstream oss;
       oss << "Value '" << new_value << "' exceeds maximum " << max;
@@ -121,7 +121,7 @@ int Option::validate(const Option::value_t &new_value, std::string *err) const
   // Generic validation: enum
   if (!enum_allowed.empty() && type == Option::TYPE_STR) {
     auto found = std::find(enum_allowed.begin(), enum_allowed.end(),
-                           boost::get<std::string>(new_value));
+                           std::get<std::string>(new_value));
     if (found == enum_allowed.end()) {
       std::ostringstream oss;
       oss << "'" << new_value << "' is not one of the permitted "
@@ -303,7 +303,7 @@ void Option::print(ostream *out) const
 {
   *out << name << " - " << desc << "\n";
   *out << "  (" << type_to_str(type) << ", " << level_to_str(level) << ")\n";
-  if (!boost::get<boost::blank>(&daemon_value)) {
+  if (daemon_value != value_t{}) {
     *out << "  Default (non-daemon): " << stringify(value) << "\n";
     *out << "  Default (daemon): " << stringify(daemon_value) << "\n";
   } else {
@@ -316,7 +316,7 @@ void Option::print(ostream *out) const
     }
     *out << "\n";
   }
-  if (!boost::get<boost::blank>(&min)) {
+  if (min != value_t{}) {
     *out << "  Minimum: " << stringify(min) << "\n"
         << "  Maximum: " << stringify(max) << "\n";
   }
index bdf1ab18ed95e095f71e63cb1dc6cdfe782a4416..e1d4ec16ed704c88925cec163a2d76da527420dc 100644 (file)
@@ -5,8 +5,8 @@
 
 #include <chrono>
 #include <string>
+#include <variant>
 #include <vector>
-#include <boost/variant.hpp>
 #include "include/str_list.h"
 #include "msg/msg_types.h"
 #include "include/uuid.h"
@@ -136,8 +136,8 @@ struct Option {
     }
   };
 
-  using value_t = boost::variant<
-    boost::blank,
+  using value_t = std::variant<
+    std::monostate,
     std::string,
     uint64_t,
     int64_t,
@@ -198,7 +198,7 @@ struct Option {
   Option(std::string const &name, type_t t, level_t l)
     : name(name), type(t), level(l)
   {
-    // While value_t is nullable (via boost::blank), we don't ever
+    // While value_t is nullable (via std::monostate), we don't ever
     // want it set that way in an Option instance: within an instance,
     // the type of ::value should always match the declared type.
     switch (type) {
index f4ee27e8c312889a1de5bdd5695fa5f9fc0a31ba..e78cc458fc724cce87cdfb1f8902554378361b80 100644 (file)
@@ -1117,7 +1117,7 @@ PyObject *ActivePyModules::get_foreign_config(
     value = p->second;
   } else {
     if (!entity.is_client() &&
-       !boost::get<boost::blank>(&opt->daemon_value)) {
+       opt->daemon_value != Option::value_t{}) {
       value = Option::to_str(opt->daemon_value);
     } else {
       value = Option::to_str(opt->value);
index 222d129aa3b24f1f1e58dd69304805ab22611cd2..d4dc0f3dfd8d24062fb0bc5eb7c40f546893852f 100644 (file)
@@ -348,7 +348,7 @@ bool ConfigMonitor::preprocess_command(MonOpRequestRef op)
        goto reply;
       }
       if (!entity.is_client() &&
-         !boost::get<boost::blank>(&opt->daemon_value)) {
+         opt->daemon_value != Option::value_t{}) {
        odata.append(Option::to_str(opt->daemon_value));
       } else {
        odata.append(Option::to_str(opt->value));