summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorfonsinchen <fonsinchen@openttd.org>2014-09-21 16:25:15 +0000
committerfonsinchen <fonsinchen@openttd.org>2014-09-21 16:25:15 +0000
commit1a5b2f0e17c5e694401e4d3730ed348e9f6d5f76 (patch)
tree5da4da1b8ae7c4c5b201583a93b55adc63174905
parent2128f1e92906563fc7237b8a43f6fa723de70e7c (diff)
downloadopenttd-1a5b2f0e17c5e694401e4d3730ed348e9f6d5f76.tar.xz
(svn r26894) -Feature: Swap method for script lists
-rw-r--r--src/script/api/ai/ai_list.hpp.sq1
-rw-r--r--src/script/api/ai_changelog.hpp1
-rw-r--r--src/script/api/game/game_list.hpp.sq1
-rw-r--r--src/script/api/game_changelog.hpp1
-rw-r--r--src/script/api/script_list.cpp24
-rw-r--r--src/script/api/script_list.hpp6
6 files changed, 34 insertions, 0 deletions
diff --git a/src/script/api/ai/ai_list.hpp.sq b/src/script/api/ai/ai_list.hpp.sq
index db724da83..201113823 100644
--- a/src/script/api/ai/ai_list.hpp.sq
+++ b/src/script/api/ai/ai_list.hpp.sq
@@ -40,6 +40,7 @@ void SQAIList_Register(Squirrel *engine)
SQAIList.DefSQMethod(engine, &ScriptList::SetValue, "SetValue", 3, "xii");
SQAIList.DefSQMethod(engine, &ScriptList::Sort, "Sort", 3, "xib");
SQAIList.DefSQMethod(engine, &ScriptList::AddList, "AddList", 2, "xx");
+ SQAIList.DefSQMethod(engine, &ScriptList::SwapList, "SwapList", 2, "xx");
SQAIList.DefSQMethod(engine, &ScriptList::RemoveAboveValue, "RemoveAboveValue", 2, "xi");
SQAIList.DefSQMethod(engine, &ScriptList::RemoveBelowValue, "RemoveBelowValue", 2, "xi");
SQAIList.DefSQMethod(engine, &ScriptList::RemoveBetweenValue, "RemoveBetweenValue", 3, "xii");
diff --git a/src/script/api/ai_changelog.hpp b/src/script/api/ai_changelog.hpp
index 01e05b61e..6c21deeb6 100644
--- a/src/script/api/ai_changelog.hpp
+++ b/src/script/api/ai_changelog.hpp
@@ -20,6 +20,7 @@
* 1.5.0 is not yet released. The following changes are not set in stone yet.
*
* API additions:
+ * \li AIList::SwapList
* \li AIStation::GetCargoPlanned
* \li AIStation::GetCargoPlannedFrom
* \li AIStation::GetCargoPlannedFromVia
diff --git a/src/script/api/game/game_list.hpp.sq b/src/script/api/game/game_list.hpp.sq
index 03049b314..a46696def 100644
--- a/src/script/api/game/game_list.hpp.sq
+++ b/src/script/api/game/game_list.hpp.sq
@@ -40,6 +40,7 @@ void SQGSList_Register(Squirrel *engine)
SQGSList.DefSQMethod(engine, &ScriptList::SetValue, "SetValue", 3, "xii");
SQGSList.DefSQMethod(engine, &ScriptList::Sort, "Sort", 3, "xib");
SQGSList.DefSQMethod(engine, &ScriptList::AddList, "AddList", 2, "xx");
+ SQGSList.DefSQMethod(engine, &ScriptList::SwapList, "SwapList", 2, "xx");
SQGSList.DefSQMethod(engine, &ScriptList::RemoveAboveValue, "RemoveAboveValue", 2, "xi");
SQGSList.DefSQMethod(engine, &ScriptList::RemoveBelowValue, "RemoveBelowValue", 2, "xi");
SQGSList.DefSQMethod(engine, &ScriptList::RemoveBetweenValue, "RemoveBetweenValue", 3, "xii");
diff --git a/src/script/api/game_changelog.hpp b/src/script/api/game_changelog.hpp
index a627e3270..99fe57a7e 100644
--- a/src/script/api/game_changelog.hpp
+++ b/src/script/api/game_changelog.hpp
@@ -20,6 +20,7 @@
* 1.5.0 is not yet released. The following changes are not set in stone yet.
*
* API additions:
+ * \li GSList::SwapList
* \li GSStation::GetCargoPlanned
* \li GSStation::GetCargoPlannedFrom
* \li GSStation::GetCargoPlannedFromVia
diff --git a/src/script/api/script_list.cpp b/src/script/api/script_list.cpp
index cd605a509..22da0abf2 100644
--- a/src/script/api/script_list.cpp
+++ b/src/script/api/script_list.cpp
@@ -58,6 +58,17 @@ public:
* Callback from the list if an item gets removed.
*/
virtual void Remove(int item) = 0;
+
+ /**
+ * 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.
+ */
+ virtual void Retarget(ScriptList *new_list)
+ {
+ this->list = new_list;
+ }
};
/**
@@ -549,6 +560,19 @@ void ScriptList::AddList(ScriptList *list)
}
}
+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);
+ this->sorter->Retarget(this);
+ list->sorter->Retarget(list);
+}
+
void ScriptList::RemoveAboveValue(int32 value)
{
this->modifications++;
diff --git a/src/script/api/script_list.hpp b/src/script/api/script_list.hpp
index e6f4d137d..4e844e1c9 100644
--- a/src/script/api/script_list.hpp
+++ b/src/script/api/script_list.hpp
@@ -153,6 +153,12 @@ public:
void AddList(ScriptList *list);
/**
+ * Swap the contents of two lists.
+ * @param list The list that will be swapped with.
+ */
+ void SwapList(ScriptList *list);
+
+ /**
* Removes all items with a higher value than 'value'.
* @param value the value above which all items are removed.
*/