So I was mulling over a few different things, and I remembered how I was talking to you, Nick, about making the current-plugin management cleaner. My suggestion at the time had exception safety issues. But today I think I have a solution that's much cleaner -and- has no exception safety issues. Here's the code:
Example:
If an exception occurs anywhere within the scope, the destructor is automatically called as the stack is unwound. Also, it's destructed automatically at the end of its scope anyways.
I'm not planning on using this just yet, but it seems to be a nice construct, and I'll definitely be putting it on my list of cleanup ideas. What are your thoughts?
class PluginScoper
{
public:
PluginScoper (CMUSHclientDoc* doc, CPlugin* plugin)
: _doc(doc)
{
_saved_plugin = _doc.m_CurrentPlugin;
_doc.m_CurrentPlugin = plugin;
}
~PluginScoper ()
{
_doc.m_CurrentPlugin = _saved_plugin;
}
private:
CMUSHclientDoc* _doc;
CPlugin* _saved_plugin;
};Example:
{
PluginScoper pscope (doc, plugin);
// stuff here
// automatically reset here
}If an exception occurs anywhere within the scope, the destructor is automatically called as the stack is unwound. Also, it's destructed automatically at the end of its scope anyways.
I'm not planning on using this just yet, but it seems to be a nice construct, and I'll definitely be putting it on my list of cleanup ideas. What are your thoughts?