The Gaudi Framework  v30r3 (a5ef0a68)
Guards.h
Go to the documentation of this file.
1 #ifndef GAUDIKERNEL_GUARDS_H
2 #define GAUDIKERNEL_GUARDS_H 1
3 // ============================================================================
4 // Include files
5 // ============================================================================
6 // STD & STL
7 // ============================================================================
8 #include <exception>
9 // ============================================================================
10 // GaudiKernel
11 // ============================================================================
12 #include "GaudiKernel/IAuditor.h"
15 #include "GaudiKernel/SmartIF.h"
16 // ============================================================================
17 // forward declaration
18 // ============================================================================
19 class GaudiException;
20 // ============================================================================
21 namespace Gaudi
22 {
73  namespace Guards
74  {
108  {
109  public:
110  /* constructor form the object/scope, function and log-stream
111  * @param obj the object/scope
112  * @param fun function to be used
113  * @param log output log-stream
114  */
115  template <class OBJECT, class FUNCTION>
116  ExceptionGuard( OBJECT obj, FUNCTION fun, MsgStream& log, IExceptionSvc* svc = 0 )
117  {
118  try {
119  // execute the functor:
120  m_sc = fun( obj );
121  // in the case of error try use Exception Service
122  if ( svc && m_sc.isFailure() ) {
123  m_sc = svc->handleErr( *obj, m_sc );
124  }
125  } catch ( const GaudiException& e ) {
126  // Use the local handler and then (if possible) the Exception Service
127  handle( e, log );
128  if ( svc ) {
129  m_sc = svc->handle( *obj, e );
130  }
131  } catch ( const std::exception& e ) {
132  // Use the local handler and then (if possible) the Exception Service
133  handle( e, log );
134  if ( svc ) {
135  m_sc = svc->handle( *obj, e );
136  }
137  } catch ( ... ) {
138  // Use the local handler and then (if possible) the Exception Service
139  handle( log );
140  if ( svc ) {
141  m_sc = svc->handle( *obj );
142  }
143  }
144  }
146  ~ExceptionGuard();
147 
148  public:
150  const StatusCode& code() const { return m_sc; }
152  operator const StatusCode&() const { return code(); }
153 
154  private:
155  // delete default/copy constructor and assignment
156  ExceptionGuard() = delete;
157  ExceptionGuard( const ExceptionGuard& ) = delete;
158  ExceptionGuard& operator=( const ExceptionGuard& ) = delete;
159 
160  protected:
162  void handle( const GaudiException& e, MsgStream& s );
164  void handle( const std::exception& e, MsgStream& s );
166  void handle( MsgStream& s );
167 
168  private:
169  // status code: result of the function evaluation
171  };
172  // ========================================================================
216  {
217  public:
222 
231 
236 
238  ~AuditorGuard();
239 
240  public:
241  // get the status code
242  const StatusCode& code() const { return *m_sc; }
243 
244  private:
245  // delete the default/copy constructor and assigment
246  AuditorGuard() = delete;
247  AuditorGuard( const AuditorGuard& right ) = delete;
248  AuditorGuard& operator=( const AuditorGuard& right ) = delete;
249 
250  private:
252  INamedInterface* m_obj = nullptr;
256  SmartIF<IAuditor> m_svc = nullptr;
264  const StatusCode* m_sc = nullptr;
266  bool m_customEvtType = false;
267 
268  inline void i_before()
269  {
270  if ( m_svc ) { // if the service is not available, we cannot do anything
271  if ( m_obj ) {
272  if ( m_customEvtType ) {
273  m_svc->before( m_cevt, m_obj );
274  } else {
275  m_svc->before( m_evt, m_obj );
276  }
277  } else { // use object name
278  if ( m_customEvtType ) {
279  m_svc->before( m_cevt, m_objName );
280  } else {
281  m_svc->before( m_evt, m_objName );
282  }
283  }
284  }
285  }
286 
287  inline void i_after()
288  {
289  if ( m_svc ) { // if the service is not available, we cannot do anything
290  if ( m_obj ) {
291  if ( m_customEvtType ) {
292  if ( m_sc ) {
293  m_svc->after( m_cevt, m_obj, *m_sc );
294  } else {
295  m_svc->after( m_cevt, m_obj );
296  }
297  } else {
298  if ( m_sc ) {
299  m_svc->after( m_evt, m_obj, *m_sc );
300  } else {
301  m_svc->after( m_evt, m_obj );
302  }
303  }
304  } else { // use object name
305  if ( m_customEvtType ) {
306  if ( m_sc ) {
307  m_svc->after( m_cevt, m_objName, *m_sc );
308  } else {
309  m_svc->after( m_cevt, m_objName );
310  }
311  } else {
312  if ( m_sc ) {
313  m_svc->after( m_evt, m_objName, *m_sc );
314  } else {
315  m_svc->after( m_evt, m_objName );
316  }
317  }
318  }
319  m_svc.reset();
320  }
321  }
322  };
323  } // end of namespace Gaudi::Guards
324 } // end of namespace Gaudi
325 
326 // ============================================================================
327 // The END
328 // ============================================================================
329 #endif // GAUDIKERNEL_GUARDS_H
330 // ============================================================================
virtual void before(StandardEventType, INamedInterface *)=0
Audit the start of a standard "event".
constexpr static const auto FAILURE
Definition: StatusCode.h:88
Definition of the MsgStream class used to transmit messages.
Definition: MsgStream.h:24
IAuditor::StandardEventType m_evt
Event type (standard events)
Definition: Guards.h:258
Define general base for Gaudi exception.
const StatusCode & code() const
the result of function evaluation
Definition: Guards.h:150
StandardEventType
Defines the standard (= used by the framework) auditable event types.
Definition: IAuditor.h:25
IAuditor::CustomEventType m_cevt
Event type (custom events)
Definition: Guards.h:260
PropertyMgr & operator=(const PropertyMgr &)=delete
STL class.
const StatusCode & code() const
Definition: Guards.h:242
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:51
STL class.
std::string m_objName
the guarded object name (if there is no INamedInterface)
Definition: Guards.h:254
The most simple guard - it execute the certain code withing typical "try {} catch" clause...
Definition: Guards.h:107
IInterface compliant class extending IInterface with the name() method.
ExceptionGuard(OBJECT obj, FUNCTION fun, MsgStream &log, IExceptionSvc *svc=0)
Definition: Guards.h:116
double fun(const std::vector< double > &x)
Definition: PFuncTest.cpp:26
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:253
void reset(TYPE *ptr=nullptr)
Set the internal pointer to the passed one disposing of the old one.
Definition: SmartIF.h:92
It is a simple guard, which "locks" the scope for the Auditor Service is am exception-safe way...
Definition: Guards.h:215
#define GAUDI_API
Definition: Kernel.h:104
A DataObject is the base class of any identifiable object on any data store.
Definition: DataObject.h:30
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:24
evt
Definition: IOTest.py:96