1
0
mirror of https://github.com/OpenTTD/OpenTTD synced 2026-01-16 17:02:37 +01:00

Codechange: Use std::unique_ptr for ScriptList::sorter. (#13517)

Avoids manual pointer management.
This commit is contained in:
Peter Nelson
2025-02-10 19:20:52 +00:00
committed by GitHub
parent faadf00a6a
commit 687829fa14
2 changed files with 11 additions and 13 deletions

View File

@@ -406,7 +406,7 @@ public:
ScriptList::ScriptList()
{
/* Default sorter */
this->sorter = new ScriptListSorterValueDescending(this);
this->sorter = std::make_unique<ScriptListSorterValueDescending>(this);
this->sorter_type = SORT_BY_VALUE;
this->sort_ascending = false;
this->initialized = false;
@@ -415,7 +415,6 @@ ScriptList::ScriptList()
ScriptList::~ScriptList()
{
delete this->sorter;
}
bool ScriptList::HasItem(SQInteger item)
@@ -527,21 +526,20 @@ void ScriptList::Sort(SorterType sorter, bool ascending)
if (sorter != SORT_BY_VALUE && sorter != SORT_BY_ITEM) return;
if (sorter == this->sorter_type && ascending == this->sort_ascending) return;
delete this->sorter;
switch (sorter) {
case SORT_BY_ITEM:
if (ascending) {
this->sorter = new ScriptListSorterItemAscending(this);
this->sorter = std::make_unique<ScriptListSorterItemAscending>(this);
} else {
this->sorter = new ScriptListSorterItemDescending(this);
this->sorter = std::make_unique<ScriptListSorterItemDescending>(this);
}
break;
case SORT_BY_VALUE:
if (ascending) {
this->sorter = new ScriptListSorterValueAscending(this);
this->sorter = std::make_unique<ScriptListSorterValueAscending>(this);
} else {
this->sorter = new ScriptListSorterValueDescending(this);
this->sorter = std::make_unique<ScriptListSorterValueDescending>(this);
}
break;
@@ -576,11 +574,11 @@ void ScriptList::SwapList(ScriptList *list)
this->items.swap(list->items);
this->buckets.swap(list->buckets);
Swap(this->sorter, list->sorter);
Swap(this->sorter_type, list->sorter_type);
Swap(this->sort_ascending, list->sort_ascending);
Swap(this->initialized, list->initialized);
Swap(this->modifications, list->modifications);
std::swap(this->sorter, list->sorter);
std::swap(this->sorter_type, list->sorter_type);
std::swap(this->sort_ascending, list->sort_ascending);
std::swap(this->initialized, list->initialized);
std::swap(this->modifications, list->modifications);
this->sorter->Retarget(this);
list->sorter->Retarget(list);
}

View File

@@ -36,7 +36,7 @@ public:
static const bool SORT_DESCENDING = false;
private:
ScriptListSorter *sorter; ///< Sorting algorithm
std::unique_ptr<ScriptListSorter> sorter; ///< Sorting algorithm
SorterType sorter_type; ///< Sorting type
bool sort_ascending; ///< Whether to sort ascending or descending
bool initialized; ///< Whether an iteration has been started