Add Defer<FnT> type to defer actions to the end of scope.

This commit is contained in:
Lion Kortlepel 2021-08-15 04:36:52 +02:00 committed by Lion
parent d054214b7f
commit 8ec90d5186
2 changed files with 19 additions and 0 deletions

1
.gitignore vendored
View File

@ -1,4 +1,5 @@
.idea/
*.orig
*.toml
boost_*
Resources

18
include/Defer.h Normal file
View File

@ -0,0 +1,18 @@
#pragma once
#include <functional>
template <typename FnT>
class Defer final {
public:
Defer(FnT fn)
: mFunction([&fn] { (void)fn(); }) { }
~Defer() {
if (mFunction) {
mFunction();
}
}
private:
std::function<void()> mFunction;
};