Line | |
---|
1 | #ifndef SIGNAL_TRANSLATOR_H |
---|
2 | #define SIGNAL_TRANSLATOR_H |
---|
3 | |
---|
4 | #include <signal.h> |
---|
5 | #include <setjmp.h> |
---|
6 | |
---|
7 | namespace CppTestHarness |
---|
8 | { |
---|
9 | |
---|
10 | template <int SIGNAL> |
---|
11 | class SignalTranslator { |
---|
12 | public: |
---|
13 | SignalTranslator() |
---|
14 | { |
---|
15 | //setup new signal handler |
---|
16 | struct sigaction act; |
---|
17 | act.sa_handler = signalHandler; |
---|
18 | sigemptyset(&act.sa_mask); |
---|
19 | act.sa_flags = 0; |
---|
20 | |
---|
21 | sigaction(SIGNAL, &act, &m_oldAction); |
---|
22 | |
---|
23 | if (sigsetjmp(getJumpPoint(), 1) != 0) |
---|
24 | { |
---|
25 | //if signal thrown we will return here from handler |
---|
26 | throw "Unhandled system exception"; |
---|
27 | } |
---|
28 | } |
---|
29 | |
---|
30 | ~SignalTranslator() |
---|
31 | { |
---|
32 | sigaction(SIGNAL, &m_oldAction, 0); |
---|
33 | } |
---|
34 | |
---|
35 | private: |
---|
36 | static void signalHandler(int signum) |
---|
37 | { |
---|
38 | siglongjmp(getJumpPoint(), signum); |
---|
39 | } |
---|
40 | |
---|
41 | static sigjmp_buf& getJumpPoint() |
---|
42 | { |
---|
43 | static sigjmp_buf jmpPnt; |
---|
44 | return jmpPnt; |
---|
45 | } |
---|
46 | |
---|
47 | struct sigaction m_oldAction; |
---|
48 | }; |
---|
49 | |
---|
50 | } //CppTestHarness |
---|
51 | |
---|
52 | #endif //SIGNAL_TRANSLATOR_H |
---|
53 | |
---|
Note: See
TracBrowser
for help on using the repository browser.