The Gaudi Framework  master (82fdf313)
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 StatusCode queryInterface( const InterfaceID& riid, void** ppvUnknown ) override;
64 void const* i_cast( const InterfaceID& riid ) const override;
65
67 const std::string& name() const override;
68
70 const std::string& type() const override;
71
73 const IInterface* parent() const override;
74
75 // State machine implementation
77 StatusCode initialize() override;
78 StatusCode start() override;
79 StatusCode stop() override;
80 StatusCode finalize() override;
82 StatusCode reinitialize() override;
83 StatusCode restart() override;
84 Gaudi::StateMachine::State FSMState() const override { return m_state; }
86
88 StatusCode sysInitialize() override;
89
91 StatusCode sysStart() override;
92
94 StatusCode sysStop() override;
95
97 StatusCode sysFinalize() override;
98
100 StatusCode sysReinitialize() override;
101
103 StatusCode sysRestart() override;
104
105public:
111 AlgTool( std::string type, std::string name, const IInterface* parent );
112
114 SmartIF<ISvcLocator>& serviceLocator() const override;
115
117 ISvcLocator* svcLoc() const { return serviceLocator(); }
118
122 IDataProviderSvc* evtSvc() const;
123
125 IToolSvc* toolSvc() const;
126
128 SmartIF<IService> service( std::string_view name, const bool createIf = true, const bool quiet = false ) const;
129
130 template <typename T>
131 SmartIF<T> service( std::string_view name, const bool createIf = true, const bool quiet = false ) const {
132 return SmartIF<T>( service( name, createIf, quiet ) );
133 }
134
135protected:
136 template <typename I>
137 void declareInterface( I* i ) {
138 m_interfaceList.emplace_back( I::interfaceID(), i );
139 }
140
141public:
142 using PropertyHolderImpl::declareProperty;
143
144 template <class T>
146 const std::string& doc = "none" ) {
147 this->declareTool( hndl, hndl.typeAndName() ).ignore();
148 return PropertyHolderImpl::declareProperty( name, hndl, doc );
149 }
150
151 template <class T>
152 StatusCode declareTool( ToolHandle<T>& handle, bool createIf = true ) {
153 return this->declareTool( handle, handle.typeAndName(), createIf );
154 }
155
156 template <class T>
157 StatusCode declareTool( ToolHandle<T>& handle, const std::string& toolTypeAndName, bool createIf = true ) {
158
159 StatusCode sc = handle.initialize( toolTypeAndName, handle.isPublic() ? nullptr : this, createIf );
160 if ( !sc ) {
161 throw GaudiException{ std::string{ "Cannot create handle for " } + ( handle.isPublic() ? "public" : "private" ) +
162 " tool " + toolTypeAndName,
163 name(), sc };
164 }
165
166 m_toolHandles.push_back( &handle );
167
168 return sc;
169 }
170
171 // declare ToolHandleArrays to the AlgTool
172 template <class T>
174 const std::string& doc = "none" ) {
175 addToolsArray( hndlArr );
176 return PropertyHolderImpl::declareProperty( name, hndlArr, doc );
177 }
178
179 template <class T>
181 m_toolHandleArrays.push_back( &hndlArr );
182 }
183
184public:
185 void acceptDHVisitor( IDataHandleVisitor* ) const override;
186
187public:
188 void registerTool( IAlgTool* tool ) const {
189 if ( msgLevel( MSG::DEBUG ) ) debug() << "Registering tool " << tool->name() << endmsg;
190 m_tools.push_back( tool );
191 }
192
193 void deregisterTool( IAlgTool* tool ) const {
194 auto it = std::find( m_tools.begin(), m_tools.end(), tool );
195 if ( it != m_tools.end() ) {
196 if ( msgLevel( MSG::DEBUG ) ) debug() << "De-Registering tool " << tool->name() << endmsg;
197 m_tools.erase( it );
198 } else {
199 if ( msgLevel( MSG::DEBUG ) ) debug() << "Could not de-register tool " << tool->name() << endmsg;
200 }
201 }
202
203 const std::vector<IAlgTool*>& tools() const;
204
205protected:
206 std::vector<IAlgTool*>& tools();
207
209 std::unique_ptr<IDataHandleVisitor> m_updateDataHandles;
210
211private:
212 // place IAlgTools defined via ToolHandles in m_tools
213 void initToolHandles() const;
214
215public:
217 IAuditorSvc* auditorSvc() const;
218
228 inline IMonitorSvc* monitorSvc() const {
229 // If not already located try to locate it without forcing a creation
230 if ( !m_pMonitorSvc ) m_pMonitorSvc = service( m_monitorSvcName, false, true );
231 return m_pMonitorSvc.get();
232 }
233
239 template <class T>
240 void declareInfo( const std::string& name, const T& var, const std::string& desc ) const {
241 IMonitorSvc* mS = monitorSvc();
242 if ( mS ) mS->declareInfo( name, var, desc, this );
243 }
244
252 void declareInfo( const std::string& name, const std::string& format, const void* var, int size,
253 const std::string& desc ) const {
254 IMonitorSvc* mS = monitorSvc();
255 if ( mS ) mS->declareInfo( name, format, var, size, desc, this );
256 }
257
258 // Standard destructor.
259 ~AlgTool() override;
260
261private:
262 typedef std::list<std::pair<InterfaceID, void*>> InterfaceList;
263
264 std::string m_type;
265 const std::string m_name;
266 const IInterface* m_parent = nullptr;
267
273
275
276 // Properties
277 // initialize output level from MessageSvc and initialize messaging (before enabling update handler)
279 this, "OutputLevel", setUpMessaging(),
281 "output level" };
282
283 Gaudi::Property<std::string> m_monitorSvcName{ this, "MonitorService", "MonitorSvc",
284 "name to use for Monitor Service" };
285
286 Gaudi::Property<bool> m_auditorInitialize{ this, "AuditInitialize", false, "trigger auditor on initialize()" };
287 Gaudi::Property<bool> m_auditorStart{ this, "AuditStart", false, "trigger auditor on start()" };
288 Gaudi::Property<bool> m_auditorStop{ this, "AuditStop", false, "trigger auditor on stop()" };
289 Gaudi::Property<bool> m_auditorFinalize{ this, "AuditFinalize", false, "trigger auditor on finalize()" };
290 Gaudi::Property<bool> m_auditorReinitialize{ this, "AuditReinitialize", false, "trigger auditor on reinitialize()" };
291 Gaudi::Property<bool> m_auditorRestart{ this, "AuditRestart", false, "trigger auditor on restart()" };
292
293 // tools used by tool
294 mutable std::vector<IAlgTool*> m_tools;
295 mutable std::vector<BaseToolHandle*> m_toolHandles;
296 mutable std::vector<GaudiHandleArrayBase*> m_toolHandleArrays;
297 mutable bool m_toolHandlesInit = false;
298
301};
#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:188
StatusCode configure() override
Definition AlgTool.h:76
ISvcLocator * svcLoc() const
shortcut for the method service locator
Definition AlgTool.h:117
std::vector< GaudiHandleArrayBase * > m_toolHandleArrays
Definition AlgTool.h:296
std::unique_ptr< IDataHandleVisitor > m_updateDataHandles
Hook for for derived classes to provide a custom visitor for data handles.
Definition AlgTool.h:209
SmartIF< ISvcLocator > m_svcLocator
Pointer to Service Locator service.
Definition AlgTool.h:268
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:85
Gaudi::Details::PropertyBase * declareProperty(const std::string &name, ToolHandleArray< T > &hndlArr, const std::string &doc="none")
Definition AlgTool.h:173
void declareInfo(const std::string &name, const T &var, const std::string &desc) const
Declare monitoring information.
Definition AlgTool.h:240
Gaudi::Property< int > m_outputLevel
Definition AlgTool.h:278
const std::string m_name
AlgTool full name.
Definition AlgTool.h:265
Gaudi::StateMachine::State m_state
flag indicating whether ToolHandle tools have been added to m_tools
Definition AlgTool.h:299
Gaudi::Property< bool > m_auditorInitialize
Definition AlgTool.h:286
InterfaceList m_interfaceList
Interface list.
Definition AlgTool.h:274
const IInterface * parent() const override
Retrieve parent of the sub-algtool.
Definition AlgTool.cpp:76
AlgTool(std::string type, std::string name, const IInterface *parent)
Standard Constructor.
Definition AlgTool.cpp:95
const std::string & type() const override
Retrieve type (concrete class) of the sub-algtool.
Definition AlgTool.cpp:74
void addToolsArray(ToolHandleArray< T > &hndlArr)
Definition AlgTool.h:180
const IInterface * m_parent
AlgTool parent.
Definition AlgTool.h:266
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:355
void deregisterTool(IAlgTool *tool) const
Definition AlgTool.h:193
StatusCode declareTool(ToolHandle< T > &handle, const std::string &toolTypeAndName, bool createIf=true)
Definition AlgTool.h:157
SmartIF< IAuditorSvc > m_pAuditorSvc
Auditor Service.
Definition AlgTool.h:272
const std::string & name() const override
Retrieve full identifying name of the concrete tool object.
Definition AlgTool.cpp:72
std::vector< BaseToolHandle * > m_toolHandles
Definition AlgTool.h:295
SmartIF< T > service(std::string_view name, const bool createIf=true, const bool quiet=false) const
Definition AlgTool.h:131
Gaudi::Property< bool > m_auditorStart
Definition AlgTool.h:287
std::vector< IAlgTool * > m_tools
Definition AlgTool.h:294
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:252
StatusCode terminate() override
Definition AlgTool.h:81
std::list< std::pair< InterfaceID, void * > > InterfaceList
Definition AlgTool.h:262
bool m_toolHandlesInit
Definition AlgTool.h:297
Gaudi::Details::PropertyBase * declareProperty(const std::string &name, ToolHandle< T > &hndl, const std::string &doc="none")
Definition AlgTool.h:145
Gaudi::StateMachine::State FSMState() const override
Definition AlgTool.h:84
void declareInterface(I *i)
Definition AlgTool.h:137
Gaudi::Property< bool > m_auditorStop
Definition AlgTool.h:288
Gaudi::Property< bool > m_auditorReinitialize
Definition AlgTool.h:290
friend ToolSvc
Definition AlgTool.h:56
SmartIF< IDataProviderSvc > m_evtSvc
Event data service.
Definition AlgTool.h:269
std::string m_type
AlgTool type (concrete class name)
Definition AlgTool.h:264
void initToolHandles() const
Definition AlgTool.cpp:302
StatusCode declareTool(ToolHandle< T > &handle, bool createIf=true)
Definition AlgTool.h:152
SmartIF< IMonitorSvc > m_pMonitorSvc
Online Monitoring Service.
Definition AlgTool.h:271
IMonitorSvc * monitorSvc() const
Access the monitor service.
Definition AlgTool.h:228
Gaudi::Property< bool > m_auditorFinalize
Definition AlgTool.h:289
Gaudi::Property< std::string > m_monitorSvcName
Definition AlgTool.h:283
IAuditorSvc * auditorSvc() const
Access the auditor service.
Definition AlgTool.cpp:360
Gaudi::Property< bool > m_auditorRestart
Definition AlgTool.h:291
Gaudi::StateMachine::State m_targetState
state of the Tool
Definition AlgTool.h:300
SmartIF< ISvcLocator > & serviceLocator() const override
Retrieve pointer to service locator.
Definition AlgTool.cpp:78
SmartIF< IToolSvc > m_ptoolSvc
Tool service.
Definition AlgTool.h:270
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:399
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:30
StatusCode queryInterface(const InterfaceID &ti, void **pp) override
Definition extends.h:37
State
Allowed states for classes implementing the state machine (ApplicationMgr, Algorithm,...
@ DEBUG
Definition IMessageSvc.h:22