/***************************************************************************
			System signal Class Header ( signal_handle.h )
			-----------------------------------------

	begin      : 2015/03/18
	copyright  : (C) 2013 Solbox Inc.
	author     : Development 1 Team
					- 2015/03/19 - 1st dadamin
	email      : storage.sd@solbox.com
	version    : 3.5.0

CopyRight(C) 2005 Solbox Inc. All Rights reserved.
Redistribution and use in source and binary forms, with or with out
modification, are not permitted in outside of Solbox Inc.
***************************************************************************/

#ifndef __CLASS_SIGNAL_HANDLER__
#define __CLASS_SIGNAL_HANDLER__

// reference http://www.cs.wustl.edu/~schmidt/signal-patterns.html 

#include </usr/include/sys/signal.h>
#include <csignal>

class Event_Handler
{
public:
	// Hook method for the signal hook method.
	virtual int handle_signal(int signum) = 0;

	// ... other hook methods for other types of
	// events such as timers, I/O, and 
	// synchronization objects.
};

class Signal_Handler
{
public:
	// Entry point.
	static Signal_Handler *instance();

	// Register an event handler <eh> for <signum>
	// and return a pointer to any existing <Event_Handler>
	// that was previously registered to handle <signum>.
	Event_Handler *register_handler(int signum,
		Event_Handler *eh);

	// Remove the <Event_Handler> for <signum>
	// by setting the slot in the <signal_handlers_>
	// table to NULL.
	int remove_handler(int signum);

	// Register ignore signal
	int register_ignore(int signum);
private:
	// Ensure we're a Singleton.
	Signal_Handler();
	~Signal_Handler();

	// Singleton pointer.
	static Signal_Handler *instance_;

	// Entry point adapter installed into <sigaction> 
	// (must be a static method or a stand-alone 
	// extern "C" function).
	static void dispatcher(int signum);

	// Table of pointers to concrete <Event_Handler>s
	// registered by applications.  NSIG is the number of 
	// signals defined in </usr/include/sys/signal.h>.
	static Event_Handler *signal_handlers_[NSIG];
};

#endif // __CLASS_SIGNAL_HANDLER__
