aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/libraries/ode-0.9/tests/CppTestHarness/SignalTranslator.h
blob: 39fc2259a6eb6763f227c872042056703bf16f37 (plain)
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
#ifndef SIGNAL_TRANSLATOR_H
#define SIGNAL_TRANSLATOR_H

#include <signal.h>
#include <setjmp.h>

namespace CppTestHarness
{

template <int SIGNAL>
class SignalTranslator {
public:
	SignalTranslator()
	{
		//setup new signal handler
		struct sigaction act;
		act.sa_handler = signalHandler;
		sigemptyset(&act.sa_mask);
		act.sa_flags = 0;

		sigaction(SIGNAL, &act, &m_oldAction);

		if (sigsetjmp(getJumpPoint(), 1) != 0)
		{
			//if signal thrown we will return here from handler
			throw "Unhandled system exception";
		}
	}

	~SignalTranslator()
	{
		sigaction(SIGNAL, &m_oldAction, 0);
	}

private:
	static void signalHandler(int signum)
	{
		siglongjmp(getJumpPoint(), signum);
	}

		static sigjmp_buf& getJumpPoint()
		{
			static sigjmp_buf jmpPnt;
			return jmpPnt;
		}

	struct sigaction m_oldAction;
};

} //CppTestHarness

#endif //SIGNAL_TRANSLATOR_H