-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathSignals.hpp
85 lines (70 loc) · 2.7 KB
/
Signals.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
#ifndef LIBWHEEL_SIGNALS_HPP
#define LIBWHEEL_SIGNALS_HPP
#include <list>
#include <string>
#include <stdexcept>
#include <csignal>
#include <boost/function.hpp>
#include <sys/select.h>
#ifndef THROW
#ifdef DEBUG
#define THROW(x) throw x
#else
#define THROW(x)
#endif
#endif
namespace LibWheel
{
class SignalException : public std::exception
{
const std::string description;
const siginfo_t* siginfo;
const int number;
protected:
SignalException(int signum) throw();
SignalException(int signum, const char* desc) throw();
SignalException(int signum, const char* desc, const siginfo_t* info) throw();
public:
virtual ~SignalException() throw();
const siginfo_t* getInfo() const throw();
virtual const char* what() const throw();
int getSignalNumber() const throw();
};
class Interrupt : public SignalException
{
public:
Interrupt() throw();
explicit Interrupt(const siginfo_t* info) throw();
static int getSignalNumber();
};
namespace SignalQueue
{
enum Action {DEFAULT, IGNORE, HANDLE};
void setHandler(int sig, Action act) THROW((std::domain_error, std::invalid_argument));
void addHandler(int sig, boost::function<void()> act) THROW((std::invalid_argument));
template <typename T> void deleteHandler(int sig, const T& act) THROW((std::invalid_argument));
void deleteHandlers(int sig) THROW((std::invalid_argument));
void handleNext();
void handleAll();
bool pending() THROW((std::runtime_error));
int getReadFD();
int select(int n, fd_set* readfds, fd_set* writefds, fd_set* exceptfds, struct timeval* timeout);
std::list<boost::function<void()> >* getSignalTable();
template <typename T>
void
deleteHandler(int sig, const T& act) THROW((std::invalid_argument))
{
if ((sig < 1) || (sig > _NSIG))
throw std::invalid_argument("Invalid signal number");
std::list<boost::function<void()> >& list = getSignalTable()[sig];
for (std::list<boost::function<void()> >::iterator i = list.begin(); i != list.end(); )
{
if (boost::function_equal(*i, act))
i = list.erase(i);
else
++i;
}
}
} // namespace SignalQueue
} // namespace LibWheel
#endif /* LIBWHEEL_SIGNAL_HPP */