Changeset - r27615:4608ed940341
[Not reviewed]
master
0 5 0
Rubidium - 12 months ago 2023-06-13 21:46:08
rubidium@openttd.org
Codechange: make creating temporary StringParameters easier
5 files changed with 76 insertions and 93 deletions:
0 comments (0 inline, 0 general)
src/industry_cmd.cpp
Show inline comments
 
@@ -2428,14 +2428,13 @@ void Industry::RecomputeProductionMultip
 
		p.rate = ClampTo<uint8_t>(CeilDiv(indspec->production_rate[&p - this->produced.data()] * this->prod_level, PRODLEVEL_DEFAULT));
 
	}
 
}
 

	
 
void Industry::FillCachedName() const
 
{
 
	int64 args_array[] = { this->index };
 
	StringParameters tmp_params(args_array);
 
	auto tmp_params = MakeParameters(this->index);
 
	this->cached_name = GetStringWithArgs(STR_INDUSTRY_NAME, tmp_params);
 
}
 

	
 
void ClearAllIndustryCachedNames()
 
{
 
	for (Industry *ind : Industry::Iterate()) {
src/station_cmd.cpp
Show inline comments
 
@@ -458,14 +458,13 @@ void UpdateAllStationVirtCoords()
 
		st->UpdateVirtCoord();
 
	}
 
}
 

	
 
void BaseStation::FillCachedName() const
 
{
 
	int64 args_array[] = { this->index };
 
	StringParameters tmp_params(args_array);
 
	auto tmp_params = MakeParameters(this->index);
 
	this->cached_name = GetStringWithArgs(Waypoint::IsExpected(this) ? STR_WAYPOINT_NAME : STR_STATION_NAME, tmp_params);
 
}
 

	
 
void ClearAllStationCachedNames()
 
