Changeset - r24054:96f94ee4b3e1
[Not reviewed]
master
0 10 0
Jonathan G Rennison - 5 years ago 2020-01-06 20:40:31
j.g.rennison@gmail.com
Codechange: Cache resolved town, station and industry name strings
10 files changed with 95 insertions and 0 deletions:
0 comments (0 inline, 0 general)
src/base_station_base.h
Show inline comments
 
@@ -56,6 +56,7 @@ struct BaseStation : StationPool::PoolIt
 

	
 
	char *name;                     ///< Custom name
 
	StringID string_id;             ///< Default name (town area) of station
 
	mutable std::string cached_name; ///< NOSAVE: Cache of the resolved name of the station, if not using a custom name
 

	
 
	Town *town;                     ///< The town this station is associated with
 
	Owner owner;                    ///< The owner of this station
 
@@ -108,6 +109,13 @@ struct BaseStation : StationPool::PoolIt
 
	 */
 
	virtual void UpdateVirtCoord() = 0;
 

	
 
	inline const char *GetCachedName() const
 
	{
 
		if (this->name != nullptr) return this->name;
 
		if (this->cached_name.empty()) this->FillCachedName();
 
		return this->cached_name.c_str();
 
	}
 

	
 
	virtual void MoveSign(TileIndex new_xy)
 
	{
 
		this->xy = new_xy;
 
@@ -161,6 +169,9 @@ struct BaseStation : StationPool::PoolIt
 
	}
 

	
 
	static void PostDestructor(size_t index);
 

	
 
private:
 
	void FillCachedName() const;
 
};
 

	
 
/**
src/industry.h
Show inline comments
 
@@ -62,6 +62,7 @@ struct Industry : IndustryPool::PoolItem
 

	
 
	PartOfSubsidy part_of_subsidy; ///< NOSAVE: is this industry a source/destination of a subsidy?
 
	StationList stations_near;     ///< NOSAVE: List of nearby stations.
 
	mutable std::string cached_name; ///< NOSAVE: Cache of the resolved name of the industry
 

	
 
	Owner founder;                 ///< Founder of the industry
 
	Date construction_date;        ///< Date of the construction of the industry
 
@@ -157,10 +158,21 @@ struct Industry : IndustryPool::PoolItem
 
		memset(&counts, 0, sizeof(counts));
 
	}
 

	
 
	inline const char *GetCachedName() const
 
	{
 
		if (this->cached_name.empty()) this->FillCachedName();
 
		return this->cached_name.c_str();
 
	}
 

	
 
private:
 
	void FillCachedName() const;
 

	
 
protected:
 
	static uint16 counts[NUM_INDUSTRYTYPES]; ///< Number of industries per type ingame
 
};
 

	
 
void ClearAllIndustryCachedNames();
 

	
 
void PlantRandomFarmField(const Industry *i);
 

	
 
void ReleaseDisastersTargetingIndustry(IndustryID);
src/industry_cmd.cpp
Show inline comments
 
@@ -2319,6 +2319,21 @@ void Industry::RecomputeProductionMultip
 
	}
 
}
 

	
 
void Industry::FillCachedName() const
 
{
 
	char buf[256];
 
	int64 args_array[] = { this->index };
 
	StringParameters tmp_params(args_array);
 
	char *end = GetStringWithArgs(buf, STR_INDUSTRY_NAME, &tmp_params, lastof(buf));
 
	this->cached_name.assign(buf, end);
 
}
 

	
 
void ClearAllIndustryCachedNames()
 
{
 
	for (Industry *ind : Industry::Iterate()) {
 
		ind->cached_name.clear();
 
	}
 
}
 

	
 
/**
 
 * Set the #probability and #min_number fields for the industry type \a it for a running game.
src/saveload/afterload.cpp
Show inline comments
 
@@ -222,6 +222,13 @@ void UpdateAllVirtCoords()
 
	RebuildViewportKdtree();
 
}
 

	
 
void ClearAllCachedNames()
 
{
 
	ClearAllStationCachedNames();
 
	ClearAllTownCachedNames();
 
	ClearAllIndustryCachedNames();
 
}
 

	
 
/**
 
 * Initialization of the windows and several kinds of caches.
 
 * This is not done directly in AfterLoadGame because these
 
@@ -238,6 +245,7 @@ static void InitializeWindowsAndCaches()
 
	SetupColoursAndInitialWindow();
 

	
 
	/* Update coordinates of the signs. */
 
	ClearAllCachedNames();
 
	UpdateAllVirtCoords();
 
	ResetViewportAfterLoadGame();
 

	
src/settings_gui.cpp
Show inline comments
 
@@ -526,6 +526,7 @@ struct GameOptionsWindow : Window {
 
				ReadLanguagePack(&_languages[index]);
 
				DeleteWindowByClass(WC_QUERY_STRING);
 
				CheckForMissingGlyphs();
 
				ClearAllCachedNames();
 
				UpdateAllVirtCoords();
 
				ReInitAllWindows();
 
				break;
src/station_cmd.cpp
Show inline comments
 
@@ -453,6 +453,22 @@ void UpdateAllStationVirtCoords()
 
	}
 
}
 

	
 
