-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbasics.h
45 lines (37 loc) · 1.05 KB
/
basics.h
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
#pragma once
#include <iostream>
#include <utility>
using namespace std::string_view_literals;
template<typename F>
struct ScopeExit
{
F f;
ScopeExit(F f)
: f(f)
{
}
~ScopeExit() { f(); }
};
struct DEFER_TAG
{
};
template<class F>
ScopeExit<F> operator+(DEFER_TAG, F &&f)
{
return ScopeExit<F>{std::forward<F>(f)};
}
#define DEFER_1(x, y) x##y
#define DEFER_2(x, y) DEFER_1(x, y)
#define defer auto DEFER_2(ScopeExit, __LINE__) = DEFER_TAG{} + [&]()
#define UNREACHED \
do \
{ \
std::cout << "UNREACHED macro reached" << std::endl; \
std::abort(); \
} while (false);
#define FATAL(message) \
do \
{ \
std::cout << message << std::endl; \
std::abort(); \
} while (false);