libassa 3.5.0
|
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 00030 #if !defined(WIN32) 00031 00044 class CFUNC_Handler : public EventHandler 00045 { 00046 public: 00047 CFUNC_Handler (C_SIG_HANDLER csigh_); 00048 00049 int handle_signal (int signum_); 00050 C_SIG_HANDLER handler () { return m_c_sig_hand; } 00051 00052 private: 00053 C_SIG_HANDLER m_c_sig_hand; 00054 }; 00055 00065 class SigHandlersList 00066 { 00067 public: 00068 typedef EventHandler* key_type; 00069 typedef EventHandler* data_type; 00070 00071 struct CompSHL { 00072 bool operator () (const key_type c1_, const key_type c2_) const 00073 { 00074 // This wouldn't fly on 64-bit machines, 'cause ptr size there is 8 bytes long 00075 // return int(c1_) < int(c2_); 00076 // 00077 return (c1_ < c2_); 00078 } 00079 }; 00080 00081 typedef set< key_type, CompSHL > set_t; 00082 typedef set< key_type, CompSHL >::iterator iterator; 00083 00087 static SigHandlersList* instance (int signum_); 00088 00090 ~SigHandlersList (); 00091 00093 bool empty () const; 00094 00096 size_t size () const; 00097 00101 bool insert (data_type data_); 00102 00105 void erase (const key_type key_); 00106 00110 void erase (iterator it_); 00111 00114 void erase (); 00115 00118 iterator begin (); 00119 00122 iterator end (); 00123 00127 iterator find (const key_type key_); 00128 00134 CFUNC_Handler* cfunc_handler (CFUNC_Handler* cfp_); 00135 00139 CFUNC_Handler* cfunc_handler () const; 00140 00144 void seen_cfunc_handler (bool ft_); 00145 00149 bool seen_cfunc_handler () const; 00150 00151 protected: 00152 SigHandlersList (); // Singleton 00153 SigHandlersList (const SigHandlersList& map_); // prohibit copying 00154 SigHandlersList& operator= (const SigHandlersList& map_); 00155 00156 public: 00159 static SigHandlersList* m_instance[NSIG]; 00160 00161 private: 00163 set_t* m_set; 00164 00169 int m_seen_cfh; 00170 00173 CFUNC_Handler* m_cfhp; 00174 }; 00175 00176 //------------------------------------------------------------------------- 00177 //----------------------- SigHandlersList Inlines ------------------------- 00178 //------------------------------------------------------------------------- 00179 00180 inline 00181 SigHandlersList:: 00182 SigHandlersList () 00183 : m_seen_cfh (false), m_cfhp (NULL) 00184 { 00185 trace_with_mask("SigHandlersList::SigHandlersList", SIGHAND); 00186 00187 m_set = new set_t; 00188 } 00189 00190 inline 00191 SigHandlersList:: 00192 ~SigHandlersList () 00193 { 00194 trace_with_mask("SigHandlersList::~SigHandlersList", SIGHAND); 00195 00196 erase (); 00197 delete m_set; 00198 m_set = NULL; 00199 } 00200 00201 inline SigHandlersList* 00202 SigHandlersList:: 00203 instance (int signum_) 00204 { 00205 trace_with_mask("SigHandlersList::instance", SIGHAND); 00206 00207 DL((APP, "m_instance[%d] = 0x%x\n", signum_, 00208 SigHandlersList::m_instance[signum_])); 00209 00210 if (SigHandlersList::m_instance[signum_] == 0) { 00211 DL((APP, "new SigHandlersList allocated\n")); 00212 SigHandlersList::m_instance[signum_] = new SigHandlersList(); 00213 } 00214 return SigHandlersList::m_instance[signum_]; 00215 } 00216 00217 inline bool 00218 SigHandlersList:: 00219 empty () const 00220 { 00221 trace_with_mask("SigHandlersList::empty", SIGHAND); 00222 00223 // true if map is empty, false otherwise 00224 00225 return m_set->empty (); 00226 } 00227 00228 inline size_t 00229 SigHandlersList:: 00230 size () const 00231 { 00232 trace_with_mask("SigHandlersList::size", SIGHAND); 00233 00234 // return number of elements in the map 00235 00236 return m_set->size (); 00237 } 00238 00239 inline bool 00240 SigHandlersList:: 00241 insert (data_type eh_) 00242 { 00243 trace_with_mask("SigHandlersList::insert", SIGHAND); 00244 00245 /*--- 00246 Insert 'eh_' into the set. set::insert() returns a 'pair' object. 00247 00248 If the set doesn't contain an element that matches 'eh_', insert a 00249 copy of 'eh_' and returns a 'pair' whose first element is an 00250 iterator positioned at the new element and second element is 00251 'true'. 00252 00253 If the set already contains an element that matches 'eh_', returns 00254 a pair whose first element is an iterator positioned at the 00255 existing element and second element is false! 00256 ---*/ 00257 00258 set_t::const_iterator it = m_set->find (eh_); 00259 00260 /*--- Not in the set ---*/ 00261 if (it == m_set->end ()) { 00262 return (m_set->insert (eh_)).second; 00263 } 00264 /*--- Already in the set ---*/ 00265 return true; 00266 } 00267 00268 inline void 00269 SigHandlersList:: 00270 erase (const key_type key_) 00271 { 00272 // return number of erased elements 00273 trace_with_mask("SigHandlersList::erase(key_)", SIGHAND); 00274 00275 m_set->erase (key_); 00276 } 00277 00278 inline void 00279 SigHandlersList:: 00280 erase () 00281 { 00282 // empty the map 00283 trace_with_mask("SigHandlersList::erase(void)", SIGHAND); 00284 00285 m_set->erase (m_set->begin(), m_set->end()); 00286 } 00287 00288 inline void 00289 SigHandlersList:: 00290 erase(iterator it_) 00291 { 00292 // erase element pointed by iterator 00293 trace_with_mask("SigHandlersList::erase(it_)", SIGHAND); 00294 00295 m_set->erase(it_); 00296 } 00297 00298 inline SigHandlersList::iterator 00299 SigHandlersList:: 00300 begin () 00301 { 00302 trace_with_mask("SigHandlersList::begin()", SIGHAND); 00303 00304 return m_set->begin (); 00305 } 00306 00307 inline SigHandlersList::iterator 00308 SigHandlersList:: 00309 end () 00310 { 00311 trace_with_mask("SigHandlersList::end", SIGHAND); 00312 00313 return m_set->end (); 00314 } 00315 00316 inline SigHandlersList::iterator 00317 SigHandlersList:: 00318 find (const key_type key_) 00319 { 00320 trace_with_mask("SigHandlersList::find", SIGHAND); 00321 00322 return m_set->find (key_); 00323 } 00324 00325 00326 inline CFUNC_Handler* 00327 SigHandlersList:: 00328 cfunc_handler (CFUNC_Handler* cfhp_) 00329 { 00330 trace_with_mask("SigHandlersList::cfunc_handler", SIGHAND); 00331 00332 CFUNC_Handler* old_cfhp = m_cfhp; 00333 m_cfhp = cfhp_; 00334 m_seen_cfh = cfhp_ == NULL ? false : true; 00335 return old_cfhp; 00336 } 00337 00338 inline CFUNC_Handler* 00339 SigHandlersList:: 00340 cfunc_handler () const 00341 { 00342 trace_with_mask("SigHandlersList::cfunc_handler", SIGHAND); 00343 00344 return m_cfhp; 00345 } 00346 00347 inline void 00348 SigHandlersList:: 00349 seen_cfunc_handler (bool ft_) 00350 { 00351 trace_with_mask("SigHandlersList::seen_cfunc_handler", SIGHAND); 00352 00353 m_seen_cfh = ft_; 00354 } 00355 00356 inline bool 00357 SigHandlersList:: 00358 seen_cfunc_handler () const 00359 { 00360 trace_with_mask("SigHandlersList::seen_cfunc_handler", SIGHAND); 00361 00362 return m_seen_cfh; 00363 } 00364 00365 //------------------------------------------------------------------------- 00366 //------------------------ CFUNC_Handler Inlines -------------------------- 00367 //------------------------------------------------------------------------- 00368 00369 inline 00370 CFUNC_Handler:: 00371 CFUNC_Handler (C_SIG_HANDLER csigh_) 00372 : m_c_sig_hand (csigh_) 00373 { 00374 trace_with_mask("CFUNC_Handler::CFUNC_Handler", SIGHAND); 00375 } 00376 00377 inline int 00378 CFUNC_Handler:: 00379 handle_signal (int signum_) 00380 { 00381 trace_with_mask("CFUNC_Handler::handle_signal", SIGHAND); 00382 00383 if (m_c_sig_hand) { 00384 (*m_c_sig_hand)(signum_); 00385 } 00386 return 1; 00387 } 00388 00389 #endif // !defined(WIN32) 00390 00391 } // end namespace ASSA 00392 00393 #endif /* _SigHandlersList_h */ 00394