From: Kefu Chai Date: Sun, 27 Jun 2021 02:38:39 +0000 (+0800) Subject: common/options: s/boost::variant/std::variant/ X-Git-Tag: v17.1.0~1533^2~2 X-Git-Url: http://git.apps.os.sepia.ceph.com/?a=commitdiff_plain;h=863d8385d0cff55ca2429135f22d4a5cb45f981b;p=ceph.git common/options: s/boost::variant/std::variant/ we should use standard library for more well defined behavior, and less dependencies on 3rd party libraries. Signed-off-by: Kefu Chai --- diff --git a/src/common/config.cc b/src/common/config.cc index 855a284b719d6..ca9f194a125b7 100644 --- a/src/common/config.cc +++ b/src/common/config.cc @@ -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(&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(&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(&default_val); + auto* def_str = std::get_if(&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(&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(&in); + const auto str = std::get_if(&in); if (!str) { // strings only! return in; @@ -1490,19 +1490,19 @@ class assign_visitor : public boost::static_visitor<> { T *member = const_cast(&(conf->*(boost::get(ptr)))); - *member = boost::get(val); + *member = std::get(val); } void operator()(uint64_t Config::* ptr) const { using T = uint64_t; auto member = const_cast(&(conf->*(boost::get(ptr)))); - *member = boost::apply_visitor(get_size_visitor{}, val); + *member = std::visit(get_size_visitor{}, val); } void operator()(int64_t Config::* ptr) const { using T = int64_t; auto member = const_cast(&(conf->*(boost::get(ptr)))); - *member = boost::apply_visitor(get_size_visitor{}, val); + *member = std::visit(get_size_visitor{}, 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(&in)) { + if (const auto v = std::get_if(&in)) { f->dump_bool(ceph_conf_level_name(level), *v); - } else if (const int64_t *v = boost::get(&in)) { + } else if (const auto v = std::get_if(&in)) { f->dump_int(ceph_conf_level_name(level), *v); - } else if (const uint64_t *v = boost::get(&in)) { + } else if (const auto v = std::get_if(&in)) { f->dump_unsigned(ceph_conf_level_name(level), *v); - } else if (const double *v = boost::get(&in)) { + } else if (const auto v = std::get_if(&in)) { f->dump_float(ceph_conf_level_name(level), *v); } else { f->dump_stream(ceph_conf_level_name(level)) << Option::to_str(in); diff --git a/src/common/config.h b/src/common/config.h index ef7d5b34fdc6b..8b5402b1095b7 100644 --- a/src/common/config.h +++ b/src/common/config.h @@ -17,6 +17,7 @@ #include #include +#include #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 { return std::forward(cb)( - boost::get(this->get_val_generic(values, key)), + std::get(this->get_val_generic(values, key)), std::forward(args)...); } @@ -357,7 +358,7 @@ public: template const T md_config_t::get_val(const ConfigValues& values, const std::string_view key) const { - return boost::get(this->get_val_generic(values, key)); + return std::get(this->get_val_generic(values, key)); } inline std::ostream& operator<<(std::ostream& o, const boost::blank& ) { diff --git a/src/common/options.cc b/src/common/options.cc index ac760e3491d6c..33fd6032b46b0 100644 --- a/src/common/options.cc +++ b/src/common/options.cc @@ -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(&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(v)); break; + f->dump_int(field_name, std::get(v)); break; case TYPE_UINT: - f->dump_unsigned(field_name, boost::get(v)); break; + f->dump_unsigned(field_name, std::get(v)); break; case TYPE_STR: - f->dump_string(field_name, boost::get(v)); break; + f->dump_string(field_name, std::get(v)); break; case TYPE_FLOAT: - f->dump_float(field_name, boost::get(v)); break; + f->dump_float(field_name, std::get(v)); break; case TYPE_BOOL: - f->dump_bool(field_name, boost::get(v)); break; + f->dump_bool(field_name, std::get(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(&(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(&(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(new_value)); + std::get(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(&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(&min)) { + if (min != value_t{}) { *out << " Minimum: " << stringify(min) << "\n" << " Maximum: " << stringify(max) << "\n"; } diff --git a/src/common/options.h b/src/common/options.h index bdf1ab18ed95e..e1d4ec16ed704 100644 --- a/src/common/options.h +++ b/src/common/options.h @@ -5,8 +5,8 @@ #include #include +#include #include -#include #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) { diff --git a/src/mgr/ActivePyModules.cc b/src/mgr/ActivePyModules.cc index f4ee27e8c3128..e78cc458fc724 100644 --- a/src/mgr/ActivePyModules.cc +++ b/src/mgr/ActivePyModules.cc @@ -1117,7 +1117,7 @@ PyObject *ActivePyModules::get_foreign_config( value = p->second; } else { if (!entity.is_client() && - !boost::get(&opt->daemon_value)) { + opt->daemon_value != Option::value_t{}) { value = Option::to_str(opt->daemon_value); } else { value = Option::to_str(opt->value); diff --git a/src/mon/ConfigMonitor.cc b/src/mon/ConfigMonitor.cc index 222d129aa3b24..d4dc0f3dfd8d2 100644 --- a/src/mon/ConfigMonitor.cc +++ b/src/mon/ConfigMonitor.cc @@ -348,7 +348,7 @@ bool ConfigMonitor::preprocess_command(MonOpRequestRef op) goto reply; } if (!entity.is_client() && - !boost::get(&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));