mirror of https://github.com/OpenTTD/OpenTTD
Codechange: String parameters can now be explicitly not initialised.
As the visitor is now more complex this requires an unspecialised GetNextParameter() to avoid it being repeated for each return type.pull/13528/head
parent
0c0e7606d9
commit
4010313180
|
@ -83,6 +83,7 @@ public:
|
||||||
uint64_t GetNextParameter()
|
uint64_t GetNextParameter()
|
||||||
{
|
{
|
||||||
struct visitor {
|
struct visitor {
|
||||||
|
uint64_t operator()(const std::monostate &) { throw std::out_of_range("Attempt to read uninitialised parameter as integer"); }
|
||||||
uint64_t operator()(const uint64_t &arg) { return arg; }
|
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"); }
|
uint64_t operator()(const std::string &) { throw std::out_of_range("Attempt to read string parameter as integer"); }
|
||||||
};
|
};
|
||||||
|
@ -113,11 +114,12 @@ public:
|
||||||
const char *GetNextParameterString()
|
const char *GetNextParameterString()
|
||||||
{
|
{
|
||||||
struct visitor {
|
struct visitor {
|
||||||
|
const char *operator()(const std::monostate &) { throw std::out_of_range("Attempt to read uninitialised parameter as string"); }
|
||||||
const char *operator()(const uint64_t &) { throw std::out_of_range("Attempt to read integer parameter as string"); }
|
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 char *operator()(const std::string &arg) { return arg.c_str(); }
|
||||||
};
|
};
|
||||||
|
|
||||||
const auto ¶m = GetNextParameterReference();
|
const auto ¶m = this->GetNextParameterReference();
|
||||||
return std::visit(visitor{}, param.data);
|
return std::visit(visitor{}, param.data);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -72,7 +72,7 @@ static constexpr StringID SPECSTR_SILLY_NAME = 0x70E5; ///< Special string for s
|
||||||
static constexpr StringID SPECSTR_ANDCO_NAME = 0x70E6; ///< Special string for Surname & Co company names.
|
static constexpr StringID SPECSTR_ANDCO_NAME = 0x70E6; ///< Special string for Surname & Co company names.
|
||||||
static constexpr StringID SPECSTR_PRESIDENT_NAME = 0x70E7; ///< Special string for the president's name.
|
static constexpr StringID SPECSTR_PRESIDENT_NAME = 0x70E7; ///< Special string for the president's name.
|
||||||
|
|
||||||
using StringParameterData = std::variant<uint64_t, std::string>;
|
using StringParameterData = std::variant<std::monostate, uint64_t, std::string>;
|
||||||
|
|
||||||
/** The data required to format and validate a single parameter of a string. */
|
/** The data required to format and validate a single parameter of a string. */
|
||||||
struct StringParameter {
|
struct StringParameter {
|
||||||
|
@ -82,6 +82,7 @@ struct StringParameter {
|
||||||
StringParameter() = default;
|
StringParameter() = default;
|
||||||
inline StringParameter(StringParameterData &&data) : data(std::move(data)), type(0) {}
|
inline StringParameter(StringParameterData &&data) : data(std::move(data)), type(0) {}
|
||||||
|
|
||||||
|
inline StringParameter(const std::monostate &data) : data(data), type(0) {}
|
||||||
inline StringParameter(uint64_t data) : data(data), type(0) {}
|
inline StringParameter(uint64_t data) : data(data), type(0) {}
|
||||||
|
|
||||||
inline StringParameter(const char *data) : data(std::string{data}), type(0) {}
|
inline StringParameter(const char *data) : data(std::string{data}), type(0) {}
|
||||||
|
|
Loading…
Reference in New Issue