rntviewer/src/defer.h

16 lines
427 B
C
Raw Normal View History

2024-07-12 09:53:01 +02:00
template <typename F>
2024-07-10 17:04:11 +02:00
struct Defer_Guard {
Defer_Guard(F && f) : _f(f) {}
~Defer_Guard() { _f(); }
2024-07-12 09:53:01 +02:00
F _f;
2024-07-10 17:04:11 +02:00
};
struct Defer_Guard_Helper {
2024-09-27 16:31:21 +02:00
template <typename F>
Defer_Guard<F> operator+(F&& f) { return Defer_Guard<F>(static_cast<F &&>(f)); }
2024-07-10 17:04:11 +02:00
};
#define CONCAT_STR_INTERNAL(A, B) A##B
#define CONCAT_STR(A, B) CONCAT_STR_INTERNAL(A, B)
#define defer const auto CONCAT_STR(defer_, __LINE__) = Defer_Guard_Helper{} + [&]