mirror of
https://github.com/beefytech/Beef.git
synced 2025-06-10 04:22:20 +02:00
New defer
This commit is contained in:
parent
c8ca66ec5c
commit
578b886526
1 changed files with 13 additions and 33 deletions
|
@ -253,42 +253,22 @@ struct RemoveTypePointer<T*>
|
||||||
};
|
};
|
||||||
|
|
||||||
#ifndef BF_SMALL
|
#ifndef BF_SMALL
|
||||||
struct OnScopeExit
|
template <typename F>
|
||||||
{
|
struct BF_Defer {
|
||||||
std::function<void()> mFunc;
|
F f;
|
||||||
|
BF_Defer(F f) : f(f) {}
|
||||||
OnScopeExit(std::function<void()> func) : mFunc(func)
|
~BF_Defer() { f(); }
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
~OnScopeExit()
|
|
||||||
{
|
|
||||||
mFunc();
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#define CONCAT_INTERNAL(x,y) x##y
|
template <typename F>
|
||||||
#define CONCAT(x,y) CONCAT_INTERNAL(x,y)
|
BF_Defer<F> BF_defer_func(F f) {
|
||||||
|
return BF_Defer<F>(f);
|
||||||
|
}
|
||||||
|
|
||||||
template<typename T>
|
#define DEFER_1(x, y) x##y
|
||||||
struct ExitScope
|
#define DEFER_2(x, y) DEFER_1(x, y)
|
||||||
{
|
#define DEFER_3(x) DEFER_2(x, __COUNTER__)
|
||||||
T lambda;
|
#define defer(code) auto DEFER_3(_defer_) = BF_defer_func([&](){code;})
|
||||||
ExitScope(T lambda) :lambda(lambda) {}
|
|
||||||
~ExitScope() { lambda(); }
|
|
||||||
ExitScope(const ExitScope&);
|
|
||||||
private:
|
|
||||||
ExitScope & operator =(const ExitScope&);
|
|
||||||
};
|
|
||||||
|
|
||||||
class ExitScopeHelp
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
template<typename T>
|
|
||||||
ExitScope<T> operator+(T t) { return t; }
|
|
||||||
};
|
|
||||||
|
|
||||||
#define defer const auto& CONCAT(defer__, __LINE__) = ExitScopeHelp() + [&]()
|
|
||||||
#endif //BF_SMALL
|
#endif //BF_SMALL
|
||||||
|
|
||||||
NS_BF_END
|
NS_BF_END
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue