Changeset - r27424:6a028979ce01
[Not reviewed]
master
0 14 0
Peter Nelson - 18 months ago 2023-05-23 11:23:50
peter1138@openttd.org
Codechange: Base CargoArray off std::array.

This avoids needing to define array accessors and allows use of
default value initialization.
14 files changed with 26 insertions and 68 deletions:
0 comments (0 inline, 0 general)
src/articulated_vehicles.cpp
Show inline comments
 
@@ -139,7 +139,7 @@ static inline CargoTypes GetAvailableVeh
 
 */
 
CargoArray GetCapacityOfArticulatedParts(EngineID engine)
 
{
 
	CargoArray capacity;
 
	CargoArray capacity{};
 
	const Engine *e = Engine::Get(engine);
 

	
 
	CargoID cargo_type;
 
@@ -284,7 +284,7 @@ void CheckConsistencyOfArticulatedVehicl
 

	
 
	CargoTypes real_refit_union = 0;
 
	CargoTypes real_refit_intersection = ALL_CARGOTYPES;
 
	CargoArray real_default_capacity;
 
	CargoArray real_default_capacity{};
 

	
 
	do {
 
		CargoTypes refit_mask = GetAvailableVehicleCargoTypes(v->engine_type, true);
src/cargo_type.h
Show inline comments
 
@@ -79,41 +79,7 @@ typedef uint64 CargoTypes;
 
static const CargoTypes ALL_CARGOTYPES = (CargoTypes)UINT64_MAX;
 

	
 
/** Class for storing amounts of cargo */
 
struct CargoArray {
 
private:
 
	uint amount[NUM_CARGO]; ///< Amount of each type of cargo.
 

	
 
public:
 
	/** Default constructor. */
 
	inline CargoArray()
 
	{
 
		this->Clear();
 
	}
 

	
 
	/** Reset all entries. */
 
	inline void Clear()
 
	{
 
		memset(this->amount, 0, sizeof(this->amount));
 
	}
 

	
 
	/**
 
	 * Read/write access to an amount of a specific cargo type.
 
	 * @param cargo Cargo type to access.
 
	 */
 
	inline uint &operator[](CargoID cargo)
 
	{
 
		return this->amount[cargo];
 
	}
 

	
 
	/**
 
	 * Read-only access to an amount of a specific cargo type.
 
	 * @param cargo Cargo type to access.
 
	 */
 
	inline const uint &operator[](CargoID cargo) const
 
	{
 
		return this->amount[cargo];
 
	}
 

	
 
struct CargoArray : std::array<uint, NUM_CARGO> {
 
	/**
 
	 * Get the sum of all cargo amounts.
 
	 * @return The sum.
 
@@ -121,24 +87,16 @@ public:
 
	template <typename T>
 
	inline const T GetSum() const
 
	{
 
		T ret = 0;
 
		for (size_t i = 0; i < lengthof(this->amount); i++) {
 
			ret += this->amount[i];
 
		}
 
		return ret;
 
		return std::reduce(this->begin(), this->end(), T{});
 
	}
 

	
 
	/**
 
	 * Get the amount of cargos that have an amount.
 
	 * @return The amount.
 
	 */
 
	inline byte GetCount() const
 
	inline uint GetCount() const
 
	{
 
		byte count = 0;
 
		for (size_t i = 0; i < lengthof(this->amount); i++) {
 
			if (this->amount[i] != 0) count++;
 
		}
 
		return count;
 
		return std::count_if(this->begin(), this->end(), [](uint amount) { return amount != 0; });
 
	}
 
};
 

	
src/company_base.h
Show inline comments
 
@@ -22,7 +22,7 @@
 
struct CompanyEconomyEntry {
 
	Money income;               ///< The amount of income.
 
	Money expenses;             ///< The amount of expenses.
 
	CargoArray delivered_cargo; ///< The amount of delivered cargo.
 
	CargoArray delivered_cargo{}; ///< The amount of delivered cargo.
 
	int32 performance_history;  ///< Company score (scale 0-1000)
 
	Money company_value;        ///< The value of the company.
 
};
src/depot_gui.cpp
Show inline comments
 
@@ -855,7 +855,7 @@ struct DepotWindow : Window {
 

	
 
		if (v == nullptr || mode != MODE_DRAG_VEHICLE) return false;
 

	
 
		CargoArray capacity, loaded;
 
		CargoArray capacity{}, loaded{};
 

	
 
		/* Display info for single (articulated) vehicle, or for whole chain starting with selected vehicle */
 
		bool whole_chain = (this->type == VEH_TRAIN && _ctrl_pressed);
src/economy.cpp
Show inline comments
 
@@ -1588,7 +1588,7 @@ static void LoadUnloadVehicle(Vehicle *f
 

	
 
	StationIDStack next_station = front->GetNextStoppingStation();
 
	bool use_autorefit = front->current_order.IsRefit() && front->current_order.GetRefitCargo() == CT_AUTO_REFIT;
 
	CargoArray consist_capleft;
 
	CargoArray consist_capleft{};
 
	if (_settings_game.order.improved_load && use_autorefit ?
 
			front->cargo_payment == nullptr : (front->current_order.GetLoadType() & OLFB_FULL_LOAD) != 0) {
 
		ReserveConsist(st, front,
src/misc_gui.cpp
Show inline comments
 
@@ -167,7 +167,7 @@ public:
 

	
 
		td.grf = nullptr;
 

	
 
		CargoArray acceptance;
 
		CargoArray acceptance{};
 
		AddAcceptedCargo(tile, acceptance, nullptr);
 
		GetTileDesc(tile, &td);
 

	
src/roadveh_gui.cpp
Show inline comments
 
@@ -39,7 +39,7 @@ void DrawRoadVehDetails(const Vehicle *v
 
	y += FONT_HEIGHT_NORMAL;
 

	
 
	if (v->HasArticulatedPart()) {
 
		CargoArray max_cargo;
 
		CargoArray max_cargo{};
 
		StringID subtype_text[NUM_CARGO];
 
		char capacity[512];
 

	
src/station_cmd.cpp
Show inline comments
 
@@ -513,7 +513,7 @@ static void ShowRejectOrAcceptNews(const
 
 */
 
CargoArray GetProductionAroundTiles(TileIndex north_tile, int w, int h, int rad)
 
{
 
	CargoArray produced;
 
	CargoArray produced{};
 
	std::set<IndustryID> industries;
 
	TileArea ta = TileArea(north_tile, w, h).Expand(rad);
 

	
 
@@ -552,7 +552,7 @@ CargoArray GetProductionAroundTiles(Tile
 
 */
 
CargoArray GetAcceptanceAroundTiles(TileIndex center_tile, int w, int h, int rad, CargoTypes *always_accepted)
 
{
 
	CargoArray acceptance;
 
	CargoArray acceptance{};
 
	if (always_accepted != nullptr) *always_accepted = 0;
 

	
 
	TileArea ta = TileArea(center_tile, w, h).Expand(rad);
 
@@ -574,7 +574,7 @@ CargoArray GetAcceptanceAroundTiles(Tile
 
 */
 
static CargoArray GetAcceptanceAroundStation(const Station *st, CargoTypes *always_accepted)
 
{
 
	CargoArray acceptance;
 
	CargoArray acceptance{};
 
	if (always_accepted != nullptr) *always_accepted = 0;
 

	
 
	BitmapTileIterator it(st->catchment_tiles);
 
@@ -596,7 +596,7 @@ void UpdateStationAcceptance(Station *st
 
	CargoTypes old_acc = GetAcceptanceMask(st);
 

	
 
	/* And retrieve the acceptance. */
 
	CargoArray acceptance;
 
	CargoArray acceptance{};
 
	if (!st->rect.IsEmpty()) {
 
		acceptance = GetAcceptanceAroundStation(st, &st->always_accepted);
 
	}
src/stdafx.h
Show inline comments
 
@@ -74,6 +74,7 @@
 
#include <limits>
 
#include <map>
 
#include <memory>
 
#include <numeric>
 
#include <optional>
 
#include <set>
 
#include <stdexcept>
src/subsidy.cpp
Show inline comments
 
@@ -323,7 +323,7 @@ bool FindSubsidyTownCargoRoute()
 
	if (src_town->cache.population < SUBSIDY_CARGO_MIN_POPULATION) return false;
 

	
 
	/* Calculate the produced cargo of houses around town center. */
 
	CargoArray town_cargo_produced;
 
	CargoArray town_cargo_produced{};
 
	TileArea ta = TileArea(src_town->xy, 1, 1).Expand(SUBSIDY_TOWN_CARGO_RADIUS);
 
	for (TileIndex tile : ta) {
 
		if (IsTileType(tile, MP_HOUSE)) {
 
@@ -431,7 +431,7 @@ bool FindSubsidyCargoDestination(CargoID
 
			const Town *dst_town = Town::GetRandom();
 

	
 
			/* Calculate cargo acceptance of houses around town center. */
 
			CargoArray town_cargo_accepted;
 
			CargoArray town_cargo_accepted{};
 
			TileArea ta = TileArea(dst_town->xy, 1, 1).Expand(SUBSIDY_TOWN_CARGO_RADIUS);
 
			for (TileIndex tile : ta) {
 
				if (IsTileType(tile, MP_HOUSE)) {
src/train_gui.cpp
Show inline comments
 
@@ -322,8 +322,8 @@ int GetTrainDetailsWndVScroll(VehicleID 
 
	int num = 0;
 

	
 
	if (det_tab == TDW_TAB_TOTALS) { // Total cargo tab
 
		CargoArray act_cargo;
 
		CargoArray max_cargo;
 
		CargoArray act_cargo{};
 
		CargoArray max_cargo{};
 
		for (const Vehicle *v = Vehicle::Get(veh_id); v != nullptr; v = v->Next()) {
 
			act_cargo[v->cargo_type] += v->cargo.StoredCount();
 
			max_cargo[v->cargo_type] += v->cargo_cap;
 
@@ -435,8 +435,8 @@ void DrawTrainDetails(const Train *v, co
 
		}
 
	} else {
 
		int y = r.top;
 
		CargoArray act_cargo;
 
		CargoArray max_cargo;
 
		CargoArray act_cargo{};
 
		CargoArray max_cargo{};
 
		Money feeder_share = 0;
 

	
 
		for (const Vehicle *u = v; u != nullptr; u = u->Next()) {
src/vehicle_cmd.cpp
Show inline comments
 
@@ -145,7 +145,7 @@ std::tuple<CommandCost, VehicleID, uint,
 
	VehicleID veh_id = INVALID_VEHICLE;
 
	uint refitted_capacity = 0;
 
	uint16 refitted_mail_capacity = 0;
 
	CargoArray cargo_capacities;
 
	CargoArray cargo_capacities{};
 
	if (value.Succeeded()) {
 
		if (subflags & DC_EXEC) {
 
			v->unitnumber = unit_num;
 
@@ -166,7 +166,6 @@ std::tuple<CommandCost, VehicleID, uint,
 
				refitted_mail_capacity = 0;
 
			} else {
 
				refitted_capacity = e->GetDisplayDefaultCapacity(&refitted_mail_capacity);
 
				cargo_capacities.Clear();
 
				cargo_capacities[default_cargo] = refitted_capacity;
 
				cargo_capacities[CT_MAIL] = refitted_mail_capacity;
 
			}
 
@@ -355,7 +354,7 @@ static std::tuple<CommandCost, uint, uin
 
	uint total_capacity = 0;
 
	uint total_mail_capacity = 0;
 
	num_vehicles = num_vehicles == 0 ? UINT8_MAX : num_vehicles;
 
	CargoArray cargo_capacities;
 
	CargoArray cargo_capacities{};
 

	
 
	VehicleSet vehicles_to_refit;
 
	if (!only_this) {
src/vehicle_gui.cpp
Show inline comments
 
@@ -1375,7 +1375,7 @@ static bool VehicleProfitLastYearSorter(
 
static bool VehicleCargoSorter(const Vehicle * const &a, const Vehicle * const &b)
 
{
 
	const Vehicle *v;
 
	CargoArray diff;
 
	CargoArray diff{};
 

	
 
	/* Append the cargo of the connected waggons */
 
	for (v = a; v != nullptr; v = v->Next()) diff[v->cargo_type] += v->cargo_cap;
src/vehicle_gui.h
Show inline comments
 
@@ -43,7 +43,7 @@ struct TestedEngineDetails {
 
	CargoID cargo;        ///< Cargo type
 
	uint capacity;        ///< Cargo capacity
 
	uint16 mail_capacity; ///< Mail capacity if available
 
	CargoArray all_capacities; ///< Capacities for all cargoes
 
	CargoArray all_capacities{}; ///< Capacities for all cargoes
 

	
 
	void FillDefaultCapacities(const Engine *e);
 
};
0 comments (0 inline, 0 general)