The Gaudi Framework  master (82fdf313)
Loading...
Searching...
No Matches
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
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, EventContext const& context = {} );
207 AuditorGuard( std::string name, IAuditor* svc, std::string const& evt, StatusCode const& sc,
208 EventContext const& context = {} );
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{};
235 const EventContext& m_context;
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 }
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
#define GAUDI_API
Definition Kernel.h:49
DataObject OBJECT
This class represents an entry point to all the event specific data.
const StatusCode code() const
Definition Guards.h:213
std::string const m_evt
Event type.
Definition Guards.h:226
SmartIF< IAuditor > m_svc
auditor service
Definition Guards.h:224
const EventContext & m_context
Pointer to a EventContext instance, to be passed to the "before" and "after" function If given,...
Definition Guards.h:234
AuditorGuard & operator=(const AuditorGuard &right)=delete
AuditorGuard(std::string name, IAuditor *svc, std::string const &evt, EventContext const &context={})
Definition Guards.cpp:42
std::string m_objName
the guarded object name (if there is no INamedInterface)
Definition Guards.h:222
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:230
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