Config: support time duration conversion and assignment

This commit is contained in:
Charles Dang 2024-10-27 20:22:18 -04:00
parent 7f4d2a437b
commit cd3e8d7652

View file

@ -31,6 +31,7 @@
#include "tstring.hpp"
#include "utils/variant.hpp"
#include <chrono>
#include <climits>
#include <ctime>
#include <iosfwd>
@ -127,6 +128,12 @@ public:
config_attribute_value& operator=(const std::string_view &v);
config_attribute_value& operator=(const t_string &v);
template<typename... Args>
config_attribute_value& operator=(const std::chrono::duration<Args...>& v)
{
return this->operator=(v.count());
}
/** Calls @ref operator=(const std::string&) if @a v is not empty. */
void write_if_not_empty(const std::string& v);
void write_if_not_empty(const t_string& v);
@ -142,12 +149,21 @@ public:
std::string str(const std::string& fallback = "") const;
t_string t_str() const;
template<typename Duration>
auto to_duration(const Duration& def = Duration{0}) const
{
return Duration{to_long_long(def.count())};
}
bool to(const bool def) const { return to_bool(def); }
int to(int def) const { return to_int(def); }
unsigned to(unsigned def) const { return to_unsigned(def); }
double to(double def) const { return to_double(def); }
std::string to(const std::string& def) const { return str(def); }
template<typename... Args>
auto to(const std::chrono::duration<Args...>& def) const { return to_duration(def); }
// Implicit conversions:
operator std::string() const { return str(); }
operator t_string() const { return t_str(); }