void BaseStation::FillCachedName() const
 
{
 
	char buf[MAX_LENGTH_STATION_NAME_CHARS * MAX_CHAR_LENGTH];
 
	int64 args_array[] = { this->index };
 
	StringParameters tmp_params(args_array);
 
	char *end = GetStringWithArgs(buf, Waypoint::IsExpected(this) ? STR_WAYPOINT_NAME : STR_STATION_NAME, &tmp_params, lastof(buf));
 
	this->cached_name.assign(buf, end);
 
}
 

	
 
void ClearAllStationCachedNames()
 
{
 
	for (BaseStation *st : BaseStation::Iterate()) {
 
		st->cached_name.clear();
 
	}
 
}
 

	
 
/**
 
 * Get a mask of the cargo types that the station accepts.
 
 * @param st Station to query
 
@@ -3933,6 +3949,7 @@ CommandCost CmdRenameStation(TileIndex t
 
	}
 

	
 
	if (flags & DC_EXEC) {
 
		st->cached_name.clear();
 
		free(st->name);
 
		st->name = reset ? nullptr : stredup(text);
 

	
src/station_func.h
Show inline comments
 
@@ -26,6 +26,7 @@ void FindStationsAroundTiles(const TileA
 

	
 
void ShowStationViewWindow(StationID station);
 
void UpdateAllStationVirtCoords();
 
void ClearAllStationCachedNames();
 

	
 
CargoArray GetProductionAroundTiles(TileIndex tile, int w, int h, int rad);
 
CargoArray GetAcceptanceAroundTiles(TileIndex tile, int w, int h, int rad, CargoTypes *always_accepted = nullptr);
src/town.h
Show inline comments
 
@@ -60,6 +60,7 @@ struct Town : TownPool::PoolItem<&_town_
 
	uint16 townnametype;
 
	uint32 townnameparts;
 
	char *name;                    ///< Custom town name. If nullptr, the town was not renamed and uses the generated name.
 
	mutable std::string cached_name; ///< NOSAVE: Cache of the resolved name of the town, if not using a custom town name
 

	
 
	byte flags;                    ///< See #TownFlags.
 

	
 
@@ -130,6 +131,13 @@ struct Town : TownPool::PoolItem<&_town_
 

	
 
	void UpdateVirtCoord();
 

	
 
	inline const char *GetCachedName() const
 
	{
 
		if (this->name != nullptr) return this->name;
 
		if (this->cached_name.empty()) this->FillCachedName();
 
		return this->cached_name.c_str();
 
	}
 

	
 
	static inline Town *GetByTile(TileIndex tile)
 
	{
 
		return Town::Get(GetTownIndex(tile));
 
@@ -137,11 +145,15 @@ struct Town : TownPool::PoolItem<&_town_
 

	
 
	static Town *GetRandom();
 
	static void PostDestructor(size_t index);
 

	
 
private:
 
	void FillCachedName() const;
 
};
 

	
 
uint32 GetWorldPopulation();
 

	
 
void UpdateAllTownVirtCoords();
 
void ClearAllTownCachedNames();
 
void ShowTownViewWindow(TownID town);
 
void ExpandTown(Town *t);
 

	
src/town_cmd.cpp
Show inline comments
 
@@ -199,6 +199,13 @@ void Town::InitializeLayout(TownLayout l
 
	return Town::Get(index);
 
}
 

	
 
void Town::FillCachedName() const
 
{
 
	char buf[MAX_LENGTH_TOWN_NAME_CHARS * MAX_CHAR_LENGTH];
 
	char *end = GetTownName(buf, this, lastof(buf));
 
	this->cached_name.assign(buf, end);
 
}
 

	
 
/**
 
 * Get the cost for removing this house
 
 * @return the cost (inflation corrected etc)
 
@@ -412,6 +419,13 @@ void UpdateAllTownVirtCoords()
 
	}
 
}
 

	
 
void ClearAllTownCachedNames()
 
{
 
	for (Town *t : Town::Iterate()) {
 
		t->cached_name.clear();
 
	}
 
}
 

	
 
/**
 
 * Change the towns population
 
 * @param t Town which population has changed
 
@@ -2681,11 +2695,14 @@ CommandCost CmdRenameTown(TileIndex tile
 
	}
 

	
 
	if (flags & DC_EXEC) {
 
		t->cached_name.clear();
 
		free(t->name);
 
		t->name = reset ? nullptr : stredup(text);
 

	
 
		t->UpdateVirtCoord();
 
		InvalidateWindowData(WC_TOWN_DIRECTORY, 0, TDIWD_FORCE_RESORT);
 
		ClearAllStationCachedNames();
 
		ClearAllIndustryCachedNames();
 
		UpdateAllStationVirtCoords();
 
	}
 
	return CommandCost();
src/viewport_func.h
Show inline comments
 
@@ -74,6 +74,7 @@ bool ScrollMainWindowToTile(TileIndex ti
 
bool ScrollMainWindowTo(int x, int y, int z = -1, bool instant = false);
 

	
 
void UpdateAllVirtCoords();
 
void ClearAllCachedNames();
 

	
 
extern Point _tile_fract_coords;
 

	
0 comments (0 inline, 0 general)