The "current plugin" mechanism itself isn't very clean, but I don't want to start making sweeping changes here. I just wanted to propose a simple technique to clean up the usage of the current plugin system.
So far there are two general approaches:
CurrentPluginScope pscope (pDoc); // saves current plugin
pDoc->m_CurrentPlugin = stuff; // this is done manually
// reset automatically
CurrentPluginScope pScope (pDoc, plugin); // saves as well as sets
// do stuff
// reset automatically
The first is kinder to loops, since you set it manually. The second would need to be within the loop to be clean, which is needless overhead. Neither really cares what you do within the "scope", it just guarantees that the current plugin will be unchanged after the scope ends.
Nick Gammon said: *save the current plugin
*call each enabled plugin
*restore the current plugin
Exactly. I prefer the approach where CurrentPluginScope only handles steps one and three; the meat of the algorithm remains in the user code, including setting the current plugin.
Nick Gammon said: And since you pass the document to it, conceivably as a side-effect it could do anything, so that isn't particularly clean.
We could get into making it generic RAIIScope and pass it user-defined acquire/release functors, but that's really getting out of hand. Theres no way to reset the current plugin without that CMUSHclientDoc pointer, hence it must be passed. Like I said, I don't really want to make sweeping changes here. Ideally m_CurrentPlugin wouldn't be needed at all, but that's not what I'm trying to solve. |