The Gaudi Framework  master (d98a2936)
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
Guards.h
Go to the documentation of this file.
1 /***********************************************************************************\
2 * (c) Copyright 1998-2025 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 #pragma once
12 
13 #include <Gaudi/IAuditor.h>
16 #include <GaudiKernel/SmartIF.h>
17 #include <exception>
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;
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:
205  // user facing constructors
206  AuditorGuard( std::string name, IAuditor* svc, std::string const& evt, EventContext const& context = {} );
207  AuditorGuard( std::string name, IAuditor* svc, std::string const& evt, StatusCode const& sc,
208  EventContext const& context = {} );
209 
210  ~AuditorGuard() { i_after(); }
211 
212  public:
213  // get the status code
214  const StatusCode code() const { return m_sc ? *m_sc : StatusCode::SUCCESS; }
215 
216  private:
217  // delete the default/copy constructor and assigment
218  AuditorGuard( const AuditorGuard& right ) = delete;
219  AuditorGuard& operator=( const AuditorGuard& right ) = delete;
220 
221  private:
223  std::string m_objName;
225  SmartIF<IAuditor> m_svc = nullptr;
227  std::string const m_evt;
231  StatusCode const* m_sc{};
236 
237  inline void i_before() {
238  if ( m_svc ) { // if the service is not available, we cannot do anything
239  m_svc->before( m_evt, m_objName, m_context );
240  }
241  }
242 
243  inline void i_after() {
244  if ( m_svc ) { // if the service is not available, we cannot do anything
245  if ( m_sc ) {
246  m_svc->after( m_evt, m_objName, m_context, *m_sc );
247  } else {
248  m_svc->after( m_evt, m_objName, m_context );
249  }
250  m_svc.reset();
251  }
252  }
253  };
254  } // namespace Guards
255 } // namespace Gaudi
IOTest.evt
evt
Definition: IOTest.py:107
Gaudi.Configuration.log
log
Definition: Configuration.py:28
IExceptionSvc
Definition: IExceptionSvc.h:25
Gaudi::Guards::ExceptionGuard::operator=
ExceptionGuard & operator=(const ExceptionGuard &)=delete
gaudirun.s
string s
Definition: gaudirun.py:346
Gaudi::IAuditor
The IAuditor is the interface implemented by the Auditor base class.
Definition: IAuditor.h:26
SmartIF::reset
void reset(TYPE *ptr=nullptr)
Set the internal pointer to the passed one disposing of the old one.
Definition: SmartIF.h:88
Gaudi::Guards::AuditorGuard::operator=
AuditorGuard & operator=(const AuditorGuard &right)=delete
GaudiException
Definition: GaudiException.h:29
Gaudi::Guards::AuditorGuard::m_evt
std::string const m_evt
Event type.
Definition: Guards.h:227
Gaudi::Guards::ExceptionGuard::ExceptionGuard
ExceptionGuard()=delete
IExceptionSvc.h
Gaudi::Guards::AuditorGuard
Definition: Guards.h:203
SmartIF.h
StatusCode
Definition: StatusCode.h:64
Gaudi::Guards::AuditorGuard::m_objName
std::string m_objName
the guarded object name (if there is no INamedInterface)
Definition: Guards.h:223
Gaudi::Guards::AuditorGuard::AuditorGuard
AuditorGuard(const AuditorGuard &right)=delete
IAuditor.h
SmartIF
Definition: IConverter.h:22
Gaudi::Guards::ExceptionGuard
Definition: Guards.h:105
MsgStream
Definition: MsgStream.h:29
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:237
Gaudi::Guards::AuditorGuard::m_context
const EventContext & m_context
Pointer to a EventContext instance, to be passed to the "before" and "after" function If given,...
Definition: Guards.h:235
StatusCode::SUCCESS
constexpr static const auto SUCCESS
Definition: StatusCode.h:99
ConditionsStallTest.name
name
Definition: ConditionsStallTest.py:77
EventContext.h
EventContext
Definition: EventContext.h:34
DataObject
Definition: DataObject.h:37
Gaudi::Guards::ExceptionGuard::code
const StatusCode & code() const
the result of function evaluation
Definition: Guards.h:138
StatusCode::FAILURE
constexpr static const auto FAILURE
Definition: StatusCode.h:100
Gaudi::Guards::ExceptionGuard::ExceptionGuard
ExceptionGuard(OBJECT obj, FUNCTION fun, MsgStream &log, IExceptionSvc *svc=0)
Definition: Guards.h:113
Gaudi::Guards::ExceptionGuard::ExceptionGuard
ExceptionGuard(const ExceptionGuard &)=delete
GAUDI_API
#define GAUDI_API
Definition: Kernel.h:49
Gaudi::Guards::AuditorGuard::i_after
void i_after()
Definition: Guards.h:243
Gaudi::Guards::AuditorGuard::~AuditorGuard
~AuditorGuard()
Definition: Guards.h:210
Gaudi::Guards::AuditorGuard::code
const StatusCode code() const
Definition: Guards.h:214