Utilities
Utilities header only library
utils::OnEnd Class Reference

Helper class that performs an action when it is destroyed. More...

Public Types

using Callback = std::function< void()>
 Callback is a void function taking 0 arguments.
 

Public Member Functions

 OnEnd (Callback F)
 Constructor sets Callback.
 
 ~OnEnd ()
 Destructor calls Callback.
 
 OnEnd (const OnEnd &)=delete
 
OnEnd & operator= (const OnEnd &)=delete
 
 OnEnd (OnEnd &&)=delete
 
OnEnd & operator= (OnEnd &&)=delete
 

Detailed Description

Helper class that performs an action when it is destroyed.

Motivation:

  • When a function has many boundary conditions and returns in multiple places.

Example usage:

bool Init()
{
m->device->LockGUI(); // Locks GUI
OnEnd _([&]() { m->device->UnlockGUI(); });
if (condition_1)
return false; // GUI is unlocked
if (condition_2)
return false; // GUI is unlocked
return true; // GUI is unlocked
}