Merge branch 'master' into windows-clang

This commit is contained in:
qurious-pixel 2025-10-13 08:45:02 -07:00 committed by GitHub
commit 47b938cf38
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
308 changed files with 6531 additions and 2063 deletions

View file

@ -166,6 +166,55 @@ bool try_to_uint64(u64* out, std::string_view value, u64 min, u64 max)
return true;
}
bool try_to_uint128(u128* out, std::string_view value)
{
if (value.empty())
{
if (out) cfg_log.error("cfg::try_to_uint128(): called with an empty string");
return false;
}
u64 result_low = 0, result_high = 0;
const char* start_high64 = value.data();
const char* end = value.data() + value.size();
if (start_high64[0] == '0' && value.size() >= 2 && (start_high64[1] == 'x' || start_high64[1] == 'X'))
{
// Hex support
start_high64 += 2;
}
const char* start_low64 = end - std::min<usz>(end - start_high64, 16);
// Hexadecimal-only
constexpr int base = 16;
auto ret = std::from_chars(start_low64, end, result_low, base);
if (ret.ec != std::errc() || ret.ptr != end)
{
if (out) cfg_log.error("cfg::try_to_uint128('%s'): invalid integer", value);
return false;
}
if (start_high64 == start_low64)
{
if (out) *out = result_low;
return true;
}
ret = std::from_chars(start_high64, start_low64, result_high, base);
if (ret.ec != std::errc() || ret.ptr != start_low64)
{
if (out) cfg_log.error("cfg::try_to_uint128('%s'): invalid integer", value);
return false;
}
if (out) *out = result_low + (u128{result_high} << 64);
return true;
}
std::vector<std::string> cfg::make_float_range(f64 min, f64 max)
{
return {std::to_string(min), std::to_string(max)};
@ -278,6 +327,19 @@ bool cfg::try_to_enum_value(u64* out, decltype(&fmt_class_string<int>::format) f
return true;
}
std::string cfg::uint128::to_string(u128 value) noexcept
{
std::string result = "0x";
result.resize(result.size() + 32);
for (u32 i = 0; i < 32; i++)
{
result[result.size() - 1 - i] = "0123456789ABCDEF"[static_cast<u64>(value >> (i * 4)) % 16];
}
return result;
}
std::vector<std::string> cfg::try_to_enum_list(decltype(&fmt_class_string<int>::format) func)
{
std::vector<std::string> result;
@ -553,19 +615,12 @@ void cfg::node::from_default()
}
}
void cfg::_bool::from_default()
void cfg::node::restore_defaults()
{
m_value = def;
}
void cfg::string::from_default()
{
m_value = def;
}
void cfg::set_entry::from_default()
{
m_set = {};
for (auto& node : m_nodes)
{
node->restore_defaults();
}
}
std::string cfg::map_entry::get_value(std::string_view key)

View file

