From 0cded250338229dabeddb5a93fb6d45e5245a9e8 Mon Sep 17 00:00:00 2001 From: Evil Eye Date: Wed, 24 Aug 2022 20:38:52 +0200 Subject: [PATCH] Remove various string copies --- apps/openmw/mwclass/armor.cpp | 4 ++-- apps/openmw/mwclass/clothing.cpp | 2 +- apps/openmw/mwclass/container.cpp | 12 +++++------ apps/openmw/mwclass/creaturelevlist.cpp | 2 +- apps/openmw/mwclass/door.cpp | 21 ++++++++++---------- apps/openmw/mwclass/npc.cpp | 2 +- apps/openmw/mwdialogue/journalentry.cpp | 2 +- apps/openmw/mwdialogue/journalentry.hpp | 2 +- apps/openmw/mwdialogue/journalimp.cpp | 2 +- apps/openmw/mwdialogue/quest.cpp | 4 ++-- apps/openmw/mwdialogue/quest.hpp | 2 +- apps/openmw/mwdialogue/topic.cpp | 2 +- apps/openmw/mwdialogue/topic.hpp | 2 +- apps/openmw/mwgui/class.cpp | 5 ----- apps/openmw/mwgui/class.hpp | 1 - apps/openmw/mwgui/journalviewmodel.cpp | 12 +++++------ apps/openmw/mwgui/journalviewmodel.hpp | 4 ++-- apps/openmw/mwgui/journalwindow.cpp | 4 ++-- apps/openmw/mwmechanics/levelledlist.hpp | 16 +++++++-------- apps/openmw/mwscript/containerextensions.cpp | 2 +- apps/openmw/mwworld/containerstore.cpp | 4 ++-- apps/openmw/mwworld/containerstore.hpp | 2 +- apps/openmw/mwworld/worldimp.cpp | 2 +- components/widgets/list.cpp | 10 ++++++---- components/widgets/list.hpp | 4 ++-- 25 files changed, 60 insertions(+), 65 deletions(-) diff --git a/apps/openmw/mwclass/armor.cpp b/apps/openmw/mwclass/armor.cpp index 0eb4ff3e59..a72f0e9faa 100644 --- a/apps/openmw/mwclass/armor.cpp +++ b/apps/openmw/mwclass/armor.cpp @@ -115,7 +115,7 @@ namespace MWClass { const MWWorld::LiveCellRef *ref = ptr.get(); - std::string typeGmst; + std::string_view typeGmst; switch (ref->mBase->mData.mType) { @@ -298,7 +298,7 @@ namespace MWClass if (npc.getClass().isNpc()) { - std::string npcRace = npc.get()->mBase->mRace; + const std::string& npcRace = npc.get()->mBase->mRace; // Beast races cannot equip shoes / boots, or full helms (head part vs hair part) const ESM::Race* race = MWBase::Environment::get().getWorld()->getStore().get().find(npcRace); diff --git a/apps/openmw/mwclass/clothing.cpp b/apps/openmw/mwclass/clothing.cpp index 8d74f18eab..9a78968871 100644 --- a/apps/openmw/mwclass/clothing.cpp +++ b/apps/openmw/mwclass/clothing.cpp @@ -205,7 +205,7 @@ namespace MWClass if (npc.getClass().isNpc()) { - std::string npcRace = npc.get()->mBase->mRace; + const std::string& npcRace = npc.get()->mBase->mRace; // Beast races cannot equip shoes / boots, or full helms (head part vs hair part) const ESM::Race* race = MWBase::Environment::get().getWorld()->getStore().get().find(npcRace); diff --git a/apps/openmw/mwclass/container.cpp b/apps/openmw/mwclass/container.cpp index fe38c1da09..66e10c9341 100644 --- a/apps/openmw/mwclass/container.cpp +++ b/apps/openmw/mwclass/container.cpp @@ -147,8 +147,8 @@ namespace MWClass return action; } - const std::string lockedSound = "LockedChest"; - const std::string trapActivationSound = "Disarm Trap Fail"; + const std::string_view lockedSound = "LockedChest"; + const std::string_view trapActivationSound = "Disarm Trap Fail"; MWWorld::Ptr player = MWBase::Environment::get().getWorld ()->getPlayerPtr(); MWWorld::InventoryStore& invStore = player.getClass().getInventoryStore(player); @@ -156,9 +156,9 @@ namespace MWClass bool isLocked = ptr.getCellRef().getLockLevel() > 0; bool isTrapped = !ptr.getCellRef().getTrap().empty(); bool hasKey = false; - std::string keyName; + std::string_view keyName; - const std::string keyId = ptr.getCellRef().getKey(); + const std::string& keyId = ptr.getCellRef().getKey(); if (!keyId.empty()) { MWWorld::Ptr keyPtr = invStore.search(keyId); @@ -171,7 +171,7 @@ namespace MWClass if (isLocked && hasKey) { - MWBase::Environment::get().getWindowManager ()->messageBox (keyName + " #{sKeyUsed}"); + MWBase::Environment::get().getWindowManager ()->messageBox(std::string{keyName} + " #{sKeyUsed}"); ptr.getCellRef().unlock(); // using a key disarms the trap if(isTrapped) @@ -204,7 +204,7 @@ namespace MWClass } else { - std::unique_ptr action = std::make_unique(std::string(), ptr); + std::unique_ptr action = std::make_unique(std::string_view{}, ptr); action->setSound(lockedSound); return action; } diff --git a/apps/openmw/mwclass/creaturelevlist.cpp b/apps/openmw/mwclass/creaturelevlist.cpp index 822ced836d..41d6b9b280 100644 --- a/apps/openmw/mwclass/creaturelevlist.cpp +++ b/apps/openmw/mwclass/creaturelevlist.cpp @@ -132,7 +132,7 @@ namespace MWClass ptr.get(); auto& prng = MWBase::Environment::get().getWorld()->getPrng(); - std::string id = MWMechanics::getLevelledItem(ref->mBase, true, prng); + std::string_view id = MWMechanics::getLevelledItem(ref->mBase, true, prng); if (!id.empty()) { diff --git a/apps/openmw/mwclass/door.cpp b/apps/openmw/mwclass/door.cpp index 1f04db4582..8012e90382 100644 --- a/apps/openmw/mwclass/door.cpp +++ b/apps/openmw/mwclass/door.cpp @@ -118,18 +118,11 @@ namespace MWClass const std::string_view lockedSound = "LockedDoor"; const std::string_view trapActivationSound = "Disarm Trap Fail"; - MWWorld::ContainerStore &invStore = actor.getClass().getContainerStore(actor); - - bool isLocked = ptr.getCellRef().getLockLevel() > 0; - bool isTrapped = !ptr.getCellRef().getTrap().empty(); - bool hasKey = false; - std::string keyName; - // FIXME: If NPC activate teleporting door, it can lead to crash due to iterator invalidation in the Actors update. // Make such activation a no-op for now, like how it is in the vanilla game. if (actor != MWMechanics::getPlayer() && ptr.getCellRef().getTeleport()) { - std::unique_ptr action = std::make_unique(std::string(), ptr); + std::unique_ptr action = std::make_unique(std::string_view{}, ptr); action->setSound(lockedSound); return action; } @@ -150,7 +143,13 @@ namespace MWClass } } - const std::string keyId = ptr.getCellRef().getKey(); + MWWorld::ContainerStore &invStore = actor.getClass().getContainerStore(actor); + + bool isLocked = ptr.getCellRef().getLockLevel() > 0; + bool isTrapped = !ptr.getCellRef().getTrap().empty(); + bool hasKey = false; + std::string_view keyName; + const std::string& keyId = ptr.getCellRef().getKey(); if (!keyId.empty()) { MWWorld::Ptr keyPtr = invStore.search(keyId); @@ -164,7 +163,7 @@ namespace MWClass if (isLocked && hasKey) { if(actor == MWMechanics::getPlayer()) - MWBase::Environment::get().getWindowManager()->messageBox(keyName + " #{sKeyUsed}"); + MWBase::Environment::get().getWindowManager()->messageBox(std::string{keyName} + " #{sKeyUsed}"); ptr.getCellRef().unlock(); //Call the function here. because that makes sense. // using a key disarms the trap if(isTrapped) @@ -236,7 +235,7 @@ namespace MWClass else { // locked, and we can't open. - std::unique_ptr action = std::make_unique(std::string(), ptr); + std::unique_ptr action = std::make_unique(std::string_view{}, ptr); action->setSound(lockedSound); return action; } diff --git a/apps/openmw/mwclass/npc.cpp b/apps/openmw/mwclass/npc.cpp index 9f44555c5a..8d1f455d1a 100644 --- a/apps/openmw/mwclass/npc.cpp +++ b/apps/openmw/mwclass/npc.cpp @@ -1150,7 +1150,7 @@ namespace MWClass { MWBase::Environment::get().getWorld()->breakInvisibility(actor); MWMechanics::CastSpell cast(actor, actor); - std::string recordId = consumable.getCellRef().getRefId(); + const std::string& recordId = consumable.getCellRef().getRefId(); MWBase::Environment::get().getLuaManager()->itemConsumed(consumable, actor); actor.getClass().getContainerStore(actor).remove(consumable, 1, actor); return cast.cast(recordId); diff --git a/apps/openmw/mwdialogue/journalentry.cpp b/apps/openmw/mwdialogue/journalentry.cpp index 4b022c251e..961977ca03 100644 --- a/apps/openmw/mwdialogue/journalentry.cpp +++ b/apps/openmw/mwdialogue/journalentry.cpp @@ -45,7 +45,7 @@ namespace MWDialogue Entry::Entry (const ESM::JournalEntry& record) : mInfoId (record.mInfo), mText (record.mText), mActorName(record.mActorName) {} - std::string Entry::getText() const + const std::string& Entry::getText() const { return mText; } diff --git a/apps/openmw/mwdialogue/journalentry.hpp b/apps/openmw/mwdialogue/journalentry.hpp index 709f5627aa..c4b93de7c4 100644 --- a/apps/openmw/mwdialogue/journalentry.hpp +++ b/apps/openmw/mwdialogue/journalentry.hpp @@ -30,7 +30,7 @@ namespace MWDialogue Entry (const ESM::JournalEntry& record); - std::string getText() const; + const std::string& getText() const; void write (ESM::JournalEntry& entry) const; }; diff --git a/apps/openmw/mwdialogue/journalimp.cpp b/apps/openmw/mwdialogue/journalimp.cpp index c7ccb06f9a..906ae97363 100644 --- a/apps/openmw/mwdialogue/journalimp.cpp +++ b/apps/openmw/mwdialogue/journalimp.cpp @@ -96,7 +96,7 @@ namespace MWDialogue if(quest.addEntry(entry)) // we are doing slicing on purpose here { // Restart all "other" quests with the same name as well - std::string name = quest.getName(); + std::string_view name = quest.getName(); for(auto& it : mQuests) { if(it.second.isFinished() && Misc::StringUtils::ciEqual(it.second.getName(), name)) diff --git a/apps/openmw/mwdialogue/quest.cpp b/apps/openmw/mwdialogue/quest.cpp index ce05676965..e9b0166f9f 100644 --- a/apps/openmw/mwdialogue/quest.cpp +++ b/apps/openmw/mwdialogue/quest.cpp @@ -23,7 +23,7 @@ namespace MWDialogue : Topic (state.mTopic), mIndex (state.mState), mFinished (state.mFinished!=0) {} - std::string Quest::getName() const + std::string_view Quest::getName() const { const ESM::Dialogue *dialogue = MWBase::Environment::get().getWorld()->getStore().get().find (mTopic); @@ -33,7 +33,7 @@ namespace MWDialogue if (iter->mQuestStatus==ESM::DialInfo::QS_Name) return iter->mResponse; - return ""; + return {}; } int Quest::getIndex() const diff --git a/apps/openmw/mwdialogue/quest.hpp b/apps/openmw/mwdialogue/quest.hpp index 53b4d02f65..17ed62b8c9 100644 --- a/apps/openmw/mwdialogue/quest.hpp +++ b/apps/openmw/mwdialogue/quest.hpp @@ -24,7 +24,7 @@ namespace MWDialogue Quest (const ESM::QuestState& state); - std::string getName() const override; + std::string_view getName() const override; ///< May be an empty string int getIndex() const; diff --git a/apps/openmw/mwdialogue/topic.cpp b/apps/openmw/mwdialogue/topic.cpp index c9d9b2c9df..50316cfabf 100644 --- a/apps/openmw/mwdialogue/topic.cpp +++ b/apps/openmw/mwdialogue/topic.cpp @@ -44,7 +44,7 @@ namespace MWDialogue return mTopic; } - std::string Topic::getName() const + std::string_view Topic::getName() const { return mName; } diff --git a/apps/openmw/mwdialogue/topic.hpp b/apps/openmw/mwdialogue/topic.hpp index 4e5c936ecd..bf74b54e13 100644 --- a/apps/openmw/mwdialogue/topic.hpp +++ b/apps/openmw/mwdialogue/topic.hpp @@ -47,7 +47,7 @@ namespace MWDialogue std::string getTopic() const; - virtual std::string getName() const; + virtual std::string_view getName() const; void removeLastAddedResponse(std::string_view actorName); diff --git a/apps/openmw/mwgui/class.cpp b/apps/openmw/mwgui/class.cpp index 830d9024ce..44624d6af4 100644 --- a/apps/openmw/mwgui/class.cpp +++ b/apps/openmw/mwgui/class.cpp @@ -55,11 +55,6 @@ namespace MWGui center(); } - std::string GenerateClassResultDialog::getClassId() const - { - return mClassName->getCaption(); - } - void GenerateClassResultDialog::setClassId(const std::string &classId) { mCurrentClassId = classId; diff --git a/apps/openmw/mwgui/class.hpp b/apps/openmw/mwgui/class.hpp index a927767d9a..6f45459ad7 100644 --- a/apps/openmw/mwgui/class.hpp +++ b/apps/openmw/mwgui/class.hpp @@ -71,7 +71,6 @@ namespace MWGui public: GenerateClassResultDialog(); - std::string getClassId() const; void setClassId(const std::string &classId); bool exit() override { return false; } diff --git a/apps/openmw/mwgui/journalviewmodel.cpp b/apps/openmw/mwgui/journalviewmodel.cpp index 681398a06d..7e0832bb47 100644 --- a/apps/openmw/mwgui/journalviewmodel.cpp +++ b/apps/openmw/mwgui/journalviewmodel.cpp @@ -35,12 +35,12 @@ struct JournalViewModelImpl : JournalViewModel } /// \todo replace this nasty BS - static Utf8Span toUtf8Span (std::string const & str) + static Utf8Span toUtf8Span(std::string_view str) { if (str.size () == 0) return Utf8Span (Utf8Point (nullptr), Utf8Point (nullptr)); - Utf8Point point = reinterpret_cast (str.c_str ()); + Utf8Point point = reinterpret_cast(str.data()); return Utf8Span (point, point + str.size ()); } @@ -191,11 +191,11 @@ struct JournalViewModelImpl : JournalViewModel }; - void visitQuestNames (bool active_only, std::function visitor) const override + void visitQuestNames(bool active_only, std::function visitor) const override { MWBase::Journal * journal = MWBase::Environment::get ().getJournal (); - std::set visitedQuests; + std::set> visitedQuests; // Note that for purposes of the journal GUI, quests are identified by the name, not the ID, so several // different quest IDs can end up in the same quest log. A quest log should be considered finished @@ -225,7 +225,7 @@ struct JournalViewModelImpl : JournalViewModel visitor (quest.getName(), isFinished); - visitedQuests.insert(quest.getName()); + visitedQuests.emplace(quest.getName()); } } } @@ -305,7 +305,7 @@ struct JournalViewModelImpl : JournalViewModel visitor (toUtf8Span (topic.getName())); } - void visitTopicNamesStartingWith (Utf8Stream::UnicodeChar character, std::function < void (const std::string&) > visitor) const override + void visitTopicNamesStartingWith(Utf8Stream::UnicodeChar character, std::function < void (std::string_view) > visitor) const override { MWBase::Journal * journal = MWBase::Environment::get().getJournal(); diff --git a/apps/openmw/mwgui/journalviewmodel.hpp b/apps/openmw/mwgui/journalviewmodel.hpp index eda51b9300..c49ccf3e20 100644 --- a/apps/openmw/mwgui/journalviewmodel.hpp +++ b/apps/openmw/mwgui/journalviewmodel.hpp @@ -73,7 +73,7 @@ namespace MWGui virtual bool isEmpty () const = 0; /// walks the active and optionally completed, quests providing the name and completed status - virtual void visitQuestNames (bool active_only, std::function visitor) const = 0; + virtual void visitQuestNames(bool active_only, std::function visitor) const = 0; /// walks over the journal entries related to all quests with the given name /// If \a questName is empty, simply visits all journal entries @@ -83,7 +83,7 @@ namespace MWGui virtual void visitTopicName (TopicId topicId, std::function visitor) const = 0; /// walks over the topics whose names start with the character - virtual void visitTopicNamesStartingWith (Utf8Stream::UnicodeChar character, std::function < void (const std::string&) > visitor) const = 0; + virtual void visitTopicNamesStartingWith(Utf8Stream::UnicodeChar character, std::function < void (std::string_view) > visitor) const = 0; /// walks over the topic entries for the topic specified by its identifier virtual void visitTopicEntries (TopicId topicId, std::function visitor) const = 0; diff --git a/apps/openmw/mwgui/journalwindow.cpp b/apps/openmw/mwgui/journalwindow.cpp index 908028e239..45d5b6aa80 100644 --- a/apps/openmw/mwgui/journalwindow.cpp +++ b/apps/openmw/mwgui/journalwindow.cpp @@ -518,7 +518,7 @@ namespace AddNamesToList(Gui::MWList* list) : mList(list) {} Gui::MWList* mList; - void operator () (const std::string& name, bool finished=false) + void operator() (std::string_view name, bool finished = false) { mList->addItem(name); } @@ -528,7 +528,7 @@ namespace SetNamesInactive(Gui::MWList* list) : mList(list) {} Gui::MWList* mList; - void operator () (const std::string& name, bool finished) + void operator() (std::string_view name, bool finished) { if (finished) { diff --git a/apps/openmw/mwmechanics/levelledlist.hpp b/apps/openmw/mwmechanics/levelledlist.hpp index be5c5962bb..57dca34cf0 100644 --- a/apps/openmw/mwmechanics/levelledlist.hpp +++ b/apps/openmw/mwmechanics/levelledlist.hpp @@ -19,7 +19,7 @@ namespace MWMechanics { /// @return ID of resulting item, or empty if none - inline std::string getLevelledItem (const ESM::LevelledListBase* levItem, bool creature, Misc::Rng::Generator& prng) + inline std::string_view getLevelledItem(const ESM::LevelledListBase* levItem, bool creature, Misc::Rng::Generator& prng) { const std::vector& items = levItem->mList; @@ -27,9 +27,9 @@ namespace MWMechanics int playerLevel = player.getClass().getCreatureStats(player).getLevel(); if (Misc::Rng::roll0to99(prng) < levItem->mChanceNone) - return std::string(); + return {}; - std::vector candidates; + std::vector candidates; int highestLevel = 0; for (const auto& levelledItem : items) { @@ -42,7 +42,7 @@ namespace MWMechanics if (creature) allLevels = levItem->mFlags & ESM::CreatureLevList::AllLevels; - std::pair highest = std::make_pair(-1, ""); + std::pair highest = {-1, {}}; for (const auto& levelledItem : items) { if (playerLevel >= levelledItem.mLevel @@ -54,14 +54,14 @@ namespace MWMechanics } } if (candidates.empty()) - return std::string(); - std::string item = candidates[Misc::Rng::rollDice(candidates.size(), prng)]; + return {}; + std::string_view item = candidates[Misc::Rng::rollDice(candidates.size(), prng)]; // Vanilla doesn't fail on nonexistent items in levelled lists - if (!MWBase::Environment::get().getWorld()->getStore().find(Misc::StringUtils::lowerCase(item))) + if (!MWBase::Environment::get().getWorld()->getStore().find(item)) { Log(Debug::Warning) << "Warning: ignoring nonexistent item '" << item << "' in levelled list '" << levItem->mId << "'"; - return std::string(); + return {}; } // Is this another levelled item or a real item? diff --git a/apps/openmw/mwscript/containerextensions.cpp b/apps/openmw/mwscript/containerextensions.cpp index 68d714d58b..2fc2e9294a 100644 --- a/apps/openmw/mwscript/containerextensions.cpp +++ b/apps/openmw/mwscript/containerextensions.cpp @@ -64,7 +64,7 @@ namespace else { auto& prng = MWBase::Environment::get().getWorld()->getPrng(); - std::string itemId = MWMechanics::getLevelledItem(itemPtr.get()->mBase, false, prng); + std::string_view itemId = MWMechanics::getLevelledItem(itemPtr.get()->mBase, false, prng); if (itemId.empty()) return; MWWorld::ManualRef manualRef(MWBase::Environment::get().getWorld()->getStore(), itemId, 1); diff --git a/apps/openmw/mwworld/containerstore.cpp b/apps/openmw/mwworld/containerstore.cpp index e6a5581f44..ab55047756 100644 --- a/apps/openmw/mwworld/containerstore.cpp +++ b/apps/openmw/mwworld/containerstore.cpp @@ -543,7 +543,7 @@ void MWWorld::ContainerStore::fillNonRandom (const ESM::InventoryList& items, co mResolved = false; } -void MWWorld::ContainerStore::addInitialItem (const std::string& id, const std::string& owner, int count, +void MWWorld::ContainerStore::addInitialItem(std::string_view id, const std::string& owner, int count, Misc::Rng::Generator* prng, bool topLevel) { if (count == 0) return; //Don't restock with nothing. @@ -584,7 +584,7 @@ void MWWorld::ContainerStore::addInitialItemImp(const MWWorld::Ptr& ptr, const s } else { - std::string itemId = MWMechanics::getLevelledItem(ptr.get()->mBase, false, *prng); + std::string_view itemId = MWMechanics::getLevelledItem(ptr.get()->mBase, false, *prng); if (itemId.empty()) return; addInitialItem(itemId, owner, count, prng, false); diff --git a/apps/openmw/mwworld/containerstore.hpp b/apps/openmw/mwworld/containerstore.hpp index 74e46ea5f4..8bbd32af0d 100644 --- a/apps/openmw/mwworld/containerstore.hpp +++ b/apps/openmw/mwworld/containerstore.hpp @@ -126,7 +126,7 @@ namespace MWWorld std::weak_ptr mResolutionListener; ContainerStoreIterator addImp (const Ptr& ptr, int count, bool markModified = true); - void addInitialItem (const std::string& id, const std::string& owner, int count, Misc::Rng::Generator* prng, bool topLevel=true); + void addInitialItem(std::string_view id, const std::string& owner, int count, Misc::Rng::Generator* prng, bool topLevel=true); void addInitialItemImp (const MWWorld::Ptr& ptr, const std::string& owner, int count, Misc::Rng::Generator* prng, bool topLevel=true); template diff --git a/apps/openmw/mwworld/worldimp.cpp b/apps/openmw/mwworld/worldimp.cpp index 18b33616cd..b39971c8e0 100644 --- a/apps/openmw/mwworld/worldimp.cpp +++ b/apps/openmw/mwworld/worldimp.cpp @@ -3692,7 +3692,7 @@ namespace MWWorld for (int i=0; ifindWidget (getName() + "_item_" + name)->castType(); + std::string search = getName() + "_item_"; + search += name; + return mScrollView->findWidget(search)->castType(); } void MWList::scrollToTop() diff --git a/components/widgets/list.hpp b/components/widgets/list.hpp index be0339f33b..8d27d16ffa 100644 --- a/components/widgets/list.hpp +++ b/components/widgets/list.hpp @@ -36,14 +36,14 @@ namespace Gui */ void adjustSize(); - void addItem(const std::string& name); + void addItem(std::string_view name); void addSeparator(); ///< add a seperator between the current and the next item. void removeItem(const std::string& name); unsigned int getItemCount(); std::string getItemNameAt(unsigned int at); ///< \attention if there are separators, this method will return "" at the place where the separator is void clear(); - MyGUI::Button* getItemWidget(const std::string& name); + MyGUI::Button* getItemWidget(std::string_view name); ///< get widget for an item name, useful to set up tooltip void scrollToTop();