1
0
Fork 0

Codechange: Use std::unique_ptr for ScriptList::sorter.

Avoids manual pointer management.
pull/13517/head
Peter Nelson 2025-02-09 23:21:06 +00:00
parent 77bf3084da
commit 159b59ea8e
No known key found for this signature in database
GPG Key ID: 8EF8F0A467DF75ED
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