1
0
mirror of https://github.com/OpenTTD/OpenTTD synced 2026-01-16 17:02:37 +01:00

Codechange: Use std::variant to store string parameter data.

This avoids storing two separate values and makes the test for which type is held clearer.

This replaces use of unique_ptr for conditionally storing a string, and is also used in place of StringParameterBackup.
This commit is contained in:
Peter Nelson
2024-07-29 17:58:32 +01:00
committed by Peter Nelson
parent b449839538
commit 3d8d0e0d26
10 changed files with 39 additions and 92 deletions

View File

@@ -113,7 +113,7 @@ void SetDParam(size_t n, uint64_t v)
*/
uint64_t GetDParam(size_t n)
{
return _global_string_params.GetParam(n);
return std::get<uint64_t>(_global_string_params.GetParam(n));
}
/**
@@ -156,15 +156,10 @@ void SetDParamMaxDigits(size_t n, uint count, FontSize size)
* Copy the parameters from the backup into the global string parameter array.
* @param backup The backup to copy from.
*/
void CopyInDParam(const std::span<const StringParameterBackup> backup)
void CopyInDParam(const std::span<const StringParameterData> backup)
{
for (size_t i = 0; i < backup.size(); i++) {
auto &value = backup[i];
if (value.string.has_value()) {
_global_string_params.SetParam(i, value.string.value());
} else {
_global_string_params.SetParam(i, value.data);
}
_global_string_params.SetParam(i, backup[i]);
}
}
@@ -173,16 +168,11 @@ void CopyInDParam(const std::span<const StringParameterBackup> backup)
* @param backup The backup to write to.
* @param num Number of string parameters to copy.
*/
void CopyOutDParam(std::vector<StringParameterBackup> &backup, size_t num)
void CopyOutDParam(std::vector<StringParameterData> &backup, size_t num)
{
backup.resize(num);
for (size_t i = 0; i < backup.size(); i++) {
const char *str = _global_string_params.GetParamStr(i);
if (str != nullptr) {
backup[i] = str;
} else {
backup[i] = _global_string_params.GetParam(i);
}
backup[i] = _global_string_params.GetParam(i);
}
}
@@ -191,20 +181,12 @@ void CopyOutDParam(std::vector<StringParameterBackup> &backup, size_t num)
* @param backup The backup to check against.
* @return True when the parameters have changed, otherwise false.
*/
bool HaveDParamChanged(const std::span<const StringParameterBackup> backup)
bool HaveDParamChanged(const std::span<const StringParameterData> backup)
{
bool changed = false;
for (size_t i = 0; !changed && i < backup.size(); i++) {
bool global_has_string = _global_string_params.GetParamStr(i) != nullptr;
if (global_has_string != backup[i].string.has_value()) return true;
if (global_has_string) {
changed = backup[i].string.value() != _global_string_params.GetParamStr(i);
} else {
changed = backup[i].data != _global_string_params.GetParam(i);
}
for (size_t i = 0; i < backup.size(); i++) {
if (backup[i] != _global_string_params.GetParam(i)) return true;
}
return changed;
return false;
}
static void StationGetSpecialString(StringBuilder &builder, StationFacility x);
@@ -1106,7 +1088,7 @@ static void FormatString(StringBuilder &builder, const char *str_arg, StringPara
case SCC_PLURAL_LIST: { // {P}
int plural_form = *str++; // contains the plural form for this string
size_t offset = orig_offset + (uint8_t)*str++;
int64_t v = args.GetParam(offset); // contains the number that determines plural
int64_t v = std::get<uint64_t>(args.GetParam(offset)); // contains the number that determines plural
str = ParseStringChoice(str, DeterminePluralForm(v, plural_form), builder);
break;
}