finally.hh: delete copy constructor which is a bad idea

Change-Id: I6d0b5736893c44bddc6f5789b452b434f8671b9b
This commit is contained in:
jade 2024-03-09 22:05:50 -08:00
parent af515baf6e
commit 45f6e3521a

View file

@ -9,8 +9,15 @@ class Finally
{
private:
Fn fun;
bool movedFrom = false;
public:
Finally(Fn fun) : fun(std::move(fun)) { }
~Finally() { 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() { if (!movedFrom) fun(); }
};