diff --git a/apps/openmw/mwscript/scriptmanagerimp.cpp b/apps/openmw/mwscript/scriptmanagerimp.cpp index 0dcf34afb..267ff7da6 100644 --- a/apps/openmw/mwscript/scriptmanagerimp.cpp +++ b/apps/openmw/mwscript/scriptmanagerimp.cpp @@ -171,7 +171,7 @@ namespace MWScript { Compiler::Locals locals; - mErrorHandler.setContext(name2 + "[local variables]"); + const Compiler::ContextOverride override(mErrorHandler, name2 + "[local variables]"); std::istringstream stream (script->mScriptText); Compiler::QuickFileParser parser (mErrorHandler, mCompilerContext, locals); diff --git a/components/compiler/streamerrorhandler.cpp b/components/compiler/streamerrorhandler.cpp index b5a3a8c9f..1c41d3f7f 100644 --- a/components/compiler/streamerrorhandler.cpp +++ b/components/compiler/streamerrorhandler.cpp @@ -62,4 +62,14 @@ namespace Compiler } StreamErrorHandler::StreamErrorHandler() {} + + ContextOverride::ContextOverride(StreamErrorHandler& handler, const std::string& context) : mHandler(handler), mContext(handler.mContext) + { + mHandler.setContext(context); + } + + ContextOverride::~ContextOverride() + { + mHandler.setContext(mContext); + } } diff --git a/components/compiler/streamerrorhandler.hpp b/components/compiler/streamerrorhandler.hpp index e203a6ef7..1f3b6e1ec 100644 --- a/components/compiler/streamerrorhandler.hpp +++ b/components/compiler/streamerrorhandler.hpp @@ -7,12 +7,14 @@ namespace Compiler { + class ContextOverride; /// \brief Error handler implementation: Write errors into logging stream class StreamErrorHandler : public ErrorHandler { std::string mContext; + friend class ContextOverride; // not implemented StreamErrorHandler (const StreamErrorHandler&); @@ -33,6 +35,19 @@ namespace Compiler StreamErrorHandler (); ///< constructor }; + + class ContextOverride + { + StreamErrorHandler& mHandler; + const std::string mContext; + public: + ContextOverride (StreamErrorHandler& handler, const std::string& context); + + ContextOverride (const ContextOverride&) = delete; + ContextOverride& operator= (const ContextOverride&) = delete; + + ~ContextOverride(); + }; } #endif