From 1a5b2f0e17c5e694401e4d3730ed348e9f6d5f76 Mon Sep 17 00:00:00 2001 From: fonsinchen Date: Sun, 21 Sep 2014 16:25:15 +0000 Subject: (svn r26894) -Feature: Swap method for script lists --- src/script/api/script_list.cpp | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) (limited to 'src/script/api/script_list.cpp') 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++; -- cgit v1.2.3-54-g00ecf