From 46e34c500c4b832a8fa740a93007cc9bdde4d50e Mon Sep 17 00:00:00 2001 From: elsid Date: Fri, 23 Apr 2021 23:57:58 +0200 Subject: [PATCH 1/3] Use algorithms to add/remove listener to SpellList --- apps/openmw/mwmechanics/spelllist.cpp | 18 +++++------------- 1 file changed, 5 insertions(+), 13 deletions(-) diff --git a/apps/openmw/mwmechanics/spelllist.cpp b/apps/openmw/mwmechanics/spelllist.cpp index 891b28619..b87b98573 100644 --- a/apps/openmw/mwmechanics/spelllist.cpp +++ b/apps/openmw/mwmechanics/spelllist.cpp @@ -153,23 +153,15 @@ namespace MWMechanics void SpellList::addListener(Spells* spells) { - for(const auto ptr : mListeners) - { - if(ptr == spells) - return; - } + if (std::find(mListeners.begin(), mListeners.end(), spells) != mListeners.end()) + return; mListeners.push_back(spells); } void SpellList::removeListener(Spells* spells) { - for(auto it = mListeners.begin(); it != mListeners.end(); it++) - { - if(*it == spells) - { - mListeners.erase(it); - break; - } - } + const auto it = std::find(mListeners.begin(), mListeners.end(), spells); + if (it != mListeners.end()) + mListeners.erase(it); } } From ca7add0624699736fd3d6c8b53b002e64a1bef1f Mon Sep 17 00:00:00 2001 From: elsid Date: Fri, 23 Apr 2021 23:48:55 +0200 Subject: [PATCH 2/3] Add move constructor to MWMechanics::Spells --- apps/openmw/mwmechanics/spelllist.cpp | 8 ++++++++ apps/openmw/mwmechanics/spelllist.hpp | 2 ++ apps/openmw/mwmechanics/spells.cpp | 9 +++++++++ apps/openmw/mwmechanics/spells.hpp | 2 +- 4 files changed, 20 insertions(+), 1 deletion(-) diff --git a/apps/openmw/mwmechanics/spelllist.cpp b/apps/openmw/mwmechanics/spelllist.cpp index b87b98573..d8fbcf25a 100644 --- a/apps/openmw/mwmechanics/spelllist.cpp +++ b/apps/openmw/mwmechanics/spelllist.cpp @@ -164,4 +164,12 @@ namespace MWMechanics if (it != mListeners.end()) mListeners.erase(it); } + + void SpellList::updateListener(Spells* before, Spells* after) + { + const auto it = std::find(mListeners.begin(), mListeners.end(), before); + if (it == mListeners.end()) + return mListeners.push_back(after); + *it = after; + } } diff --git a/apps/openmw/mwmechanics/spelllist.hpp b/apps/openmw/mwmechanics/spelllist.hpp index b01722fe8..c95ee812b 100644 --- a/apps/openmw/mwmechanics/spelllist.hpp +++ b/apps/openmw/mwmechanics/spelllist.hpp @@ -61,6 +61,8 @@ namespace MWMechanics void removeListener(Spells* spells); + void updateListener(Spells* before, Spells* after); + const std::vector getSpells() const; }; } diff --git a/apps/openmw/mwmechanics/spells.cpp b/apps/openmw/mwmechanics/spells.cpp index 0af74e01b..b87137600 100644 --- a/apps/openmw/mwmechanics/spells.cpp +++ b/apps/openmw/mwmechanics/spells.cpp @@ -32,6 +32,15 @@ namespace MWMechanics mSpellList->addListener(this); } + Spells::Spells(Spells&& spells) : mSpellList(std::move(spells.mSpellList)), mSpells(std::move(spells.mSpells)), + mSelectedSpell(std::move(spells.mSelectedSpell)), mUsedPowers(std::move(spells.mUsedPowers)), + mSpellsChanged(std::move(spells.mSpellsChanged)), mEffects(std::move(spells.mEffects)), + mSourcedEffects(std::move(spells.mSourcedEffects)) + { + if (mSpellList) + mSpellList->updateListener(&spells, this); + } + std::map::const_iterator Spells::begin() const { return mSpells.begin(); diff --git a/apps/openmw/mwmechanics/spells.hpp b/apps/openmw/mwmechanics/spells.hpp index 3df89a537..055339795 100644 --- a/apps/openmw/mwmechanics/spells.hpp +++ b/apps/openmw/mwmechanics/spells.hpp @@ -59,7 +59,7 @@ namespace MWMechanics Spells(const Spells&); - Spells(const Spells&&) = delete; + Spells(Spells&& spells); ~Spells(); From e1d955d92b80dc26fd59f865467545aa1a7f2125 Mon Sep 17 00:00:00 2001 From: elsid Date: Sat, 24 Apr 2021 00:16:57 +0200 Subject: [PATCH 3/3] Remove noexcept specifier from CreatureCustomData move ctor C++17 doesn't allow to declare defaulted move ctor with not matching exception specification to the calculated one: /Users/eddie/Downloads/BUILD/openmw-master/apps/openmw/mwclass/creature.cpp:63:9: error: exception specification of explicitly defaulted move constructor does not match the calculated one CreatureCustomData(CreatureCustomData&& other) noexcept = default; ^ http://wg21.link/p1286r2 makes this possible in C++20 and newer compilers already have it working even with -std=c++17 but older onces give an error. --- apps/openmw/mwclass/creature.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/openmw/mwclass/creature.cpp b/apps/openmw/mwclass/creature.cpp index 6b8d2f3f2..5ce9a66ab 100644 --- a/apps/openmw/mwclass/creature.cpp +++ b/apps/openmw/mwclass/creature.cpp @@ -60,7 +60,7 @@ namespace MWClass CreatureCustomData() = default; CreatureCustomData(const CreatureCustomData& other); - CreatureCustomData(CreatureCustomData&& other) noexcept = default; + CreatureCustomData(CreatureCustomData&& other) = default; CreatureCustomData& asCreatureCustomData() override {