Add *DefaultValue() method to SelectorFlagProxy
The functions to check if the flag has the default value and
to return the default value if exists are added to
SelectorFlagProxy.
Bug: 271302757
Test: Run locally
Change-Id: I8d68cde54bf2bb8486f3b375ee1d775b3742c470
diff --git a/host/commands/cvd/selector/flag.cpp b/host/commands/cvd/selector/flag.cpp
index f50ae1b..2315758 100644
--- a/host/commands/cvd/selector/flag.cpp
+++ b/host/commands/cvd/selector/flag.cpp
@@ -40,6 +40,25 @@
return name;
}
+Result<bool> SelectorFlagProxy::HasDefaultValue() const {
+ auto decoder = Overload{
+ [](const SelectorFlag<bool>& bool_flag) -> Result<bool> {
+ return bool_flag.HasDefaultValue();
+ },
+ [](const SelectorFlag<std::int32_t>& int32_flag) -> Result<bool> {
+ return int32_flag.HasDefaultValue();
+ },
+ [](const SelectorFlag<std::string>& string_flag) -> Result<bool> {
+ return string_flag.HasDefaultValue();
+ },
+ [](auto) -> Result<bool> {
+ return CF_ERR("The type is not handled by SelectorFlagProxy");
+ },
+ };
+ auto has_default_value = CF_EXPECT(std::visit(decoder, flag_));
+ return has_default_value;
+}
+
std::vector<SelectorFlagProxy> FlagCollection::Flags() const {
std::vector<SelectorFlagProxy> flags;
flags.reserve(name_flag_map_.size());
diff --git a/host/commands/cvd/selector/flag.h b/host/commands/cvd/selector/flag.h
index 70dc8bf..1eebdf8 100644
--- a/host/commands/cvd/selector/flag.h
+++ b/host/commands/cvd/selector/flag.h
@@ -112,7 +112,22 @@
return std::get_if<SelectorFlag<T>>(&flag_);
}
+ /*
+ * If the actual type of flag_ is not handled by SelectorFlagProxy, it is a
+ * developer error, and the Name() and HasDefaultValue() will returns
+ * CF_ERR
+ */
Result<std::string> Name() const;
+ Result<bool> HasDefaultValue() const;
+
+ template <typename T>
+ Result<T> DefaultValue() const {
+ const bool has_default_value = CF_EXPECT(HasDefaultValue());
+ CF_EXPECT(has_default_value == true);
+ const auto* ptr = CF_EXPECT(std::get_if<SelectorFlag<T>>(&flag_));
+ CF_EXPECT(ptr != nullptr);
+ return ptr->DefaultValue();
+ }
private:
std::variant<SelectorFlag<std::int32_t>, SelectorFlag<bool>,