/build/buildd/libassa-3.4.1/assa/SigHandlersList.h

Go to the documentation of this file.
00001 // -*- c++ -*-
00002 //---------------------------------------------------------------------------
00003 //                            SigHandlersList.h
00004 //------------------------------------------------------------------------------
00005 //  Copyright (c) 1997 by Vladislav Grinchenko
00006 //
00007 //  Permission to use, copy, modify, and distribute this software      
00008 //  and its documentation for any purpose and without fee is hereby     
00009 //  granted, provided that the above copyright notice appear in all     
00010 //  copies.  The author makes no representations about the suitability  
00011 //  of this software for any purpose.  It is provided "as is" without   
00012 //  express or implied warranty.
00013 //---------------------------------------------------------------------------
00014 
00015 #ifndef _SigHandlersList_h
00016 #define _SigHandlersList_h
00017 
00018 #include <signal.h>
00019 #include <errno.h>
00020 #include <sys/time.h>   
00021 #include <sys/types.h>
00022 
00023 #include "assa/SigHandler.h"
00024 
00025 #include <set>
00026 using std::set;
00027 
00028 namespace ASSA {
00029 
00042 class CFUNC_Handler : public EventHandler
00043 {
00044 public:
00045     CFUNC_Handler (C_SIG_HANDLER csigh_);
00046 
00047     int           handle_signal (int signum_);
00048     C_SIG_HANDLER handler       () { return m_c_sig_hand; }
00049     
00050 private:
00051     C_SIG_HANDLER m_c_sig_hand;
00052 };
00053 
00063 class SigHandlersList 
00064 {
00065 public:
00066     typedef EventHandler* key_type;
00067     typedef EventHandler* data_type;
00068 
00069     struct CompSHL {
00070         bool operator () (const key_type c1_, const key_type c2_) const
00071         {
00072 // This wouldn't fly on 64-bit machines, 'cause ptr size there is 8 bytes long
00073 //          return int(c1_) < int(c2_);
00074 //
00075             return (c1_ < c2_);
00076         }
00077     };
00078 
00079     typedef set< key_type, CompSHL > set_t;
00080     typedef set< key_type, CompSHL >::iterator iterator;
00081 
00085     static SigHandlersList* instance (int signum_);
00086 
00088     ~SigHandlersList ();
00089 
00091     bool empty () const;
00092 
00094     size_t size () const;
00095 
00099     bool insert (data_type data_);
00100 
00103     void erase (const key_type key_);
00104 
00108     void erase (iterator it_);
00109     
00112     void erase ();
00113     
00116     iterator begin ();
00117     
00120     iterator end ();
00121 
00125     iterator find (const key_type key_);
00126 
00132     CFUNC_Handler* cfunc_handler (CFUNC_Handler* cfp_);
00133 
00137     CFUNC_Handler* cfunc_handler () const;
00138 
00142     void seen_cfunc_handler (bool ft_);
00143 
00147     bool seen_cfunc_handler () const;
00148 
00149 protected:
00150     SigHandlersList ();     // Singleton
00151     SigHandlersList (const SigHandlersList& map_); // prohibit copying
00152     SigHandlersList& operator= (const SigHandlersList& map_);
00153     
00154 public:
00157     static SigHandlersList* m_instance[NSIG];
00158 
00159 private:
00161     set_t*         m_set;
00162 
00167     int            m_seen_cfh;  
00168 
00171     CFUNC_Handler*  m_cfhp;
00172 };
00173 
00174 //-------------------------------------------------------------------------
00175 //----------------------- SigHandlersList Inlines -------------------------
00176 //-------------------------------------------------------------------------
00177 
00178 inline
00179 SigHandlersList::
00180 SigHandlersList ()
00181     : m_seen_cfh (false), m_cfhp (NULL)
00182 {
00183     trace_with_mask("SigHandlersList::SigHandlersList", SIGHAND);
00184 
00185     m_set = new set_t;
00186 }
00187 
00188 inline
00189 SigHandlersList::
00190 ~SigHandlersList ()
00191 {
00192     trace_with_mask("SigHandlersList::~SigHandlersList", SIGHAND);
00193 
00194     erase ();
00195     delete m_set;
00196     m_set = NULL;
00197 }
00198 
00199 inline  SigHandlersList*
00200 SigHandlersList::
00201 instance (int signum_)
00202 {
00203     trace_with_mask("SigHandlersList::instance", SIGHAND);
00204 
00205     DL((APP, "m_instance[%d] = 0x%x\n", signum_,
00206         SigHandlersList::m_instance[signum_]));
00207 
00208     if (SigHandlersList::m_instance[signum_] == 0) {
00209         DL((APP, "new SigHandlersList allocated\n"));
00210         SigHandlersList::m_instance[signum_] = new SigHandlersList();
00211     }
00212     return SigHandlersList::m_instance[signum_];
00213 }
00214 
00215 inline bool
00216 SigHandlersList::
00217 empty () const
00218 {
00219     trace_with_mask("SigHandlersList::empty", SIGHAND);
00220 
00221     // true if map is empty, false otherwise
00222 
00223     return m_set->empty ();
00224 }
00225 
00226 inline size_t
00227 SigHandlersList::
00228 size () const
00229 {
00230     trace_with_mask("SigHandlersList::size", SIGHAND);
00231 
00232     // return number of elements in the map
00233 
00234     return m_set->size ();
00235 }
00236 
00237 inline bool
00238 SigHandlersList::
00239 insert (data_type eh_)
00240 {
00241     trace_with_mask("SigHandlersList::insert", SIGHAND);
00242 
00243     /*---
00244       Insert 'eh_' into the set. set::insert() returns a 'pair' object.
00245 
00246       If the set doesn't contain an element that matches 'eh_', insert a 
00247       copy of 'eh_' and returns a 'pair' whose first element is an
00248       iterator positioned at the new element and second element is
00249       'true'.
00250 
00251       If the set already contains an element that matches 'eh_', returns
00252       a pair whose first element is an iterator positioned at the
00253       existing element and second element is false!
00254       ---*/
00255 
00256     set_t::const_iterator it = m_set->find (eh_);
00257 
00258     /*--- Not in the set ---*/
00259     if (it == m_set->end ()) { 
00260         return (m_set->insert (eh_)).second;
00261     }
00262     /*--- Already in the set ---*/
00263     return true;
00264 }
00265 
00266 inline void
00267 SigHandlersList::
00268 erase (const key_type key_)
00269 {
00270     // return number of erased elements
00271     trace_with_mask("SigHandlersList::erase(key_)", SIGHAND);
00272     
00273     m_set->erase (key_);
00274 }
00275 
00276 inline void
00277 SigHandlersList::
00278 erase ()
00279 {
00280     // empty the map
00281     trace_with_mask("SigHandlersList::erase(void)", SIGHAND);
00282 
00283     m_set->erase (m_set->begin(), m_set->end());
00284 }
00285 
00286 inline void
00287 SigHandlersList::
00288 erase(iterator it_)
00289 {
00290     // erase element pointed by iterator
00291     trace_with_mask("SigHandlersList::erase(it_)", SIGHAND);
00292 
00293     m_set->erase(it_);
00294 }
00295 
00296 inline SigHandlersList::iterator
00297 SigHandlersList::
00298 begin ()
00299 {
00300     trace_with_mask("SigHandlersList::begin()", SIGHAND);
00301 
00302     return m_set->begin ();
00303 }
00304 
00305 inline SigHandlersList::iterator
00306 SigHandlersList::
00307 end ()
00308 {
00309     trace_with_mask("SigHandlersList::end", SIGHAND);
00310 
00311     return m_set->end ();
00312 }
00313 
00314 inline SigHandlersList::iterator
00315 SigHandlersList::
00316 find (const key_type key_)
00317 {
00318     trace_with_mask("SigHandlersList::find", SIGHAND);
00319 
00320     return m_set->find (key_);
00321 }
00322 
00323 
00324 inline CFUNC_Handler*
00325 SigHandlersList::
00326 cfunc_handler (CFUNC_Handler* cfhp_)
00327 {
00328     trace_with_mask("SigHandlersList::cfunc_handler", SIGHAND);
00329     
00330     CFUNC_Handler* old_cfhp = m_cfhp;
00331     m_cfhp = cfhp_;
00332     m_seen_cfh = cfhp_ == NULL ? false : true;
00333     return old_cfhp;
00334 }
00335 
00336 inline CFUNC_Handler*
00337 SigHandlersList::
00338 cfunc_handler () const
00339 {
00340     trace_with_mask("SigHandlersList::cfunc_handler", SIGHAND);
00341     
00342     return m_cfhp;
00343 }
00344 
00345 inline void
00346 SigHandlersList::
00347 seen_cfunc_handler (bool ft_) 
00348 {
00349     trace_with_mask("SigHandlersList::seen_cfunc_handler", SIGHAND);
00350 
00351     m_seen_cfh = ft_;
00352 }
00353 
00354 inline bool
00355 SigHandlersList::
00356 seen_cfunc_handler () const
00357 {
00358     trace_with_mask("SigHandlersList::seen_cfunc_handler", SIGHAND);
00359 
00360     return m_seen_cfh;
00361 }
00362 
00363 //-------------------------------------------------------------------------
00364 //------------------------ CFUNC_Handler Inlines --------------------------
00365 //-------------------------------------------------------------------------
00366 
00367 inline
00368 CFUNC_Handler::
00369 CFUNC_Handler (C_SIG_HANDLER csigh_)
00370     : m_c_sig_hand (csigh_)
00371 {
00372     trace_with_mask("CFUNC_Handler::CFUNC_Handler", SIGHAND);
00373 }
00374 
00375 inline int
00376 CFUNC_Handler::
00377 handle_signal (int signum_)
00378 {
00379     trace_with_mask("CFUNC_Handler::handle_signal", SIGHAND);
00380 
00381     if (m_c_sig_hand) {
00382         (*m_c_sig_hand)(signum_);
00383     }
00384     return 1;
00385 }
00386 
00387 } // end namespace ASSA
00388 
00389 #endif /* _SigHandlersList_h */
00390 

Generated on Wed Jun 21 01:42:50 2006 for libassa by  doxygen 1.4.6