From 4eb6c48285e30ca5306f63ab622bd5ae3a6085b6 Mon Sep 17 00:00:00 2001 From: Evil Eye Date: Fri, 12 Aug 2022 18:42:12 +0200 Subject: [PATCH] Avoid copying in InputManager --- apps/openmw/mwbase/inputmanager.hpp | 6 +++--- apps/openmw/mwgui/settingswindow.cpp | 15 ++++++--------- apps/openmw/mwinput/bindingsmanager.cpp | 12 ++++++------ apps/openmw/mwinput/bindingsmanager.hpp | 6 +++--- apps/openmw/mwinput/inputmanagerimp.cpp | 6 +++--- apps/openmw/mwinput/inputmanagerimp.hpp | 6 +++--- apps/openmw/mwscript/interpretercontext.cpp | 16 ++++++++-------- 7 files changed, 32 insertions(+), 35 deletions(-) diff --git a/apps/openmw/mwbase/inputmanager.hpp b/apps/openmw/mwbase/inputmanager.hpp index e22d7f00bc..14679ffd25 100644 --- a/apps/openmw/mwbase/inputmanager.hpp +++ b/apps/openmw/mwbase/inputmanager.hpp @@ -52,7 +52,7 @@ namespace MWBase virtual void toggleControlSwitch(std::string_view sw, bool value) = 0; virtual bool getControlSwitch(std::string_view sw) = 0; - virtual std::string getActionDescription (int action) const = 0; + virtual std::string_view getActionDescription(int action) const = 0; virtual std::string getActionKeyBindingName (int action) const = 0; virtual std::string getActionControllerBindingName (int action) const = 0; virtual bool actionIsActive(int action) const = 0; @@ -64,9 +64,9 @@ namespace MWBase virtual int getMouseMoveY() const = 0; ///Actions available for binding to keyboard buttons - virtual std::vector getActionKeySorting() = 0; + virtual const std::initializer_list& getActionKeySorting() = 0; ///Actions available for binding to controller buttons - virtual std::vector getActionControllerSorting() = 0; + virtual const std::initializer_list& getActionControllerSorting() = 0; virtual int getNumActions() = 0; ///If keyboard is true, only pay attention to keyboard events. If false, only pay attention to controller events (excluding esc) virtual void enableDetectingBindingMode (int action, bool keyboard) = 0; diff --git a/apps/openmw/mwgui/settingswindow.cpp b/apps/openmw/mwgui/settingswindow.cpp index f83d3a562f..d2429dfcf0 100644 --- a/apps/openmw/mwgui/settingswindow.cpp +++ b/apps/openmw/mwgui/settingswindow.cpp @@ -700,23 +700,20 @@ namespace MWGui MyGUI::Gui::getInstance().destroyWidget(mControlsBox->getChildAt(0)); MWBase::Environment::get().getWindowManager()->removeStaticMessageBox(); - std::vector actions; - if(mKeyboardMode) - actions = MWBase::Environment::get().getInputManager()->getActionKeySorting(); - else - actions = MWBase::Environment::get().getInputManager()->getActionControllerSorting(); + const auto inputManager = MWBase::Environment::get().getInputManager(); + const auto& actions = mKeyboardMode ? inputManager->getActionKeySorting() : inputManager->getActionControllerSorting(); for (const int& action : actions) { - std::string desc = MWBase::Environment::get().getInputManager()->getActionDescription (action); - if (desc == "") + std::string desc{inputManager->getActionDescription(action)}; + if (desc.empty()) continue; std::string binding; if(mKeyboardMode) - binding = MWBase::Environment::get().getInputManager()->getActionKeyBindingName(action); + binding = inputManager->getActionKeyBindingName(action); else - binding = MWBase::Environment::get().getInputManager()->getActionControllerBindingName(action); + binding = inputManager->getActionControllerBindingName(action); Gui::SharedStateButton* leftText = mControlsBox->createWidget("SandTextButton", MyGUI::IntCoord(), MyGUI::Align::Default); leftText->setCaptionWithReplacing(desc); diff --git a/apps/openmw/mwinput/bindingsmanager.cpp b/apps/openmw/mwinput/bindingsmanager.cpp index b3e3590125..a1b4845250 100644 --- a/apps/openmw/mwinput/bindingsmanager.cpp +++ b/apps/openmw/mwinput/bindingsmanager.cpp @@ -421,7 +421,7 @@ namespace MWInput } } - std::string BindingsManager::getActionDescription(int action) + std::string_view BindingsManager::getActionDescription(int action) { switch (action) { @@ -506,7 +506,7 @@ namespace MWInput case A_TogglePostProcessorHUD: return "#{SettingsMenu:TogglePostProcessorHUD}"; default: - return std::string(); // not configurable + return {}; // not configurable } } @@ -557,9 +557,9 @@ namespace MWInput return "#{sNone}"; } - std::vector BindingsManager::getActionKeySorting() + const std::initializer_list& BindingsManager::getActionKeySorting() { - static const std::vector actions + static const std::initializer_list actions { A_MoveForward, A_MoveBackward, A_MoveLeft, A_MoveRight, A_TogglePOV, A_ZoomIn, A_ZoomOut, A_Run, A_AlwaysRun, A_Sneak, A_Activate, A_Use, A_ToggleWeapon, A_ToggleSpell, @@ -572,9 +572,9 @@ namespace MWInput return actions; } - std::vector BindingsManager::getActionControllerSorting() + const std::initializer_list& BindingsManager::getActionControllerSorting() { - static const std::vector actions + static const std::initializer_list actions { A_TogglePOV, A_ZoomIn, A_ZoomOut, A_Sneak, A_Activate, A_Use, A_ToggleWeapon, A_ToggleSpell, A_AutoMove, A_Jump, A_Inventory, A_Journal, A_Rest, A_QuickSave, A_QuickLoad, A_ToggleHUD, diff --git a/apps/openmw/mwinput/bindingsmanager.hpp b/apps/openmw/mwinput/bindingsmanager.hpp index 668cccd4ca..3808a21dfd 100644 --- a/apps/openmw/mwinput/bindingsmanager.hpp +++ b/apps/openmw/mwinput/bindingsmanager.hpp @@ -19,11 +19,11 @@ namespace MWInput virtual ~BindingsManager(); - std::string getActionDescription (int action); + std::string_view getActionDescription(int action); std::string getActionKeyBindingName (int action); std::string getActionControllerBindingName (int action); - std::vector getActionKeySorting(); - std::vector getActionControllerSorting(); + const std::initializer_list& getActionKeySorting(); + const std::initializer_list& getActionControllerSorting(); void enableDetectingBindingMode (int action, bool keyboard); bool isDetectingBindingState() const; diff --git a/apps/openmw/mwinput/inputmanagerimp.cpp b/apps/openmw/mwinput/inputmanagerimp.cpp index abc66de70d..9a7b84722a 100644 --- a/apps/openmw/mwinput/inputmanagerimp.cpp +++ b/apps/openmw/mwinput/inputmanagerimp.cpp @@ -146,7 +146,7 @@ namespace MWInput return mActionManager->getIdleTime() > 0.5; } - std::string InputManager::getActionDescription(int action) const + std::string_view InputManager::getActionDescription(int action) const { return mBindingsManager->getActionDescription(action); } @@ -191,12 +191,12 @@ namespace MWInput return mMouseManager->getMouseMoveY(); } - std::vector InputManager::getActionKeySorting() + const std::initializer_list& InputManager::getActionKeySorting() { return mBindingsManager->getActionKeySorting(); } - std::vector InputManager::getActionControllerSorting() + const std::initializer_list& InputManager::getActionControllerSorting() { return mBindingsManager->getActionControllerSorting(); } diff --git a/apps/openmw/mwinput/inputmanagerimp.hpp b/apps/openmw/mwinput/inputmanagerimp.hpp index 7b3029e11a..dbc980e446 100644 --- a/apps/openmw/mwinput/inputmanagerimp.hpp +++ b/apps/openmw/mwinput/inputmanagerimp.hpp @@ -76,7 +76,7 @@ namespace MWInput void toggleControlSwitch(std::string_view sw, bool value) override; bool getControlSwitch(std::string_view sw) override; - std::string getActionDescription (int action) const override; + std::string_view getActionDescription(int action) const override; std::string getActionKeyBindingName (int action) const override; std::string getActionControllerBindingName (int action) const override; bool actionIsActive(int action) const override; @@ -88,8 +88,8 @@ namespace MWInput int getMouseMoveY() const override; int getNumActions() override { return A_Last; } - std::vector getActionKeySorting() override; - std::vector getActionControllerSorting() override; + const std::initializer_list& getActionKeySorting() override; + const std::initializer_list& getActionControllerSorting() override; void enableDetectingBindingMode (int action, bool keyboard) override; void resetToDefaultKeyBindings() override; void resetToDefaultControllerBindings() override; diff --git a/apps/openmw/mwscript/interpretercontext.cpp b/apps/openmw/mwscript/interpretercontext.cpp index 78e41d9aeb..535697df89 100644 --- a/apps/openmw/mwscript/interpretercontext.cpp +++ b/apps/openmw/mwscript/interpretercontext.cpp @@ -254,11 +254,11 @@ namespace MWScript std::string InterpreterContext::getActionBinding(std::string_view targetAction) const { MWBase::InputManager* input = MWBase::Environment::get().getInputManager(); - std::vector actions = input->getActionKeySorting (); + const auto& actions = input->getActionKeySorting(); for (const int action : actions) { - std::string desc = input->getActionDescription (action); - if(desc == "") + std::string_view desc = input->getActionDescription(action); + if(desc.empty()) continue; if(desc == targetAction) @@ -334,14 +334,14 @@ namespace MWScript std::string InterpreterContext::getPCRace() const { MWBase::World *world = MWBase::Environment::get().getWorld(); - std::string race = world->getPlayerPtr().get()->mBase->mRace; + const std::string& race = world->getPlayerPtr().get()->mBase->mRace; return world->getStore().get().find(race)->mName; } std::string InterpreterContext::getPCClass() const { MWBase::World *world = MWBase::Environment::get().getWorld(); - std::string class_ = world->getPlayerPtr().get()->mBase->mClass; + const std::string& class_ = world->getPlayerPtr().get()->mBase->mClass; return world->getStore().get().find(class_)->mName; } @@ -369,7 +369,7 @@ namespace MWScript const ESM::Faction *faction = store.get().find(factionId); if(rank < 0 || rank > 9) // there are only 10 ranks - return ""; + return {}; return faction->mRanks[rank]; } @@ -399,7 +399,7 @@ namespace MWScript const ESM::Faction *faction = store.get().find(factionId); if(rank < 0) - return ""; + return {}; return faction->mRanks[rank]; } @@ -487,7 +487,7 @@ namespace MWScript MWWorld::Ptr InterpreterContext::getReference(bool required) const { - return getReferenceImp ("", true, required); + return getReferenceImp({}, true, required); } void InterpreterContext::updatePtr(const MWWorld::Ptr& base, const MWWorld::Ptr& updated)