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/SmartIF.h"
13 #include "GaudiKernel/INamedInterface.h"
14 #include "GaudiKernel/IExceptionSvc.h"
15 #include "GaudiKernel/IAuditor.h"
16 // ============================================================================
17 // forward declaration
18 // ============================================================================
19 class GaudiException ;
20 // ============================================================================
21 namespace Gaudi
22 {
73  namespace Guards
74  {
107  class GAUDI_API ExceptionGuard
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
117  ( OBJECT obj ,
118  FUNCTION fun ,
119  MsgStream& log ,
120  IExceptionSvc* svc = 0 )
121  {
122  try
123  {
124  // execute the functor:
125  m_sc = fun ( obj ) ;
126  // in the case of error try use Exception Service
127  if ( svc && m_sc.isFailure() ) { m_sc = svc->handleErr ( *obj , m_sc ) ; }
128  }
129  catch ( const GaudiException& e )
130  {
131  // Use the local handler and then (if possible) the Exception Service
132  handle ( e , log ) ;
133  if ( svc ) { m_sc = svc -> handle ( *obj , e ) ; }
134  }
135  catch ( const std::exception& e )
136  {
137  // Use the local handler and then (if possible) the Exception Service
138  handle ( e , log ) ;
139  if ( svc ) { m_sc = svc -> handle ( *obj , e ) ; }
140  }
141  catch ( ... )
142  {
143  // Use the local handler and then (if possible) the Exception Service
144  handle ( log ) ;
145  if ( svc ) { m_sc = svc -> handle ( *obj ) ; }
146  }
147  }
149  ~ExceptionGuard() ;
150  public:
152  const StatusCode& code () const { return m_sc ; }
154  operator const StatusCode&() const { return code() ; }
155  private:
156  // delete default/copy constructor and assignment
157  ExceptionGuard() = delete;
158  ExceptionGuard ( const ExceptionGuard& ) = delete;
159  ExceptionGuard& operator=( const ExceptionGuard& ) = delete;
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  private:
168  // status code: result of the function evaluation
170  } ;
171  // ========================================================================
214  class GAUDI_API AuditorGuard final
215  {
216  public:
218  AuditorGuard ( INamedInterface* obj ,
219  IAuditor* svc ,
222  AuditorGuard ( INamedInterface* obj ,
223  IAuditor* svc ,
225 
227  AuditorGuard ( INamedInterface* obj ,
228  IAuditor* svc ,
230  const StatusCode &sc ) ;
232  AuditorGuard ( INamedInterface* obj ,
233  IAuditor* svc ,
235  const StatusCode &sc ) ;
237  AuditorGuard ( std::string name ,
238  IAuditor* svc ,
241  AuditorGuard ( std::string name ,
242  IAuditor* svc ,
244 
246  AuditorGuard ( std::string name ,
247  IAuditor* svc ,
249  const StatusCode &sc ) ;
251  AuditorGuard ( std::string name ,
252  IAuditor* svc ,
254  const StatusCode &sc ) ;
255 
257  ~AuditorGuard() ;
258  public:
259  // get the status code
260  const StatusCode &code () const { return *m_sc ; }
261  private:
262  // delete the default/copy constructor and assigment
263  AuditorGuard () = delete;
264  AuditorGuard ( const AuditorGuard& right ) = delete;
265  AuditorGuard& operator=( const AuditorGuard& right ) = delete;
266  private :
268  INamedInterface* m_obj = nullptr ;
270  std::string m_objName;
272  SmartIF<IAuditor> m_svc = nullptr;
280  const StatusCode *m_sc = nullptr;
282  bool m_customEvtType = false;
283 
284  inline void i_before() {
285  if ( m_svc ) { // if the service is not available, we cannot do anything
286  if (m_obj) {
287  if (m_customEvtType) {
288  m_svc->before(m_cevt,m_obj);
289  } else {
290  m_svc->before(m_evt,m_obj);
291  }
292  } else { // use object name
293  if (m_customEvtType) {
294  m_svc->before(m_cevt,m_objName);
295  } else {
296  m_svc->before(m_evt,m_objName);
297  }
298  }
299  }
300  }
301 
302  inline void i_after() {
303  if ( m_svc ) { // if the service is not available, we cannot do anything
304  if ( m_obj) {
305  if (m_customEvtType) {
306  if ( m_sc) {
307  m_svc->after(m_cevt,m_obj,*m_sc);
308  } else {
309  m_svc->after(m_cevt,m_obj);
310  }
311  } else {
312  if (m_sc) {
313  m_svc->after(m_evt,m_obj,*m_sc);
314  } else {
315  m_svc->after(m_evt,m_obj);
316  }
317  }
318  } else { // use object name
319  if (m_customEvtType) {
320  if (m_sc) {
321  m_svc->after(m_cevt,m_objName,*m_sc);
322  } else {
323  m_svc->after(m_cevt,m_objName);
324  }
325  } else {
326  if (m_sc) {
327  m_svc->after(m_evt,m_objName,*m_sc);
328  } else {
329  m_svc->after(m_evt,m_objName);
330  }
331  }
332  }
333  m_svc.reset();
334  }
335  }
336  } ;
337  } // end of namespace Gaudi::Guards
338 } //end of namespace Gaudi
339 
340 
341 // ============================================================================
342 // The END
343 // ============================================================================
344 #endif // GAUDIKERNEL_GUARDS_H
345 // ============================================================================
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.
#define GAUDI_API
Definition: Kernel.h:107
StandardEventType
Defines the standard (= used by the framework) auditable event types.
Definition: IAuditor.h:24
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:26
IInterface compliant class extending IInterface with the name() method.
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:245
void reset(TYPE *ptr=nullptr)
Set the internal pointer to the passed one disposing of the old one.
Definition: SmartIF.h:88
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