![]() |
|
|
Generated: 24 Nov 2008 |
00001 // $Id: Guards.h,v 1.2 2008/04/03 14:40:19 marcocle Exp $ 00002 // ============================================================================ 00003 #ifndef GAUDIKERNEL_GUARDS_H 00004 #define GAUDIKERNEL_GUARDS_H 1 00005 // ============================================================================ 00006 // Include files 00007 // ============================================================================ 00008 // STD & STL 00009 // ============================================================================ 00010 #include <exception> 00011 // ============================================================================ 00012 // GaudiKernel 00013 // ============================================================================ 00014 #include "GaudiKernel/INamedInterface.h" 00015 #include "GaudiKernel/IExceptionSvc.h" 00016 #include "GaudiKernel/IAuditor.h" 00017 // ============================================================================ 00018 // forward declaration 00019 // ============================================================================ 00020 class GaudiException ; 00021 // ============================================================================ 00022 namespace Gaudi 00023 { 00074 namespace Guards 00075 { 00108 class ExceptionGuard 00109 { 00110 public: 00111 /* constructor form the object/scope, function and log-stream 00112 * @param obj the object/scope 00113 * @param fun function to be used 00114 * @param log output log-stream 00115 */ 00116 template <class OBJECT, class FUNCTION> 00117 ExceptionGuard 00118 ( OBJECT obj , 00119 FUNCTION fun , 00120 MsgStream& log , 00121 IExceptionSvc* svc = 0 ) 00122 : m_sc ( StatusCode::FAILURE ) 00123 { 00124 try 00125 { 00126 // execute the functor: 00127 m_sc = fun ( obj ) ; 00128 // in the case of error try use Exception Service 00129 if ( 0 != svc && m_sc.isFailure() ) { m_sc = svc->handleErr ( *obj , m_sc ) ; } 00130 } 00131 catch ( const GaudiException& e ) 00132 { 00133 // Use the local handler and then (if possible) the Exception Service 00134 handle ( e , log ) ; 00135 if ( 0 != svc ) { m_sc = svc -> handle ( *obj , e ) ; } 00136 } 00137 catch ( const std::exception& e ) 00138 { 00139 // Use the local handler and then (if possible) the Exception Service 00140 handle ( e , log ) ; 00141 if ( 0 != svc ) { m_sc = svc -> handle ( *obj , e ) ; } 00142 } 00143 catch ( ... ) 00144 { 00145 // Use the local handler and then (if possible) the Exception Service 00146 handle ( log ) ; 00147 if ( 0 != svc ) { m_sc = svc -> handle ( *obj ) ; } 00148 } 00149 } 00151 ~ExceptionGuard() ; 00152 public: 00154 const StatusCode& code () const { return m_sc ; } 00156 operator const StatusCode&() const { return code() ; } 00157 private: 00158 // default constructor is disabled 00159 ExceptionGuard() ; 00160 // copy constructor is disabled 00161 ExceptionGuard ( const ExceptionGuard& ) ; 00162 // assignement operator is disabled 00163 ExceptionGuard& operator=( const ExceptionGuard& ) ; 00164 protected: 00166 void handle ( const GaudiException& e , MsgStream& s ) ; 00168 void handle ( const std::exception& e , MsgStream& s ) ; 00170 void handle ( MsgStream& s ) ; 00171 private: 00172 // status code: result of the function evaluation 00173 StatusCode m_sc ; 00174 } ; 00175 // ======================================================================== 00218 class AuditorGuard 00219 { 00220 public: 00222 AuditorGuard ( INamedInterface* obj , 00223 IAuditor* svc , 00224 IAuditor::StandardEventType evt ) ; 00226 AuditorGuard ( INamedInterface* obj , 00227 IAuditor* svc , 00228 IAuditor::CustomEventTypeRef evt ) ; 00229 00231 AuditorGuard ( INamedInterface* obj , 00232 IAuditor* svc , 00233 IAuditor::StandardEventType evt , 00234 const StatusCode &sc ) ; 00236 AuditorGuard ( INamedInterface* obj , 00237 IAuditor* svc , 00238 IAuditor::CustomEventTypeRef evt , 00239 const StatusCode &sc ) ; 00241 AuditorGuard ( const std::string &name , 00242 IAuditor* svc , 00243 IAuditor::StandardEventType evt ) ; 00245 AuditorGuard ( const std::string &name , 00246 IAuditor* svc , 00247 IAuditor::CustomEventTypeRef evt ) ; 00248 00250 AuditorGuard ( const std::string &name , 00251 IAuditor* svc , 00252 IAuditor::StandardEventType evt , 00253 const StatusCode &sc ) ; 00255 AuditorGuard ( const std::string &name , 00256 IAuditor* svc , 00257 IAuditor::CustomEventTypeRef evt , 00258 const StatusCode &sc ) ; 00259 00261 ~AuditorGuard() ; 00262 public: 00263 // get the status code 00264 const StatusCode &code () const { return *m_sc ; } 00265 private: 00266 // the default constructor is disabled 00267 AuditorGuard () ; 00268 // the copy constructor is disabled 00269 AuditorGuard ( const AuditorGuard& right ) ; 00270 // assignement operator is disabled 00271 AuditorGuard& operator=( const AuditorGuard& right ) ; 00272 private : 00274 INamedInterface* m_obj ; 00276 std::string m_objName; 00278 IAuditor* m_svc ; 00280 IAuditor::StandardEventType m_evt ; 00282 IAuditor::CustomEventType m_cevt ; 00286 const StatusCode *m_sc ; 00288 bool m_customEvtType; 00289 00290 inline void i_before() { 00291 if ( 0 != m_svc ) { // if the service is not available, we cannot do anything 00292 m_svc->addRef(); // increase the reference counting 00293 if (0 != m_obj) { 00294 if (m_customEvtType) { 00295 m_svc->before(m_cevt,m_obj); 00296 } else { 00297 m_svc->before(m_evt,m_obj); 00298 } 00299 } else { // use object name 00300 if (m_customEvtType) { 00301 m_svc->before(m_cevt,m_objName); 00302 } else { 00303 m_svc->before(m_evt,m_objName); 00304 } 00305 } 00306 } 00307 } 00308 00309 inline void i_after() { 00310 if ( 0 != m_svc ) { // if the service is not available, we cannot do anything 00311 if (0 != m_obj) { 00312 if (m_customEvtType) { 00313 if (0 != m_sc) { 00314 m_svc->after(m_cevt,m_obj,*m_sc); 00315 } else { 00316 m_svc->after(m_cevt,m_obj); 00317 } 00318 } else { 00319 if (0 != m_sc) { 00320 m_svc->after(m_evt,m_obj,*m_sc); 00321 } else { 00322 m_svc->after(m_evt,m_obj); 00323 } 00324 } 00325 } else { // use object name 00326 if (m_customEvtType) { 00327 if (0 != m_sc) { 00328 m_svc->after(m_cevt,m_objName,*m_sc); 00329 } else { 00330 m_svc->after(m_cevt,m_objName); 00331 } 00332 } else { 00333 if (0 != m_sc) { 00334 m_svc->after(m_evt,m_objName,*m_sc); 00335 } else { 00336 m_svc->after(m_evt,m_objName); 00337 } 00338 } 00339 } 00340 m_svc->release(); // we do not need the service anymore 00341 m_svc = 0 ; 00342 } 00343 } 00344 } ; 00345 } // end of namespace Gaudi::Guards 00346 } //end of namespace Gaudi 00347 00348 00349 // ============================================================================ 00350 // The END 00351 // ============================================================================ 00352 #endif // GAUDIKERNEL_GUARDS_H 00353 // ============================================================================