lix/src/libutil/finally.hh

24 lines
530 B
C++
Raw Normal View History

#pragma once
///@file
/**
* A trivial class to run a function at the end of a scope.
*/
template<typename Fn>
class Finally
{
private:
Fn fun;
bool movedFrom = false;
public:
Finally(Fn fun) : fun(std::move(fun)) { }
// Copying Finallys is definitely not a good idea and will cause them to be
// called twice.
Finally(Finally &other) = delete;
Finally(Finally &&other) : fun(std::move(other.fun)) {
other.movedFrom = true;
}
~Finally() noexcept(false) { if (!movedFrom) fun(); }
};