Loading...
Searching...
No Matches
SigHandlersList.h
Go to the documentation of this file.
// -*- c++ -*-
//---------------------------------------------------------------------------
// SigHandlersList.h
//------------------------------------------------------------------------------
// Copyright (c) 1997 by Vladislav Grinchenko
//
// Permission to use, copy, modify, and distribute this software
// and its documentation for any purpose and without fee is hereby
// granted, provided that the above copyright notice appear in all
// copies. The author makes no representations about the suitability
// of this software for any purpose. It is provided "as is" without
// express or implied warranty.
//---------------------------------------------------------------------------
#ifndef _SigHandlersList_h
#define _SigHandlersList_h
#include <signal.h>
#include <errno.h>
#include <sys/time.h>
#include <sys/types.h>
#include "assa/SigHandler.h"
#include <set>
using std::set;
namespace ASSA {
#if !defined(WIN32)
class CFUNC_Handler : public EventHandler
{
public:
CFUNC_Handler (C_SIG_HANDLER csigh_);
int handle_signal (int signum_);
C_SIG_HANDLER handler () { return m_c_sig_hand; }
private:
C_SIG_HANDLER m_c_sig_hand;
};
class SigHandlersList
{
public:
typedef EventHandler* key_type;
typedef EventHandler* data_type;
struct CompSHL {
bool operator () (const key_type c1_, const key_type c2_) const
{
// This wouldn't fly on 64-bit machines, 'cause ptr size there is 8 bytes long
// return int(c1_) < int(c2_);
//
return (c1_ < c2_);
}
};
typedef set< key_type, CompSHL > set_t;
typedef set< key_type, CompSHL >::iterator iterator;
static SigHandlersList* instance (int signum_);
~SigHandlersList ();
bool empty () const;
size_t size () const;
bool insert (data_type data_);
void erase (const key_type key_);
void erase (iterator it_);
void erase ();
iterator begin ();
iterator end ();
iterator find (const key_type key_);
CFUNC_Handler* cfunc_handler (CFUNC_Handler* cfp_);
CFUNC_Handler* cfunc_handler () const;
void seen_cfunc_handler (bool ft_);
bool seen_cfunc_handler () const;
protected:
SigHandlersList (); // Singleton
SigHandlersList (const SigHandlersList& map_); // prohibit copying
SigHandlersList& operator= (const SigHandlersList& map_);
public:
static SigHandlersList* m_instance[NSIG];
private:
set_t* m_set;
int m_seen_cfh;
CFUNC_Handler* m_cfhp;
};
//-------------------------------------------------------------------------
//----------------------- SigHandlersList Inlines -------------------------
//-------------------------------------------------------------------------
inline
SigHandlersList::
SigHandlersList ()
: m_seen_cfh (false), m_cfhp (NULL)
{
trace_with_mask("SigHandlersList::SigHandlersList", SIGHAND);
m_set = new set_t;
}
inline
SigHandlersList::
~SigHandlersList ()
{
trace_with_mask("SigHandlersList::~SigHandlersList", SIGHAND);
erase ();
delete m_set;
m_set = NULL;
}
inline SigHandlersList*
SigHandlersList::
instance (int signum_)
{
trace_with_mask("SigHandlersList::instance", SIGHAND);
DL((APP, "m_instance[%d] = 0x%x\n", signum_,
SigHandlersList::m_instance[signum_]));
if (SigHandlersList::m_instance[signum_] == 0) {
DL((APP, "new SigHandlersList allocated\n"));
SigHandlersList::m_instance[signum_] = new SigHandlersList();
}
return SigHandlersList::m_instance[signum_];
}
inline bool
SigHandlersList::
empty () const
{
trace_with_mask("SigHandlersList::empty", SIGHAND);
// true if map is empty, false otherwise
return m_set->empty ();
}
inline size_t
SigHandlersList::
size () const
{
trace_with_mask("SigHandlersList::size", SIGHAND);
// return number of elements in the map
return m_set->size ();
}
inline bool
SigHandlersList::
insert (data_type eh_)
{
trace_with_mask("SigHandlersList::insert", SIGHAND);
/*---
Insert 'eh_' into the set. set::insert() returns a 'pair' object.
If the set doesn't contain an element that matches 'eh_', insert a
copy of 'eh_' and returns a 'pair' whose first element is an
iterator positioned at the new element and second element is
'true'.
If the set already contains an element that matches 'eh_', returns
a pair whose first element is an iterator positioned at the
existing element and second element is false!
---*/
set_t::const_iterator it = m_set->find (eh_);
/*--- Not in the set ---*/
if (it == m_set->end ()) {
return (m_set->insert (eh_)).second;
}
/*--- Already in the set ---*/
return true;
}
inline void
SigHandlersList::
erase (const key_type key_)
{
// return number of erased elements
trace_with_mask("SigHandlersList::erase(key_)", SIGHAND);
m_set->erase (key_);
}
inline void
SigHandlersList::
erase ()
{
// empty the map
trace_with_mask("SigHandlersList::erase(void)", SIGHAND);
m_set->erase (m_set->begin(), m_set->end());
}
inline void
SigHandlersList::
erase(iterator it_)
{
// erase element pointed by iterator
trace_with_mask("SigHandlersList::erase(it_)", SIGHAND);
m_set->erase(it_);
}
inline SigHandlersList::iterator
SigHandlersList::
begin ()
{
trace_with_mask("SigHandlersList::begin()", SIGHAND);
return m_set->begin ();
}
inline SigHandlersList::iterator
SigHandlersList::
end ()
{
trace_with_mask("SigHandlersList::end", SIGHAND);
return m_set->end ();
}
inline SigHandlersList::iterator
SigHandlersList::
find (const key_type key_)
{
trace_with_mask("SigHandlersList::find", SIGHAND);
return m_set->find (key_);
}
inline CFUNC_Handler*
SigHandlersList::
cfunc_handler (CFUNC_Handler* cfhp_)
{
trace_with_mask("SigHandlersList::cfunc_handler", SIGHAND);
CFUNC_Handler* old_cfhp = m_cfhp;
m_cfhp = cfhp_;
m_seen_cfh = cfhp_ == NULL ? false : true;
return old_cfhp;
}
inline CFUNC_Handler*
SigHandlersList::
cfunc_handler () const
{
trace_with_mask("SigHandlersList::cfunc_handler", SIGHAND);
return m_cfhp;
}
inline void
SigHandlersList::
seen_cfunc_handler (bool ft_)
{
trace_with_mask("SigHandlersList::seen_cfunc_handler", SIGHAND);
m_seen_cfh = ft_;
}
inline bool
SigHandlersList::
seen_cfunc_handler () const
{
trace_with_mask("SigHandlersList::seen_cfunc_handler", SIGHAND);
return m_seen_cfh;
}
//-------------------------------------------------------------------------
//------------------------ CFUNC_Handler Inlines --------------------------
//-------------------------------------------------------------------------
inline
CFUNC_Handler::
CFUNC_Handler (C_SIG_HANDLER csigh_)
: m_c_sig_hand (csigh_)
{
trace_with_mask("CFUNC_Handler::CFUNC_Handler", SIGHAND);
}
inline int
CFUNC_Handler::
handle_signal (int signum_)
{
trace_with_mask("CFUNC_Handler::handle_signal", SIGHAND);
if (m_c_sig_hand) {
(*m_c_sig_hand)(signum_);
}
return 1;
}
#endif // !defined(WIN32)
} // end namespace ASSA
#endif /* _SigHandlersList_h */
DL
#define DL(X)
A macro for writing debug message to the Logger.
Definition Logger.h:273
trace_with_mask
#define trace_with_mask(s, m)
trace_with_mask() is used to trace function call chain in C++ program.
Definition Logger.h:437
C_SIG_HANDLER
void(* C_SIG_HANDLER)(int)
Definition SigAction.h:28
SigHandler.h
Class SigHandler is a UNIX signal handlers manager/dispatcher class.
ASSA::AutoPtrRef
A wrapper class to provide AutoPtr with reference semantics.
Definition AutoPtr.h:32
ASSA::CFUNC_Handler
CFUNC_Handler class.
Definition SigHandlersList.h:45
ASSA::CFUNC_Handler::handle_signal
int handle_signal(int signum_)
Signal handler callback.
Definition SigHandlersList.h:379
ASSA::CFUNC_Handler::handler
C_SIG_HANDLER handler()
Definition SigHandlersList.h:50
ASSA::CFUNC_Handler::CFUNC_Handler
CFUNC_Handler(C_SIG_HANDLER csigh_)
Definition SigHandlersList.h:371
ASSA::CFUNC_Handler::m_c_sig_hand
C_SIG_HANDLER m_c_sig_hand
Definition SigHandlersList.h:53
ASSA::EventHandler
EventHandler class.
Definition EventHandler.h:103
ASSA::SigHandlersList
SigHandlersList class.
Definition SigHandlersList.h:66
ASSA::SigHandlersList::SigHandlersList
SigHandlersList(const SigHandlersList &map_)
ASSA::SigHandlersList::empty
bool empty() const
Is list empty.
Definition SigHandlersList.h:219
ASSA::SigHandlersList::operator=
SigHandlersList & operator=(const SigHandlersList &map_)
ASSA::SigHandlersList::key_type
EventHandler * key_type
Definition SigHandlersList.h:68
ASSA::SigHandlersList::data_type
EventHandler * data_type
Definition SigHandlersList.h:69
ASSA::SigHandlersList::m_instance
static SigHandlersList * m_instance[NSIG]
Static map of signal numbers to SigHandlerLists.
Definition SigHandlersList.h:159
ASSA::SigHandlersList::find
iterator find(const key_type key_)
Find event handler by its pointer key_.
Definition SigHandlersList.h:318
ASSA::SigHandlersList::m_seen_cfh
int m_seen_cfh
If true this flag indicates that 3rd party event handler has already been installed prior taking cont...
Definition SigHandlersList.h:169
ASSA::SigHandlersList::m_set
set_t * m_set
Set of all event handlers registered for this signal.
Definition SigHandlersList.h:163
ASSA::SigHandlersList::begin
iterator begin()
Return an iterator pointing to the beginning of the list.
Definition SigHandlersList.h:300
ASSA::SigHandlersList::cfunc_handler
CFUNC_Handler * cfunc_handler() const
Retrieve pointer to 3rd party C function handler.
Definition SigHandlersList.h:340
ASSA::SigHandlersList::insert
bool insert(data_type data_)
Add an event handler data_ to the list.
Definition SigHandlersList.h:241
ASSA::SigHandlersList::end
iterator end()
Return an iterator pointing to the end of the list.
Definition SigHandlersList.h:309
ASSA::SigHandlersList::~SigHandlersList
~SigHandlersList()
Destructor.
Definition SigHandlersList.h:192
ASSA::SigHandlersList::erase
void erase()
Empty event handlers' list.
Definition SigHandlersList.h:280
ASSA::SigHandlersList::m_cfhp
CFUNC_Handler * m_cfhp
Pointer to the 3rd party signal handler in the set.
Definition SigHandlersList.h:173
ASSA::SigHandlersList::set_t
set< key_type, CompSHL > set_t
Definition SigHandlersList.h:81
ASSA::SigHandlersList::SigHandlersList
SigHandlersList()
Definition SigHandlersList.h:182
ASSA::SigHandlersList::size
size_t size() const
Size of the list.
Definition SigHandlersList.h:230
ASSA::SigHandlersList::instance
static SigHandlersList * instance(int signum_)
Retrieve a pointer to the list of event handlers listening to signum_ signal delivery.
Definition SigHandlersList.h:203
ASSA::SigHandlersList::seen_cfunc_handler
bool seen_cfunc_handler() const
Definition SigHandlersList.h:358
ASSA::SigHandlersList::iterator
set< key_type, CompSHL >::iterator iterator
Definition SigHandlersList.h:82
ASSA
Definition Acceptor.h:40
ASSA::SIGHAND
@ SIGHAND
Class SigHandler(s) messages
Definition LogMask.h:38
ASSA::APP
@ APP
Application-level messages
Definition LogMask.h:27
ASSA::SigHandlersList::CompSHL
Definition SigHandlersList.h:71
ASSA::SigHandlersList::CompSHL::operator()
bool operator()(const key_type c1_, const key_type c2_) const
Definition SigHandlersList.h:72