diff --git a/src/newgrf_airport.cpp b/src/newgrf_airport.cpp index bdac26abaf..b50246bbe7 100644 --- a/src/newgrf_airport.cpp +++ b/src/newgrf_airport.cpp @@ -219,7 +219,7 @@ uint32_t AirportResolverObject::GetDebugID() const */ TownScopeResolver *AirportResolverObject::GetTown() { - if (!this->town_scope) { + if (!this->town_scope.has_value()) { Town *t = nullptr; if (this->airport_scope.st != nullptr) { t = this->airport_scope.st->town; @@ -227,9 +227,9 @@ TownScopeResolver *AirportResolverObject::GetTown() t = ClosestTownFromTile(this->airport_scope.tile, UINT_MAX); } if (t == nullptr) return nullptr; - this->town_scope.reset(new TownScopeResolver(*this, t, this->airport_scope.st == nullptr)); + this->town_scope.emplace(*this, t, this->airport_scope.st == nullptr); } - return this->town_scope.get(); + return &*this->town_scope; } /** diff --git a/src/newgrf_airport.h b/src/newgrf_airport.h index 7099e42e47..45b2244bd7 100644 --- a/src/newgrf_airport.h +++ b/src/newgrf_airport.h @@ -174,7 +174,7 @@ struct AirportScopeResolver : public ScopeResolver { /** Resolver object for airports. */ struct AirportResolverObject : public ResolverObject { AirportScopeResolver airport_scope; - std::unique_ptr town_scope; ///< The town scope resolver (created on the first call). + std::optional town_scope = std::nullopt; ///< The town scope resolver (created on the first call). AirportResolverObject(TileIndex tile, Station *st, uint8_t airport_id, uint8_t layout, CallbackID callback = CBID_NO_CALLBACK, uint32_t callback_param1 = 0, uint32_t callback_param2 = 0); diff --git a/src/newgrf_industries.cpp b/src/newgrf_industries.cpp index 7f574fb9b9..7ad754f8d5 100644 --- a/src/newgrf_industries.cpp +++ b/src/newgrf_industries.cpp @@ -467,24 +467,18 @@ static const GRFFile *GetGrffile(IndustryType type) IndustriesResolverObject::IndustriesResolverObject(TileIndex tile, Industry *indus, IndustryType type, uint32_t random_bits, CallbackID callback, uint32_t callback_param1, uint32_t callback_param2) : ResolverObject(GetGrffile(type), callback, callback_param1, callback_param2), - industries_scope(*this, tile, indus, type, random_bits), - town_scope(nullptr) + industries_scope(*this, tile, indus, type, random_bits) { this->root_spritegroup = GetIndustrySpec(type)->grf_prop.spritegroup[0]; } -IndustriesResolverObject::~IndustriesResolverObject() -{ - delete this->town_scope; -} - /** * Get or create the town scope object associated with the industry. * @return The associated town scope, if it exists. */ TownScopeResolver *IndustriesResolverObject::GetTown() { - if (this->town_scope == nullptr) { + if (!this->town_scope.has_value()) { Town *t = nullptr; bool readonly = true; if (this->industries_scope.industry != nullptr) { @@ -494,9 +488,9 @@ TownScopeResolver *IndustriesResolverObject::GetTown() t = ClosestTownFromTile(this->industries_scope.tile, UINT_MAX); } if (t == nullptr) return nullptr; - this->town_scope = new TownScopeResolver(*this, t, readonly); + this->town_scope.emplace(*this, t, readonly); } - return this->town_scope; + return &*this->town_scope; } GrfSpecFeature IndustriesResolverObject::GetFeature() const diff --git a/src/newgrf_industries.h b/src/newgrf_industries.h index a195e978a4..827302ec5d 100644 --- a/src/newgrf_industries.h +++ b/src/newgrf_industries.h @@ -41,11 +41,10 @@ struct IndustriesScopeResolver : public ScopeResolver { /** Resolver for industries. */ struct IndustriesResolverObject : public ResolverObject { IndustriesScopeResolver industries_scope; ///< Scope resolver for the industry. - TownScopeResolver *town_scope; ///< Scope resolver for the associated town (if needed and available, else \c nullptr). + std::optional town_scope = std::nullopt; ///< Scope resolver for the associated town (if needed and available, else \c std::nullopt). IndustriesResolverObject(TileIndex tile, Industry *indus, IndustryType type, uint32_t random_bits = 0, CallbackID callback = CBID_NO_CALLBACK, uint32_t callback_param1 = 0, uint32_t callback_param2 = 0); - ~IndustriesResolverObject(); TownScopeResolver *GetTown(); diff --git a/src/newgrf_object.cpp b/src/newgrf_object.cpp index d938bb4930..7d9765f1f6 100644 --- a/src/newgrf_object.cpp +++ b/src/newgrf_object.cpp @@ -378,16 +378,10 @@ ObjectResolverObject::ObjectResolverObject(const ObjectSpec *spec, Object *obj, CallbackID callback, uint32_t param1, uint32_t param2) : ResolverObject(spec->grf_prop.grffile, callback, param1, param2), object_scope(*this, obj, spec, tile, view) { - this->town_scope = nullptr; this->root_spritegroup = (obj == nullptr && spec->grf_prop.spritegroup[OBJECT_SPRITE_GROUP_PURCHASE] != nullptr) ? spec->grf_prop.spritegroup[OBJECT_SPRITE_GROUP_PURCHASE] : spec->grf_prop.spritegroup[OBJECT_SPRITE_GROUP_DEFAULT]; } -ObjectResolverObject::~ObjectResolverObject() -{ - delete this->town_scope; -} - /** * Get the town resolver scope that belongs to this object resolver. * On the first call, the town scope is created (if possible). @@ -395,7 +389,7 @@ ObjectResolverObject::~ObjectResolverObject() */ TownScopeResolver *ObjectResolverObject::GetTown() { - if (this->town_scope == nullptr) { + if (!this->town_scope.has_value()) { Town *t; if (this->object_scope.obj != nullptr) { t = this->object_scope.obj->town; @@ -403,9 +397,9 @@ TownScopeResolver *ObjectResolverObject::GetTown() t = ClosestTownFromTile(this->object_scope.tile, UINT_MAX); } if (t == nullptr) return nullptr; - this->town_scope = new TownScopeResolver(*this, t, this->object_scope.obj == nullptr); + this->town_scope.emplace(*this, t, this->object_scope.obj == nullptr); } - return this->town_scope; + return &*this->town_scope; } GrfSpecFeature ObjectResolverObject::GetFeature() const diff --git a/src/newgrf_object.h b/src/newgrf_object.h index 8b17a7c7a6..9b124ab0f8 100644 --- a/src/newgrf_object.h +++ b/src/newgrf_object.h @@ -133,11 +133,10 @@ struct ObjectScopeResolver : public ScopeResolver { /** A resolver object to be used with feature 0F spritegroups. */ struct ObjectResolverObject : public ResolverObject { ObjectScopeResolver object_scope; ///< The object scope resolver. - TownScopeResolver *town_scope; ///< The town scope resolver (created on the first call). + std::optional town_scope = std::nullopt; ///< The town scope resolver (created on the first call). ObjectResolverObject(const ObjectSpec *spec, Object *o, TileIndex tile, uint8_t view = 0, CallbackID callback = CBID_NO_CALLBACK, uint32_t param1 = 0, uint32_t param2 = 0); - ~ObjectResolverObject(); ScopeResolver *GetScope(VarSpriteGroupScope scope = VSG_SCOPE_SELF, uint8_t relative = 0) override { diff --git a/src/newgrf_roadstop.cpp b/src/newgrf_roadstop.cpp index f45f051dee..eaa76fe5f9 100644 --- a/src/newgrf_roadstop.cpp +++ b/src/newgrf_roadstop.cpp @@ -210,10 +210,7 @@ const SpriteGroup *RoadStopResolverObject::ResolveReal(const RealSpriteGroup *gr RoadStopResolverObject::RoadStopResolverObject(const RoadStopSpec *roadstopspec, BaseStation *st, TileIndex tile, RoadType roadtype, StationType type, uint8_t view, CallbackID callback, uint32_t param1, uint32_t param2) : ResolverObject(roadstopspec->grf_prop.grffile, callback, param1, param2), roadstop_scope(*this, st, roadstopspec, tile, roadtype, type, view) - { - - this->town_scope = nullptr; - +{ CargoID ctype = SpriteGroupCargo::SG_DEFAULT_NA; if (st == nullptr) { @@ -240,14 +237,9 @@ RoadStopResolverObject::RoadStopResolverObject(const RoadStopSpec *roadstopspec, this->root_spritegroup = roadstopspec->grf_prop.spritegroup[ctype]; } -RoadStopResolverObject::~RoadStopResolverObject() -{ - delete this->town_scope; -} - TownScopeResolver *RoadStopResolverObject::GetTown() { - if (this->town_scope == nullptr) { + if (!this->town_scope.has_value()) { Town *t; if (this->roadstop_scope.st != nullptr) { t = this->roadstop_scope.st->town; @@ -255,9 +247,9 @@ TownScopeResolver *RoadStopResolverObject::GetTown() t = ClosestTownFromTile(this->roadstop_scope.tile, UINT_MAX); } if (t == nullptr) return nullptr; - this->town_scope = new TownScopeResolver(*this, t, this->roadstop_scope.st == nullptr); + this->town_scope.emplace(*this, t, this->roadstop_scope.st == nullptr); } - return this->town_scope; + return &*this->town_scope; } uint16_t GetRoadStopCallback(CallbackID callback, uint32_t param1, uint32_t param2, const RoadStopSpec *roadstopspec, BaseStation *st, TileIndex tile, RoadType roadtype, StationType type, uint8_t view) diff --git a/src/newgrf_roadstop.h b/src/newgrf_roadstop.h index 03df70e9fa..780afd5d86 100644 --- a/src/newgrf_roadstop.h +++ b/src/newgrf_roadstop.h @@ -95,10 +95,9 @@ struct RoadStopScopeResolver : public ScopeResolver { /** Road stop resolver. */ struct RoadStopResolverObject : public ResolverObject { RoadStopScopeResolver roadstop_scope; ///< The stop scope resolver. - TownScopeResolver *town_scope; ///< The town scope resolver (created on the first call). + std::optional town_scope = std::nullopt; ///< The town scope resolver (created on the first call). RoadStopResolverObject(const RoadStopSpec *roadstopspec, BaseStation *st, TileIndex tile, RoadType roadtype, StationType type, uint8_t view, CallbackID callback = CBID_NO_CALLBACK, uint32_t param1 = 0, uint32_t param2 = 0); - ~RoadStopResolverObject(); ScopeResolver *GetScope(VarSpriteGroupScope scope = VSG_SCOPE_SELF, uint8_t relative = 0) override { diff --git a/src/newgrf_station.cpp b/src/newgrf_station.cpp index f6c68eaa10..1997b10274 100644 --- a/src/newgrf_station.cpp +++ b/src/newgrf_station.cpp @@ -254,7 +254,7 @@ static struct { */ TownScopeResolver *StationResolverObject::GetTown() { - if (this->town_scope == nullptr) { + if (!this->town_scope.has_value()) { Town *t = nullptr; if (this->station_scope.st != nullptr) { t = this->station_scope.st->town; @@ -262,9 +262,9 @@ TownScopeResolver *StationResolverObject::GetTown() t = ClosestTownFromTile(this->station_scope.tile, UINT_MAX); } if (t == nullptr) return nullptr; - this->town_scope = new TownScopeResolver(*this, t, this->station_scope.st == nullptr); + this->town_scope.emplace(*this, t, this->station_scope.st == nullptr); } - return this->town_scope; + return &*this->town_scope; } /* virtual */ uint32_t StationScopeResolver::GetVariable(uint8_t variable, [[maybe_unused]] uint32_t parameter, bool *available) const @@ -569,7 +569,7 @@ uint32_t StationResolverObject::GetDebugID() const StationResolverObject::StationResolverObject(const StationSpec *statspec, BaseStation *base_station, TileIndex tile, CallbackID callback, uint32_t callback_param1, uint32_t callback_param2) : ResolverObject(statspec->grf_prop.grffile, callback, callback_param1, callback_param2), - station_scope(*this, statspec, base_station, tile), town_scope(nullptr) + station_scope(*this, statspec, base_station, tile) { /* Invalidate all cached vars */ _svc.valid = 0; @@ -600,11 +600,6 @@ StationResolverObject::StationResolverObject(const StationSpec *statspec, BaseSt this->root_spritegroup = this->station_scope.statspec->grf_prop.spritegroup[this->station_scope.cargo_type]; } -StationResolverObject::~StationResolverObject() -{ - delete this->town_scope; -} - /** * Resolve sprites for drawing a station tile. * @param statspec Station spec diff --git a/src/newgrf_station.h b/src/newgrf_station.h index 2924ff4288..f1c619153e 100644 --- a/src/newgrf_station.h +++ b/src/newgrf_station.h @@ -49,11 +49,10 @@ struct StationScopeResolver : public ScopeResolver { /** Station resolver. */ struct StationResolverObject : public ResolverObject { StationScopeResolver station_scope; ///< The station scope resolver. - TownScopeResolver *town_scope; ///< The town scope resolver (created on the first call). + std::optional town_scope = std::nullopt; ///< The town scope resolver (created on the first call). StationResolverObject(const StationSpec *statspec, BaseStation *st, TileIndex tile, CallbackID callback = CBID_NO_CALLBACK, uint32_t callback_param1 = 0, uint32_t callback_param2 = 0); - ~StationResolverObject(); TownScopeResolver *GetTown();