mirror of https://github.com/OpenTTD/OpenTTD
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
parent
60ae50e016
commit
25c5a64d39
|
@ -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 ¶m = GetNextParameterReference();
|
||||
const uint64_t *data = std::get_if<uint64_t>(¶m.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 ¶m = GetNextParameterReference();
|
||||
const std::string *data = std::get_if<std::string>(¶m.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);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Reference in New Issue