Finally.h

// Finally.h
//
// Author David Barrett-Lennard
// (C)opyright Cedanet Pty Ltd 2013

#pragma once
#ifndef Ceda_cxUtils_Finally_H
#define Ceda_cxUtils_Finally_H

#include <functional>

namespace ceda
{

/*
Example usage:

    void test()
    {
        PSpace* ps = OpenPSpace(...);
        Finally _( [ps] { Close(ps); } );
    }

or

    void test()
    {
        PSpace* ps = OpenPSpace(...);
        Finally _( [=] { Close(ps); } );
    }
*/

struct Finally
{
    Finally(std::function<void()> f) : f_(f) {}
    ~Finally() { f_(); }
    std::function<void()> f_;
};

} // namespace ceda

#endif // include guard