From a288e4d82ff85819aa49061d66ee7e0cfd87e802 Mon Sep 17 00:00:00 2001 From: frosch Date: Sat, 27 Jun 2009 21:06:58 +0000 Subject: (svn r16678) -Codechange: Turn CargoArray into a class, so one does not have to deal with sizeof() wrt. typedef-ed arrays. --- src/ai/api/ai_tile.cpp | 6 ++---- src/ai/api/ai_tilelist.cpp | 6 ++---- src/cargo_type.h | 28 ++++++++++++++++++++++++++-- src/depot_gui.cpp | 2 -- src/industry_cmd.cpp | 4 ++-- src/misc_gui.cpp | 9 ++++----- src/roadveh_gui.cpp | 1 - src/station_cmd.cpp | 25 +++++++++++-------------- src/station_func.h | 4 ++-- src/tile_cmd.h | 8 ++++---- src/town_cmd.cpp | 4 ++-- src/train_gui.cpp | 12 +++--------- src/unmovable_cmd.cpp | 2 +- src/vehicle_gui.cpp | 1 - 14 files changed, 59 insertions(+), 53 deletions(-) (limited to 'src') diff --git a/src/ai/api/ai_tile.cpp b/src/ai/api/ai_tile.cpp index c7f67f603..ea13fe456 100644 --- a/src/ai/api/ai_tile.cpp +++ b/src/ai/api/ai_tile.cpp @@ -179,8 +179,7 @@ { if (!::IsValidTile(tile) || width <= 0 || height <= 0 || radius <= 0) return -1; - CargoArray acceptance; - ::GetAcceptanceAroundTiles(acceptance, tile, width, height, _settings_game.station.modified_catchment ? radius : (int)CA_UNMODIFIED); + CargoArray acceptance = ::GetAcceptanceAroundTiles(tile, width, height, _settings_game.station.modified_catchment ? radius : (int)CA_UNMODIFIED); return acceptance[cargo_type]; } @@ -188,8 +187,7 @@ { if (!::IsValidTile(tile) || width <= 0 || height <= 0 || radius <= 0) return -1; - CargoArray produced; - ::GetProductionAroundTiles(produced, tile, width, height, _settings_game.station.modified_catchment ? radius : (int)CA_UNMODIFIED); + CargoArray produced = ::GetProductionAroundTiles(tile, width, height, _settings_game.station.modified_catchment ? radius : (int)CA_UNMODIFIED); return produced[cargo_type]; } diff --git a/src/ai/api/ai_tilelist.cpp b/src/ai/api/ai_tilelist.cpp index 38b99ef9a..587460a75 100644 --- a/src/ai/api/ai_tilelist.cpp +++ b/src/ai/api/ai_tilelist.cpp @@ -92,8 +92,7 @@ AITileList_IndustryAccepting::AITileList_IndustryAccepting(IndustryID industry_i /* Only add the tile if it accepts the cargo (sometimes just 1 tile of an * industry triggers the acceptance). */ - CargoArray acceptance; - ::GetAcceptanceAroundTiles(acceptance, cur_tile, 1, 1, radius); + CargoArray acceptance = ::GetAcceptanceAroundTiles(cur_tile, 1, 1, radius); { bool cargo_accepts = false; for (byte j = 0; j < lengthof(i->accepts_cargo); j++) { @@ -130,8 +129,7 @@ AITileList_IndustryProducing::AITileList_IndustryProducing(IndustryID industry_i /* Only add the tile if it produces the cargo (a bug in OpenTTD makes this * inconsitance). */ - CargoArray produced; - ::GetProductionAroundTiles(produced, cur_tile, 1, 1, radius); + CargoArray produced = ::GetProductionAroundTiles(cur_tile, 1, 1, radius); { bool cargo_produces = false; for (byte j = 0; j < lengthof(i->produced_cargo); j++) { diff --git a/src/cargo_type.h b/src/cargo_type.h index 94bc9dbb6..4fd424d35 100644 --- a/src/cargo_type.h +++ b/src/cargo_type.h @@ -55,7 +55,31 @@ enum CargoTypes { CT_INVALID = 0xFF }; -/** Array for storing amounts of cargo */ -typedef uint CargoArray[NUM_CARGO]; +/** Class for storing amounts of cargo */ +struct CargoArray { +private: + uint amount[NUM_CARGO]; + +public: + FORCEINLINE CargoArray() + { + this->Clear(); + } + + FORCEINLINE void Clear() + { + memset(this->amount, 0, sizeof(this->amount)); + } + + FORCEINLINE uint &operator[](CargoID cargo) + { + return this->amount[cargo]; + } + + FORCEINLINE const uint &operator[](CargoID cargo) const + { + return this->amount[cargo]; + } +}; #endif /* CARGO_TYPE_H */ diff --git a/src/depot_gui.cpp b/src/depot_gui.cpp index f6c5117e4..6618d0b40 100644 --- a/src/depot_gui.cpp +++ b/src/depot_gui.cpp @@ -848,8 +848,6 @@ struct DepotWindow : Window { if (v != NULL && mode == MODE_DRAG_VEHICLE) { CargoArray capacity, loaded; - memset(capacity, 0, sizeof(capacity)); - memset(loaded, 0, sizeof(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/industry_cmd.cpp b/src/industry_cmd.cpp index 4e355c738..0f7bf1a7c 100644 --- a/src/industry_cmd.cpp +++ b/src/industry_cmd.cpp @@ -380,7 +380,7 @@ static Foundation GetFoundation_Industry(TileIndex tile, Slope tileh) return FlatteningFoundation(tileh); } -static void AddAcceptedCargo_Industry(TileIndex tile, CargoArray acceptance) +static void AddAcceptedCargo_Industry(TileIndex tile, CargoArray &acceptance) { IndustryGfx gfx = GetIndustryGfx(tile); const IndustryTileSpec *itspec = GetIndustryTileSpec(gfx); @@ -868,7 +868,7 @@ static TrackStatus GetTileTrackStatus_Industry(TileIndex tile, TransportType mod return 0; } -static void AddProducedCargo_Industry(TileIndex tile, CargoArray produced) +static void AddProducedCargo_Industry(TileIndex tile, CargoArray &produced) { const Industry *i = GetIndustryByTile(tile); diff --git a/src/misc_gui.cpp b/src/misc_gui.cpp index 4ebdb53ec..bfdfd7818 100644 --- a/src/misc_gui.cpp +++ b/src/misc_gui.cpp @@ -130,7 +130,6 @@ public: td.grf = NULL; CargoArray acceptance; - memset(acceptance, 0, sizeof(CargoArray)); AddAcceptedCargo(tile, acceptance); GetTileDesc(tile, &td); @@ -811,7 +810,7 @@ void GuiShowTooltips(StringID str, uint paramcount, const uint64 params[], bool } -static int DrawStationCoverageText(const CargoArray cargos, +static int DrawStationCoverageText(const CargoArray &cargos, int str_x, int str_y, StationCoverageType sct, bool supplies) { bool first = true; @@ -863,12 +862,12 @@ static int DrawStationCoverageText(const CargoArray cargos, int DrawStationCoverageAreaText(int sx, int sy, StationCoverageType sct, int rad, bool supplies) { TileIndex tile = TileVirtXY(_thd.pos.x, _thd.pos.y); - CargoArray cargos; if (tile < MapSize()) { + CargoArray cargos; if (supplies) { - GetProductionAroundTiles(cargos, tile, _thd.size.x / TILE_SIZE, _thd.size.y / TILE_SIZE , rad); + cargos = GetProductionAroundTiles(tile, _thd.size.x / TILE_SIZE, _thd.size.y / TILE_SIZE, rad); } else { - GetAcceptanceAroundTiles(cargos, tile, _thd.size.x / TILE_SIZE, _thd.size.y / TILE_SIZE , rad); + cargos = GetAcceptanceAroundTiles(tile, _thd.size.x / TILE_SIZE, _thd.size.y / TILE_SIZE, rad); } return DrawStationCoverageText(cargos, sx, sy, sct, supplies); } diff --git a/src/roadveh_gui.cpp b/src/roadveh_gui.cpp index f81588d13..da079b9f3 100644 --- a/src/roadveh_gui.cpp +++ b/src/roadveh_gui.cpp @@ -38,7 +38,6 @@ void DrawRoadVehDetails(const Vehicle *v, int left, int right, int y) StringID subtype_text[NUM_CARGO]; char capacity[512]; - memset(max_cargo, 0, sizeof(max_cargo)); memset(subtype_text, 0, sizeof(subtype_text)); for (const Vehicle *u = v; u != NULL; u = u->Next()) { diff --git a/src/station_cmd.cpp b/src/station_cmd.cpp index 46c8428ee..bd2c41dfe 100644 --- a/src/station_cmd.cpp +++ b/src/station_cmd.cpp @@ -434,17 +434,15 @@ static void ShowRejectOrAcceptNews(const Station *st, uint num_items, CargoID *c } /** - * Get a list of the cargo types being produced around the tile (in a rectangle). - * @param produced Destination array of produced cargo + * Get the cargo types being produced around the tile (in a rectangle). * @param tile Northtile of area * @param w X extent of the area * @param h Y extent of the area * @param rad Search radius in addition to the given area */ -void GetProductionAroundTiles(CargoArray produced, TileIndex tile, - int w, int h, int rad) +CargoArray GetProductionAroundTiles(TileIndex tile, int w, int h, int rad) { - memset(produced, 0, sizeof(CargoArray)); // sizeof(CargoArray) != sizeof(produced) (== sizeof(uint *)) + CargoArray produced; int x = TileX(tile); int y = TileY(tile); @@ -468,20 +466,20 @@ void GetProductionAroundTiles(CargoArray produced, TileIndex tile, AddProducedCargo(tile, produced); } } + + return produced; } /** - * Get a list of the cargo types that are accepted around the tile. - * @param accepts Destination array of accepted cargo + * Get the acceptance of cargos around the tile in 1/8. * @param tile Center of the search area * @param w X extent of area * @param h Y extent of area * @param rad Search radius in addition to given area */ -void GetAcceptanceAroundTiles(CargoArray acceptance, TileIndex tile, - int w, int h, int rad) +CargoArray GetAcceptanceAroundTiles(TileIndex tile, int w, int h, int rad) { - memset(acceptance, 0, sizeof(CargoArray)); // sizeof(CargoArray) != sizeof(acceptance) (== sizeof(uint *)) + CargoArray acceptance; int x = TileX(tile); int y = TileY(tile); @@ -504,6 +502,8 @@ void GetAcceptanceAroundTiles(CargoArray acceptance, TileIndex tile, AddAcceptedCargo(tile, acceptance); } } + + return acceptance; } /** Update the acceptance for a station. @@ -521,15 +521,12 @@ static void UpdateStationAcceptance(Station *st, bool show_msg) /* And retrieve the acceptance. */ CargoArray acceptance; if (!st->rect.IsEmpty()) { - GetAcceptanceAroundTiles( - acceptance, + acceptance = GetAcceptanceAroundTiles( TileXY(st->rect.left, st->rect.top), st->rect.right - st->rect.left + 1, st->rect.bottom - st->rect.top + 1, st->GetCatchmentRadius() ); - } else { - memset(acceptance, 0, sizeof(acceptance)); } /* Adjust in case our station only accepts fewer kinds of goods */ diff --git a/src/station_func.h b/src/station_func.h index ec7af7dd4..1f1ea408b 100644 --- a/src/station_func.h +++ b/src/station_func.h @@ -22,8 +22,8 @@ void FindStationsAroundTiles(TileIndex tile, int w_prod, int h_prod, StationList void ShowStationViewWindow(StationID station); void UpdateAllStationVirtCoord(); -void GetProductionAroundTiles(CargoArray produced, TileIndex tile, int w, int h, int rad); -void GetAcceptanceAroundTiles(CargoArray acceptance, TileIndex tile, int w, int h, int rad); +CargoArray GetProductionAroundTiles(TileIndex tile, int w, int h, int rad); +CargoArray GetAcceptanceAroundTiles(TileIndex tile, int w, int h, int rad); const DrawTileSprites *GetStationTileLayout(StationType st, byte gfx); void StationPickerDrawSprite(int x, int y, StationType st, RailType railtype, RoadType roadtype, int image); diff --git a/src/tile_cmd.h b/src/tile_cmd.h index 66787f303..1b6c44128 100644 --- a/src/tile_cmd.h +++ b/src/tile_cmd.h @@ -74,7 +74,7 @@ typedef CommandCost ClearTileProc(TileIndex tile, DoCommandFlag flags); * @param tile Tile queried for its accepted cargo * @param acceptance Storage destination of the cargo acceptance in 1/8 */ -typedef void AddAcceptedCargoProc(TileIndex tile, CargoArray acceptance); +typedef void AddAcceptedCargoProc(TileIndex tile, CargoArray &acceptance); /** * Tile callback function signature for obtaining a tile description @@ -103,7 +103,7 @@ typedef TrackStatus GetTileTrackStatusProc(TileIndex tile, TransportType mode, u * @param tile Tile being queried * @param produced Destination array for produced cargo */ -typedef void AddProducedCargoProc(TileIndex tile, CargoArray produced); +typedef void AddProducedCargoProc(TileIndex tile, CargoArray &produced); typedef bool ClickTileProc(TileIndex tile); typedef void AnimateTileProc(TileIndex tile); typedef void TileLoopProc(TileIndex tile); @@ -157,14 +157,14 @@ VehicleEnterTileStatus VehicleEnterTile(Vehicle *v, TileIndex tile, int x, int y void ChangeTileOwner(TileIndex tile, Owner old_owner, Owner new_owner); void GetTileDesc(TileIndex tile, TileDesc *td); -static inline void AddAcceptedCargo(TileIndex tile, CargoArray acceptance) +static inline void AddAcceptedCargo(TileIndex tile, CargoArray &acceptance) { AddAcceptedCargoProc *proc = _tile_type_procs[GetTileType(tile)]->add_accepted_cargo_proc; if (proc == NULL) return; proc(tile, acceptance); } -static inline void AddProducedCargo(TileIndex tile, CargoArray produced) +static inline void AddProducedCargo(TileIndex tile, CargoArray &produced) { AddProducedCargoProc *proc = _tile_type_procs[GetTileType(tile)]->add_produced_cargo_proc; if (proc == NULL) return; diff --git a/src/town_cmd.cpp b/src/town_cmd.cpp index d22527e5d..edc8db5bd 100644 --- a/src/town_cmd.cpp +++ b/src/town_cmd.cpp @@ -555,7 +555,7 @@ static CommandCost ClearTile_Town(TileIndex tile, DoCommandFlag flags) return cost; } -static void AddProducedCargo_Town(TileIndex tile, CargoArray produced) +static void AddProducedCargo_Town(TileIndex tile, CargoArray &produced) { HouseID house_id = GetHouseType(tile); const HouseSpec *hs = HouseSpec::Get(house_id); @@ -582,7 +582,7 @@ static void AddProducedCargo_Town(TileIndex tile, CargoArray produced) } } -static void AddAcceptedCargo_Town(TileIndex tile, CargoArray acceptance) +static void AddAcceptedCargo_Town(TileIndex tile, CargoArray &acceptance) { const HouseSpec *hs = HouseSpec::Get(GetHouseType(tile)); CargoID accepts[3]; diff --git a/src/train_gui.cpp b/src/train_gui.cpp index aded6e28d..458b51a43 100644 --- a/src/train_gui.cpp +++ b/src/train_gui.cpp @@ -182,14 +182,11 @@ static void TrainDetailsCapacityTab(const Vehicle *v, int left, int right, int y */ int GetTrainDetailsWndVScroll(VehicleID veh_id, TrainDetailsWindowTabs det_tab) { - CargoArray act_cargo; - CargoArray max_cargo; int num = 0; if (det_tab == TDW_TAB_TOTALS) { // Total cargo tab - memset(max_cargo, 0, sizeof(max_cargo)); - memset(act_cargo, 0, sizeof(act_cargo)); - + CargoArray act_cargo; + CargoArray max_cargo; for (const Vehicle *v = Vehicle::Get(veh_id) ; v != NULL ; v = v->Next()) { act_cargo[v->cargo_type] += v->cargo.Count(); max_cargo[v->cargo_type] += v->cargo_cap; @@ -274,10 +271,7 @@ void DrawTrainDetails(const Vehicle *v, int left, int right, int y, int vscroll_ } else { CargoArray act_cargo; CargoArray max_cargo; - Money feeder_share = 0; - - memset(max_cargo, 0, sizeof(max_cargo)); - memset(act_cargo, 0, sizeof(act_cargo)); + Money feeder_share = 0; for (const Vehicle *u = v; u != NULL ; u = u->Next()) { act_cargo[u->cargo_type] += u->cargo.Count(); diff --git a/src/unmovable_cmd.cpp b/src/unmovable_cmd.cpp index ef2f89990..15170b90e 100644 --- a/src/unmovable_cmd.cpp +++ b/src/unmovable_cmd.cpp @@ -291,7 +291,7 @@ static CommandCost ClearTile_Unmovable(TileIndex tile, DoCommandFlag flags) return CommandCost(); } -static void AddAcceptedCargo_Unmovable(TileIndex tile, CargoArray acceptance) +static void AddAcceptedCargo_Unmovable(TileIndex tile, CargoArray &acceptance) { if (!IsCompanyHQ(tile)) return; diff --git a/src/vehicle_gui.cpp b/src/vehicle_gui.cpp index 9d7542071..9813f8696 100644 --- a/src/vehicle_gui.cpp +++ b/src/vehicle_gui.cpp @@ -577,7 +577,6 @@ static int CDECL VehicleCargoSorter(const Vehicle * const *a, const Vehicle * co { const Vehicle *v; CargoArray diff; - memset(diff, 0, sizeof(diff)); /* Append the cargo of the connected weagons */ for (v = *a; v != NULL; v = v->Next()) diff[v->cargo_type] += v->cargo_cap; -- cgit v1.2.3-70-g09d2