I\'ve written a small utility class for C++11 which I use as a scope guard for easier handling of exception safety and similar things.
Seems somewhat like a hack. Bu
The implementation could be very much simplified using tr1::function and tr1::unique_ptr, as below:
namespace detail
{
class ScopeGuard
{
public:
explicit ScopeGuard(std::function onExitScope)
: onExitScope_(onExitScope), dismissed_(false)
{ }
~ScopeGuard()
{
try
{
if(!dismissed_)
{
onExitScope_();
}
}
catch(...){}
}
void Dismiss()
{
dismissed_ = true;
}
private:
std::function onExitScope_;
bool dismissed_;
// noncopyable
private:
ScopeGuard(ScopeGuard const&);
ScopeGuard& operator=(ScopeGuard const&);
};
}
inline std::unique_ptr CreateScopeGuard(std::function onExitScope)
{
return std::unique_ptr(new detail::ScopeGuard(onExitScope));
}