@ -38,6 +38,7 @@ namespace cfg
_enum, // cfg::_enum type
_int, // cfg::_int type
uint, // cfg::uint type
uint128, // cfg::uint128 type
string, // cfg::string type
set, // cfg::set_entry type
map, // cfg::map_entry type
@ -90,6 +91,9 @@ namespace cfg
// Reset defaults
virtual void from_default() = 0;
// Restore default members
virtual void restore_defaults() = 0;
// Convert to string (optional)
virtual std::string to_string() const
{
@ -151,11 +155,15 @@ namespace cfg
// Set default values
void from_default() override;
// Restore default members
void restore_defaults() override;
};
class _bool final : public _base
{
atomic_t<bool> m_value;
bool original_def;
public:
bool def;
@ -163,6 +171,7 @@ namespace cfg
_bool(node* owner, std::string name, bool def = false, bool dynamic = false)
: _base(type::_bool, owner, std::move(name), dynamic)
, m_value(def)
, original_def(def)
, def(def)
{
}
@ -177,7 +186,15 @@ namespace cfg
return m_value;
}
void from_default() override;
void from_default() override
{
m_value = def;
}
void restore_defaults() override
{
def = original_def;
}
std::string to_string() const override
{
@ -220,14 +237,16 @@ namespace cfg
class _enum : public _base
{
atomic_t<T> m_value;
T original_def;
public:
const T def;
T def;
_enum(node* owner, const std::string& name, T value = {}, bool dynamic = false)
_enum(node* owner, const std::string& name, T def = {}, bool dynamic = false)
: _base(type::_enum, owner, name, dynamic)
, m_value(value)
, def(value)
, m_value(def)
, original_def(def)
, def(def)
{
}
@ -256,6 +275,11 @@ namespace cfg
m_value = def;
}
void restore_defaults() override
{
def = original_def;
}
std::string to_string() const override
{
std::string result;
@ -300,6 +324,7 @@ namespace cfg
using int_type = std::conditional_t<Min >= s32{smin} && Max <= s32{smax}, s32, s64>;
atomic_t<int_type> m_value;
int_type original_def;
public:
int_type def;
@ -311,6 +336,7 @@ namespace cfg
_int(node* owner, const std::string& name, int_type def = std::min<int_type>(Max, std::max<int_type>(Min, 0)), bool dynamic = false)
: _base(type::_int, owner, name, dynamic)
, m_value(def)
, original_def(def)
, def(def)
{
}
@ -330,6 +356,11 @@ namespace cfg
m_value = def;
}
void restore_defaults() override
{
def = original_def;
}
std::string to_string() const override
{
return std::to_string(m_value);
@ -372,6 +403,7 @@ namespace cfg
using float_type = f64;
atomic_t<float_type> m_value;
float_type original_def;
public:
float_type def;
@ -383,6 +415,7 @@ namespace cfg
_float(node* owner, const std::string& name, float_type def = std::min<float_type>(Max, std::max<float_type>(Min, 0)), bool dynamic = false)
: _base(type::_int, owner, name, dynamic)
, m_value(def)
, original_def(def)
, def(def)
{
}
@ -402,6 +435,11 @@ namespace cfg
m_value = def;
}
void restore_defaults() override
{
def = original_def;
}
std::string to_string() const override
{
std::string result;
@ -464,6 +502,7 @@ namespace cfg
using int_type = std::conditional_t<Max <= u32{umax}, u32, u64>;
atomic_t<int_type> m_value;
int_type original_def;
public:
int_type def;
@ -475,6 +514,7 @@ namespace cfg
uint(node* owner, const std::string& name, int_type def = std::max<int_type>(Min, 0), bool dynamic = false)
: _base(type::uint, owner, name, dynamic)
, m_value(def)
, original_def(def)
, def(def)
{
}
@ -494,6 +534,11 @@ namespace cfg
m_value = def;
}
void restore_defaults() override
{
def = original_def;
}
std::string to_string() const override
{
return std::to_string(m_value);
@ -534,10 +579,91 @@ namespace cfg
// Alias for 64 bit int
using uint64 = uint<0, u64{umax}>;
// Unsigned 128-bit integer entry.
class uint128 final : public _base
{
using int_type = u128;
atomic_t<int_type> m_value{};
int_type original_def = 0;
public:
int_type def;
uint128(node* owner, const std::string& name, int_type def = 0, bool dynamic = false)
: _base(type::uint128, owner, name, dynamic)
, m_value(def)
, original_def(def)
, def(def)
{
}
operator int_type() const
{
return m_value;
}
operator ullong() const
{
return static_cast<ullong>(m_value.load());
}
int_type get() const
{
return m_value;
}
void from_default() override
{
m_value = def;
}
void restore_defaults() override
{
def = original_def;
}
static std::string to_string(u128 value) noexcept;
std::string to_string() const override
{
return to_string(m_value.load());
}
std::string def_to_string() const override
{
return to_string(def);
}
bool from_string(std::string_view value, bool /*dynamic*/ = false) override
{
u128 result;
if (try_to_uint128(&result, value))
{
m_value = result;
return true;
}
return false;
}
void set(u128 value)
{
m_value = value;
}
std::vector<std::string> to_list() const override
{
// Should not be used
return make_uint_range(0, 1);
}
};
// Simple string entry with mutex
class string : public _base
{
atomic_ptr<std::string> m_value;
std::string original_def;
public:
std::string def;
@ -545,6 +671,7 @@ namespace cfg
string(node* owner, std::string name, std::string def = {}, bool dynamic = false)
: _base(type::string, owner, std::move(name), dynamic)
, m_value(def)
, original_def(def)
, def(std::move(def))
{
}
@ -554,7 +681,15 @@ namespace cfg
return *m_value.load().get();
}
void from_default() override;
void from_default() override
{
m_value = def;
}
void restore_defaults() override
{
def = original_def;
}
std::string to_string() const override
{
@ -595,7 +730,14 @@ namespace cfg
m_set = std::move(set);
}
void from_default() override;
void from_default() override
{
m_set = {};
}
void restore_defaults() override
{
}
std::vector<std::string> to_list() const override
{
@ -636,6 +778,10 @@ namespace cfg
void erase(std::string_view key);
void from_default() override;
void restore_defaults() override
{
}
};
class node_map_entry final : public map_entry
@ -665,6 +811,10 @@ namespace cfg
void set_map(map_of_type<logs::level>&& map);
void from_default() override;
void restore_defaults() override
{
}
};
struct device_info
@ -702,5 +852,9 @@ namespace cfg
void set_map(map_of_type<device_info>&& map);
void from_default() override;
void restore_defaults() override
{
}
};
}

View file

@ -28,6 +28,9 @@ bool try_to_int64(s64* out, std::string_view value, s64 min, s64 max);
// Convert string to unsigned integer
bool try_to_uint64(u64* out, std::string_view value, u64 min, u64 max);
// Convert string to unsigned int128_t
bool try_to_uint128(u128* out, std::string_view value);
// Convert string to float
bool try_to_float(f64* out, std::string_view value, f64 min, f64 max);