less complicated context override

pull/578/head
Evil Eye 5 years ago
parent a16727d5e3
commit 8c433d587c

@ -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);

@ -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);
}
}

@ -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();
};
}

Loading…
Cancel
Save