-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDefer.h
More file actions
37 lines (28 loc) · 1 KB
/
Copy pathDefer.h
File metadata and controls
37 lines (28 loc) · 1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#pragma once
#include <utility>
NS_BEGIN
//#################################################################################################
template<typename F>
struct DeferGuard final
{
explicit DeferGuard(F &&fnDefer) : m_fnDefer(std::move(fnDefer)) {}
DeferGuard(const DeferGuard &src) = delete;
DeferGuard(DeferGuard &&src) = default; // Move constructor is required for C++14 support
~DeferGuard(void) {m_fnDefer();}
DeferGuard &operator=(const DeferGuard &src) = delete;
DeferGuard &operator=(DeferGuard &&src) = delete;
F m_fnDefer;
};
// Helper function for C++14 support
template<typename F>
DeferGuard<F> MakeDefer(F &&fnDefer)
{
return DeferGuard<F>(std::move(fnDefer));
}
#define CONCAT_IMPL(a, b) a##b
#define CONCAT(a, b) CONCAT_IMPL(a, b)
// C++14 version
#define DEFER(code) const auto CONCAT(_defer_, __LINE__) = MakeDefer([&](void){code;})
// C++17 version
//#define DEFER(code) const auto CONCAT(_defer_, __LINE__) = DeferGuard([&](void){code;})
NS_END