Codechange: Use std::optional for town parent scope resolver. (#12530)

When resolving NewGRF, the parent town_scope is lazily initialised as it does not always need to be used.

Replace the manually managed pointer with std::optional to simplify. Using std::optional avoids extra memory allocation.
This commit is contained in:
Peter Nelson
2024-04-18 22:14:16 +01:00
committed by GitHub
parent 3b75d8bbf8
commit 774f811217
10 changed files with 23 additions and 52 deletions

View File

@@ -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;
}
/**