From 8d312b305bccadcd53cd4c717d274cb76adc4fbf Mon Sep 17 00:00:00 2001 From: Peter Nelson Date: Fri, 29 Mar 2024 14:49:48 +0000 Subject: [PATCH] Codechange: Replace currency macros with functions. (#12396) --- src/company_gui.cpp | 2 +- src/currency.cpp | 6 ++--- src/currency.h | 24 ++++++++++++++---- src/graph_gui.cpp | 2 +- src/settings.cpp | 2 +- src/settings_gui.cpp | 60 ++++++++++++++++++++++---------------------- src/strings.cpp | 6 ++--- src/survey.cpp | 2 +- 8 files changed, 59 insertions(+), 45 deletions(-) diff --git a/src/company_gui.cpp b/src/company_gui.cpp index 253bc187ed..21743d31f9 100644 --- a/src/company_gui.cpp +++ b/src/company_gui.cpp @@ -2568,7 +2568,7 @@ struct CompanyWindow : Window default: NOT_REACHED(); case WID_C_GIVE_MONEY: { - Money money = std::strtoull(str, nullptr, 10) / _currency->rate; + Money money = std::strtoull(str, nullptr, 10) / GetCurrency().rate; Command::Post(STR_ERROR_CAN_T_GIVE_MONEY, money, (CompanyID)this->window_number); break; } diff --git a/src/currency.cpp b/src/currency.cpp index 98a89cda6e..afc735d4d4 100644 --- a/src/currency.cpp +++ b/src/currency.cpp @@ -26,7 +26,7 @@ * | | Euro year | | | | name * | | | | | | | | */ /** The original currency specifications. */ -static const CurrencySpec origin_currency_specs[CURRENCY_END] = { +static const std::array origin_currency_specs = {{ { 1, "", CF_NOEURO, "\u00a3", "", "GBP", 0, STR_GAME_OPTIONS_CURRENCY_GBP }, ///< british pound { 2, "", CF_NOEURO, "$", "", "USD", 0, STR_GAME_OPTIONS_CURRENCY_USD }, ///< american dollar { 2, "", CF_ISEURO, "\u20ac", "", "EUR", 0, STR_GAME_OPTIONS_CURRENCY_EUR }, ///< euro @@ -71,10 +71,10 @@ static const CurrencySpec origin_currency_specs[CURRENCY_END] = { { 5, "", CF_NOEURO, "RM", "", "MYR", 0, STR_GAME_OPTIONS_CURRENCY_MYR }, ///< Malaysian Ringgit { 1, "", 2014, "", NBSP "Ls", "LVL", 1, STR_GAME_OPTIONS_CURRENCY_LVL }, ///< latvian lats { 400, "", 2002, "", "$00", "PTE", 1, STR_GAME_OPTIONS_CURRENCY_PTE }, ///< portuguese escudo -}; +}}; /** Array of currencies used by the system */ -CurrencySpec _currency_specs[CURRENCY_END]; +std::array _currency_specs; /** * This array represent the position of OpenTTD's currencies, diff --git a/src/currency.h b/src/currency.h index 1ae229f772..71d1315aca 100644 --- a/src/currency.h +++ b/src/currency.h @@ -11,7 +11,7 @@ #define CURRENCY_H #include "timer/timer_game_calendar.h" -#include "string_func.h" +#include "settings_type.h" #include "strings_type.h" static constexpr TimerGameCalendar::Year CF_NOEURO = 0; ///< Currency never switches to the Euro (as far as known). @@ -99,11 +99,25 @@ struct CurrencySpec { } }; -extern CurrencySpec _currency_specs[CURRENCY_END]; +extern std::array _currency_specs; -/* XXX small hack, but makes the rest of the code a bit nicer to read */ -#define _custom_currency (_currency_specs[CURRENCY_CUSTOM]) -#define _currency ((const CurrencySpec*)&_currency_specs[GetGameSettings().locale.currency]) +/** + * Get the custom currency. + * @return Reference to custom currency. + */ +inline CurrencySpec &GetCustomCurrency() +{ + return _currency_specs[CURRENCY_CUSTOM]; +} + +/** + * Get the currently selected currency. + * @return Read-only reference to the current currency. + */ +inline const CurrencySpec &GetCurrency() +{ + return _currency_specs[GetGameSettings().locale.currency]; +} uint64_t GetMaskOfAllowedCurrencies(); void ResetCurrencies(bool preserve_custom = true); diff --git a/src/graph_gui.cpp b/src/graph_gui.cpp index 743bf8bda8..919474d911 100644 --- a/src/graph_gui.cpp +++ b/src/graph_gui.cpp @@ -1252,7 +1252,7 @@ struct PerformanceRatingDetailWindow : Window { * least 999 999 M which roughly is equally long. Furthermore if the * exchange rate is that high, 999 999 k is usually not enough anymore * to show the different currency numbers. */ - if (_currency->rate < 1000) max /= _currency->rate; + if (GetCurrency().rate < 1000) max /= GetCurrency().rate; SetDParam(0, max); SetDParam(1, max); uint score_detail_width = GetStringBoundingBox(STR_PERFORMANCE_DETAIL_AMOUNT_CURRENCY).width; diff --git a/src/settings.cpp b/src/settings.cpp index 88d96c0b48..6ba8df735e 100644 --- a/src/settings.cpp +++ b/src/settings.cpp @@ -1280,7 +1280,7 @@ static void HandleSettingDescs(IniFile &generic_ini, IniFile &private_ini, IniFi proc(secrets_ini, table, "patches", &_settings_newgame, only_startup); } - proc(generic_ini, _currency_settings, "currency", &_custom_currency, only_startup); + proc(generic_ini, _currency_settings, "currency", &GetCustomCurrency(), only_startup); proc(generic_ini, _company_settings, "company", &_settings_client.company, only_startup); if (!only_startup) { diff --git a/src/settings_gui.cpp b/src/settings_gui.cpp index 29f10e21f6..ba0ce024f7 100644 --- a/src/settings_gui.cpp +++ b/src/settings_gui.cpp @@ -397,7 +397,7 @@ struct GameOptionsWindow : Window { /* Add non-custom currencies; sorted naturally */ for (const CurrencySpec ¤cy : _currency_specs) { - int i = ¤cy - _currency_specs; + int i = ¤cy - _currency_specs.data(); if (i == CURRENCY_CUSTOM) continue; if (currency.code.empty()) { list.push_back(std::make_unique(currency.name, i, HasBit(disabled, i))); @@ -2689,7 +2689,7 @@ struct GameSettingsWindow : Window { if (this->last_clicked == pe && !sd->IsBoolSetting() && !(sd->flags & SF_GUI_DROPDOWN)) { int64_t value64 = value; /* Show the correct currency-translated value */ - if (sd->flags & SF_GUI_CURRENCY) value64 *= _currency->rate; + if (sd->flags & SF_GUI_CURRENCY) value64 *= GetCurrency().rate; CharSetFilter charset_filter = CS_NUMERAL; //default, only numeric input allowed if (sd->min < 0) charset_filter = CS_NUMERAL_SIGNED; // special case, also allow '-' sign for negative input @@ -2725,7 +2725,7 @@ struct GameSettingsWindow : Window { long long llvalue = atoll(str); /* Save the correct currency-translated value */ - if (sd->flags & SF_GUI_CURRENCY) llvalue /= _currency->rate; + if (sd->flags & SF_GUI_CURRENCY) llvalue /= GetCurrency().rate; value = ClampTo(llvalue); } else { @@ -2979,22 +2979,22 @@ struct CustomCurrencyWindow : Window { void SetButtonState() { - this->SetWidgetDisabledState(WID_CC_RATE_DOWN, _custom_currency.rate == 1); - this->SetWidgetDisabledState(WID_CC_RATE_UP, _custom_currency.rate == UINT16_MAX); - this->SetWidgetDisabledState(WID_CC_YEAR_DOWN, _custom_currency.to_euro == CF_NOEURO); - this->SetWidgetDisabledState(WID_CC_YEAR_UP, _custom_currency.to_euro == CalendarTime::MAX_YEAR); + this->SetWidgetDisabledState(WID_CC_RATE_DOWN, GetCustomCurrency().rate == 1); + this->SetWidgetDisabledState(WID_CC_RATE_UP, GetCustomCurrency().rate == UINT16_MAX); + this->SetWidgetDisabledState(WID_CC_YEAR_DOWN, GetCustomCurrency().to_euro == CF_NOEURO); + this->SetWidgetDisabledState(WID_CC_YEAR_UP, GetCustomCurrency().to_euro == CalendarTime::MAX_YEAR); } void SetStringParameters(WidgetID widget) const override { switch (widget) { case WID_CC_RATE: SetDParam(0, 1); SetDParam(1, 1); break; - case WID_CC_SEPARATOR: SetDParamStr(0, _custom_currency.separator); break; - case WID_CC_PREFIX: SetDParamStr(0, _custom_currency.prefix); break; - case WID_CC_SUFFIX: SetDParamStr(0, _custom_currency.suffix); break; + case WID_CC_SEPARATOR: SetDParamStr(0, GetCustomCurrency().separator); break; + case WID_CC_PREFIX: SetDParamStr(0, GetCustomCurrency().prefix); break; + case WID_CC_SUFFIX: SetDParamStr(0, GetCustomCurrency().suffix); break; case WID_CC_YEAR: - SetDParam(0, (_custom_currency.to_euro != CF_NOEURO) ? STR_CURRENCY_SWITCH_TO_EURO : STR_CURRENCY_SWITCH_TO_EURO_NEVER); - SetDParam(1, _custom_currency.to_euro); + SetDParam(0, (GetCustomCurrency().to_euro != CF_NOEURO) ? STR_CURRENCY_SWITCH_TO_EURO : STR_CURRENCY_SWITCH_TO_EURO_NEVER); + SetDParam(1, GetCustomCurrency().to_euro); break; case WID_CC_PREVIEW: @@ -3039,19 +3039,19 @@ struct CustomCurrencyWindow : Window { switch (widget) { case WID_CC_RATE_DOWN: - if (_custom_currency.rate > 1) _custom_currency.rate--; - if (_custom_currency.rate == 1) this->DisableWidget(WID_CC_RATE_DOWN); + if (GetCustomCurrency().rate > 1) GetCustomCurrency().rate--; + if (GetCustomCurrency().rate == 1) this->DisableWidget(WID_CC_RATE_DOWN); this->EnableWidget(WID_CC_RATE_UP); break; case WID_CC_RATE_UP: - if (_custom_currency.rate < UINT16_MAX) _custom_currency.rate++; - if (_custom_currency.rate == UINT16_MAX) this->DisableWidget(WID_CC_RATE_UP); + if (GetCustomCurrency().rate < UINT16_MAX) GetCustomCurrency().rate++; + if (GetCustomCurrency().rate == UINT16_MAX) this->DisableWidget(WID_CC_RATE_UP); this->EnableWidget(WID_CC_RATE_DOWN); break; case WID_CC_RATE: - SetDParam(0, _custom_currency.rate); + SetDParam(0, GetCustomCurrency().rate); str = STR_JUST_INT; len = 5; line = WID_CC_RATE; @@ -3060,7 +3060,7 @@ struct CustomCurrencyWindow : Window { case WID_CC_SEPARATOR_EDIT: case WID_CC_SEPARATOR: - SetDParamStr(0, _custom_currency.separator); + SetDParamStr(0, GetCustomCurrency().separator); str = STR_JUST_RAW_STRING; len = 7; line = WID_CC_SEPARATOR; @@ -3068,7 +3068,7 @@ struct CustomCurrencyWindow : Window { case WID_CC_PREFIX_EDIT: case WID_CC_PREFIX: - SetDParamStr(0, _custom_currency.prefix); + SetDParamStr(0, GetCustomCurrency().prefix); str = STR_JUST_RAW_STRING; len = 15; line = WID_CC_PREFIX; @@ -3076,26 +3076,26 @@ struct CustomCurrencyWindow : Window { case WID_CC_SUFFIX_EDIT: case WID_CC_SUFFIX: - SetDParamStr(0, _custom_currency.suffix); + SetDParamStr(0, GetCustomCurrency().suffix); str = STR_JUST_RAW_STRING; len = 15; line = WID_CC_SUFFIX; break; case WID_CC_YEAR_DOWN: - _custom_currency.to_euro = (_custom_currency.to_euro <= MIN_EURO_YEAR) ? CF_NOEURO : _custom_currency.to_euro - 1; - if (_custom_currency.to_euro == CF_NOEURO) this->DisableWidget(WID_CC_YEAR_DOWN); + GetCustomCurrency().to_euro = (GetCustomCurrency().to_euro <= MIN_EURO_YEAR) ? CF_NOEURO : GetCustomCurrency().to_euro - 1; + if (GetCustomCurrency().to_euro == CF_NOEURO) this->DisableWidget(WID_CC_YEAR_DOWN); this->EnableWidget(WID_CC_YEAR_UP); break; case WID_CC_YEAR_UP: - _custom_currency.to_euro = Clamp(_custom_currency.to_euro + 1, MIN_EURO_YEAR, CalendarTime::MAX_YEAR); - if (_custom_currency.to_euro == CalendarTime::MAX_YEAR) this->DisableWidget(WID_CC_YEAR_UP); + GetCustomCurrency().to_euro = Clamp(GetCustomCurrency().to_euro + 1, MIN_EURO_YEAR, CalendarTime::MAX_YEAR); + if (GetCustomCurrency().to_euro == CalendarTime::MAX_YEAR) this->DisableWidget(WID_CC_YEAR_UP); this->EnableWidget(WID_CC_YEAR_DOWN); break; case WID_CC_YEAR: - SetDParam(0, _custom_currency.to_euro); + SetDParam(0, GetCustomCurrency().to_euro); str = STR_JUST_INT; len = 7; line = WID_CC_YEAR; @@ -3118,25 +3118,25 @@ struct CustomCurrencyWindow : Window { switch (this->query_widget) { case WID_CC_RATE: - _custom_currency.rate = Clamp(atoi(str), 1, UINT16_MAX); + GetCustomCurrency().rate = Clamp(atoi(str), 1, UINT16_MAX); break; case WID_CC_SEPARATOR: // Thousands separator - _custom_currency.separator = str; + GetCustomCurrency().separator = str; break; case WID_CC_PREFIX: - _custom_currency.prefix = str; + GetCustomCurrency().prefix = str; break; case WID_CC_SUFFIX: - _custom_currency.suffix = str; + GetCustomCurrency().suffix = str; break; case WID_CC_YEAR: { // Year to switch to euro TimerGameCalendar::Year val = atoi(str); - _custom_currency.to_euro = (val < MIN_EURO_YEAR ? CF_NOEURO : std::min(val, CalendarTime::MAX_YEAR)); + GetCustomCurrency().to_euro = (val < MIN_EURO_YEAR ? CF_NOEURO : std::min(val, CalendarTime::MAX_YEAR)); break; } } diff --git a/src/strings.cpp b/src/strings.cpp index 0b40014476..e02ae292a4 100644 --- a/src/strings.cpp +++ b/src/strings.cpp @@ -542,7 +542,7 @@ static void FormatGenericCurrency(StringBuilder &builder, const CurrencySpec *sp } const char *separator = _settings_game.locale.digit_group_separator_currency.c_str(); - if (StrEmpty(separator)) separator = _currency->separator.c_str(); + if (StrEmpty(separator)) separator = GetCurrency().separator.c_str(); if (StrEmpty(separator)) separator = _langpack.langpack->digit_group_separator_currency; FormatNumber(builder, number, separator); if (number_str != STR_NULL) { @@ -1324,11 +1324,11 @@ static void FormatString(StringBuilder &builder, const char *str_arg, StringPara } case SCC_CURRENCY_SHORT: // {CURRENCY_SHORT} - FormatGenericCurrency(builder, _currency, args.GetNextParameter(), true); + FormatGenericCurrency(builder, &GetCurrency(), args.GetNextParameter(), true); break; case SCC_CURRENCY_LONG: // {CURRENCY_LONG} - FormatGenericCurrency(builder, _currency, args.GetNextParameter(), false); + FormatGenericCurrency(builder, &GetCurrency(), args.GetNextParameter(), false); break; case SCC_DATE_TINY: // {DATE_TINY} diff --git a/src/survey.cpp b/src/survey.cpp index f9310c976d..89c699ad0d 100644 --- a/src/survey.cpp +++ b/src/survey.cpp @@ -166,7 +166,7 @@ void SurveySettings(nlohmann::json &survey, bool skip_if_default) for (auto &table : GenericSettingTables()) { SurveySettingsTable(survey, table, &_settings_game, skip_if_default); } - SurveySettingsTable(survey, _currency_settings, &_custom_currency, skip_if_default); + SurveySettingsTable(survey, _currency_settings, &GetCustomCurrency(), skip_if_default); SurveySettingsTable(survey, _company_settings, &_settings_client.company, skip_if_default); }