Loading [MathJax]/extensions/tex2jax.js
The Gaudi Framework  v31r0 (aeb156f0)
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
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 {
72  namespace Guards {
106  public:
107  /* constructor form the object/scope, function and log-stream
108  * @param obj the object/scope
109  * @param fun function to be used
110  * @param log output log-stream
111  */
112  template <class OBJECT, class FUNCTION>
113  ExceptionGuard( OBJECT obj, FUNCTION fun, MsgStream& log, IExceptionSvc* svc = 0 ) {
114  try {
115  // execute the functor:
116  m_sc = fun( obj );
117  // in the case of error try use Exception Service
118  if ( svc && m_sc.isFailure() ) { m_sc = svc->handleErr( *obj, m_sc ); }
119  } catch ( const GaudiException& e ) {
120  // Use the local handler and then (if possible) the Exception Service
121  handle( e, log );
122  if ( svc ) { m_sc = svc->handle( *obj, e ); }
123  } catch ( const std::exception& e ) {
124  // Use the local handler and then (if possible) the Exception Service
125  handle( e, log );
126  if ( svc ) { m_sc = svc->handle( *obj, e ); }
127  } catch ( ... ) {
128  // Use the local handler and then (if possible) the Exception Service
129  handle( log );
130  if ( svc ) { m_sc = svc->handle( *obj ); }
131  }
132  }
134  ~ExceptionGuard();
135 
136  public:
138  const StatusCode& code() const { return m_sc; }
140  operator const StatusCode&() const { return code(); }
141 
142  private:
143  // delete default/copy constructor and assignment
144  ExceptionGuard() = delete;
145  ExceptionGuard( const ExceptionGuard& ) = delete;
146  ExceptionGuard& operator=( const ExceptionGuard& ) = delete;
147 
148  protected:
150  void handle( const GaudiException& e, MsgStream& s );
152  void handle( const std::exception& e, MsgStream& s );
154  void handle( MsgStream& s );
155 
156  private:
157  // status code: result of the function evaluation
159  };
160  // ========================================================================
203  class GAUDI_API AuditorGuard final {
204  public:
209 
218 
223 
225  ~AuditorGuard();
226 
227  public:
228  // get the status code
229  const StatusCode& code() const { return *m_sc; }
230 
231  private:
232  // delete the default/copy constructor and assigment
233  AuditorGuard() = delete;
234  AuditorGuard( const AuditorGuard& right ) = delete;
235  AuditorGuard& operator=( const AuditorGuard& right ) = delete;
236 
237  private:
239  INamedInterface* m_obj = nullptr;
243  SmartIF<IAuditor> m_svc = nullptr;
251  const StatusCode* m_sc = nullptr;
253  bool m_customEvtType = false;
254 
255  inline void i_before() {
256  if ( m_svc ) { // if the service is not available, we cannot do anything
257  if ( m_obj ) {
258  if ( m_customEvtType ) {
259  m_svc->before( m_cevt, m_obj );
260  } else {
261  m_svc->before( m_evt, m_obj );
262  }
263  } else { // use object name
264  if ( m_customEvtType ) {
265  m_svc->before( m_cevt, m_objName );
266  } else {
267  m_svc->before( m_evt, m_objName );
268  }
269  }
270  }
271  }
272 
273  inline void i_after() {
274  if ( m_svc ) { // if the service is not available, we cannot do anything
275  if ( m_obj ) {
276  if ( m_customEvtType ) {
277  if ( m_sc ) {
278  m_svc->after( m_cevt, m_obj, *m_sc );
279  } else {
280  m_svc->after( m_cevt, m_obj );
281  }
282  } else {
283  if ( m_sc ) {
284  m_svc->after( m_evt, m_obj, *m_sc );
285  } else {
286  m_svc->after( m_evt, m_obj );
287  }
288  }
289  } else { // use object name
290  if ( m_customEvtType ) {
291  if ( m_sc ) {
292  m_svc->after( m_cevt, m_objName, *m_sc );
293  } else {
294  m_svc->after( m_cevt, m_objName );
295  }
296  } else {
297  if ( m_sc ) {
298  m_svc->after( m_evt, m_objName, *m_sc );
299  } else {
300  m_svc->after( m_evt, m_objName );
301  }
302  }
303  }
304  m_svc.reset();
305  }
306  }
307  };
308  } // namespace Guards
309 } // end of namespace Gaudi
310 
311 // ============================================================================
312 // The END
313 // ============================================================================
314 #endif // GAUDIKERNEL_GUARDS_H
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
IAuditor::StandardEventType m_evt
Event type (standard events)
Definition: Guards.h:245
Define general base for Gaudi exception.
const StatusCode & code() const
the result of function evaluation
Definition: Guards.h:138
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:247
PropertyMgr & operator=(const PropertyMgr &)=delete
STL class.
const StatusCode & code() const
Definition: Guards.h:229
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:50
STL class.
std::string m_objName
the guarded object name (if there is no INamedInterface)
Definition: Guards.h:241
The most simple guard - it execute the certain code withing typical "try {} catch" clause...
Definition: Guards.h:105
IInterface compliant class extending IInterface with the name() method.
ExceptionGuard(OBJECT obj, FUNCTION fun, MsgStream &log, IExceptionSvc *svc=0)
Definition: Guards.h:113
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:312
constexpr static const auto FAILURE
Definition: StatusCode.h:86
void reset(TYPE *ptr=nullptr)
Set the internal pointer to the passed one disposing of the old one.
Definition: SmartIF.h:86
It is a simple guard, which "locks" the scope for the Auditor Service is am exception-safe way...
Definition: Guards.h:203
#define GAUDI_API
Definition: Kernel.h:71
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:94