Guards.h
Go to the documentation of this file.
1 // $Id: Guards.h,v 1.2 2008/04/03 14:40:19 marcocle Exp $
2 // ============================================================================
3 #ifndef GAUDIKERNEL_GUARDS_H
4 #define GAUDIKERNEL_GUARDS_H 1
5 // ============================================================================
6 // Include files
7 // ============================================================================
8 // STD & STL
9 // ============================================================================
10 #include <exception>
11 // ============================================================================
12 // GaudiKernel
13 // ============================================================================
14 #include "GaudiKernel/INamedInterface.h"
15 #include "GaudiKernel/IExceptionSvc.h"
16 #include "GaudiKernel/IAuditor.h"
17 // ============================================================================
18 // forward declaration
19 // ============================================================================
20 class GaudiException ;
21 // ============================================================================
22 namespace Gaudi
23 {
74  namespace Guards
75  {
108  class GAUDI_API ExceptionGuard
109  {
110  public:
111  /* constructor form the object/scope, function and log-stream
112  * @param obj the object/scope
113  * @param fun function to be used
114  * @param log output log-stream
115  */
116  template <class OBJECT, class FUNCTION>
117  ExceptionGuard
118  ( OBJECT obj ,
119  FUNCTION fun ,
120  MsgStream& log ,
121  IExceptionSvc* svc = 0 )
122  : m_sc ( StatusCode::FAILURE )
123  {
124  try
125  {
126  // execute the functor:
127  m_sc = fun ( obj ) ;
128  // in the case of error try use Exception Service
129  if ( 0 != svc && m_sc.isFailure() ) { m_sc = svc->handleErr ( *obj , m_sc ) ; }
130  }
131  catch ( const GaudiException& e )
132  {
133  // Use the local handler and then (if possible) the Exception Service
134  handle ( e , log ) ;
135  if ( 0 != svc ) { m_sc = svc -> handle ( *obj , e ) ; }
136  }
137  catch ( const std::exception& e )
138  {
139  // Use the local handler and then (if possible) the Exception Service
140  handle ( e , log ) ;
141  if ( 0 != svc ) { m_sc = svc -> handle ( *obj , e ) ; }
142  }
143  catch ( ... )
144  {
145  // Use the local handler and then (if possible) the Exception Service
146  handle ( log ) ;
147  if ( 0 != svc ) { m_sc = svc -> handle ( *obj ) ; }
148  }
149  }
151  ~ExceptionGuard() ;
152  public:
154  const StatusCode& code () const { return m_sc ; }
156  operator const StatusCode&() const { return code() ; }
157  private:
158  // default constructor is disabled
159  ExceptionGuard() ;
160  // copy constructor is disabled
161  ExceptionGuard ( const ExceptionGuard& ) ;
162  // assignment operator is disabled
163  ExceptionGuard& operator=( const ExceptionGuard& ) ;
164  protected:
166  void handle ( const GaudiException& e , MsgStream& s ) ;
168  void handle ( const std::exception& e , MsgStream& s ) ;
170  void handle ( MsgStream& s ) ;
171  private:
172  // status code: result of the function evaluation
173  StatusCode m_sc ;
174  } ;
175  // ========================================================================
218  class GAUDI_API AuditorGuard
219  {
220  public:
222  AuditorGuard ( INamedInterface* obj ,
223  IAuditor* svc ,
226  AuditorGuard ( INamedInterface* obj ,
227  IAuditor* svc ,
229 
231  AuditorGuard ( INamedInterface* obj ,
232  IAuditor* svc ,
234  const StatusCode &sc ) ;
236  AuditorGuard ( INamedInterface* obj ,
237  IAuditor* svc ,
239  const StatusCode &sc ) ;
241  AuditorGuard ( const std::string &name ,
242  IAuditor* svc ,
245  AuditorGuard ( const std::string &name ,
246  IAuditor* svc ,
248 
250  AuditorGuard ( const std::string &name ,
251  IAuditor* svc ,
253  const StatusCode &sc ) ;
255  AuditorGuard ( const std::string &name ,
256  IAuditor* svc ,
258  const StatusCode &sc ) ;
259 
261  ~AuditorGuard() ;
262  public:
263  // get the status code
264  const StatusCode &code () const { return *m_sc ; }
265  private:
266  // the default constructor is disabled
267  AuditorGuard () ;
268  // the copy constructor is disabled
269  AuditorGuard ( const AuditorGuard& right ) ;
270  // assignement operator is disabled
271  AuditorGuard& operator=( const AuditorGuard& right ) ;
272  private :
274  INamedInterface* m_obj ;
276  std::string m_objName;
278  IAuditor* m_svc ;
286  const StatusCode *m_sc ;
288  bool m_customEvtType;
289 
290  inline void i_before() {
291  if ( 0 != m_svc ) { // if the service is not available, we cannot do anything
292  m_svc->addRef(); // increase the reference counting
293  if (0 != m_obj) {
294  if (m_customEvtType) {
295  m_svc->before(m_cevt,m_obj);
296  } else {
297  m_svc->before(m_evt,m_obj);
298  }
299  } else { // use object name
300  if (m_customEvtType) {
301  m_svc->before(m_cevt,m_objName);
302  } else {
303  m_svc->before(m_evt,m_objName);
304  }
305  }
306  }
307  }
308 
309  inline void i_after() {
310  if ( 0 != m_svc ) { // if the service is not available, we cannot do anything
311  if (0 != m_obj) {
312  if (m_customEvtType) {
313  if (0 != m_sc) {
314  m_svc->after(m_cevt,m_obj,*m_sc);
315  } else {
316  m_svc->after(m_cevt,m_obj);
317  }
318  } else {
319  if (0 != m_sc) {
320  m_svc->after(m_evt,m_obj,*m_sc);
321  } else {
322  m_svc->after(m_evt,m_obj);
323  }
324  }
325  } else { // use object name
326  if (m_customEvtType) {
327  if (0 != m_sc) {
328  m_svc->after(m_cevt,m_objName,*m_sc);
329  } else {
330  m_svc->after(m_cevt,m_objName);
331  }
332  } else {
333  if (0 != m_sc) {
334  m_svc->after(m_evt,m_objName,*m_sc);
335  } else {
336  m_svc->after(m_evt,m_objName);
337  }
338  }
339  }
340  m_svc->release(); // we do not need the service anymore
341  m_svc = 0 ;
342  }
343  }
344  } ;
345  } // end of namespace Gaudi::Guards
346 } //end of namespace Gaudi
347 
348 
349 // ============================================================================
350 // The END
351 // ============================================================================
352 #endif // GAUDIKERNEL_GUARDS_H
353 // ============================================================================
virtual void before(StandardEventType, INamedInterface *)=0
Audit the start of a standard "event".
Definition of the MsgStream class used to transmit messages.
Definition: MsgStream.h:24
Define general base for Gaudi exception.
StandardEventType
Defines the standard (= used by the framework) auditable event types.
Definition: IAuditor.h:24
#define GAUDI_API
Definition: Kernel.h:108
const CustomEventType & CustomEventTypeRef
Used in function calls for optimization purposes.
Definition: IAuditor.h:41
std::string CustomEventType
Type used to allow users to specify a custom event to be audit.
Definition: IAuditor.h:39
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:30
IInterface compliant class extending IInterface with the name() method.
virtual unsigned long release()=0
Release Interface instance.
double fun(const std::vector< double > &x)
Definition: PFuncTest.cpp:27
virtual void after(StandardEventType, INamedInterface *, const StatusCode &sc=StatusCode(StatusCode::SUCCESS, true))=0
Audit the end of a standard "event".
string s
Definition: gaudirun.py:244
virtual unsigned long addRef()=0
Increment the reference count of Interface instance.
A DataObject is the base class of any identifiable object on any data store.
Definition: DataObject.h:31
The IAuditor is the interface implmented by the AlgAuditor base class.
Definition: IAuditor.h:18
Helper functions to set/get the application return code.
Definition: __init__.py:1
The abstract interface for exception handling service.
Definition: IExceptionSvc.h:27