The Gaudi Framework  master (37c0b60a)
Guards.h
Go to the documentation of this file.
1 /***********************************************************************************\
2 * (c) Copyright 1998-2024 CERN for the benefit of the LHCb and ATLAS collaborations *
3 * *
4 * This software is distributed under the terms of the Apache version 2 licence, *
5 * copied verbatim in the file "LICENSE". *
6 * *
7 * In applying this licence, CERN does not waive the privileges and immunities *
8 * granted to it by virtue of its status as an Intergovernmental Organization *
9 * or submit itself to any jurisdiction. *
10 \***********************************************************************************/
11 #ifndef GAUDIKERNEL_GUARDS_H
12 #define GAUDIKERNEL_GUARDS_H 1
13 // ============================================================================
14 // Include files
15 // ============================================================================
16 // STD & STL
17 // ============================================================================
18 #include <exception>
19 // ============================================================================
20 // GaudiKernel
21 // ============================================================================
22 #include <GaudiKernel/IAuditor.h>
25 #include <GaudiKernel/SmartIF.h>
26 // ============================================================================
27 // forward declaration
28 // ============================================================================
29 class GaudiException;
30 // ============================================================================
31 namespace Gaudi {
82  namespace Guards {
116  public:
117  /* constructor form the object/scope, function and log-stream
118  * @param obj the object/scope
119  * @param fun function to be used
120  * @param log output log-stream
121  */
122  template <class OBJECT, class FUNCTION>
123  ExceptionGuard( OBJECT obj, FUNCTION fun, MsgStream& log, IExceptionSvc* svc = 0 ) {
124  try {
125  // execute the functor:
126  m_sc = fun( obj );
127  // in the case of error try use Exception Service
128  if ( svc && m_sc.isFailure() ) { m_sc = svc->handleErr( *obj, m_sc ); }
129  } catch ( const GaudiException& e ) {
130  // Use the local handler and then (if possible) the Exception Service
131  handle( e, log );
132  if ( svc ) { m_sc = svc->handle( *obj, e ); }
133  } catch ( const std::exception& e ) {
134  // Use the local handler and then (if possible) the Exception Service
135  handle( e, log );
136  if ( svc ) { m_sc = svc->handle( *obj, e ); }
137  } catch ( ... ) {
138  // Use the local handler and then (if possible) the Exception Service
139  handle( log );
140  if ( svc ) { m_sc = svc->handle( *obj ); }
141  }
142  }
144  ~ExceptionGuard();
145 
146  public:
148  const StatusCode& code() const { return m_sc; }
150  operator const StatusCode&() const { return code(); }
151 
152  private:
153  // delete default/copy constructor and assignment
154  ExceptionGuard() = delete;
155  ExceptionGuard( const ExceptionGuard& ) = delete;
157 
158  protected:
160  void handle( const GaudiException& e, MsgStream& s );
162  void handle( const std::exception& e, MsgStream& s );
164  void handle( MsgStream& s );
165 
166  private:
167  // status code: result of the function evaluation
169  };
170  // ========================================================================
213  class GAUDI_API AuditorGuard final {
214  public:
219 
228 
233 
235  ~AuditorGuard();
236 
237  public:
238  // get the status code
239  const StatusCode& code() const { return *m_sc; }
240 
241  private:
242  // delete the default/copy constructor and assigment
243  AuditorGuard() = delete;
244  AuditorGuard( const AuditorGuard& right ) = delete;
245  AuditorGuard& operator=( const AuditorGuard& right ) = delete;
246 
247  private:
249  INamedInterface* m_obj = nullptr;
253  SmartIF<IAuditor> m_svc = nullptr;
261  const StatusCode* m_sc = nullptr;
263  bool m_customEvtType = false;
264 
265  inline void i_before() {
266  if ( m_svc ) { // if the service is not available, we cannot do anything
267  if ( m_obj ) {
268  if ( m_customEvtType ) {
269  m_svc->before( m_cevt, m_obj );
270  } else {
271  m_svc->before( m_evt, m_obj );
272  }
273  } else { // use object name
274  if ( m_customEvtType ) {
275  m_svc->before( m_cevt, m_objName );
276  } else {
277  m_svc->before( m_evt, m_objName );
278  }
279  }
280  }
281  }
282 
283  inline void i_after() {
284  if ( m_svc ) { // if the service is not available, we cannot do anything
285  if ( m_obj ) {
286  if ( m_customEvtType ) {
287  if ( m_sc ) {
288  m_svc->after( m_cevt, m_obj, *m_sc );
289  } else {
290  m_svc->after( m_cevt, m_obj );
291  }
292  } else {
293  if ( m_sc ) {
294  m_svc->after( m_evt, m_obj, *m_sc );
295  } else {
296  m_svc->after( m_evt, m_obj );
297  }
298  }
299  } else { // use object name
300  if ( m_customEvtType ) {
301  if ( m_sc ) {
302  m_svc->after( m_cevt, m_objName, *m_sc );
303  } else {
304  m_svc->after( m_cevt, m_objName );
305  }
306  } else {
307  if ( m_sc ) {
308  m_svc->after( m_evt, m_objName, *m_sc );
309  } else {
310  m_svc->after( m_evt, m_objName );
311  }
312  }
313  }
314  m_svc.reset();
315  }
316  }
317  };
318  } // namespace Guards
319 } // end of namespace Gaudi
320 
321 // ============================================================================
322 // The END
323 // ============================================================================
324 #endif // GAUDIKERNEL_GUARDS_H
IOTest.evt
evt
Definition: IOTest.py:107
std::string
STL class.
std::exception
STL class.
Gaudi.Configuration.log
log
Definition: Configuration.py:28
IExceptionSvc
Definition: IExceptionSvc.h:34
Gaudi::Guards::ExceptionGuard::operator=
ExceptionGuard & operator=(const ExceptionGuard &)=delete
gaudirun.s
string s
Definition: gaudirun.py:346
SmartIF::reset
void reset(TYPE *ptr=nullptr)
Set the internal pointer to the passed one disposing of the old one.
Definition: SmartIF.h:96
Gaudi::Guards::AuditorGuard::operator=
AuditorGuard & operator=(const AuditorGuard &right)=delete
GaudiException
Definition: GaudiException.h:31
Gaudi::Guards::ExceptionGuard::ExceptionGuard
ExceptionGuard()=delete
IExceptionSvc.h
Gaudi::Guards::AuditorGuard
Definition: Guards.h:213
SmartIF.h
INamedInterface.h
StatusCode
Definition: StatusCode.h:65
Gaudi::Guards::AuditorGuard::m_objName
std::string m_objName
the guarded object name (if there is no INamedInterface)
Definition: Guards.h:251
Gaudi::Guards::AuditorGuard::AuditorGuard
AuditorGuard(const AuditorGuard &right)=delete
Gaudi::Guards::AuditorGuard::AuditorGuard
AuditorGuard()=delete
IAuditor.h
SmartIF< IAuditor >
Gaudi::Guards::ExceptionGuard
Definition: Guards.h:115
MsgStream
Definition: MsgStream.h:33
Gaudi
This file provides a Grammar for the type Gaudi::Accumulators::Axis It allows to use that type from p...
Definition: __init__.py:1
Gaudi::Guards::AuditorGuard::i_before
void i_before()
Definition: Guards.h:265
Gaudi::Guards::AuditorGuard::code
const StatusCode & code() const
Definition: Guards.h:239
INamedInterface
Definition: INamedInterface.h:25
ConditionsStallTest.name
name
Definition: ConditionsStallTest.py:77
IAuditor::StandardEventType
StandardEventType
Defines the standard (= used by the framework) auditable event types.
Definition: IAuditor.h:34
DataObject
Definition: DataObject.h:36
Gaudi::Guards::ExceptionGuard::code
const StatusCode & code() const
the result of function evaluation
Definition: Guards.h:148
StatusCode::FAILURE
constexpr static const auto FAILURE
Definition: StatusCode.h:101
Gaudi::Guards::ExceptionGuard::ExceptionGuard
ExceptionGuard(OBJECT obj, FUNCTION fun, MsgStream &log, IExceptionSvc *svc=0)
Definition: Guards.h:123
Gaudi::Guards::ExceptionGuard::ExceptionGuard
ExceptionGuard(const ExceptionGuard &)=delete
Gaudi::Guards::AuditorGuard::m_evt
IAuditor::StandardEventType m_evt
Event type (standard events)
Definition: Guards.h:255
GAUDI_API
#define GAUDI_API
Definition: Kernel.h:81
Gaudi::Guards::AuditorGuard::i_after
void i_after()
Definition: Guards.h:283
IAuditor
Definition: IAuditor.h:28
Gaudi::Guards::AuditorGuard::m_cevt
IAuditor::CustomEventType m_cevt
Event type (custom events)
Definition: Guards.h:257