From 8958e29187683194ee9d4e0775caf47884331585 Mon Sep 17 00:00:00 2001 From: Evil Eye Date: Thu, 2 Apr 2020 20:14:52 +0200 Subject: [PATCH 1/3] reset errorhandler context --- apps/openmw/mwscript/scriptmanagerimp.cpp | 2 +- components/compiler/streamerrorhandler.cpp | 17 ++++++++++++++++- components/compiler/streamerrorhandler.hpp | 13 ++++++++++++- 3 files changed, 29 insertions(+), 3 deletions(-) diff --git a/apps/openmw/mwscript/scriptmanagerimp.cpp b/apps/openmw/mwscript/scriptmanagerimp.cpp index 0dcf34afb3..c63beeec3f 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]"); + Compiler::ContextRestore restore = mErrorHandler.setContext(name2 + "[local variables]", true); std::istringstream stream (script->mScriptText); Compiler::QuickFileParser parser (mErrorHandler, mCompilerContext, locals); diff --git a/components/compiler/streamerrorhandler.cpp b/components/compiler/streamerrorhandler.cpp index b5a3a8c9fa..2528b53798 100644 --- a/components/compiler/streamerrorhandler.cpp +++ b/components/compiler/streamerrorhandler.cpp @@ -56,10 +56,25 @@ namespace Compiler Log(logLevel) << text.str(); } - void StreamErrorHandler::setContext(const std::string &context) + ContextRestore StreamErrorHandler::setContext(const std::string &context, bool restore) { + if (!restore) + { + mContext = context; + return {nullptr, {}}; + } + ContextRestore restorer(this, mContext); mContext = context; + return restorer; } StreamErrorHandler::StreamErrorHandler() {} + + ContextRestore::ContextRestore(StreamErrorHandler* handler, const std::string& context) : mHandler(handler), mContext(context) {} + + ContextRestore::~ContextRestore() + { + if(mHandler) + mHandler->setContext(mContext); + } } diff --git a/components/compiler/streamerrorhandler.hpp b/components/compiler/streamerrorhandler.hpp index e203a6ef71..2b4aeda50f 100644 --- a/components/compiler/streamerrorhandler.hpp +++ b/components/compiler/streamerrorhandler.hpp @@ -7,6 +7,7 @@ namespace Compiler { + class ContextRestore; /// \brief Error handler implementation: Write errors into logging stream class StreamErrorHandler : public ErrorHandler @@ -26,13 +27,23 @@ namespace Compiler public: - void setContext(const std::string& context); + ContextRestore setContext(const std::string& context, bool restore = false); // constructors StreamErrorHandler (); ///< constructor }; + + class ContextRestore + { + StreamErrorHandler* mHandler; + const std::string mContext; + public: + ContextRestore (StreamErrorHandler* handler, const std::string& context); + + ~ContextRestore(); + }; } #endif From a16727d5e30a31ab35923ba83ddbd217272e2792 Mon Sep 17 00:00:00 2001 From: Evil Eye Date: Thu, 2 Apr 2020 20:27:52 +0200 Subject: [PATCH 2/3] implement move constructor --- apps/openmw/mwscript/scriptmanagerimp.cpp | 2 +- components/compiler/streamerrorhandler.cpp | 5 +++++ components/compiler/streamerrorhandler.hpp | 2 ++ 3 files changed, 8 insertions(+), 1 deletion(-) diff --git a/apps/openmw/mwscript/scriptmanagerimp.cpp b/apps/openmw/mwscript/scriptmanagerimp.cpp index c63beeec3f..9966875475 100644 --- a/apps/openmw/mwscript/scriptmanagerimp.cpp +++ b/apps/openmw/mwscript/scriptmanagerimp.cpp @@ -171,7 +171,7 @@ namespace MWScript { Compiler::Locals locals; - Compiler::ContextRestore restore = mErrorHandler.setContext(name2 + "[local variables]", true); + const Compiler::ContextRestore&& restore = mErrorHandler.setContext(name2 + "[local variables]", true); std::istringstream stream (script->mScriptText); Compiler::QuickFileParser parser (mErrorHandler, mCompilerContext, locals); diff --git a/components/compiler/streamerrorhandler.cpp b/components/compiler/streamerrorhandler.cpp index 2528b53798..c9a3a4e7fb 100644 --- a/components/compiler/streamerrorhandler.cpp +++ b/components/compiler/streamerrorhandler.cpp @@ -72,6 +72,11 @@ namespace Compiler ContextRestore::ContextRestore(StreamErrorHandler* handler, const std::string& context) : mHandler(handler), mContext(context) {} + ContextRestore::ContextRestore(ContextRestore&& other) : mHandler(other.mHandler), mContext(other.mContext) + { + other.mHandler = nullptr; + } + ContextRestore::~ContextRestore() { if(mHandler) diff --git a/components/compiler/streamerrorhandler.hpp b/components/compiler/streamerrorhandler.hpp index 2b4aeda50f..848c976a39 100644 --- a/components/compiler/streamerrorhandler.hpp +++ b/components/compiler/streamerrorhandler.hpp @@ -42,6 +42,8 @@ namespace Compiler public: ContextRestore (StreamErrorHandler* handler, const std::string& context); + ContextRestore (ContextRestore&& other); + ~ContextRestore(); }; } From 8c433d587c319ad7052b285a8e71d8c78d67e320 Mon Sep 17 00:00:00 2001 From: Evil Eye Date: Sat, 4 Apr 2020 14:09:00 +0200 Subject: [PATCH 3/3] less complicated context override --- apps/openmw/mwscript/scriptmanagerimp.cpp | 2 +- components/compiler/streamerrorhandler.cpp | 20 +++++--------------- components/compiler/streamerrorhandler.hpp | 16 +++++++++------- 3 files changed, 15 insertions(+), 23 deletions(-) diff --git a/apps/openmw/mwscript/scriptmanagerimp.cpp b/apps/openmw/mwscript/scriptmanagerimp.cpp index 9966875475..267ff7da68 100644 --- a/apps/openmw/mwscript/scriptmanagerimp.cpp +++ b/apps/openmw/mwscript/scriptmanagerimp.cpp @@ -171,7 +171,7 @@ namespace MWScript { Compiler::Locals locals; - const Compiler::ContextRestore&& restore = mErrorHandler.setContext(name2 + "[local variables]", true); + 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 c9a3a4e7fb..1c41d3f7fc 100644 --- a/components/compiler/streamerrorhandler.cpp +++ b/components/compiler/streamerrorhandler.cpp @@ -56,30 +56,20 @@ namespace Compiler Log(logLevel) << text.str(); } - ContextRestore StreamErrorHandler::setContext(const std::string &context, bool restore) + void StreamErrorHandler::setContext(const std::string &context) { - if (!restore) - { - mContext = context; - return {nullptr, {}}; - } - ContextRestore restorer(this, mContext); mContext = context; - return restorer; } StreamErrorHandler::StreamErrorHandler() {} - ContextRestore::ContextRestore(StreamErrorHandler* handler, const std::string& context) : mHandler(handler), mContext(context) {} - - ContextRestore::ContextRestore(ContextRestore&& other) : mHandler(other.mHandler), mContext(other.mContext) + ContextOverride::ContextOverride(StreamErrorHandler& handler, const std::string& context) : mHandler(handler), mContext(handler.mContext) { - other.mHandler = nullptr; + mHandler.setContext(context); } - ContextRestore::~ContextRestore() + ContextOverride::~ContextOverride() { - if(mHandler) - mHandler->setContext(mContext); + mHandler.setContext(mContext); } } diff --git a/components/compiler/streamerrorhandler.hpp b/components/compiler/streamerrorhandler.hpp index 848c976a39..1f3b6e1ece 100644 --- a/components/compiler/streamerrorhandler.hpp +++ b/components/compiler/streamerrorhandler.hpp @@ -7,13 +7,14 @@ namespace Compiler { - class ContextRestore; + 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&); @@ -27,7 +28,7 @@ namespace Compiler public: - ContextRestore setContext(const std::string& context, bool restore = false); + void setContext(const std::string& context); // constructors @@ -35,16 +36,17 @@ namespace Compiler ///< constructor }; - class ContextRestore + class ContextOverride { - StreamErrorHandler* mHandler; + StreamErrorHandler& mHandler; const std::string mContext; public: - ContextRestore (StreamErrorHandler* handler, const std::string& context); + ContextOverride (StreamErrorHandler& handler, const std::string& context); - ContextRestore (ContextRestore&& other); + ContextOverride (const ContextOverride&) = delete; + ContextOverride& operator= (const ContextOverride&) = delete; - ~ContextRestore(); + ~ContextOverride(); }; }