1
0
Fork 0

Codechange: Use std::visit instead of std::get_if for string parameters. (#13100)

It's tidier and ensures all cases are handled, and doesn't use pointers.
pull/13103/head
Peter Nelson 2024-11-19 18:17:41 +00:00 committed by GitHub
parent 60ae50e016
commit 25c5a64d39
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 12 additions and 6 deletions

View File

@ -91,10 +91,13 @@ public:
template <typename T>
T GetNextParameter()
{
struct visitor {
uint64_t operator()(const uint64_t &arg) { return arg; }
uint64_t operator()(const std::string &) { throw std::out_of_range("Attempt to read string parameter as integer"); }
};
const auto &param = GetNextParameterReference();
const uint64_t *data = std::get_if<uint64_t>(&param.data);
if (data != nullptr) return static_cast<T>(*data);
throw std::out_of_range("Attempt to read string parameter as integer");
return static_cast<T>(std::visit(visitor{}, param.data));
}
/**
@ -105,10 +108,13 @@ public:
*/
const char *GetNextParameterString()
{
struct visitor {
const char *operator()(const uint64_t &) { throw std::out_of_range("Attempt to read integer parameter as string"); }
const char *operator()(const std::string &arg) { return arg.c_str(); }
};
const auto &param = GetNextParameterReference();
const std::string *data = std::get_if<std::string>(&param.data);
if (data != nullptr) return data->c_str();
throw std::out_of_range("Attempt to read integer parameter as string");
return std::visit(visitor{}, param.data);
}
/**