1 |
torben |
26 |
// |
2 |
|
|
// C++ Interface: %{MODULE} |
3 |
|
|
// |
4 |
|
|
// Description: |
5 |
|
|
// |
6 |
|
|
// |
7 |
|
|
// Author: %{AUTHOR} <%{EMAIL}>, (C) %{YEAR} |
8 |
|
|
// |
9 |
|
|
// Copyright: See COPYING file that comes with this distribution |
10 |
|
|
// |
11 |
|
|
// |
12 |
|
|
#ifndef _PosixSignalDispatcher_h_ |
13 |
|
|
#define _PosixSignalDispatcher_h_ |
14 |
|
|
|
15 |
|
|
#include <stdexcept> |
16 |
|
|
|
17 |
|
|
/* |
18 |
|
|
* Forward declarations. |
19 |
|
|
*/ |
20 |
|
|
class PosixSignalHandler ; |
21 |
|
|
|
22 |
|
|
/** |
23 |
|
|
|
24 |
|
|
@note The signal dispatcher will not interfere with any signals for which |
25 |
|
|
there are no signal handlers attached. Similarly, if a signal handler |
26 |
|
|
function is already attached to a signal that the dispatcher is asked to |
27 |
|
|
administer, that signal handler will be called after all the attached |
28 |
|
|
PosixSignalHandlers. |
29 |
|
|
|
30 |
|
|
@todo Make this a singleton class. |
31 |
|
|
|
32 |
|
|
*/ |
33 |
|
|
class PosixSignalDispatcher |
34 |
|
|
{ |
35 |
|
|
public: |
36 |
|
|
/** |
37 |
|
|
* This is a singleton class and there is only one instance of this |
38 |
|
|
* class per process. This instance can be obtained using the |
39 |
|
|
* GetInstance() method. |
40 |
|
|
*/ |
41 |
|
|
static PosixSignalDispatcher& Instance() ; |
42 |
|
|
|
43 |
|
|
/** |
44 |
|
|
* Exception thrown when AttachHandler() fails due to a runtime |
45 |
|
|
* error. |
46 |
|
|
*/ |
47 |
|
|
class CannotAttachHandler : public std::runtime_error |
48 |
|
|
{ |
49 |
|
|
public: |
50 |
|
|
CannotAttachHandler( const std::string& whatArg ) : |
51 |
|
|
runtime_error(whatArg) { } |
52 |
|
|
} ; |
53 |
|
|
|
54 |
|
|
/** |
55 |
|
|
* Exception thrown when DetachHandler() fails due to a runtime |
56 |
|
|
* error. |
57 |
|
|
*/ |
58 |
|
|
class CannotDetachHandler : public std::runtime_error |
59 |
|
|
{ |
60 |
|
|
public: |
61 |
|
|
CannotDetachHandler( const std::string& whatArg ) : |
62 |
|
|
runtime_error(whatArg) { } |
63 |
|
|
} ; |
64 |
|
|
|
65 |
|
|
/** |
66 |
|
|
* Attach a signal handler to the signal dispatcher. The signal |
67 |
|
|
* handler's HandlePosixSignal() method will be called every time |
68 |
|
|
* the specified signal is received. The signal handler should |
69 |
|
|
* not be destroyed while it attached to the signal dispatcher. |
70 |
|
|
* Otherwise, weird things are bound to happen (i.e. "undefined |
71 |
|
|
* behavior" shall ensue). Make sure you call DetachHandler() from |
72 |
|
|
* the destructor of the signal handler. |
73 |
|
|
* |
74 |
|
|
* If a PosixSignalHandler is attached to the same signal number |
75 |
|
|
* multiple times, it will be called multiple times. Furthermore, |
76 |
|
|
* it should also be detached as many times as it was attached |
77 |
|
|
* before it is destroyed. Otherwise, undefined behavior may result. |
78 |
|
|
* |
79 |
|
|
* @param posixSignalNumber The signal number that will result in |
80 |
|
|
* call to the HandlePosixSignal() method of the signal handler. |
81 |
|
|
* |
82 |
|
|
* @param signalHandler The signal handler to be invoked on receiving |
83 |
|
|
* a posixSignalNumber signal. |
84 |
|
|
*/ |
85 |
|
|
void AttachHandler( const int posixSignalNumber, |
86 |
|
|
PosixSignalHandler& signalHandler ) |
87 |
|
|
throw( CannotAttachHandler ) ; |
88 |
|
|
|
89 |
|
|
/** |
90 |
|
|
* Detach the specified signal handler from the signal dispatcher. |
91 |
|
|
* The signal handler will stop being called on receiving the |
92 |
|
|
* corresponding POSIX signal. If the signal handler is not |
93 |
|
|
* attached to the signal dispatcher when this method is called |
94 |
|
|
* then this method has no effect. |
95 |
|
|
* |
96 |
|
|
* @param posixSignalNumber The signal number corresponding to |
97 |
|
|
* the signal handler. |
98 |
|
|
* |
99 |
|
|
* @param signalHandler The signal handler to be detached. |
100 |
|
|
*/ |
101 |
|
|
void DetachHandler( const int posixSignalNumber, |
102 |
|
|
const PosixSignalHandler& signalHandler ) |
103 |
|
|
throw( CannotDetachHandler, |
104 |
|
|
std::logic_error ) ; |
105 |
|
|
private: |
106 |
|
|
/** |
107 |
|
|
* This is a singleton class and the only instances of this class |
108 |
|
|
* can only be accessed using the Instance() method. This is |
109 |
|
|
* enforced by making the default constructor a private member |
110 |
|
|
* disalloweing construction of new instances of this class |
111 |
|
|
*/ |
112 |
|
|
PosixSignalDispatcher() ; |
113 |
|
|
|
114 |
|
|
/** |
115 |
|
|
* This class cannot be subclassed. We enforce this by making |
116 |
|
|
* the destructor a private member. |
117 |
|
|
*/ |
118 |
|
|
~PosixSignalDispatcher() ; |
119 |
|
|
|
120 |
|
|
/** |
121 |
|
|
* Copying of an instance of this class is not allowed. We |
122 |
|
|
* enforce this by making the copy constructor and the |
123 |
|
|
* assignment operator private members. |
124 |
|
|
*/ |
125 |
|
|
PosixSignalDispatcher( const PosixSignalDispatcher& otherInstance ) ; |
126 |
|
|
|
127 |
|
|
/** |
128 |
|
|
* Copying of an instance of this class is not allowed. We |
129 |
|
|
* enforce this by making the copy constructor and the |
130 |
|
|
* assignment operator private members. |
131 |
|
|
*/ |
132 |
|
|
const PosixSignalDispatcher& |
133 |
|
|
operator=( const PosixSignalDispatcher& otherInstance ) ; |
134 |
|
|
} ; |
135 |
|
|
|
136 |
|
|
#endif |