00001
00002
00003 #ifndef GAUDIKERNEL_GUARDS_H
00004 #define GAUDIKERNEL_GUARDS_H 1
00005
00006
00007
00008
00009
00010 #include <exception>
00011
00012
00013
00014 #include "GaudiKernel/INamedInterface.h"
00015 #include "GaudiKernel/IExceptionSvc.h"
00016 #include "GaudiKernel/IAuditor.h"
00017
00018
00019
00020 class GaudiException ;
00021
00022 namespace Gaudi
00023 {
00074 namespace Guards
00075 {
00108 class ExceptionGuard
00109 {
00110 public:
00111
00112
00113
00114
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
00127 m_sc = fun ( obj ) ;
00128
00129 if ( 0 != svc && m_sc.isFailure() ) { m_sc = svc->handleErr ( *obj , m_sc ) ; }
00130 }
00131 catch ( const GaudiException& e )
00132 {
00133
00134 handle ( e , log ) ;
00135 if ( 0 != svc ) { m_sc = svc -> handle ( *obj , e ) ; }
00136 }
00137 catch ( const std::exception& e )
00138 {
00139
00140 handle ( e , log ) ;
00141 if ( 0 != svc ) { m_sc = svc -> handle ( *obj , e ) ; }
00142 }
00143 catch ( ... )
00144 {
00145
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
00159 ExceptionGuard() ;
00160
00161 ExceptionGuard ( const ExceptionGuard& ) ;
00162
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
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
00264 const StatusCode &code () const { return *m_sc ; }
00265 private:
00266
00267 AuditorGuard () ;
00268
00269 AuditorGuard ( const AuditorGuard& right ) ;
00270
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 ) {
00292 m_svc->addRef();
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 {
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 ) {
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 {
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();
00341 m_svc = 0 ;
00342 }
00343 }
00344 } ;
00345 }
00346 }
00347
00348
00349
00350
00351
00352 #endif // GAUDIKERNEL_GUARDS_H
00353