diff --git a/BeefySysLib/Common.h b/BeefySysLib/Common.h index 4e5d3824..9911c067 100644 --- a/BeefySysLib/Common.h +++ b/BeefySysLib/Common.h @@ -253,42 +253,22 @@ struct RemoveTypePointer }; #ifndef BF_SMALL -struct OnScopeExit -{ - std::function mFunc; - - OnScopeExit(std::function func) : mFunc(func) - { - } - - ~OnScopeExit() - { - mFunc(); - } +template +struct BF_Defer { + F f; + BF_Defer(F f) : f(f) {} + ~BF_Defer() { f(); } }; -#define CONCAT_INTERNAL(x,y) x##y -#define CONCAT(x,y) CONCAT_INTERNAL(x,y) +template +BF_Defer BF_defer_func(F f) { + return BF_Defer(f); +} -template -struct ExitScope -{ - T lambda; - ExitScope(T lambda) :lambda(lambda) {} - ~ExitScope() { lambda(); } - ExitScope(const ExitScope&); -private: - ExitScope & operator =(const ExitScope&); -}; - -class ExitScopeHelp -{ -public: - template - ExitScope operator+(T t) { return t; } -}; - -#define defer const auto& CONCAT(defer__, __LINE__) = ExitScopeHelp() + [&]() +#define DEFER_1(x, y) x##y +#define DEFER_2(x, y) DEFER_1(x, y) +#define DEFER_3(x) DEFER_2(x, __COUNTER__) +#define defer(code) auto DEFER_3(_defer_) = BF_defer_func([&](){code;}) #endif //BF_SMALL NS_BF_END