1
0
Fork 0

Cleanup: Remove global string parameters.

Global parameters, and functions for dealing with them, are now gone.
pull/13736/head
Peter Nelson 2025-03-03 23:32:51 +00:00 committed by Peter Nelson
parent e818fcbf19
commit 27761ae431
10 changed files with 14 additions and 264 deletions

View File

@ -33,7 +33,7 @@ using CompanyPropertiesMap = std::map<uint, std::unique_ptr<CompanyProperties>>;
struct LoadCheckData {
bool checkable; ///< True if the savegame could be checked by SL_LOAD_CHECK. (Old savegames are not checkable.)
StringID error; ///< Error message from loading. INVALID_STRING_ID if no error.
std::string error_msg; ///< Data to pass to SetDParamStr when displaying #error.
std::string error_msg; ///< Data to pass to string parameters when displaying #error.
uint32_t map_size_x, map_size_y;
TimerGameCalendar::Date current_date;

View File

@ -856,7 +856,7 @@ Dimension GetStringBoundingBox(std::string_view str, FontSize start_fontsize)
}
/**
* Get bounding box of a string. Uses parameters set by #SetDParam if needed.
* Get bounding box of a string.
* Has the same restrictions as #GetStringBoundingBox(std::string_view str, FontSize start_fontsize).
* @param strid String to examine.
* @return Width and height of the bounding box for the string in pixels.

View File

@ -59,19 +59,6 @@ TextDirection _current_text_dir; ///< Text direction of the currently selected l
std::unique_ptr<icu::Collator> _current_collator; ///< Collator for the language currently in use.
#endif /* WITH_ICU_I18N */
ArrayStringParameters<20> _global_string_params;
/**
* Prepare the string parameters for the next formatting run. This means
* resetting the type information and resetting the offset to the begin.
*/
void StringParameters::PrepareForNextRun()
{
for (auto &param : this->parameters) param.type = 0;
this->offset = 0;
}
/**
* Get the next parameter from our parameters.
* This updates the offset, so the next time this is called the next parameter
@ -224,26 +211,6 @@ std::string EncodedString::GetDecodedString() const
return GetString(STR_JUST_RAW_STRING, this->string);
}
/**
* Set a string parameter \a v at index \a n in the global string parameter array.
* @param n Index of the string parameter.
* @param v Value of the string parameter.
*/
void SetDParam(size_t n, uint64_t v)
{
_global_string_params.SetParam(n, v);
}
/**
* Get the current string parameter at index \a n from the global string parameter array.
* @param n Index of the string parameter.
* @return Value of the requested string parameter.
*/
uint64_t GetDParam(size_t n)
{
return std::get<uint64_t>(_global_string_params.GetParam(n));
}
/**
* Get some number that is suitable for string size computations.
* @param count Number of digits which shall be displayable.
@ -260,17 +227,6 @@ uint64_t GetParamMaxDigits(uint count, FontSize size)
return val;
}
/**
* Set DParam n to some number that is suitable for string size computations.
* @param n Index of the string parameter.
* @param count Number of digits which shall be displayable.
* @param size Font of the number
*/
void SetDParamMaxDigits(size_t n, uint count, FontSize size)
{
SetDParam(n, GetParamMaxDigits(count, size));
}
/**
* Get some number that is suitable for string size computations.
* @param max_value The biggest value which shall be displayed.
@ -289,56 +245,6 @@ uint64_t GetParamMaxValue(uint64_t max_value, uint min_count, FontSize size)
return GetParamMaxDigits(std::max(min_count, num_digits), size);
}
/**
* Set DParam n to some number that is suitable for string size computations.
* @param n Index of the string parameter.
* @param max_value The biggest value which shall be displayed.
* For the result only the number of digits of \a max_value matter.
* @param min_count Minimum number of digits independent of \a max.
* @param size Font of the number
*/
void SetDParamMaxValue(size_t n, uint64_t max_value, uint min_count, FontSize size)
{
SetDParam(n, GetParamMaxValue(max_value, min_count, 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 StringParameterData> backup)
{
for (size_t i = 0; i < backup.size(); i++) {
_global_string_params.SetParam(i, backup[i]);
}
}
/**
* Copy \a num string parameters from the global string parameter array to the \a backup.
* @param backup The backup to write to.
* @param num Number of string parameters to copy.
*/
void CopyOutDParam(std::vector<StringParameterData> &backup, size_t num)
{
backup.resize(num);
for (size_t i = 0; i < backup.size(); i++) {
backup[i] = _global_string_params.GetParam(i);
}
}
/**
* Checks whether the global string parameters have changed compared to the given backup.
* @param backup The backup to check against.
* @return True when the parameters have changed, otherwise false.
*/
bool HaveDParamChanged(const std::span<const StringParameterData> backup)
{
for (size_t i = 0; i < backup.size(); i++) {
if (backup[i] != _global_string_params.GetParam(i)) return true;
}
return false;
}
static void StationGetSpecialString(StringBuilder &builder, StationFacilities x);
static bool GetSpecialNameString(StringBuilder &builder, StringID string, StringParameters &args);
@ -503,28 +409,24 @@ void GetStringWithArgs(StringBuilder &builder, StringID string, std::span<String
}
/**
* Resolve the given StringID into a std::string with all the associated
* DParam lookups and formatting.
* Resolve the given StringID into a std::string with formatting but no parameters.
* @param string The unique identifier of the translatable string.
* @return The std::string of the translated string.
*/
std::string GetString(StringID string)
{
_global_string_params.PrepareForNextRun();
return GetStringWithArgs(string, _global_string_params);
return GetStringWithArgs(string, {});
}
/**
* Resolve the given StringID and append in place into an existing std::string with all the associated
* DParam lookups and formatting.
* Resolve the given StringID and append in place into an existing std::string with formatting but no parameters.
* @param result The std::string to place the translated string.
* @param string The unique identifier of the translatable string.
*/
void AppendStringInPlace(std::string &result, StringID string)
{
_global_string_params.PrepareForNextRun();
StringBuilder builder(result);
GetStringWithArgs(builder, string, _global_string_params);
GetStringWithArgs(builder, string, {});
}
void AppendStringWithArgsInPlace(std::string &result, StringID string, std::span<StringParameter> params)
@ -556,39 +458,6 @@ std::string GetStringWithArgs(StringID string, std::span<StringParameter> args)
return result;
}
/**
* This function is used to "bind" a C string to a OpenTTD dparam slot.
* @param n slot of the string
* @param str string to bind
*/
void SetDParamStr(size_t n, const char *str)
{
_global_string_params.SetParam(n, str);
}
/**
* This function is used to "bind" the C string of a std::string to a OpenTTD dparam slot.
* The caller has to ensure that the std::string reference remains valid while the string is shown.
* @param n slot of the string
* @param str string to bind
*/
void SetDParamStr(size_t n, const std::string &str)
{
_global_string_params.SetParam(n, str);
}
/**
* This function is used to "bind" the std::string to a OpenTTD dparam slot.
* Contrary to the other \c SetDParamStr functions, this moves the string into
* the parameter slot.
* @param n slot of the string
* @param str string to bind
*/
void SetDParamStr(size_t n, std::string &&str)
{
_global_string_params.SetParam(n, std::move(str));
}
static const char *GetDecimalSeparator()
{
const char *decimal_separator = _settings_game.locale.digit_decimal_separator.c_str();

View File

@ -81,7 +81,7 @@ uint ConvertDisplaySpeedToKmhishSpeed(uint speed, VehicleType type);
* Pack velocity and vehicle type for use with SCC_VELOCITY string parameter.
* @param speed Display speed for parameter.
* @param type Type of vehicle for parameter.
* @return Bit-packed velocity and vehicle type, for use with SetDParam().
* @return Bit-packed velocity and vehicle type, for use with string parameters.
*/
inline int64_t PackVelocity(uint speed, VehicleType type)
{
@ -93,30 +93,6 @@ inline int64_t PackVelocity(uint speed, VehicleType type)
uint64_t GetParamMaxValue(uint64_t max_value, uint min_count = 0, FontSize size = FS_NORMAL);
uint64_t GetParamMaxDigits(uint count, FontSize size = FS_NORMAL);
void SetDParam(size_t n, uint64_t v);
void SetDParamMaxValue(size_t n, uint64_t max_value, uint min_count = 0, FontSize size = FS_NORMAL);
void SetDParamMaxDigits(size_t n, uint count, FontSize size = FS_NORMAL);
void SetDParam(size_t n, ConvertibleThroughBase auto v)
{
SetDParam(n, v.base());
}
void SetDParamMaxValue(size_t n, ConvertibleThroughBase auto max_value, uint min_count = 0, FontSize size = FS_NORMAL)
{
SetDParamMaxValue(n, max_value.base(), min_count, size);
}
void SetDParamStr(size_t n, const char *str);
void SetDParamStr(size_t n, const std::string &str);
void SetDParamStr(size_t n, std::string &&str);
void CopyInDParam(const std::span<const StringParameterData> backup);
void CopyOutDParam(std::vector<StringParameterData> &backup, size_t num);
bool HaveDParamChanged(const std::span<const StringParameterData> backup);
uint64_t GetDParam(size_t n);
extern TextDirection _current_text_dir; ///< Text direction of the currently selected language
void InitializeLanguagePacks();

View File

@ -35,7 +35,6 @@ public:
StringParameters(std::span<StringParameter> parameters = {}) : parameters(parameters) {}
void PrepareForNextRun();
void SetTypeOfNextParameter(char32_t type) { this->next_type = type; }
/**
@ -126,7 +125,7 @@ public:
/**
* Get a new instance of StringParameters that is a "range" into the
* remaining existing parameters. Upon destruction the offset in the parent
* is not updated. However, calls to SetDParam do update the parameters.
* is not updated. However, calls to SetParam do update the parameters.
*
* The returned StringParameters must not outlive this StringParameters.
* @return A "range" of the string parameters.
@ -136,7 +135,7 @@ public:
/**
* Get a new instance of StringParameters that is a "range" into the
* remaining existing parameters from the given offset. Upon destruction the
* offset in the parent is not updated. However, calls to SetDParam do
* offset in the parent is not updated. However, calls to SetParam do
* update the parameters.
*
* The returned StringParameters must not outlive this StringParameters.
@ -199,38 +198,6 @@ public:
}
};
/**
* Extension of StringParameters with its own statically sized buffer for
* the parameters.
*/
template <size_t N>
class ArrayStringParameters : public StringParameters {
std::array<StringParameter, N> params{}; ///< The actual parameters
public:
ArrayStringParameters()
{
this->parameters = std::span(params.data(), params.size());
}
ArrayStringParameters(ArrayStringParameters&& other) noexcept
{
*this = std::move(other);
}
ArrayStringParameters& operator=(ArrayStringParameters &&other) noexcept
{
this->offset = other.offset;
this->next_type = other.next_type;
this->params = std::move(other.params);
this->parameters = std::span(params.data(), params.size());
return *this;
}
ArrayStringParameters(const ArrayStringParameters &other) = delete;
ArrayStringParameters& operator=(const ArrayStringParameters &other) = delete;
};
/**
* Equivalent to the std::back_insert_iterator in function, with some
* convenience helpers for string concatenation.

View File

@ -8,7 +8,6 @@ add_test_files(
mock_spritecache.cpp
mock_spritecache.h
string_func.cpp
strings_func.cpp
test_main.cpp
test_network_crypto.cpp
test_script_admin.cpp

View File

@ -1,52 +0,0 @@
/*
* This file is part of OpenTTD.
* OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
* OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
*/
/** @file strings_func.cpp Test functionality from strings_func. */
#include "../stdafx.h"
#include "../3rdparty/catch2/catch.hpp"
#include "../strings_func.h"
TEST_CASE("HaveDParamChanged")
{
SetDParam(0, 0);
SetDParamStr(1, "some string");
std::vector<StringParameterData> backup;
CopyOutDParam(backup, 2);
CHECK(HaveDParamChanged(backup) == false);
/* A different parameter 0 (both string and numeric). */
SetDParam(0, 1);
CHECK(HaveDParamChanged(backup) == true);
SetDParamStr(0, "some other string");
CHECK(HaveDParamChanged(backup) == true);
/* Back to the original state, nothing should have changed. */
SetDParam(0, 0);
CHECK(HaveDParamChanged(backup) == false);
/* A different parameter 1 (both string and numeric). */
SetDParamStr(1, "some other string");
CHECK(HaveDParamChanged(backup) == true);
SetDParam(1, 0);
CHECK(HaveDParamChanged(backup) == true);
/* Back to the original state, nothing should have changed. */
SetDParamStr(1, "some string");
CHECK(HaveDParamChanged(backup) == false);
/* Changing parameter 2 should not have any effect, as the backup is only 2 long. */
SetDParam(2, 3);
CHECK(HaveDParamChanged(backup) == false);
}

View File

@ -512,13 +512,13 @@ public:
}
/**
* Gets the speed in km-ish/h that can be sent into SetDParam for string processing.
* Gets the speed in km-ish/h that can be sent into string parameters for string processing.
* @return the vehicle's speed
*/
virtual int GetDisplaySpeed() const { return 0; }
/**
* Gets the maximum speed in km-ish/h that can be sent into SetDParam for string processing.
* Gets the maximum speed in km-ish/h that can be sent into string parameters for string processing.
* @return the vehicle's maximum speed
*/
virtual int GetDisplayMaxSpeed() const { return 0; }
@ -599,19 +599,19 @@ public:
virtual Trackdir GetVehicleTrackdir() const { return INVALID_TRACKDIR; }
/**
* Gets the running cost of a vehicle that can be sent into SetDParam for string processing.
* Gets the running cost of a vehicle that can be sent into string parameters for string processing.
* @return the vehicle's running cost
*/
Money GetDisplayRunningCost() const { return (this->GetRunningCost() >> 8); }
/**
* Gets the profit vehicle had this year. It can be sent into SetDParam for string processing.
* Gets the profit vehicle had this year. It can be sent into string parameters for string processing.
* @return the vehicle's profit this year
*/
Money GetDisplayProfitThisYear() const { return (this->profit_this_year >> 8); }
/**
* Gets the profit vehicle had last year. It can be sent into SetDParam for string processing.
* Gets the profit vehicle had last year. It can be sent into string parameters for string processing.
* @return the vehicle's profit last year
*/
Money GetDisplayProfitLastYear() const { return (this->profit_last_year >> 8); }

View File

@ -502,7 +502,6 @@ bool Window::SetFocusedWidget(WidgetID widget_index)
std::string Window::GetWidgetString([[maybe_unused]] WidgetID widget, StringID stringid) const
{
if (stringid == STR_NULL) return {};
this->SetStringParameters(widget);
return GetString(stringid);
}

View File

@ -616,14 +616,6 @@ public:
*/
virtual void UpdateWidgetSize([[maybe_unused]] WidgetID widget, [[maybe_unused]] Dimension &size, [[maybe_unused]] const Dimension &padding, [[maybe_unused]] Dimension &fill, [[maybe_unused]] Dimension &resize) {}
/**
* Initialize string parameters for a widget.
* Calls to this function are made during initialization to measure the size (that is as part of #InitNested()), during drawing,
* and while re-initializing the window. Only for widgets that render text initializing is requested.
* @param widget Widget number.
*/
virtual void SetStringParameters([[maybe_unused]] WidgetID widget) const {}
/**
* Get the raw string for a widget.
* Calls to this function are also made during initialization to measure the size (that is as part of #InitNested()), during drawing,