mirror of
https://github.com/SantaSpeen/BeamMP-Server.git
synced 2026-02-16 06:40:40 +00:00
Add Defer<FnT> type to defer actions to the end of scope.
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -1,4 +1,5 @@
|
|||||||
.idea/
|
.idea/
|
||||||
|
*.orig
|
||||||
*.toml
|
*.toml
|
||||||
boost_*
|
boost_*
|
||||||
Resources
|
Resources
|
||||||
|
|||||||
18
include/Defer.h
Normal file
18
include/Defer.h
Normal 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;
|
||||||
|
};
|
||||||
Reference in New Issue
Block a user