4.11. SigHandler Class

SigHandler is a powerful UNIX signal manager/dispatcher class. It is based on the white paper, published in [DCS97].

4.11.1. DEFINITION

		
class SigHandler {
public:

    virtual int install (int signum_,
                         EventHandler*  new_hand_,
                         SigAction*     new_disp_ = 0,
                         EventHandler** old_hand_ = 0,
                         SigAction*     old_disp_ = 0);

    virtual int remove (int signum_,
                        EventHandler* eh_       = 0,
                        SigAction*    new_disp_ = 0,
                        SigAction*    old_disp_ = 0);

    static void dispatch (int signum_);

    EventHandler* handler (int signum_, EventHandler* new_); 
    EventHandler* handler (int signum_);
};
	  

SigHandler class plays the role of a centralized signal dispatcher for the running application. It is based on Callback Pattern that overcomes two major difficulties in using UNIX signals with C++ programs:

SigHandler class allows to install a EventHandler object in order to manage application's reaction to a UNIX signal. When the registered signal is delivered by the operating system to the running application, handle_signal() method of the registered EventHandler is called to handle the signal.

4.11.2. USAGE


#include <assa/SigHandler.h>

void foo(void)
{ 
    SigHandler sh;
    EventHandler  eh;
     
    sh.install (SIGHUP, &eh);

    // ... go on with the function flow
    // ... at some point might be interrupted by SIGNUP
    // ... continume with the function flow
     
    sh.remove (SIGHUP, &eh);
}
	  

In the example above, sh plays the role of a signal manager and signal handler repository. When the SIGHUP signal is delivered to the process, sh dispatches the signal to the application by calling eh.handle_signal(SIGHUP) callback function. of eh.

Note

If conventional "C" signal handler was installed prior to calling sh.install(), it would be lost irreversibly.