{
 
	for (BaseStation *st : BaseStation::Iterate()) {
src/strings.cpp
Show inline comments
 
@@ -336,24 +336,24 @@ std::string GetStringWithArgs(StringID s
 
 * 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)
 
{
 
	SetDParam(n, (uint64)(size_t)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)
 
{
 
	SetDParamStr(n, str.c_str());
 
	_global_string_params.SetParam(n, str);
 
}
 

	
 
/**
 
 * Format a number into a string.
 
 * @param builder   the string builder to write to
 
 * @param number    the number to write down
 
@@ -458,35 +458,32 @@ static void FormatBytes(StringBuilder &b
 

	
 
static void FormatYmdString(StringBuilder &builder, TimerGameCalendar::Date date, uint case_index)
 
{
 
	TimerGameCalendar::YearMonthDay ymd;
 
	TimerGameCalendar::ConvertDateToYMD(date, &ymd);
 

	
 
	int64 args[] = {ymd.day + STR_DAY_NUMBER_1ST - 1, STR_MONTH_ABBREV_JAN + ymd.month, ymd.year};
 
	StringParameters tmp_params(args);
 
	auto tmp_params = MakeParameters(ymd.day + STR_DAY_NUMBER_1ST - 1, STR_MONTH_ABBREV_JAN + ymd.month, ymd.year);
 
	FormatString(builder, GetStringPtr(STR_FORMAT_DATE_LONG), tmp_params, case_index);
 
}
 

	
 
static void FormatMonthAndYear(StringBuilder &builder, TimerGameCalendar::Date date, uint case_index)
 
{
 
	TimerGameCalendar::YearMonthDay ymd;
 
	TimerGameCalendar::ConvertDateToYMD(date, &ymd);
 

	
 
	int64 args[] = {STR_MONTH_JAN + ymd.month, ymd.year};
 
	StringParameters tmp_params(args);
 
	auto tmp_params = MakeParameters(STR_MONTH_JAN + ymd.month, ymd.year);
 
	FormatString(builder, GetStringPtr(STR_FORMAT_DATE_SHORT), tmp_params, case_index);
 
}
 

	
 
static void FormatTinyOrISODate(StringBuilder &builder, TimerGameCalendar::Date date, StringID str)
 
{
 
	TimerGameCalendar::YearMonthDay ymd;
 
	TimerGameCalendar::ConvertDateToYMD(date, &ymd);
 

	
 
	/* Day and month are zero-padded with ZEROFILL_NUM, hence the two 2s. */
 
	int64 args[] = {ymd.day, 2, ymd.month + 1, 2, ymd.year};
 
	StringParameters tmp_params(args);
 
	auto tmp_params = MakeParameters(ymd.day, 2, ymd.month + 1, 2, ymd.year);
 
	FormatString(builder, GetStringPtr(str), tmp_params);
 
}
 

	
 
static void FormatGenericCurrency(StringBuilder &builder, const CurrencySpec *spec, Money number, bool compact)
 
{
 
	/* We are going to make number absolute for printing, so
 
@@ -1210,22 +1207,20 @@ static void FormatString(StringBuilder &
 
				if (cargo >= CargoSpec::GetArraySize()) break;
 

	
 
				StringID cargo_str = CargoSpec::Get(cargo)->units_volume;
 
				switch (cargo_str) {
 
					case STR_TONS: {
 
						assert(_settings_game.locale.units_weight < lengthof(_units_weight));
 
						int64 args_array[] = {_units_weight[_settings_game.locale.units_weight].c.ToDisplay(args.GetInt64())};
 
						StringParameters tmp_params(args_array);
 
						auto tmp_params = MakeParameters(_units_weight[_settings_game.locale.units_weight].c.ToDisplay(args.GetInt64()));
 
						FormatString(builder, GetStringPtr(_units_weight[_settings_game.locale.units_weight].l), tmp_params);
 
						break;
 
					}
 

	
 
					case STR_LITERS: {
 
						assert(_settings_game.locale.units_volume < lengthof(_units_volume));
 
						int64 args_array[] = {_units_volume[_settings_game.locale.units_volume].c.ToDisplay(args.GetInt64())};
 
						StringParameters tmp_params(args_array);
 
						auto tmp_params = MakeParameters(_units_volume[_settings_game.locale.units_volume].c.ToDisplay(args.GetInt64()));
 
						FormatString(builder, GetStringPtr(_units_volume[_settings_game.locale.units_volume].l), tmp_params);
 
						break;
 
					}
 

	
 
					default: {
 
						StringParameters tmp_params(args, 1);
 
@@ -1297,118 +1292,106 @@ static void FormatString(StringBuilder &
 
				FormatTinyOrISODate(builder, args.GetInt32(), STR_FORMAT_DATE_ISO);
 
				break;
 

	
 
			case SCC_FORCE: { // {FORCE}
 
				assert(_settings_game.locale.units_force < lengthof(_units_force));
 
				const auto &x = _units_force[_settings_game.locale.units_force];
 
				int64 args_array[] = {x.c.ToDisplay(args.GetInt64()), x.decimal_places};
 
				StringParameters tmp_params(args_array);
 
				auto tmp_params = MakeParameters(x.c.ToDisplay(args.GetInt64()), x.decimal_places);
 
				FormatString(builder, GetStringPtr(x.s), tmp_params);
 
				break;
 
			}
 

	
 
			case SCC_HEIGHT: { // {HEIGHT}
 
				assert(_settings_game.locale.units_height < lengthof(_units_height));
 
				const auto &x = _units_height[_settings_game.locale.units_height];
 
				int64 args_array[] = {x.c.ToDisplay(args.GetInt64()), x.decimal_places};
 
				StringParameters tmp_params(args_array);
 
				auto tmp_params = MakeParameters(x.c.ToDisplay(args.GetInt64()), x.decimal_places);
 
				FormatString(builder, GetStringPtr(x.s), tmp_params);
 
				break;
 
			}
 

	
 
			case SCC_POWER: { // {POWER}
 
				assert(_settings_game.locale.units_power < lengthof(_units_power));
 
				const auto &x = _units_power[_settings_game.locale.units_power];
 
				int64 args_array[] = {x.c.ToDisplay(args.GetInt64()), x.decimal_places};
 
				StringParameters tmp_params(args_array);
 
				auto tmp_params = MakeParameters(x.c.ToDisplay(args.GetInt64()), x.decimal_places);
 
				FormatString(builder, GetStringPtr(x.s), tmp_params);
 
				break;
 
			}
 

	
 
			case SCC_POWER_TO_WEIGHT: { // {POWER_TO_WEIGHT}
 
				auto setting = _settings_game.locale.units_power * 3u + _settings_game.locale.units_weight;
 
				assert(setting < lengthof(_units_power_to_weight));
 
				const auto &x = _units_power_to_weight[setting];
 
				int64 args_array[] = {x.c.ToDisplay(args.GetInt64()), x.decimal_places};
 
				StringParameters tmp_params(args_array);
 
				auto tmp_params = MakeParameters(x.c.ToDisplay(args.GetInt64()), x.decimal_places);
 
				FormatString(builder, GetStringPtr(x.s), tmp_params);
 
				break;
 
			}
 

	
 
			case SCC_VELOCITY: { // {VELOCITY}
 
				int64 arg = args.GetInt64();
 
				// Unpack vehicle type from packed argument to get desired units.
 
				VehicleType vt = static_cast<VehicleType>(GB(arg, 56, 8));
 
				byte units = GetVelocityUnits(vt);
 
				assert(units < lengthof(_units_velocity));
 
				const auto &x = _units_velocity[units];
 
				int64 args_array[] = {ConvertKmhishSpeedToDisplaySpeed(GB(arg, 0, 56), vt), x.decimal_places};
 
				StringParameters tmp_params(args_array);
 
				auto tmp_params = MakeParameters(ConvertKmhishSpeedToDisplaySpeed(GB(arg, 0, 56), vt), x.decimal_places);
 
				FormatString(builder, GetStringPtr(x.s), tmp_params);
 
				break;
 
			}
 

	
 
			case SCC_VOLUME_SHORT: { // {VOLUME_SHORT}
 
				assert(_settings_game.locale.units_volume < lengthof(_units_volume));
 
				const auto &x = _units_volume[_settings_game.locale.units_volume];
 
				int64 args_array[] = {x.c.ToDisplay(args.GetInt64()), x.decimal_places};
 
				StringParameters tmp_params(args_array);
 
				auto tmp_params = MakeParameters(x.c.ToDisplay(args.GetInt64()), x.decimal_places);
 
				FormatString(builder, GetStringPtr(x.s), tmp_params);
 
				break;
 
			}
 

	
 
			case SCC_VOLUME_LONG: { // {VOLUME_LONG}
 
				assert(_settings_game.locale.units_volume < lengthof(_units_volume));
 
				const auto &x = _units_volume[_settings_game.locale.units_volume];
 
				int64 args_array[] = {x.c.ToDisplay(args.GetInt64()), x.decimal_places};
 
				StringParameters tmp_params(args_array);
 
				auto tmp_params = MakeParameters(x.c.ToDisplay(args.GetInt64()), x.decimal_places);
 
				FormatString(builder, GetStringPtr(x.l), tmp_params);
 
				break;
 
			}
 

	
 
			case SCC_WEIGHT_SHORT: { // {WEIGHT_SHORT}
 
				assert(_settings_game.locale.units_weight < lengthof(_units_weight));
 
				const auto &x = _units_weight[_settings_game.locale.units_weight];
 
				int64 args_array[] = {x.c.ToDisplay(args.GetInt64()), x.decimal_places};
 
				StringParameters tmp_params(args_array);
 
				auto tmp_params = MakeParameters(x.c.ToDisplay(args.GetInt64()), x.decimal_places);
 
				FormatString(builder, GetStringPtr(x.s), tmp_params);
 
				break;
 
			}
 

	
 
			case SCC_WEIGHT_LONG: { // {WEIGHT_LONG}
 
				assert(_settings_game.locale.units_weight < lengthof(_units_weight));
 
				const auto &x = _units_weight[_settings_game.locale.units_weight];
 
				int64 args_array[] = {x.c.ToDisplay(args.GetInt64()), x.decimal_places};
 
				StringParameters tmp_params(args_array);
 
				auto tmp_params = MakeParameters(x.c.ToDisplay(args.GetInt64()), x.decimal_places);
 
				FormatString(builder, GetStringPtr(x.l), tmp_params);
 
				break;
 
			}
 

	
 
			case SCC_COMPANY_NAME: { // {COMPANY}
 
				const Company *c = Company::GetIfValid(args.GetInt32());
 
				if (c == nullptr) break;
 

	
 
				if (!c->name.empty()) {
 
					int64 args_array[] = {(int64)(size_t)c->name.c_str()};
 
					StringParameters tmp_params(args_array);
 
					auto tmp_params = MakeParameters(c->name);
 
					GetStringWithArgs(builder, STR_JUST_RAW_STRING, tmp_params);
 
				} else {
 
					int64 args_array[] = {c->name_2};
 
					StringParameters tmp_params(args_array);
 
					auto tmp_params = MakeParameters(c->name_2);
 
					GetStringWithArgs(builder, c->name_1, tmp_params);
 
				}
 
				break;
 
			}
 

	
 
			case SCC_COMPANY_NUM: { // {COMPANY_NUM}
 
				CompanyID company = (CompanyID)args.GetInt32();
 

	
 
				/* Nothing is added for AI or inactive companies */
 
				if (Company::IsValidHumanID(company)) {
 
					int64 args_array[] = {company + 1};
 
					StringParameters tmp_params(args_array);
 
					auto tmp_params = MakeParameters(company + 1);
 
					GetStringWithArgs(builder, STR_FORMAT_COMPANY_NUM, tmp_params);
 
				}
 
				break;
 
			}
 

	
 
			case SCC_DEPOT_NAME: { // {DEPOT}
 
@@ -1420,33 +1403,29 @@ static void FormatString(StringBuilder &
 
					GetStringWithArgs(builder, STR_FORMAT_DEPOT_NAME_AIRCRAFT, tmp_params);
 
					break;
 
				}
 

	
 
				const Depot *d = Depot::Get(args.GetInt32());
 
				if (!d->name.empty()) {
 
					int64 args_array[] = {(int64)(size_t)d->name.c_str()};
 
					StringParameters tmp_params(args_array);
 
					auto tmp_params = MakeParameters(d->name);
 
					GetStringWithArgs(builder, STR_JUST_RAW_STRING, tmp_params);
 
				} else {
 
					int64 args_array[] = {d->town->index, d->town_cn + 1};
 
					StringParameters tmp_params(args_array);
 
					auto tmp_params = MakeParameters(d->town->index, d->town_cn + 1);
 
					GetStringWithArgs(builder, STR_FORMAT_DEPOT_NAME_TRAIN + 2 * vt + (d->town_cn == 0 ? 0 : 1), tmp_params);
 
				}
 
				break;
 
			}
 

	
 
			case SCC_ENGINE_NAME: { // {ENGINE}
 
				int64 arg = args.GetInt64();
 
				const Engine *e = Engine::GetIfValid(static_cast<EngineID>(arg));
 
				if (e == nullptr) break;
 

	
 
				if (!e->name.empty() && e->IsEnabled()) {
 
					int64 args_array[] = {(int64)(size_t)e->name.c_str()};
 
					StringParameters tmp_params(args_array);
 
					auto tmp_params = MakeParameters(e->name);
 
					GetStringWithArgs(builder, STR_JUST_RAW_STRING, tmp_params);
 

	
 
					break;
 
				}
 

	
 
				if (HasBit(e->info.callback_mask, CBM_VEHICLE_NAME)) {
 
					uint16 callback = GetVehicleCallback(CBID_VEHICLE_NAME, static_cast<uint32>(arg >> 32), 0, e->index, nullptr);
 
					/* Not calling ErrorUnknownCallbackResult due to being inside string processing. */
 
@@ -1462,29 +1441,26 @@ static void FormatString(StringBuilder &
 
						StopTextRefStackUsage();
 

	
 
						break;
 
					}
 
				}
 

	
 
				StringParameters tmp_params(nullptr, 0, nullptr);
 
				auto tmp_params = AllocatedStringParameters();
 
				GetStringWithArgs(builder, e->info.string_id, tmp_params);
 
				break;
 
			}
 

	
 
			case SCC_GROUP_NAME: { // {GROUP}
 
				const Group *g = Group::GetIfValid(args.GetInt32());
 
				if (g == nullptr) break;
 

	
 
				if (!g->name.empty()) {
 
					int64 args_array[] = {(int64)(size_t)g->name.c_str()};
 
					StringParameters tmp_params(args_array);
 
					auto tmp_params = MakeParameters(g->name);
 
					GetStringWithArgs(builder, STR_JUST_RAW_STRING, tmp_params);
 
				} else {
 
					int64 args_array[] = {g->index};
 
					StringParameters tmp_params(args_array);
 

	
 
					auto tmp_params = MakeParameters(g->index);
 
					GetStringWithArgs(builder, STR_FORMAT_GROUP_NAME, tmp_params);
 
				}
 
				break;
 
			}
 

	
 
			case SCC_INDUSTRY_NAME: { // {INDUSTRY}
 
@@ -1495,36 +1471,32 @@ static void FormatString(StringBuilder &
 
				if (use_cache) { // Use cached version if first call
 
					AutoRestoreBackup cache_backup(use_cache, false);
 
					builder += i->GetCachedName();
 
				} else if (_scan_for_gender_data) {
 
					/* Gender is defined by the industry type.
 
					 * STR_FORMAT_INDUSTRY_NAME may have the town first, so it would result in the gender of the town name */
 
					StringParameters tmp_params(nullptr, 0, nullptr);
 
					auto tmp_params = AllocatedStringParameters();
 
					FormatString(builder, GetStringPtr(GetIndustrySpec(i->type)->name), tmp_params, next_substr_case_index);
 
				} else {
 
					/* First print the town name and the industry type name. */
 
					int64 args_array[2] = {i->town->index, GetIndustrySpec(i->type)->name};
 
					StringParameters tmp_params(args_array);
 

	
 
					auto tmp_params = MakeParameters(i->town->index, GetIndustrySpec(i->type)->name);
 
					FormatString(builder, GetStringPtr(STR_FORMAT_INDUSTRY_NAME), tmp_params, next_substr_case_index);
 
				}
 
				next_substr_case_index = 0;
 
				break;
 
			}
 

	
 
			case SCC_PRESIDENT_NAME: { // {PRESIDENT_NAME}
 
				const Company *c = Company::GetIfValid(args.GetInt32());
 
				if (c == nullptr) break;
 

	
 
				if (!c->president_name.empty()) {
 
					int64 args_array[] = {(int64)(size_t)c->president_name.c_str()};
 
					StringParameters tmp_params(args_array);
 
					auto tmp_params = MakeParameters(c->president_name);
 
					GetStringWithArgs(builder, STR_JUST_RAW_STRING, tmp_params);
 
				} else {
 
					int64 args_array[] = {c->president_name_2};
 
					StringParameters tmp_params(args_array);
 
					auto tmp_params = MakeParameters(c->president_name_2);
 
					GetStringWithArgs(builder, c->president_name_1, tmp_params);
 
				}
 
				break;
 
			}
 

	
 
			case SCC_STATION_NAME: { // {STATION}
 
@@ -1532,24 +1504,23 @@ static void FormatString(StringBuilder &
 
				const Station *st = Station::GetIfValid(sid);
 

	
 
				if (st == nullptr) {
 
					/* The station doesn't exist anymore. The only place where we might
 
					 * be "drawing" an invalid station is in the case of cargo that is
 
					 * in transit. */
 
					StringParameters tmp_params(nullptr, 0, nullptr);
 
					auto tmp_params = AllocatedStringParameters();
 
					GetStringWithArgs(builder, STR_UNKNOWN_STATION, tmp_params);
 
					break;
 
				}
 

	
 
				static bool use_cache = true;
 
				if (use_cache) { // Use cached version if first call
 
					AutoRestoreBackup cache_backup(use_cache, false);
 
					builder += st->GetCachedName();
 
				} else if (!st->name.empty()) {
 
					int64 args_array[] = {(int64)(size_t)st->name.c_str()};
 
					StringParameters tmp_params(args_array);
 
					auto tmp_params = MakeParameters(st->name);
 
					GetStringWithArgs(builder, STR_JUST_RAW_STRING, tmp_params);
 
				} else {
 
					StringID string_id = st->string_id;
 
					if (st->indtype != IT_INVALID) {
 
						/* Special case where the industry provides the name for the station */
 
						const IndustrySpec *indsp = GetIndustrySpec(st->indtype);
 
@@ -1576,55 +1547,49 @@ static void FormatString(StringBuilder &
 

	
 
				static bool use_cache = true;
 
				if (use_cache) { // Use cached version if first call
 
					AutoRestoreBackup cache_backup(use_cache, false);
 
					builder += t->GetCachedName();
 
				} else if (!t->name.empty()) {
 
					int64 args_array[] = {(int64)(size_t)t->name.c_str()};
 
					StringParameters tmp_params(args_array);
 
					auto tmp_params = MakeParameters(t->name);
 
					GetStringWithArgs(builder, STR_JUST_RAW_STRING, tmp_params);
 
				} else {
 
					GetTownName(builder, t);
 
				}
 
				break;
 
			}
 

	
 
			case SCC_WAYPOINT_NAME: { // {WAYPOINT}
 
				Waypoint *wp = Waypoint::GetIfValid(args.GetInt32());
 
				if (wp == nullptr) break;
 

	
 
				if (!wp->name.empty()) {
 
					int64 args_array[] = {(int64)(size_t)wp->name.c_str()};
 
					StringParameters tmp_params(args_array);
 
					auto tmp_params = MakeParameters(wp->name);
 
					GetStringWithArgs(builder, STR_JUST_RAW_STRING, tmp_params);
 
				} else {
 
					int64 args_array[] = {wp->town->index, wp->town_cn + 1};
 
					StringParameters tmp_params(args_array);
 
					auto tmp_params = MakeParameters(wp->town->index, wp->town_cn + 1);
 
					StringID string_id = ((wp->string_id == STR_SV_STNAME_BUOY) ? STR_FORMAT_BUOY_NAME : STR_FORMAT_WAYPOINT_NAME);
 
					if (wp->town_cn != 0) string_id++;
 
					GetStringWithArgs(builder, string_id, tmp_params);
 
				}
 
				break;
 
			}
 

	
 
			case SCC_VEHICLE_NAME: { // {VEHICLE}
 
				const Vehicle *v = Vehicle::GetIfValid(args.GetInt32());
 
				if (v == nullptr) break;
 

	
 
				if (!v->name.empty()) {
 
					int64 args_array[] = {(int64)(size_t)v->name.c_str()};
 
					StringParameters tmp_params(args_array);
 
					auto tmp_params = MakeParameters(v->name);
 
					GetStringWithArgs(builder, STR_JUST_RAW_STRING, tmp_params);
 
				} else if (v->group_id != DEFAULT_GROUP) {
 
					/* The vehicle has no name, but is member of a group, so print group name */
 
					int64 args_array[] = {v->group_id, v->unitnumber};
 
					StringParameters tmp_params(args_array);
 
					auto tmp_params = MakeParameters(v->group_id, v->unitnumber);
 
					GetStringWithArgs(builder, STR_FORMAT_GROUP_VEHICLE_NAME, tmp_params);
 
				} else {
 
					int64 args_array[] = {v->unitnumber};
 
					StringParameters tmp_params(args_array);
 
					auto tmp_params = MakeParameters(v->unitnumber);
 

	
 
					StringID string_id;
 
					switch (v->type) {
 
						default:           string_id = STR_INVALID_VEHICLE; break;
 
						case VEH_TRAIN:    string_id = STR_SV_TRAIN_NAME; break;
 
						case VEH_ROAD:     string_id = STR_SV_ROAD_VEHICLE_NAME; break;
 
@@ -1639,17 +1604,16 @@ static void FormatString(StringBuilder &
 

	
 
			case SCC_SIGN_NAME: { // {SIGN}
 
				const Sign *si = Sign::GetIfValid(args.GetInt32());
 
				if (si == nullptr) break;
 

	
 
				if (!si->name.empty()) {
 
					int64 args_array[] = {(int64)(size_t)si->name.c_str()};
 
					StringParameters tmp_params(args_array);
 
					auto tmp_params = MakeParameters(si->name);
 
					GetStringWithArgs(builder, STR_JUST_RAW_STRING, tmp_params);
 
				} else {
 
					StringParameters tmp_params(nullptr, 0, nullptr);
 
					auto tmp_params = AllocatedStringParameters();
 
					GetStringWithArgs(builder, STR_DEFAULT_SIGN_NAME, tmp_params);
 
				}
 
				break;
 
			}
 

	
 
			case SCC_STATION_FEATURES: { // {STATIONFEATURES}
src/strings_internal.h
Show inline comments
 
@@ -11,12 +11,13 @@
 
#define STRINGS_INTERNAL_H
 

	
 
#include "strings_func.h"
 
#include "string_func.h"
 

	
 
class StringParameters {
 
protected:
 
	StringParameters *parent; ///< If not nullptr, this instance references data from this parent instance.
 
	uint64 *data;             ///< Array with the actual data.
 
	WChar *type;              ///< Array with type information about the data. Can be nullptr when no type information is needed. See #StringControlCode.
 

	
 
public:
 
	size_t offset = 0; ///< Current offset in the data/type arrays.
 
@@ -28,24 +29,12 @@ public:
 
		parent(nullptr),
 
		data(data),
 
		type(type),
 
		num_param(num_param)
 
	{ }
 

	
 
	/** Create a new StringParameters instance. */
 
	template <size_t Tnum_param>
 
	StringParameters(int64 (&data)[Tnum_param]) :
 
		parent(nullptr),
 
		data((uint64 *)data),
 
		type(nullptr),
 
		offset(0),
 
		num_param(Tnum_param)
 
	{
 
		static_assert(sizeof(data[0]) == sizeof(uint64));
 
	}
 

	
 
	/**
 
	 * Create a new StringParameters instance that can reference part of the data of
 
	 * the given partent instance.
 
	 */
 
	StringParameters(StringParameters &parent, size_t size) :
 
		parent(&parent),
 
@@ -133,20 +122,53 @@ public:
 
	void SetParam(size_t n, uint64 v)
 
	{
 
		assert(n < this->num_param);
 
		this->data[n] = v;
 
	}
 

	
 
	void SetParam(size_t n, const char *str) { this->SetParam(n, (uint64_t)(size_t)str); }
 
	void SetParam(size_t n, const std::string &str) { this->SetParam(n, str.c_str()); }
 
	void SetParam(size_t n, std::string &&str) = delete; // block passing temporaries to SetDParam
 

	
 
	uint64 GetParam(size_t n) const
 
	{
 
		assert(n < this->num_param);
 
		return this->data[n];
 
	}
 
};
 

	
 
/**
 
 * Extension of StringParameters with its own statically allocated buffer for
 
 * the parameters.
 
 */
 
class AllocatedStringParameters : public StringParameters {
 
	std::vector<uint64_t> params; ///< The actual parameters
 

	
 
public:
 
	AllocatedStringParameters(size_t parameters = 0) : StringParameters(nullptr, parameters, nullptr), params(parameters)
 
	{
 
		this->data = params.data();
 
	}
 
};
 

	
 
/**
 
 * Helper to create the StringParameters with its own buffer with the given
 
 * parameter values.
 
 * @param args The parameters to set for the to be created StringParameters.
 
 * @return The constructed StringParameters.
 
 */
 
template <typename... Args>
 
static auto MakeParameters(const Args&... args)
 
{
 
	AllocatedStringParameters parameters(sizeof...(args));
 
	size_t index = 0;
 
	(parameters.SetParam(index++, std::forward<const Args&>(args)), ...);
 
	return parameters;
 
}
 

	
 
/**
 
 * Equivalent to the std::back_insert_iterator in function, with some
 
 * convenience helpers for string concatenation.
 
 */
 
class StringBuilder {
 
	std::string *string;
 

	
src/townname.cpp
Show inline comments
 
@@ -45,14 +45,13 @@ TownNameParams::TownNameParams(const Tow
 
 * @param par           Town name parameters.
 
 * @param townnameparts 'Encoded' town name.
 
 */
 
static void GetTownName(StringBuilder &builder, const TownNameParams *par, uint32 townnameparts)
 
{
 
	if (par->grfid == 0) {
 
		int64 args_array[1] = { townnameparts };
 
		StringParameters tmp_params(args_array);
 
		auto tmp_params = MakeParameters(townnameparts);
 
		GetStringWithArgs(builder, par->type, tmp_params);
 
		return;
 
	}
 

	
 
	GRFTownNameGenerate(builder, par->grfid, par->type, townnameparts);
 
}
0 comments (0 inline, 0 general)