1
0
Fork 0

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

Avoids manual pointer management.
pull/13524/head
Peter Nelson 2025-02-10 19:20:52 +00:00 committed by GitHub
parent faadf00a6a
commit 687829fa14
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 11 additions and 13 deletions

View File

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

View File

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