The Gaudi Framework  master (9f3f8895)
Loading...
Searching...
No Matches
Guards.h
Go to the documentation of this file.
1/***********************************************************************************\
2* (c) Copyright 1998-2026 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
19class GaudiException;
20
21namespace Gaudi {
72 namespace Guards {
104 */
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 }
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
202 */
203 class GAUDI_API AuditorGuard final {
204 public:
205 // user facing constructors
206 AuditorGuard( std::string name, IAuditor* svc, std::string const& evt );
207 AuditorGuard( std::string name, IAuditor* svc, std::string const& evt, EventContext const& context );
208 AuditorGuard( std::string name, IAuditor* svc, std::string const& evt, StatusCode const& sc );
209 AuditorGuard( std::string name, IAuditor* svc, std::string const& evt, StatusCode const& sc,
210 EventContext const& context );
212 ~AuditorGuard() { i_after(); }
213
214 public:
215 // get the status code
216 const StatusCode code() const { return m_sc ? *m_sc : StatusCode::SUCCESS; }
217
218 private:
219 // delete the default/copy constructor and assigment
220 AuditorGuard( const AuditorGuard& right ) = delete;
221 AuditorGuard& operator=( const AuditorGuard& right ) = delete;
222
223 private:
227 /// entire lifetime.
228 static EventContext const& defaultContext() {
229 static EventContext const context{};
230 return context;
231 }
233 inline void i_before() {
234 if ( m_svc ) { // if the service is not available, we cannot do anything
236 }
237 }
239 inline void i_after() {
240 if ( m_svc ) { // if the service is not available, we cannot do anything
241 if ( m_sc ) {
243 } else {
245 }
246 m_svc.reset();
247 }
248 }
249
250 private:
252 std::string m_objName;
256 std::string const m_evt;
260 StatusCode const* m_sc{};
265 EventContext const* m_context{};
266 };
267 } // namespace Guards
268} // namespace Gaudi
#define GAUDI_API
Definition Kernel.h:49
This class represents an entry point to all the event specific data.
const StatusCode code() const
Definition Guards.h:215
std::string const m_evt
Event type.
Definition Guards.h:255
SmartIF< IAuditor > m_svc
auditor service
Definition Guards.h:253
EventContext const * m_context
Pointer to an EventContext instance, to be passed to the "before" and "after" function.
Definition Guards.h:264
AuditorGuard & operator=(const AuditorGuard &right)=delete
static EventContext const & defaultContext()
Stable, default-constructed EventContext used when the guard was not given one.
Definition Guards.h:227
AuditorGuard(std::string name, IAuditor *svc, std::string const &evt)
Definition Guards.cpp:42
std::string m_objName
the guarded object name (if there is no INamedInterface)
Definition Guards.h:251
StatusCode const * m_sc
Pointer to a status code instance, to be passed to the "after" function if needed The instance must h...
Definition Guards.h:259
The most simple guard - it execute the certain code withing typical "try {} catch" clause,...
Definition Guards.h:104
ExceptionGuard & operator=(const ExceptionGuard &)=delete
void handle(const GaudiException &e, MsgStream &s)
local handler of GaudiException
Definition Guards.cpp:25
ExceptionGuard(const ExceptionGuard &)=delete
StatusCode m_sc
status code : result of function evaluation
Definition Guards.h:157
const StatusCode & code() const
the result of function evaluation
Definition Guards.h:137
ExceptionGuard(OBJECT obj, FUNCTION fun, MsgStream &log, IExceptionSvc *svc=0)
Definition Guards.h:112
The IAuditor is the interface implemented by the Auditor base class.
Definition IAuditor.h:26
Define general base for Gaudi exception.
The abstract interface for exception handling service.
Definition of the MsgStream class used to transmit messages.
Definition MsgStream.h:29
Small smart pointer class with automatic reference counting for IInterface.
Definition SmartIF.h:28
This class is used for returning status codes from appropriate routines.
Definition StatusCode.h:64
constexpr static const auto SUCCESS
Definition StatusCode.h:99
constexpr static const auto FAILURE
Definition StatusCode.h:100
Guards.h GaudiKernel/Guards.h Collection of very simple classes which allows to execution of some fun...
This file provides a Grammar for the type Gaudi::Accumulators::Axis It allows to use that type from p...
Definition __init__.py:1