class utils::OnEnd

Overview

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

#include <OnEnd.h>

class OnEnd {
public:
    // typedefs

    typedef std::function<void()> Callback;

    // construction

    OnEnd(Callback F);
    OnEnd(const OnEnd&);
    OnEnd(OnEnd&&);
    ~OnEnd();

    // methods

    OnEnd& operator=(const OnEnd&);
    OnEnd& operator=(OnEnd&&);
};

Detailed Documentation

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
}

Typedefs

typedef std::function<void()> Callback

Callback is a void function taking 0 arguments.

Construction

OnEnd(Callback F)

Constructor sets Callback.

~OnEnd()

Destructor calls Callback.