1
0
Fork 0

Codefix: Add missing `const` inside script functions. (#12794)

Scripts do not modify items directly, marking them const enforces this.
pull/12796/head
Peter Nelson 2024-06-17 07:19:56 +01:00 committed by GitHub
parent 7db70f9c3f
commit 731c56d116
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
9 changed files with 35 additions and 23 deletions

View File

@ -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; }); 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. * Test if this industry accepts any cargo.
* @return true iff the industry accepts any cargo. * @return true iff the industry accepts any cargo.

View File

@ -28,7 +28,7 @@ ScriptCargoList_IndustryAccepting::ScriptCargoList_IndustryAccepting(IndustryID
{ {
if (!ScriptIndustry::IsValidIndustry(industry_id)) return; 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) { for (const auto &a : ind->accepted) {
if (::IsValidCargoID(a.cargo)) { if (::IsValidCargoID(a.cargo)) {
this->AddItem(a.cargo); this->AddItem(a.cargo);
@ -40,7 +40,7 @@ ScriptCargoList_IndustryProducing::ScriptCargoList_IndustryProducing(IndustryID
{ {
if (!ScriptIndustry::IsValidIndustry(industry_id)) return; 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) { for (const auto &p : ind->produced) {
if (::IsValidCargoID(p.cargo)) { if (::IsValidCargoID(p.cargo)) {
this->AddItem(p.cargo); this->AddItem(p.cargo);
@ -52,7 +52,7 @@ ScriptCargoList_StationAccepting::ScriptCargoList_StationAccepting(StationID sta
{ {
if (!ScriptStation::IsValidStation(station_id)) return; 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++) { for (CargoID i = 0; i < NUM_CARGO; i++) {
if (HasBit(st->goods[i].status, GoodsEntry::GES_ACCEPTANCE)) this->AddItem(i); if (HasBit(st->goods[i].status, GoodsEntry::GES_ACCEPTANCE)) this->AddItem(i);
} }

View File

@ -71,7 +71,7 @@
{ {
EnforceDeityMode(false); EnforceDeityMode(false);
EnforcePrecondition(false, IsValidGoal(goal_id)); 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)); EnforcePrecondition(false, IsValidGoalDestination((ScriptCompany::CompanyID)g->company, type, destination));
return ScriptObject::Command<CMD_SET_GOAL_DESTINATION>::Do(goal_id, (::GoalType)type, destination); return ScriptObject::Command<CMD_SET_GOAL_DESTINATION>::Do(goal_id, (::GoalType)type, destination);
@ -113,7 +113,7 @@
EnforcePrecondition(false, IsValidGoal(goal_id)); EnforcePrecondition(false, IsValidGoal(goal_id));
EnforceDeityMode(false); EnforceDeityMode(false);
Goal *g = Goal::Get(goal_id); const Goal *g = Goal::Get(goal_id);
return g != nullptr && g->completed; return g != nullptr && g->completed;
} }

View File

@ -51,7 +51,7 @@
/* static */ ScriptDate::Date ScriptIndustry::GetConstructionDate(IndustryID industry_id) /* 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; if (i == nullptr) return ScriptDate::DATE_INVALID;
return (ScriptDate::Date)i->construction_date.base(); return (ScriptDate::Date)i->construction_date.base();
} }
@ -71,6 +71,7 @@
if (!IsValidIndustry(industry_id)) return CAS_NOT_ACCEPTED; if (!IsValidIndustry(industry_id)) return CAS_NOT_ACCEPTED;
if (!ScriptCargo::IsValidCargo(cargo_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); Industry *i = ::Industry::Get(industry_id);
if (!i->IsCargoAccepted(cargo_id)) return CAS_NOT_ACCEPTED; if (!i->IsCargoAccepted(cargo_id)) return CAS_NOT_ACCEPTED;
@ -84,7 +85,7 @@
if (!IsValidIndustry(industry_id)) return -1; if (!IsValidIndustry(industry_id)) return -1;
if (!ScriptCargo::IsValidCargo(cargo_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); auto it = i->GetCargoAccepted(cargo_id);
if (it == std::end(i->accepted)) return -1; if (it == std::end(i->accepted)) return -1;
@ -97,7 +98,7 @@
if (!IsValidIndustry(industry_id)) return -1; if (!IsValidIndustry(industry_id)) return -1;
if (!ScriptCargo::IsValidCargo(cargo_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); auto it = i->GetCargoProduced(cargo_id);
if (it == std::end(i->produced)) return -1; if (it == std::end(i->produced)) return -1;
@ -110,7 +111,7 @@
if (!IsValidIndustry(industry_id)) return -1; if (!IsValidIndustry(industry_id)) return -1;
if (!ScriptCargo::IsValidCargo(cargo_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); auto it = i->GetCargoProduced(cargo_id);
if (it == std::end(i->produced)) return -1; if (it == std::end(i->produced)) return -1;
@ -123,7 +124,7 @@
if (!IsValidIndustry(industry_id)) return -1; if (!IsValidIndustry(industry_id)) return -1;
if (!ScriptCargo::IsValidCargo(cargo_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); auto it = i->GetCargoProduced(cargo_id);
if (it == std::end(i->produced)) return -1; if (it == std::end(i->produced)) return -1;
@ -142,7 +143,7 @@
{ {
if (!IsValidIndustry(industry_id)) return -1; if (!IsValidIndustry(industry_id)) return -1;
Industry *ind = ::Industry::Get(industry_id); const Industry *ind = ::Industry::Get(industry_id);
return ind->stations_near.size(); return ind->stations_near.size();
} }
@ -220,14 +221,14 @@
/* static */ SQInteger ScriptIndustry::GetLastProductionYear(IndustryID industry_id) /* 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; if (i == nullptr) return 0;
return i->last_prod_year.base(); return i->last_prod_year.base();
} }
/* static */ ScriptDate::Date ScriptIndustry::GetCargoLastAcceptedDate(IndustryID industry_id, CargoID cargo_type) /* 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 (i == nullptr) return ScriptDate::DATE_INVALID;
if (!::IsValidCargoID(cargo_type)) { if (!::IsValidCargoID(cargo_type)) {
@ -242,7 +243,7 @@
/* static */ SQInteger ScriptIndustry::GetControlFlags(IndustryID industry_id) /* 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; if (i == nullptr) return 0;
return i->ctlflags; return i->ctlflags;
} }
@ -297,7 +298,7 @@
/* static */ SQInteger ScriptIndustry::GetProductionLevel(IndustryID industry_id) /* 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; if (i == nullptr) return 0;
return i->prod_level; return i->prod_level;
} }

View File

@ -39,7 +39,7 @@
company = ScriptCompany::ResolveCompanyID(company); company = ScriptCompany::ResolveCompanyID(company);
if (company == ScriptCompany::COMPANY_INVALID) return 0; if (company == ScriptCompany::COMPANY_INVALID) return 0;
::Company *c = ::Company::Get((::CompanyID)company); const ::Company *c = ::Company::Get((::CompanyID)company);
switch (infra_type) { switch (infra_type) {
case INFRASTRUCTURE_RAIL: case INFRASTRUCTURE_RAIL:
return c->infrastructure.GetRailTotal(); return c->infrastructure.GetRailTotal();
@ -87,7 +87,7 @@
company = ScriptCompany::ResolveCompanyID(company); company = ScriptCompany::ResolveCompanyID(company);
if (company == ScriptCompany::COMPANY_INVALID || !_settings_game.economy.infrastructure_maintenance) return 0; 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) { switch (infra_type) {
case INFRASTRUCTURE_RAIL: { case INFRASTRUCTURE_RAIL: {
Money cost; Money cost;

View File

@ -687,11 +687,11 @@ static void _DoCommandReturnSetOrderFlags(class ScriptInstance *instance)
{ {
if (vehicle_type == ScriptVehicle::VT_AIR) { if (vehicle_type == ScriptVehicle::VT_AIR) {
if (ScriptTile::IsStationTile(origin_tile)) { 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 (orig_station != nullptr && orig_station->airport.tile != INVALID_TILE) origin_tile = orig_station->airport.tile;
} }
if (ScriptTile::IsStationTile(dest_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; if (dest_station != nullptr && dest_station->airport.tile != INVALID_TILE) dest_tile = dest_station->airport.tile;
} }

View File

@ -32,7 +32,7 @@ ScriptStationList_Vehicle::ScriptStationList_Vehicle(VehicleID vehicle_id)
{ {
if (!ScriptVehicle::IsPrimaryVehicle(vehicle_id)) return; 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) { for (Order *o = v->GetFirstOrder(); o != nullptr; o = o->next) {
if (o->IsType(OT_GOTO_STATION)) this->AddItem(o->GetDestination()); if (o->IsType(OT_GOTO_STATION)) this->AddItem(o->GetDestination());

View File

@ -114,8 +114,8 @@ static inline bool StoryPageElementTypeRequiresText(StoryPageElementType type)
EnforceDeityMode(false); EnforceDeityMode(false);
EnforcePrecondition(false, IsValidStoryPageElement(story_page_element_id)); EnforcePrecondition(false, IsValidStoryPageElement(story_page_element_id));
StoryPageElement *pe = StoryPageElement::Get(story_page_element_id); const StoryPageElement *pe = StoryPageElement::Get(story_page_element_id);
StoryPage *p = StoryPage::Get(pe->page); const StoryPage *p = StoryPage::Get(pe->page);
::StoryPageElementType type = pe->type; ::StoryPageElementType type = pe->type;
std::string encoded_text; std::string encoded_text;

View File

@ -462,7 +462,7 @@
{ {
if (!IsPrimaryVehicle(vehicle_id)) return false; 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; return v->orders != nullptr && v->orders->GetNumVehicles() > 1;
} }