Changed fs::meta_value::to_string() to not be static and not require meta_type (#9510)

No need to pass in the meta_type when using std::visit() on the std::variant
This commit is contained in:
npwoods 2022-04-03 21:38:57 -04:00 committed by GitHub
parent 07a357463b
commit 945cb29e74
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 31 additions and 17 deletions

View File

@ -32,20 +32,34 @@ const char *meta_data::entry_name(meta_name name)
return "";
}
std::string meta_value::to_string(meta_type type, const meta_value &m)
std::string meta_value::to_string() const
{
switch(type) {
case meta_type::string: return m.as_string();
case meta_type::number: return util::string_format("0x%x", m.as_number());
case meta_type::flag: return m.as_flag() ? "t" : "f";
case meta_type::date: {
auto dt = m.as_date();
return util::string_format("%04d-%02d-%02d %02d:%02d:%02d",
dt.year, dt.month, dt.day_of_month,
dt.hour, dt.minute, dt.second);
}
}
return std::string("");
std::string result;
std::visit([this, &result](auto &&arg)
{
using T = std::decay_t<decltype(arg)>;
if constexpr (std::is_same_v<T, std::string>)
{
result = as_string();
}
else if constexpr (std::is_same_v<T, uint64_t>)
{
result = util::string_format("0x%x", as_number());
}
else if constexpr (std::is_same_v<T, bool>)
{
result = as_flag() ? "t" : "f";
}
else if constexpr (std::is_same_v<T, util::arbitrary_datetime>)
{
auto dt = as_date();
result = util::string_format("%04d-%02d-%02d %02d:%02d:%02d",
dt.year, dt.month, dt.day_of_month,
dt.hour, dt.minute, dt.second);
}
}, value);
return result;
}
} // namespace fs

View File

@ -46,7 +46,7 @@ enum class meta_type {
class meta_value {
public:
static std::string to_string(meta_type type, const meta_value &m);
std::string to_string() const;
static meta_value from_string(meta_type type, std::string value);
meta_value() { value = false; }

View File

@ -283,7 +283,7 @@ static void dir_scan(u32 depth, fs::filesystem_t::dir_t dir, std::vector<std::ve
if(!meta.has(m.m_name))
continue;
size_t slot = nmap.find(m.m_name)->second;
std::string val = fs::meta_value::to_string(m.m_type, meta.get(m.m_name));
std::string val = meta.get(m.m_name).to_string();
if(slot == 0)
val = head + "dir " + val;
entries[id][slot] = val;
@ -301,7 +301,7 @@ static void dir_scan(u32 depth, fs::filesystem_t::dir_t dir, std::vector<std::ve
if(!meta.has(m.m_name))
continue;
size_t slot = nmap.find(m.m_name)->second;
std::string val = fs::meta_value::to_string(m.m_type, meta.get(m.m_name));
std::string val = meta.get(m.m_name).to_string();
if(slot == 0)
val = head + (c.m_type == fs::dir_entry_type::system_file ? "sys " : "file ") + val;
entries[id][slot] = val;
@ -323,7 +323,7 @@ static int generic_dir(image_handler &ih)
if(!vmeta.empty()) {
std::string vinf = "Volume:";
for(const auto &e : vmetad)
vinf += util::string_format(" %s=%s", fs::meta_data::entry_name(e.m_name), fs::meta_value::to_string(e.m_type, vmeta.get(e.m_name)));
vinf += util::string_format(" %s=%s", fs::meta_data::entry_name(e.m_name), vmeta.get(e.m_name).to_string());
printf("%s\n\n", vinf.c_str());
}