| The Gaudi Framework v32r1 (f65d50dc)

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"
13 #include "GaudiKernel/IExceptionSvc.h"
14 #include "GaudiKernel/INamedInterface.h"
15 #include "GaudiKernel/SmartIF.h"
16 // ============================================================================
17 // forward declaration
18 // ============================================================================
19 class GaudiException;
20 // ============================================================================
21 namespace Gaudi {
72 namespace Guards {
105 class GAUDI_API ExceptionGuard {
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
158 StatusCode m_sc = StatusCode::FAILURE;
159 };
160 // ========================================================================
203 class GAUDI_API AuditorGuard final {
204 public:
206 AuditorGuard( INamedInterface* obj, IAuditor* svc, IAuditor::StandardEventType evt );
208 AuditorGuard( INamedInterface* obj, IAuditor* svc, IAuditor::CustomEventTypeRef evt );
209
211 AuditorGuard( INamedInterface* obj, IAuditor* svc, IAuditor::StandardEventType evt, const StatusCode& sc );
213 AuditorGuard( INamedInterface* obj, IAuditor* svc, IAuditor::CustomEventTypeRef evt, const StatusCode& sc );
215 AuditorGuard( std::string name, IAuditor* svc, IAuditor::StandardEventType evt );
217 AuditorGuard( std::string name, IAuditor* svc, IAuditor::CustomEventTypeRef evt );
218
220 AuditorGuard( std::string name, IAuditor* svc, IAuditor::StandardEventType evt, const StatusCode& sc );
222 AuditorGuard( std::string name, IAuditor* svc, IAuditor::CustomEventTypeRef evt, const StatusCode& sc );
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;
241 std::string m_objName;
243 SmartIF<IAuditor> m_svc = nullptr;
245 IAuditor::StandardEventType m_evt;
247 IAuditor::CustomEventType m_cevt;
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
IAuditor::before
virtual void before(StandardEventType, INamedInterface *)=0
Audit the start of a standard "event".
MsgStream
Definition of the MsgStream class used to transmit messages.
Definition: MsgStream.h:24
SmartIF< IAuditor >
Gaudi::Guards::AuditorGuard::m_evt
IAuditor::StandardEventType m_evt
Event type (standard events)
Definition: Guards.h:245
GaudiException
Define general base for Gaudi exception.
Definition: GaudiException.h:21
Gaudi::Guards::AuditorGuard::i_after
void i_after()
Definition: Guards.h:273
Gaudi::Guards::ExceptionGuard::code
const StatusCode & code() const
the result of function evaluation
Definition: Guards.h:138
gaudiComponentHelp.name
name
Definition: gaudiComponentHelp.py:41
IAuditor::StandardEventType
StandardEventType
Defines the standard (= used by the framework) auditable event types.
Definition: IAuditor.h:24
Gaudi::Guards::AuditorGuard::m_cevt
IAuditor::CustomEventType m_cevt
Event type (custom events)
Definition: Guards.h:247
IAuditor.h
std::string
STL class.
INamedInterface.h
StatusCode
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:50
SmartIF.h
std::exception
STL class.
Gaudi::Guards::AuditorGuard::m_objName
std::string m_objName
the guarded object name (if there is no INamedInterface)
Definition: Guards.h:241
Gaudi::Guards::ExceptionGuard
The most simple guard - it execute the certain code withing typical "try {} catch" clause,...
Definition: Guards.h:105
INamedInterface
IInterface compliant class extending IInterface with the name() method.
Definition: INamedInterface.h:15
Gaudi::Guards::ExceptionGuard::ExceptionGuard
ExceptionGuard(OBJECT obj, FUNCTION fun, MsgStream &log, IExceptionSvc *svc=0)
Definition: Guards.h:113
IAuditor::after
virtual void after(StandardEventType, INamedInterface *, const StatusCode &sc=StatusCode(StatusCode::SUCCESS, true))=0
Audit the end of a standard "event".
gaudirun.s
string s
Definition: gaudirun.py:316
Gaudi::Guards::AuditorGuard::i_before
void i_before()
Definition: Guards.h:255
StatusCode::FAILURE
constexpr static const auto FAILURE
Definition: StatusCode.h:86
SmartIF::reset
void reset(TYPE *ptr=nullptr)
Set the internal pointer to the passed one disposing of the old one.
Definition: SmartIF.h:86
Gaudi::Guards::AuditorGuard
It is a simple guard, which "locks" the scope for the Auditor Service is am exception-safe way.
Definition: Guards.h:203
GAUDI_API
#define GAUDI_API
Definition: Kernel.h:71
DataObject
A DataObject is the base class of any identifiable object on any data store.
Definition: DataObject.h:30
IAuditor
The IAuditor is the interface implmented by the AlgAuditor base class.
Definition: IAuditor.h:18
Gaudi::Guards::AuditorGuard::code
const StatusCode & code() const
Definition: Guards.h:229
Gaudi
Header file for std:chrono::duration-based Counters.
Definition: __init__.py:1
IExceptionSvc
The abstract interface for exception handling service.
Definition: IExceptionSvc.h:24
IOTest.evt
evt
Definition: IOTest.py:94
Gaudi.Configuration.log
log
Definition: Configuration.py:13
IExceptionSvc.h