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

Codechange: [Script] Simplify constructors and add constness

This commit is contained in:
Rubidium
2025-12-18 21:53:56 +01:00
committed by rubidium42
parent 091035b533
commit 1464d5fd06

View File

@@ -19,9 +19,15 @@
*/
class ScriptListSorter {
protected:
ScriptList *list; ///< The list that's being sorted.
bool has_no_more_items; ///< Whether we have more items to iterate over.
std::optional<SQInteger> item_next{}; ///< The next item we will show, or std::nullopt if there are no more items to iterate over.
const ScriptList *list; ///< The list that's being sorted.
bool has_no_more_items = true; ///< Whether we have more items to iterate over.
std::optional<SQInteger> item_next = std::nullopt; ///< The next item we will show, or std::nullopt if there are no more items to iterate over.
/**
* Create a new sorter.
* @param list The list to sort.
*/
ScriptListSorter(const ScriptList *list) : list(list) {}
/**
* Actually try to find the next item.
@@ -64,7 +70,7 @@ public:
/**
* See if the sorter has reached the end.
*/
bool IsEnd()
bool IsEnd() const
{
return this->list->items.empty() || this->has_no_more_items;
}
@@ -77,19 +83,16 @@ public:
if (this->IsEnd()) return;
/* If we remove the 'next' item, skip to the next */
if (item == this->item_next) {
this->FindNext();
return;
}
if (item == this->item_next) this->FindNext();
}
/**
* Attach the sorter to a new list. This assumes the content of the old list has been moved to
* the new list, too, so that we don't have to invalidate any iterators. Note that std::swap
* doesn't invalidate iterators on lists and maps, so that should be safe.
* @param target New list to attach to.
* @param new_list New list to attach to.
*/
void Retarget(ScriptList *new_list)
void Retarget(const ScriptList *new_list)
{
this->list = new_list;
}
@@ -100,18 +103,14 @@ public:
*/
class ScriptListSorterValueAscending : public ScriptListSorter {
private:
ScriptList::ScriptListSet::iterator value_iter; ///< The iterator over the value/item pairs in the set.
ScriptList::ScriptListSet::const_iterator value_iter; ///< The iterator over the value/item pairs in the set.
public:
/**
* Create a new sorter.
* @param list The list to sort.
*/
ScriptListSorterValueAscending(ScriptList *list)
{
this->list = list;
this->End();
}
ScriptListSorterValueAscending(const ScriptList *list) : ScriptListSorter(list) {}
std::optional<SQInteger> Begin() override
{
@@ -149,18 +148,14 @@ private:
/* Note: We cannot use reverse_iterator.
* The iterators must only be invalidated when the element they are pointing to is removed.
* This only holds for forward iterators. */
ScriptList::ScriptListSet::iterator value_iter; ///< The iterator over the value/item pairs in the set.
ScriptList::ScriptListSet::const_iterator value_iter; ///< The iterator over the value/item pairs in the set.
public:
/**
* Create a new sorter.
* @param list The list to sort.
*/
ScriptListSorterValueDescending(ScriptList *list)
{
this->list = list;
this->End();
}
ScriptListSorterValueDescending(const ScriptList *list) : ScriptListSorter(list) {}
std::optional<SQInteger> Begin() override
{
@@ -201,18 +196,14 @@ public:
*/
class ScriptListSorterItemAscending : public ScriptListSorter {
private:
ScriptList::ScriptListMap::iterator item_iter; ///< The iterator over the items in the map.
ScriptList::ScriptListMap::const_iterator item_iter; ///< The iterator over the items in the map.
public:
/**
* Create a new sorter.
* @param list The list to sort.
*/
ScriptListSorterItemAscending(ScriptList *list)
{
this->list = list;
this->End();
}
ScriptListSorterItemAscending(const ScriptList *list) : ScriptListSorter(list) {}
std::optional<SQInteger> Begin() override
{
@@ -250,18 +241,14 @@ private:
/* Note: We cannot use reverse_iterator.
* The iterators must only be invalidated when the element they are pointing to is removed.
* This only holds for forward iterators. */
ScriptList::ScriptListMap::iterator item_iter; ///< The iterator over the items in the map.
ScriptList::ScriptListMap::const_iterator item_iter; ///< The iterator over the items in the map.
public:
/**
* Create a new sorter.
* @param list The list to sort.
*/
ScriptListSorterItemDescending(ScriptList *list)
{
this->list = list;
this->End();
}
ScriptListSorterItemDescending(const ScriptList *list) : ScriptListSorter(list) {}
std::optional<SQInteger> Begin() override
{