From 874ff8828821c9227f6a715d1e9fc259200be1ae Mon Sep 17 00:00:00 2001 From: "florent.teppe" Date: Sat, 15 Oct 2022 17:13:50 +0200 Subject: [PATCH] In the interpretter, the id is no longer passed by vallue, but as a const reference instead. In getMembersLocal(, a reference to a reference wrapper is used, because the id can change, bu all we need to do is change a pointer.No need to change the value --- apps/openmw/mwscript/interpretercontext.cpp | 50 +++++++++++-------- apps/openmw/mwscript/interpretercontext.hpp | 16 +++--- .../openmw_test_suite/mwscript/test_utils.hpp | 12 ++--- components/interpreter/context.hpp | 12 ++--- 4 files changed, 48 insertions(+), 42 deletions(-) diff --git a/apps/openmw/mwscript/interpretercontext.cpp b/apps/openmw/mwscript/interpretercontext.cpp index 8e23121410..7508d4a785 100644 --- a/apps/openmw/mwscript/interpretercontext.cpp +++ b/apps/openmw/mwscript/interpretercontext.cpp @@ -44,7 +44,7 @@ namespace MWScript } } - const Locals& InterpreterContext::getMemberLocals(ESM::RefId& id, bool global) const + const Locals& InterpreterContext::getMemberLocals(std::reference_wrapper& id, bool global) const { if (global) { @@ -54,7 +54,7 @@ namespace MWScript { const MWWorld::Ptr ptr = getReferenceImp(id, false); - id = ptr.getClass().getScript(ptr); + id = std::ref(ptr.getClass().getScript(ptr)); ptr.getRefData().setLocals(*MWBase::Environment::get().getWorld()->getStore().get().find(id)); @@ -62,7 +62,7 @@ namespace MWScript } } - Locals& InterpreterContext::getMemberLocals(ESM::RefId& id, bool global) + Locals& InterpreterContext::getMemberLocals(std::reference_wrapper& id, bool global) { if (global) { @@ -72,7 +72,7 @@ namespace MWScript { const MWWorld::Ptr ptr = getReferenceImp(id, false); - id = ptr.getClass().getScript(ptr); + id = std::ref(ptr.getClass().getScript(ptr)); ptr.getRefData().setLocals(*MWBase::Environment::get().getWorld()->getStore().get().find(id)); @@ -428,46 +428,52 @@ namespace MWScript } } - int InterpreterContext::getMemberShort(ESM::RefId id, std::string_view name, bool global) const + int InterpreterContext::getMemberShort(const ESM::RefId& id, std::string_view name, bool global) const { - const Locals& locals = getMemberLocals(id, global); + auto idRefWrapper = std::ref(id); + const Locals& locals = getMemberLocals(idRefWrapper, global); - return locals.mShorts[findLocalVariableIndex(id, name, 's')]; + return locals.mShorts[findLocalVariableIndex(idRefWrapper, name, 's')]; } - int InterpreterContext::getMemberLong(ESM::RefId id, std::string_view name, bool global) const + int InterpreterContext::getMemberLong(const ESM::RefId& id, std::string_view name, bool global) const { - const Locals& locals = getMemberLocals(id, global); + auto idRefWrapper = std::ref(id); + const Locals& locals = getMemberLocals(idRefWrapper, global); - return locals.mLongs[findLocalVariableIndex(id, name, 'l')]; + return locals.mLongs[findLocalVariableIndex(idRefWrapper, name, 'l')]; } - float InterpreterContext::getMemberFloat(ESM::RefId id, std::string_view name, bool global) const + float InterpreterContext::getMemberFloat(const ESM::RefId& id, std::string_view name, bool global) const { - const Locals& locals = getMemberLocals(id, global); + auto idRefWrapper = std::ref(id); + const Locals& locals = getMemberLocals(idRefWrapper, global); - return locals.mFloats[findLocalVariableIndex(id, name, 'f')]; + return locals.mFloats[findLocalVariableIndex(idRefWrapper, name, 'f')]; } - void InterpreterContext::setMemberShort(ESM::RefId id, std::string_view name, int value, bool global) + void InterpreterContext::setMemberShort(const ESM::RefId& id, std::string_view name, int value, bool global) { - Locals& locals = getMemberLocals(id, global); + auto idRefWrapper = std::ref(id); + Locals& locals = getMemberLocals(idRefWrapper, global); - locals.mShorts[findLocalVariableIndex(id, name, 's')] = value; + locals.mShorts[findLocalVariableIndex(idRefWrapper, name, 's')] = value; } - void InterpreterContext::setMemberLong(ESM::RefId id, std::string_view name, int value, bool global) + void InterpreterContext::setMemberLong(const ESM::RefId& id, std::string_view name, int value, bool global) { - Locals& locals = getMemberLocals(id, global); + auto idRefWrapper = std::ref(id); + Locals& locals = getMemberLocals(idRefWrapper, global); - locals.mLongs[findLocalVariableIndex(id, name, 'l')] = value; + locals.mLongs[findLocalVariableIndex(idRefWrapper, name, 'l')] = value; } - void InterpreterContext::setMemberFloat(ESM::RefId id, std::string_view name, float value, bool global) + void InterpreterContext::setMemberFloat(const ESM::RefId& id, std::string_view name, float value, bool global) { - Locals& locals = getMemberLocals(id, global); + auto idRefWrapper = std::ref(id); + Locals& locals = getMemberLocals(idRefWrapper, global); - locals.mFloats[findLocalVariableIndex(id, name, 'f')] = value; + locals.mFloats[findLocalVariableIndex(idRefWrapper, name, 'f')] = value; } MWWorld::Ptr InterpreterContext::getReference(bool required) const diff --git a/apps/openmw/mwscript/interpretercontext.hpp b/apps/openmw/mwscript/interpretercontext.hpp index 31297ec24b..07ca70ef09 100644 --- a/apps/openmw/mwscript/interpretercontext.hpp +++ b/apps/openmw/mwscript/interpretercontext.hpp @@ -32,10 +32,10 @@ namespace MWScript const MWWorld::Ptr getReferenceImp( const ESM::RefId& id = ESM::RefId::sEmpty, bool activeOnly = false, bool doThrow = true) const; - const Locals& getMemberLocals(ESM::RefId& id, bool global) const; + const Locals& getMemberLocals(std::reference_wrapper& id, bool global) const; ///< \a id is changed to the respective script ID, if \a id wasn't a script ID before - Locals& getMemberLocals(ESM::RefId& id, bool global); + Locals& getMemberLocals(std::reference_wrapper& id, bool global); ///< \a id is changed to the respective script ID, if \a id wasn't a script ID before /// Throws an exception if local variable can't be found. @@ -113,17 +113,17 @@ namespace MWScript void executeActivation(const MWWorld::Ptr& ptr, const MWWorld::Ptr& actor); ///< Execute the activation action for this ptr. If ptr is mActivated, mark activation as handled. - int getMemberShort(ESM::RefId id, std::string_view name, bool global) const override; + int getMemberShort(const ESM::RefId& id, std::string_view name, bool global) const override; - int getMemberLong(ESM::RefId id, std::string_view name, bool global) const override; + int getMemberLong(const ESM::RefId& id, std::string_view name, bool global) const override; - float getMemberFloat(ESM::RefId id, std::string_view name, bool global) const override; + float getMemberFloat(const ESM::RefId& id, std::string_view name, bool global) const override; - void setMemberShort(ESM::RefId id, std::string_view name, int value, bool global) override; + void setMemberShort(const ESM::RefId& id, std::string_view name, int value, bool global) override; - void setMemberLong(ESM::RefId id, std::string_view name, int value, bool global) override; + void setMemberLong(const ESM::RefId& id, std::string_view name, int value, bool global) override; - void setMemberFloat(ESM::RefId id, std::string_view name, float value, bool global) override; + void setMemberFloat(const ESM::RefId& id, std::string_view name, float value, bool global) override; MWWorld::Ptr getReference(bool required = true) const; ///< Reference, that the script is running from (can be empty) diff --git a/apps/openmw_test_suite/mwscript/test_utils.hpp b/apps/openmw_test_suite/mwscript/test_utils.hpp index aa4cc046e6..4c328973b0 100644 --- a/apps/openmw_test_suite/mwscript/test_utils.hpp +++ b/apps/openmw_test_suite/mwscript/test_utils.hpp @@ -207,7 +207,7 @@ namespace std::string_view getCurrentCellName() const override { return {}; } - int getMemberShort(const ESM::RefId id, std::string_view name, bool global) const override + int getMemberShort(const ESM::RefId& id, std::string_view name, bool global) const override { auto it = mMembers.find(id.getRefIdString()); if (it != mMembers.end()) @@ -215,7 +215,7 @@ namespace return {}; } - int getMemberLong(const ESM::RefId id, std::string_view name, bool global) const override + int getMemberLong(const ESM::RefId& id, std::string_view name, bool global) const override { auto it = mMembers.find(id.getRefIdString()); if (it != mMembers.end()) @@ -223,7 +223,7 @@ namespace return {}; } - float getMemberFloat(const ESM::RefId id, std::string_view name, bool global) const override + float getMemberFloat(const ESM::RefId& id, std::string_view name, bool global) const override { auto it = mMembers.find(id.getRefIdString()); if (it != mMembers.end()) @@ -231,17 +231,17 @@ namespace return {}; } - void setMemberShort(const ESM::RefId id, std::string_view name, int value, bool global) override + void setMemberShort(const ESM::RefId& id, std::string_view name, int value, bool global) override { mMembers[id.getRefIdString()].setShort(name, value); }; - void setMemberLong(const ESM::RefId id, std::string_view name, int value, bool global) override + void setMemberLong(const ESM::RefId& id, std::string_view name, int value, bool global) override { mMembers[id.getRefIdString()].setLong(name, value); }; - void setMemberFloat(const ESM::RefId id, std::string_view name, float value, bool global) override + void setMemberFloat(const ESM::RefId& id, std::string_view name, float value, bool global) override { mMembers[id.getRefIdString()].setFloat(name, value); }; diff --git a/components/interpreter/context.hpp b/components/interpreter/context.hpp index 3bffdd642d..6162aa340a 100644 --- a/components/interpreter/context.hpp +++ b/components/interpreter/context.hpp @@ -79,17 +79,17 @@ namespace Interpreter virtual std::string_view getCurrentCellName() const = 0; - virtual int getMemberShort(ESM::RefId id, std::string_view name, bool global) const = 0; + virtual int getMemberShort(const ESM::RefId& id, std::string_view name, bool global) const = 0; - virtual int getMemberLong(ESM::RefId id, std::string_view name, bool global) const = 0; + virtual int getMemberLong(const ESM::RefId& id, std::string_view name, bool global) const = 0; - virtual float getMemberFloat(ESM::RefId id, std::string_view name, bool global) const = 0; + virtual float getMemberFloat(const ESM::RefId& id, std::string_view name, bool global) const = 0; - virtual void setMemberShort(ESM::RefId id, std::string_view name, int value, bool global) = 0; + virtual void setMemberShort(const ESM::RefId& id, std::string_view name, int value, bool global) = 0; - virtual void setMemberLong(ESM::RefId id, std::string_view name, int value, bool global) = 0; + virtual void setMemberLong(const ESM::RefId& id, std::string_view name, int value, bool global) = 0; - virtual void setMemberFloat(ESM::RefId id, std::string_view name, float value, bool global) = 0; + virtual void setMemberFloat(const ESM::RefId& id, std::string_view name, float value, bool global) = 0; }; }