From b930c1e7b62093b925ea2481c6838eb6896c27df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A1t=C3=A9=20Szab=C3=B3?= Date: Sat, 16 Mar 2024 13:44:49 +0100 Subject: [PATCH] Codechange: Use smart pointers for CargoDataEntry children container Replace manual memory management used for the container holding leaves in the CargoDataEntry tree structure with std::unique_ptr. --- src/station_gui.cpp | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/src/station_gui.cpp b/src/station_gui.cpp index 6926fa15f5..7ad463f571 100644 --- a/src/station_gui.cpp +++ b/src/station_gui.cpp @@ -1022,7 +1022,7 @@ private: }; uint num_children; ///< the number of subentries belonging to this entry. uint count; ///< sum of counts of all children or amount of cargo for this entry. - CargoDataSet *children; ///< the children of this entry. + std::unique_ptr children; ///< the children of this entry. }; CargoDataEntry::CargoDataEntry() : @@ -1030,7 +1030,7 @@ CargoDataEntry::CargoDataEntry() : station(INVALID_STATION), num_children(0), count(0), - children(new CargoDataSet(CargoSorter(CargoSortType::CargoID))) + children(std::make_unique(CargoSorter(CargoSortType::CargoID))) {} CargoDataEntry::CargoDataEntry(CargoID cargo, uint count, CargoDataEntry *parent) : @@ -1038,7 +1038,7 @@ CargoDataEntry::CargoDataEntry(CargoID cargo, uint count, CargoDataEntry *parent cargo(cargo), num_children(0), count(count), - children(new CargoDataSet) + children(std::make_unique()) {} CargoDataEntry::CargoDataEntry(StationID station, uint count, CargoDataEntry *parent) : @@ -1046,7 +1046,7 @@ CargoDataEntry::CargoDataEntry(StationID station, uint count, CargoDataEntry *pa station(station), num_children(0), count(count), - children(new CargoDataSet) + children(std::make_unique()) {} CargoDataEntry::CargoDataEntry(StationID station) : @@ -1068,7 +1068,6 @@ CargoDataEntry::CargoDataEntry(CargoID cargo) : CargoDataEntry::~CargoDataEntry() { this->Clear(); - delete this->children; } /** @@ -1146,9 +1145,7 @@ void CargoDataEntry::IncrementSize() void CargoDataEntry::Resort(CargoSortType type, SortOrder order) { - CargoDataSet *new_subs = new CargoDataSet(this->children->begin(), this->children->end(), CargoSorter(type, order)); - delete this->children; - this->children = new_subs; + this->children = std::make_unique(this->children->begin(), this->children->end(), CargoSorter(type, order)); } CargoDataEntry *CargoDataEntry::Retrieve(CargoDataSet::iterator i) const