All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 // ============================================================================
16 #include "GaudiKernel/IAuditor.h"
17 // ============================================================================
18 // forward declaration
19 // ============================================================================
20 class GaudiException ;
21 // ============================================================================
22 namespace Gaudi
23 {
74  namespace Guards
75  {
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>
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
174  } ;
175  // ========================================================================
219  {
220  public:
223  IAuditor* svc ,
227  IAuditor* svc ,
229 
232  IAuditor* svc ,
234  const StatusCode &sc ) ;
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 :
276  std::string m_objName;
286  const StatusCode *m_sc ;
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 // ============================================================================
Definition of the MsgStream class used to transmit messages.
Definition: MsgStream.h:24
IAuditor::StandardEventType m_evt
Event type (standard events)
Definition: Guards.h:280
Define general base for Gaudi exception.
const StatusCode & code() const
the result of function evaluation
Definition: Guards.h:154
StandardEventType
Defines the standard (= used by the framework) auditable event types.
Definition: IAuditor.h:24
IAuditor::CustomEventType m_cevt
Event type (custom events)
Definition: Guards.h:282
const CustomEventType & CustomEventTypeRef
Used in function calls for optimization purposes.
Definition: IAuditor.h:41
StatusCode m_sc
status code : result of function evaluation
Definition: Guards.h:173
INamedInterface * m_obj
the guarded object
Definition: Guards.h:274
const StatusCode & code() const
Definition: Guards.h:264
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
IAuditor * m_svc
auditor service
Definition: Guards.h:278
std::string m_objName
the guarded object name (if there is no INamedInterface)
Definition: Guards.h:276
The most simple guard - it execute the certain code withing typical "try {} catch" clause...
Definition: Guards.h:108
IInterface compliant class extending IInterface with the name() method.
double fun(const std::vector< double > &x)
Definition: PFuncTest.cpp:27
string s
Definition: gaudirun.py:210
This is a number of static methods for bootstrapping the Gaudi framework.
Definition: Bootstrap.h:14
It is a simple guard, which "locks" the scope for the Auditor Service is am exception-safe way...
Definition: Guards.h:218
const StatusCode * m_sc
Pointer to a status code instance, to be passed to the "after" function if needed The instance must h...
Definition: Guards.h:286
#define GAUDI_API
Definition: Kernel.h:108
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
bool m_customEvtType
Flag to remember which event type was used.
Definition: Guards.h:288
The abstract interface for exception handling service.
Definition: IExceptionSvc.h:27