forked from user1095108/generic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscopeexit.hpp
87 lines (71 loc) · 2.56 KB
/
scopeexit.hpp
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#ifndef GNR_SCOPEEXIT_HPP
# define GNR_SCOPEEXIT_HPP
# pragma once
#include <utility>
/* This counts the number of args */
#define NARGS_SEQ(_1, _2, _3, _4, _5, _6, _7, _8, _9, _10, N, ...) N
#define NARGS(...) NARGS_SEQ(__VA_ARGS__, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1)
/* This will let macros expand before concating them */
#ifndef PRIMITIVE_CAT
# define PRIMITIVE_CAT(x, y) x ## y
#endif // PRIMITIVE_CAT
#ifndef CAT
# define CAT(x, y) PRIMITIVE_CAT(x, y)
#endif // CAT
/* This will pop the last argument off */
#define POP_LAST(...) CAT(POP_LAST_, NARGS(__VA_ARGS__))(__VA_ARGS__)
#define POP_LAST_1(x1)
#define POP_LAST_2(x1, x2) x1
#define POP_LAST_3(x1, x2, x3) x1, x2
#define POP_LAST_4(x1, x2, x3, x4) x1, x2, x3
#define POP_LAST_5(x1, x2, x3, x4, x5) x1, x2, x3, x4
#define POP_LAST_6(x1, x2, x3, x4, x5, x6) x1, x2, x3, x4, x5
#define POP_LAST_7(x1, x2, x3, x4, x5, x6, x7) x1, x2, x3, x4, x5, x6
#define POP_LAST_8(x1, x2, x3, x4, x5, x6, x7, x8) x1, x2, x3, x4, x5, x6, x7
#define POP_LAST_9(x1, x2, x3, x4, x5, x6, x7, x8, x9) x1, x2, x3, x4, x5, x6, x7, x8
#define POP_LAST_10(x1, x2, x3, x4, x5, x6, x7, x8, x9, x10) x1, x2, x3, x4, x5, x6, x7, x8, x9
/* This will return the last argument */
#define LAST(...) CAT(LAST_, NARGS(__VA_ARGS__))(__VA_ARGS__)
#define LAST_1(x1) x1
#define LAST_2(x1, x2) x2
#define LAST_3(x1, x2, x3) x3
#define LAST_4(x1, x2, x3, x4) x4
#define LAST_5(x1, x2, x3, x4, x5) x5
#define LAST_6(x1, x2, x3, x4, x5, x6) x6
#define LAST_7(x1, x2, x3, x4, x5, x6, x7) x7
#define LAST_8(x1, x2, x3, x4, x5, x6, x7, x8) x8
#define LAST_9(x1, x2, x3, x4, x5, x6, x7, x8, x9) x9
#define LAST_10(x1, x2, x3, x4, x5, x6, x7, x8, x9, x10) x10
namespace
{
template <typename T>
class scope_exit
{
T f_;
public:
explicit scope_exit(T&& f) noexcept : f_(std::forward<T>(f))
{
static_assert(noexcept(f_()), "throwing functors are unsupported");
}
~scope_exit() noexcept { f_(); }
};
class scope_exit_helper { };
template<typename T>
inline scope_exit<T> make_scope_exit(T&& f) noexcept
{
return scope_exit<T>(std::forward<T>(f));
}
template<typename T>
inline scope_exit<T> operator+(scope_exit_helper&&, T&& f) noexcept
{
return scope_exit<T>(std::forward<T>(f));
}
}
#define SCOPE_EXIT(...) auto const CAT(scope_exit_, __LINE__) \
(make_scope_exit([POP_LAST(__VA_ARGS__)]() noexcept \
{ LAST(__VA_ARGS__); }))
#define SCOPE_EXIT_ auto const CAT(scope_exit_, __LINE__) = \
scope_exit_helper()+[&]() noexcept
#define SCOPE_EXIT__(...) auto const CAT(scope_exit_, __LINE__) =\
scope_exit_helper()+[__VA_ARGS__]() noexcept
#endif // GNR_SCOPEEXIT_HPP