The Gaudi Framework  master (b9786168)
Loading...
Searching...
No Matches
AlgTool.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/PluginService.h>
28
32#include <list>
33#include <vector>
34
35template <class T>
37class ToolHandleInfo;
38class ToolSvc;
39class ToolVisitor;
40
54 : public DataHandleHolderBase<
55 PropertyHolder<CommonMessaging<implements<IAlgTool, IDataHandleHolder, IProperty, IStateful>>>> {
56 friend ToolSvc;
57 friend class ToolVisitor;
58
59public:
60 using Factory = Gaudi::PluginService::Factory<IAlgTool*( const std::string&, const std::string&, const IInterface* )>;
61
63 void const* i_cast( const InterfaceID& riid ) const override;
64
66 const std::string& name() const override;
67
69 const std::string& type() const override;
70
72 const IInterface* parent() const override;
73
74 // State machine implementation
76 StatusCode initialize() override;
77 StatusCode start() override;
78 StatusCode stop() override;
79 StatusCode finalize() override;
81 StatusCode reinitialize() override;
82 StatusCode restart() override;
83 Gaudi::StateMachine::State FSMState() const override { return m_state; }
85
87 StatusCode sysInitialize() override;
88
90 StatusCode sysStart() override;
91
93 StatusCode sysStop() override;
94
96 StatusCode sysFinalize() override;
97
99 StatusCode sysReinitialize() override;
100
102 StatusCode sysRestart() override;
103
104public:
110 AlgTool( std::string type, std::string name, const IInterface* parent );
111
113 SmartIF<ISvcLocator>& serviceLocator() const override;
114
116 ISvcLocator* svcLoc() const { return serviceLocator(); }
117
121 IDataProviderSvc* evtSvc() const;
122
124 IToolSvc* toolSvc() const;
125
127 SmartIF<IService> service( std::string_view name, const bool createIf = true, const bool quiet = false ) const;
128
129 template <typename T>
130 SmartIF<T> service( std::string_view name, const bool createIf = true, const bool quiet = false ) const {
131 return SmartIF<T>( service( name, createIf, quiet ) );
132 }
133
134protected:
135 template <typename I>
136 void declareInterface( I* i ) {
137 m_interfaceList.emplace_back( I::interfaceID(), i );
138 }
139
140public:
141 using PropertyHolderImpl::declareProperty;
142
143 template <class T>
145 const std::string& doc = "none" ) {
146 this->declareTool( hndl, hndl.typeAndName() ).ignore();
147 return PropertyHolderImpl::declareProperty( name, hndl, doc );
148 }
149
150 template <class T>
151 StatusCode declareTool( ToolHandle<T>& handle, bool createIf = true ) {
152 return this->declareTool( handle, handle.typeAndName(), createIf );
153 }
154
155 template <class T>
156 StatusCode declareTool( ToolHandle<T>& handle, const std::string& toolTypeAndName, bool createIf = true ) {
157
158 StatusCode sc = handle.initialize( toolTypeAndName, handle.isPublic() ? nullptr : this, createIf );
159 if ( !sc ) {
160 throw GaudiException{ std::string{ "Cannot create handle for " } + ( handle.isPublic() ? "public" : "private" ) +
161 " tool " + toolTypeAndName,
162 name(), sc };
163 }
164
165 m_toolHandles.push_back( &handle );
166
167 return sc;
168 }
169
170 // declare ToolHandleArrays to the AlgTool
171 template <class T>
173 const std::string& doc = "none" ) {
174 addToolsArray( hndlArr );
175 return PropertyHolderImpl::declareProperty( name, hndlArr, doc );
176 }
177
178 template <class T>
180 m_toolHandleArrays.push_back( &hndlArr );
181 }
182
183public:
184 void acceptDHVisitor( IDataHandleVisitor* ) const override;
185
186public:
187 void registerTool( IAlgTool* tool ) const {
188 if ( msgLevel( MSG::DEBUG ) ) debug() << "Registering tool " << tool->name() << endmsg;
189 m_tools.push_back( tool );
190 }
191
192 void deregisterTool( IAlgTool* tool ) const {
193 auto it = std::find( m_tools.begin(), m_tools.end(), tool );
194 if ( it != m_tools.end() ) {
195 if ( msgLevel( MSG::DEBUG ) ) debug() << "De-Registering tool " << tool->name() << endmsg;
196 m_tools.erase( it );
197 } else {
198 if ( msgLevel( MSG::DEBUG ) ) debug() << "Could not de-register tool " << tool->name() << endmsg;
199 }
200 }
201
202 const std::vector<IAlgTool*>& tools() const;
203
204protected:
205 std::vector<IAlgTool*>& tools();
206
208 std::unique_ptr<IDataHandleVisitor> m_updateDataHandles;
209
210private:
211 // place IAlgTools defined via ToolHandles in m_tools
212 void initToolHandles() const;
213
214public:
216 IAuditorSvc* auditorSvc() const;
217
227 inline IMonitorSvc* monitorSvc() const {
228 // If not already located try to locate it without forcing a creation
229 if ( !m_pMonitorSvc ) m_pMonitorSvc = service( m_monitorSvcName, false, true );
230 return m_pMonitorSvc.get();
231 }
232
238 template <class T>
239 void declareInfo( const std::string& name, const T& var, const std::string& desc ) const {
240 IMonitorSvc* mS = monitorSvc();
241 if ( mS ) mS->declareInfo( name, var, desc, this );
242 }
243
251 void declareInfo( const std::string& name, const std::string& format, const void* var, int size,
252 const std::string& desc ) const {
253 IMonitorSvc* mS = monitorSvc();
254 if ( mS ) mS->declareInfo( name, format, var, size, desc, this );
255 }
256
257 // Standard destructor.
258 ~AlgTool() override;
259
260private:
261 typedef std::list<std::pair<InterfaceID, void*>> InterfaceList;
262
263 std::string m_type;
264 const std::string m_name;
265 const IInterface* m_parent = nullptr;
266
272
274
275 // Properties
276 // initialize output level from MessageSvc and initialize messaging (before enabling update handler)
278 this, "OutputLevel", setUpMessaging(),
280 "output level" };
281
282 Gaudi::Property<std::string> m_monitorSvcName{ this, "MonitorService", "MonitorSvc",
283 "name to use for Monitor Service" };
284
285 Gaudi::Property<bool> m_auditorInitialize{ this, "AuditInitialize", false, "trigger auditor on initialize()" };
286 Gaudi::Property<bool> m_auditorStart{ this, "AuditStart", false, "trigger auditor on start()" };
287 Gaudi::Property<bool> m_auditorStop{ this, "AuditStop", false, "trigger auditor on stop()" };
288 Gaudi::Property<bool> m_auditorFinalize{ this, "AuditFinalize", false, "trigger auditor on finalize()" };
289 Gaudi::Property<bool> m_auditorReinitialize{ this, "AuditReinitialize", false, "trigger auditor on reinitialize()" };
290 Gaudi::Property<bool> m_auditorRestart{ this, "AuditRestart", false, "trigger auditor on restart()" };
291
292 // tools used by tool
293 mutable std::vector<IAlgTool*> m_tools;
294 mutable std::vector<BaseToolHandle*> m_toolHandles;
295 mutable std::vector<GaudiHandleArrayBase*> m_toolHandleArrays;
296 mutable bool m_toolHandlesInit = false;
297
300};
#define GAUDI_API
Definition Kernel.h:49
GAUDI_API std::string format(const char *,...)
MsgStream format utility "a la sprintf(...)".
Definition MsgStream.cpp:93
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition MsgStream.h:198
Base class from which all the concrete tool classes should be derived.
Definition AlgTool.h:55
void registerTool(IAlgTool *tool) const
Definition AlgTool.h:187
StatusCode configure() override
Definition AlgTool.h:75
ISvcLocator * svcLoc() const
shortcut for the method service locator
Definition AlgTool.h:116
std::vector< GaudiHandleArrayBase * > m_toolHandleArrays
Definition AlgTool.h:295
std::unique_ptr< IDataHandleVisitor > m_updateDataHandles
Hook for for derived classes to provide a custom visitor for data handles.
Definition AlgTool.h:208
SmartIF< ISvcLocator > m_svcLocator
Pointer to Service Locator service.
Definition AlgTool.h:267
Gaudi::PluginService::Factory< IAlgTool *(const std::string &, const std::string &, const IInterface *)> Factory
Definition AlgTool.h:60
Gaudi::StateMachine::State targetFSMState() const override
Definition AlgTool.h:84
Gaudi::Details::PropertyBase * declareProperty(const std::string &name, ToolHandleArray< T > &hndlArr, const std::string &doc="none")
Definition AlgTool.h:172
void declareInfo(const std::string &name, const T &var, const std::string &desc) const
Declare monitoring information.
Definition AlgTool.h:239
Gaudi::Property< int > m_outputLevel
Definition AlgTool.h:277
const std::string m_name
AlgTool full name.
Definition AlgTool.h:264
Gaudi::StateMachine::State m_state
flag indicating whether ToolHandle tools have been added to m_tools
Definition AlgTool.h:298
Gaudi::Property< bool > m_auditorInitialize
Definition AlgTool.h:285
InterfaceList m_interfaceList
Interface list.
Definition AlgTool.h:273
const IInterface * parent() const override
Retrieve parent of the sub-algtool.
Definition AlgTool.cpp:61
AlgTool(std::string type, std::string name, const IInterface *parent)
Standard Constructor.
Definition AlgTool.cpp:80
const std::string & type() const override
Retrieve type (concrete class) of the sub-algtool.
Definition AlgTool.cpp:59
void addToolsArray(ToolHandleArray< T > &hndlArr)
Definition AlgTool.h:179
const IInterface * m_parent
AlgTool parent.
Definition AlgTool.h:265
friend class ToolVisitor
Definition AlgTool.h:57
SmartIF< IService > service(std::string_view name, const bool createIf=true, const bool quiet=false) const
Return a pointer to the service identified by name (or "type/name")
Definition AlgTool.cpp:340
void deregisterTool(IAlgTool *tool) const
Definition AlgTool.h:192
StatusCode declareTool(ToolHandle< T > &handle, const std::string &toolTypeAndName, bool createIf=true)
Definition AlgTool.h:156
SmartIF< IAuditorSvc > m_pAuditorSvc
Auditor Service.
Definition AlgTool.h:271
const std::string & name() const override
Retrieve full identifying name of the concrete tool object.
Definition AlgTool.cpp:57
std::vector< BaseToolHandle * > m_toolHandles
Definition AlgTool.h:294
SmartIF< T > service(std::string_view name, const bool createIf=true, const bool quiet=false) const
Definition AlgTool.h:130
Gaudi::Property< bool > m_auditorStart
Definition AlgTool.h:286
std::vector< IAlgTool * > m_tools
Definition AlgTool.h:293
void declareInfo(const std::string &name, const std::string &format, const void *var, int size, const std::string &desc) const
Declare monitoring information (special case)
Definition AlgTool.h:251
StatusCode terminate() override
Definition AlgTool.h:80
std::list< std::pair< InterfaceID, void * > > InterfaceList
Definition AlgTool.h:261
bool m_toolHandlesInit
Definition AlgTool.h:296
Gaudi::Details::PropertyBase * declareProperty(const std::string &name, ToolHandle< T > &hndl, const std::string &doc="none")
Definition AlgTool.h:144
Gaudi::StateMachine::State FSMState() const override
Definition AlgTool.h:83
void declareInterface(I *i)
Definition AlgTool.h:136
Gaudi::Property< bool > m_auditorStop
Definition AlgTool.h:287
Gaudi::Property< bool > m_auditorReinitialize
Definition AlgTool.h:289
friend ToolSvc
Definition AlgTool.h:56
SmartIF< IDataProviderSvc > m_evtSvc
Event data service.
Definition AlgTool.h:268
std::string m_type
AlgTool type (concrete class name)
Definition AlgTool.h:263
void initToolHandles() const
Definition AlgTool.cpp:287
StatusCode declareTool(ToolHandle< T > &handle, bool createIf=true)
Definition AlgTool.h:151
SmartIF< IMonitorSvc > m_pMonitorSvc
Online Monitoring Service.
Definition AlgTool.h:270
IMonitorSvc * monitorSvc() const
Access the monitor service.
Definition AlgTool.h:227
Gaudi::Property< bool > m_auditorFinalize
Definition AlgTool.h:288
Gaudi::Property< std::string > m_monitorSvcName
Definition AlgTool.h:282
IAuditorSvc * auditorSvc() const
Access the auditor service.
Definition AlgTool.cpp:345
Gaudi::Property< bool > m_auditorRestart
Definition AlgTool.h:290
Gaudi::StateMachine::State m_targetState
state of the Tool
Definition AlgTool.h:299
SmartIF< ISvcLocator > & serviceLocator() const override
Retrieve pointer to service locator.
Definition AlgTool.cpp:63
SmartIF< IToolSvc > m_ptoolSvc
Tool service.
Definition AlgTool.h:269
MsgStream & debug() const
shortcut for the method msgStream(MSG::DEBUG)
DataObjectHandle.h GaudiKernel/DataObjectHandle.h.
PropertyBase base class allowing PropertyBase* collections to be "homogeneous".
Implementation of property with value of concrete type.
Definition PropertyFwd.h:27
Define general base for Gaudi exception.
The interface implemented by the AlgTool base class.
Definition IAlgTool.h:29
The interface implemented by the IAuditorSvc base class.
Definition IAuditorSvc.h:24
Data provider interface definition.
Definition of the basic interface.
Definition IInterface.h:225
Definition of the IMonitorSvc interface, which publishes Gaudi variables to outside monitoring proces...
Definition IMonitorSvc.h:32
virtual void declareInfo(const std::string &name, const bool &var, const std::string &desc, const IInterface *owner)=0
Declare monitoring information.
The ISvcLocator is the interface implemented by the Service Factory in the Application Manager to loc...
Definition ISvcLocator.h:42
The interface implemented by the IToolSvc base class.
Definition IToolSvc.h:28
Interface ID class.
Definition IInterface.h:38
Gaudi::Details::PropertyBase & declareProperty(Gaudi::Details::PropertyBase &prop)
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
Array of Handles to be used in lieu of vector of naked pointers to tools.
Definition ToolHandle.h:393
Handle to be used in lieu of naked pointers to tools.
Definition ToolHandle.h:132
std::string typeAndName() const override
Definition ToolHandle.h:273
StatusCode initialize(const std::string &toolTypeAndName, const IInterface *parent=nullptr, bool createIf=true)
Definition ToolHandle.h:213
General info and helper functions for toolhandles and arrays.
Definition ToolHandle.h:40
bool isPublic() const noexcept
Definition ToolHandle.h:49
This service manages tools.
Definition ToolSvc.cpp:45
void const * i_cast(const InterfaceID &tid) const override
Definition extends.h:32
State
Allowed states for classes implementing the state machine (ApplicationMgr, Algorithm,...
@ DEBUG
Definition IMessageSvc.h:22