From 731c56d1168ec92b1676c99af1f6aa63bedabd50 Mon Sep 17 00:00:00 2001 From: Peter Nelson Date: Mon, 17 Jun 2024 07:19:56 +0100 Subject: [PATCH] Codefix: Add missing `const` inside script functions. (#12794) Scripts do not modify items directly, marking them const enforces this. --- src/industry.h | 11 +++++++++++ src/script/api/script_cargolist.cpp | 6 +++--- src/script/api/script_goal.cpp | 4 ++-- src/script/api/script_industry.cpp | 21 +++++++++++---------- src/script/api/script_infrastructure.cpp | 4 ++-- src/script/api/script_order.cpp | 4 ++-- src/script/api/script_stationlist.cpp | 2 +- src/script/api/script_story_page.cpp | 4 ++-- src/script/api/script_vehicle.cpp | 2 +- 9 files changed, 35 insertions(+), 23 deletions(-) diff --git a/src/industry.h b/src/industry.h index 24fe0cef8f..6c23927104 100644 --- a/src/industry.h +++ b/src/industry.h @@ -194,6 +194,17 @@ struct Industry : IndustryPool::PoolItem<&_industry_pool> { return std::find_if(std::begin(this->accepted), std::end(this->accepted), [&cargo](const auto &a) { return a.cargo == cargo; }); } + /** + * Get accepted cargo slot for a specific cargo type (const-variant). + * @param cargo CargoID to find. + * @return Iterator pointing to accepted cargo slot if it exists, or the end iterator. + */ + inline AcceptedCargoes::const_iterator GetCargoAccepted(CargoID cargo) const + { + if (!IsValidCargoID(cargo)) return std::end(this->accepted); + return std::find_if(std::begin(this->accepted), std::end(this->accepted), [&cargo](const auto &a) { return a.cargo == cargo; }); + } + /** * Test if this industry accepts any cargo. * @return true iff the industry accepts any cargo. diff --git a/src/script/api/script_cargolist.cpp b/src/script/api/script_cargolist.cpp index 14fe8ed935..5e10e2e3b7 100644 --- a/src/script/api/script_cargolist.cpp +++ b/src/script/api/script_cargolist.cpp @@ -28,7 +28,7 @@ ScriptCargoList_IndustryAccepting::ScriptCargoList_IndustryAccepting(IndustryID { if (!ScriptIndustry::IsValidIndustry(industry_id)) return; - Industry *ind = ::Industry::Get(industry_id); + const Industry *ind = ::Industry::Get(industry_id); for (const auto &a : ind->accepted) { if (::IsValidCargoID(a.cargo)) { this->AddItem(a.cargo); @@ -40,7 +40,7 @@ ScriptCargoList_IndustryProducing::ScriptCargoList_IndustryProducing(IndustryID { if (!ScriptIndustry::IsValidIndustry(industry_id)) return; - Industry *ind = ::Industry::Get(industry_id); + const Industry *ind = ::Industry::Get(industry_id); for (const auto &p : ind->produced) { if (::IsValidCargoID(p.cargo)) { this->AddItem(p.cargo); @@ -52,7 +52,7 @@ ScriptCargoList_StationAccepting::ScriptCargoList_StationAccepting(StationID sta { if (!ScriptStation::IsValidStation(station_id)) return; - Station *st = ::Station::Get(station_id); + const Station *st = ::Station::Get(station_id); for (CargoID i = 0; i < NUM_CARGO; i++) { if (HasBit(st->goods[i].status, GoodsEntry::GES_ACCEPTANCE)) this->AddItem(i); } diff --git a/src/script/api/script_goal.cpp b/src/script/api/script_goal.cpp index bcd8b4eddf..416a8df295 100644 --- a/src/script/api/script_goal.cpp +++ b/src/script/api/script_goal.cpp @@ -71,7 +71,7 @@ { EnforceDeityMode(false); EnforcePrecondition(false, IsValidGoal(goal_id)); - Goal *g = Goal::Get(goal_id); + const Goal *g = Goal::Get(goal_id); EnforcePrecondition(false, IsValidGoalDestination((ScriptCompany::CompanyID)g->company, type, destination)); return ScriptObject::Command::Do(goal_id, (::GoalType)type, destination); @@ -113,7 +113,7 @@ EnforcePrecondition(false, IsValidGoal(goal_id)); EnforceDeityMode(false); - Goal *g = Goal::Get(goal_id); + const Goal *g = Goal::Get(goal_id); return g != nullptr && g->completed; } diff --git a/src/script/api/script_industry.cpp b/src/script/api/script_industry.cpp index f55948d91b..79e89a075f 100644 --- a/src/script/api/script_industry.cpp +++ b/src/script/api/script_industry.cpp @@ -51,7 +51,7 @@ /* static */ ScriptDate::Date ScriptIndustry::GetConstructionDate(IndustryID industry_id) { - Industry *i = Industry::GetIfValid(industry_id); + const Industry *i = Industry::GetIfValid(industry_id); if (i == nullptr) return ScriptDate::DATE_INVALID; return (ScriptDate::Date)i->construction_date.base(); } @@ -71,6 +71,7 @@ if (!IsValidIndustry(industry_id)) return CAS_NOT_ACCEPTED; if (!ScriptCargo::IsValidCargo(cargo_id)) return CAS_NOT_ACCEPTED; + /* Not const because IndustryTemporarilyRefusesCargo tests a callback which needs a non-const object. */ Industry *i = ::Industry::Get(industry_id); if (!i->IsCargoAccepted(cargo_id)) return CAS_NOT_ACCEPTED; @@ -84,7 +85,7 @@ if (!IsValidIndustry(industry_id)) return -1; if (!ScriptCargo::IsValidCargo(cargo_id)) return -1; - Industry *i = ::Industry::Get(industry_id); + const Industry *i = ::Industry::Get(industry_id); auto it = i->GetCargoAccepted(cargo_id); if (it == std::end(i->accepted)) return -1; @@ -97,7 +98,7 @@ if (!IsValidIndustry(industry_id)) return -1; if (!ScriptCargo::IsValidCargo(cargo_id)) return -1; - Industry *i = ::Industry::Get(industry_id); + const Industry *i = ::Industry::Get(industry_id); auto it = i->GetCargoProduced(cargo_id); if (it == std::end(i->produced)) return -1; @@ -110,7 +111,7 @@ if (!IsValidIndustry(industry_id)) return -1; if (!ScriptCargo::IsValidCargo(cargo_id)) return -1; - Industry *i = ::Industry::Get(industry_id); + const Industry *i = ::Industry::Get(industry_id); auto it = i->GetCargoProduced(cargo_id); if (it == std::end(i->produced)) return -1; @@ -123,7 +124,7 @@ if (!IsValidIndustry(industry_id)) return -1; if (!ScriptCargo::IsValidCargo(cargo_id)) return -1; - Industry *i = ::Industry::Get(industry_id); + const Industry *i = ::Industry::Get(industry_id); auto it = i->GetCargoProduced(cargo_id); if (it == std::end(i->produced)) return -1; @@ -142,7 +143,7 @@ { if (!IsValidIndustry(industry_id)) return -1; - Industry *ind = ::Industry::Get(industry_id); + const Industry *ind = ::Industry::Get(industry_id); return ind->stations_near.size(); } @@ -220,14 +221,14 @@ /* static */ SQInteger ScriptIndustry::GetLastProductionYear(IndustryID industry_id) { - Industry *i = Industry::GetIfValid(industry_id); + const Industry *i = Industry::GetIfValid(industry_id); if (i == nullptr) return 0; return i->last_prod_year.base(); } /* static */ ScriptDate::Date ScriptIndustry::GetCargoLastAcceptedDate(IndustryID industry_id, CargoID cargo_type) { - Industry *i = Industry::GetIfValid(industry_id); + const Industry *i = Industry::GetIfValid(industry_id); if (i == nullptr) return ScriptDate::DATE_INVALID; if (!::IsValidCargoID(cargo_type)) { @@ -242,7 +243,7 @@ /* static */ SQInteger ScriptIndustry::GetControlFlags(IndustryID industry_id) { - Industry *i = Industry::GetIfValid(industry_id); + const Industry *i = Industry::GetIfValid(industry_id); if (i == nullptr) return 0; return i->ctlflags; } @@ -297,7 +298,7 @@ /* static */ SQInteger ScriptIndustry::GetProductionLevel(IndustryID industry_id) { - Industry *i = Industry::GetIfValid(industry_id); + const Industry *i = Industry::GetIfValid(industry_id); if (i == nullptr) return 0; return i->prod_level; } diff --git a/src/script/api/script_infrastructure.cpp b/src/script/api/script_infrastructure.cpp index 3d6bdd2fcc..a8d0e05510 100644 --- a/src/script/api/script_infrastructure.cpp +++ b/src/script/api/script_infrastructure.cpp @@ -39,7 +39,7 @@ company = ScriptCompany::ResolveCompanyID(company); if (company == ScriptCompany::COMPANY_INVALID) return 0; - ::Company *c = ::Company::Get((::CompanyID)company); + const ::Company *c = ::Company::Get((::CompanyID)company); switch (infra_type) { case INFRASTRUCTURE_RAIL: return c->infrastructure.GetRailTotal(); @@ -87,7 +87,7 @@ company = ScriptCompany::ResolveCompanyID(company); if (company == ScriptCompany::COMPANY_INVALID || !_settings_game.economy.infrastructure_maintenance) return 0; - ::Company *c = ::Company::Get((::CompanyID)company); + const ::Company *c = ::Company::Get((::CompanyID)company); switch (infra_type) { case INFRASTRUCTURE_RAIL: { Money cost; diff --git a/src/script/api/script_order.cpp b/src/script/api/script_order.cpp index 554e3ecfab..b68f7fd67a 100644 --- a/src/script/api/script_order.cpp +++ b/src/script/api/script_order.cpp @@ -687,11 +687,11 @@ static void _DoCommandReturnSetOrderFlags(class ScriptInstance *instance) { if (vehicle_type == ScriptVehicle::VT_AIR) { if (ScriptTile::IsStationTile(origin_tile)) { - Station *orig_station = ::Station::GetByTile(origin_tile); + const Station *orig_station = ::Station::GetByTile(origin_tile); if (orig_station != nullptr && orig_station->airport.tile != INVALID_TILE) origin_tile = orig_station->airport.tile; } if (ScriptTile::IsStationTile(dest_tile)) { - Station *dest_station = ::Station::GetByTile(dest_tile); + const Station *dest_station = ::Station::GetByTile(dest_tile); if (dest_station != nullptr && dest_station->airport.tile != INVALID_TILE) dest_tile = dest_station->airport.tile; } diff --git a/src/script/api/script_stationlist.cpp b/src/script/api/script_stationlist.cpp index c7038c1ca1..04bb8c07b1 100644 --- a/src/script/api/script_stationlist.cpp +++ b/src/script/api/script_stationlist.cpp @@ -32,7 +32,7 @@ ScriptStationList_Vehicle::ScriptStationList_Vehicle(VehicleID vehicle_id) { if (!ScriptVehicle::IsPrimaryVehicle(vehicle_id)) return; - Vehicle *v = ::Vehicle::Get(vehicle_id); + const Vehicle *v = ::Vehicle::Get(vehicle_id); for (Order *o = v->GetFirstOrder(); o != nullptr; o = o->next) { if (o->IsType(OT_GOTO_STATION)) this->AddItem(o->GetDestination()); diff --git a/src/script/api/script_story_page.cpp b/src/script/api/script_story_page.cpp index d2f963e76a..0476c16ef1 100644 --- a/src/script/api/script_story_page.cpp +++ b/src/script/api/script_story_page.cpp @@ -114,8 +114,8 @@ static inline bool StoryPageElementTypeRequiresText(StoryPageElementType type) EnforceDeityMode(false); EnforcePrecondition(false, IsValidStoryPageElement(story_page_element_id)); - StoryPageElement *pe = StoryPageElement::Get(story_page_element_id); - StoryPage *p = StoryPage::Get(pe->page); + const StoryPageElement *pe = StoryPageElement::Get(story_page_element_id); + const StoryPage *p = StoryPage::Get(pe->page); ::StoryPageElementType type = pe->type; std::string encoded_text; diff --git a/src/script/api/script_vehicle.cpp b/src/script/api/script_vehicle.cpp index 1fd0857bc4..35a6d74dda 100644 --- a/src/script/api/script_vehicle.cpp +++ b/src/script/api/script_vehicle.cpp @@ -462,7 +462,7 @@ { if (!IsPrimaryVehicle(vehicle_id)) return false; - Vehicle *v = ::Vehicle::Get(vehicle_id); + const Vehicle *v = ::Vehicle::Get(vehicle_id); return v->orders != nullptr && v->orders->GetNumVehicles() > 1; }