-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandler.h
79 lines (64 loc) · 1.68 KB
/
handler.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
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
/**
* COPYRIGHT 2014 (C) Jason Volk
* COPYRIGHT 2014 (C) Svetlana Tkachenko
*
* DISTRIBUTED UNDER THE GNU GENERAL PUBLIC LICENSE (GPL) (see: LICENSE)
*/
enum Flag : uint8_t
{
RECURRING = 0x01,
};
enum Prio : uint8_t
{
HOOK = 0,
LIB = 64,
USER = 128,
};
using flag_t = std::underlying_type<Flag>::type;
using prio_t = std::underlying_type<Prio>::type;
template<class Prototype>
struct Handler
{
std::function<Prototype> func;
flag_t flags;
prio_t prio;
public:
auto &get_flags() const { return flags; }
auto &get_prio() const { return prio; }
bool is(const flag_t &flags) const { return (this->flags & flags) == flags; }
template<class... Args> void operator()(Args&&... args) const;
Handler(const std::function<Prototype> &func = nullptr,
const flag_t &flags = 0,
const prio_t &prio = Prio::USER);
};
template<class Prototype>
Handler<Prototype>::Handler(const std::function<Prototype> &func,
const flag_t &flags,
const prio_t &prio):
func(func),
flags(flags),
prio(prio)
{
}
template<class Prototype>
template<class... Args>
void Handler<Prototype>::operator()(Args&&... args)
const try
{
func(std::forward<Args>(args)...);
}
catch(const Internal &e)
{
std::cerr << "HANDLER INTERNAL: \033[1;45;37m" << e << "\033[0m" << std::endl;
Stream::clear();
}
catch(const Interrupted &)
{
Stream::clear();
throw;
}
catch(const std::exception &e)
{
std::cerr << "UNHANDLED: \033[1;41;37m" << e.what() << "\033[0m" << std::endl;
Stream::clear();
}