diff --git a/src/articulated_vehicles.cpp b/src/articulated_vehicles.cpp index b5faa2b9de..d98656ca6c 100644 --- a/src/articulated_vehicles.cpp +++ b/src/articulated_vehicles.cpp @@ -139,7 +139,7 @@ static inline CargoTypes GetAvailableVehicleCargoTypes(EngineID engine, bool inc */ CargoArray GetCapacityOfArticulatedParts(EngineID engine) { - CargoArray capacity; + CargoArray capacity{}; const Engine *e = Engine::Get(engine); CargoID cargo_type; @@ -284,7 +284,7 @@ void CheckConsistencyOfArticulatedVehicle(const Vehicle *v) 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); diff --git a/src/cargo_type.h b/src/cargo_type.h index 561091f42d..3944909518 100644 --- a/src/cargo_type.h +++ b/src/cargo_type.h @@ -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 { /** * Get the sum of all cargo amounts. * @return The sum. @@ -121,24 +87,16 @@ public: template 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; }); } }; diff --git a/src/company_base.h b/src/company_base.h index dd7521c4a8..88bd49c7c5 100644 --- a/src/company_base.h +++ b/src/company_base.h @@ -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. }; diff --git a/src/depot_gui.cpp b/src/depot_gui.cpp index 49bec452ea..104368a8f3 100644 --- a/src/depot_gui.cpp +++ b/src/depot_gui.cpp @@ -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); diff --git a/src/economy.cpp b/src/economy.cpp index b4bc993f79..b158cc25f1 100644 --- a/src/economy.cpp +++ b/src/economy.cpp @@ -1588,7 +1588,7 @@ static void LoadUnloadVehicle(Vehicle *front) 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, diff --git a/src/misc_gui.cpp b/src/misc_gui.cpp index c130102bd4..cecb68642f 100644 --- a/src/misc_gui.cpp +++ b/src/misc_gui.cpp @@ -167,7 +167,7 @@ public: td.grf = nullptr; - CargoArray acceptance; + CargoArray acceptance{}; AddAcceptedCargo(tile, acceptance, nullptr); GetTileDesc(tile, &td); diff --git a/src/roadveh_gui.cpp b/src/roadveh_gui.cpp index 37e43b506b..6107e80012 100644 --- a/src/roadveh_gui.cpp +++ b/src/roadveh_gui.cpp @@ -39,7 +39,7 @@ void DrawRoadVehDetails(const Vehicle *v, const Rect &r) y += FONT_HEIGHT_NORMAL; if (v->HasArticulatedPart()) { - CargoArray max_cargo; + CargoArray max_cargo{}; StringID subtype_text[NUM_CARGO]; char capacity[512]; diff --git a/src/station_cmd.cpp b/src/station_cmd.cpp index e0cb2369ec..d8d42ee2ac 100644 --- a/src/station_cmd.cpp +++ b/src/station_cmd.cpp @@ -513,7 +513,7 @@ static void ShowRejectOrAcceptNews(const Station *st, uint num_items, CargoID *c */ CargoArray GetProductionAroundTiles(TileIndex north_tile, int w, int h, int rad) { - CargoArray produced; + CargoArray produced{}; std::set industries; TileArea ta = TileArea(north_tile, w, h).Expand(rad); @@ -552,7 +552,7 @@ CargoArray GetProductionAroundTiles(TileIndex north_tile, int w, int h, int rad) */ 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(TileIndex center_tile, int w, int h, int rad */ 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, bool show_msg) CargoTypes old_acc = GetAcceptanceMask(st); /* And retrieve the acceptance. */ - CargoArray acceptance; + CargoArray acceptance{}; if (!st->rect.IsEmpty()) { acceptance = GetAcceptanceAroundStation(st, &st->always_accepted); } diff --git a/src/stdafx.h b/src/stdafx.h index fcc0a82c2b..4056af22a7 100644 --- a/src/stdafx.h +++ b/src/stdafx.h @@ -74,6 +74,7 @@ #include #include #include +#include #include #include #include diff --git a/src/subsidy.cpp b/src/subsidy.cpp index c750aac1bd..14d1829d58 100644 --- a/src/subsidy.cpp +++ b/src/subsidy.cpp @@ -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 cid, SourceType src_type, SourceID src) 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)) { diff --git a/src/train_gui.cpp b/src/train_gui.cpp index b679920644..fd8c34f878 100644 --- a/src/train_gui.cpp +++ b/src/train_gui.cpp @@ -322,8 +322,8 @@ int GetTrainDetailsWndVScroll(VehicleID veh_id, TrainDetailsWindowTabs det_tab) 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, const Rect &r, int vscroll_pos, uint16 vsc } } 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()) { diff --git a/src/vehicle_cmd.cpp b/src/vehicle_cmd.cpp index ed27394d24..7f8ac804a9 100644 --- a/src/vehicle_cmd.cpp +++ b/src/vehicle_cmd.cpp @@ -145,7 +145,7 @@ std::tuple CmdBuildVehicle(DoC 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 CmdBuildVehicle(DoC 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 RefitVehicle(Vehicle *v 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) { diff --git a/src/vehicle_gui.cpp b/src/vehicle_gui.cpp index 345771a84d..240083e211 100644 --- a/src/vehicle_gui.cpp +++ b/src/vehicle_gui.cpp @@ -1375,7 +1375,7 @@ static bool VehicleProfitLastYearSorter(const Vehicle * const &a, const Vehicle 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; diff --git a/src/vehicle_gui.h b/src/vehicle_gui.h index 3a4786fca6..7c6949a090 100644 --- a/src/vehicle_gui.h +++ b/src/vehicle_gui.h @@ -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); };