| The Gaudi Framework v28r2p1 (f1a77ff4)

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

AlgTool.h
Go to the documentation of this file.
1 #ifndef GAUDIKERNEL_ALGTOOL_H
2 #define GAUDIKERNEL_ALGTOOL_H
3 // ============================================================================
4 // Include files
5 #include "GaudiKernel/CommonMessaging.h"
6 #include "GaudiKernel/IAlgTool.h"
7 #include "GaudiKernel/IAuditorSvc.h"
8 #include "GaudiKernel/IDataProviderSvc.h"
9 #include "GaudiKernel/IMessageSvc.h"
10 #include "GaudiKernel/IMonitorSvc.h"
11 #include "GaudiKernel/IProperty.h"
12 #include "GaudiKernel/IService.h"
13 #include "GaudiKernel/IStateful.h"
14 #include "GaudiKernel/ISvcLocator.h"
15 #include "GaudiKernel/IToolSvc.h"
16 #include "GaudiKernel/PropertyHolder.h"
17 #include "GaudiKernel/ToolHandle.h"
18 #include <Gaudi/PluginService.h>
19
20 #include "GaudiKernel/DataObjIDProperty.h"
21
22 #include "GaudiKernel/DataHandle.h"
23 #include "GaudiKernel/DataHandleHolderBase.h"
24 #include "GaudiKernel/IDataHandleHolder.h"
25
26 template <class T>
27 class DataObjectHandle;
28
29 class ToolHandleInfo;
30
31 #include <list>
32 #include <vector>
33
34 // Forward declarations
35
48 class GAUDI_API AlgTool
49 : public DataHandleHolderBase<PropertyHolder<CommonMessaging<implements<IAlgTool, IDataHandleHolder, IProperty, IStateful>>>>
50 {
51 public:
52 #ifndef __REFLEX__
53 typedef Gaudi::PluginService::Factory<IAlgTool*, const std::string&, const std::string&, const IInterface*> Factory;
54 #endif
55
57 StatusCode queryInterface( const InterfaceID& riid, void** ppvUnknown ) override;
58
60 const std::string& name() const override;
61
63 const std::string& type() const override;
64
66 const IInterface* parent() const override;
67
68 // State machine implementation
69 StatusCode configure() override { return StatusCode::SUCCESS; }
70 StatusCode initialize() override;
71 StatusCode start() override;
72 StatusCode stop() override;
73 StatusCode finalize() override;
74 StatusCode terminate() override { return StatusCode::SUCCESS; }
75 StatusCode reinitialize() override;
76 StatusCode restart() override;
77 Gaudi::StateMachine::State FSMState() const override { return m_state; }
78 Gaudi::StateMachine::State targetFSMState() const override { return m_targetState; }
79
81 StatusCode sysInitialize() override;
82
84 StatusCode sysStart() override;
85
87 StatusCode sysStop() override;
88
90 StatusCode sysFinalize() override;
91
93 StatusCode sysReinitialize() override;
94
96 StatusCode sysRestart() override;
97
98 public:
104 AlgTool( const std::string& type, const std::string& name, const IInterface* parent );
105
107 SmartIF<ISvcLocator>& serviceLocator() const override;
108
110 ISvcLocator* svcLoc() const { return serviceLocator(); }
111
115 IDataProviderSvc* evtSvc() const;
116
118 IToolSvc* toolSvc() const;
119
125 StatusCode setProperties();
126
130 template <class T>
131 StatusCode service( const std::string& name, T*& svc, bool createIf = true ) const
132 {
133 return service_i( name, createIf, T::interfaceID(), (void**)&svc );
134 }
135
138 template <class T>
139 StatusCode service( const std::string& type, const std::string& name, T*& svc ) const
140 {
141 return service_i( type, name, T::interfaceID(), (void**)&svc );
142 }
143
145 SmartIF<IService> service( const std::string& name, const bool createIf = true, const bool quiet = false ) const;
146
147 template <typename T>
148 SmartIF<T> service( const std::string& name, const bool createIf = true, const bool quiet = false ) const
149 {
150 return SmartIF<T>( service( name, createIf, quiet ) );
151 }
152
153 protected:
154 template <typename I>
155 void declareInterface( I* i )
156 {
157 m_interfaceList.emplace_back( I::interfaceID(), i );
158 }
159
160 public:
161 using PropertyHolderImpl::declareProperty;
162
163 template <class T>
164 Gaudi::Details::PropertyBase* declareProperty( const std::string& name, ToolHandle<T>& hndl,
165 const std::string& doc = "none" )
166 {
167 this->declareTool( hndl ).ignore();
168 return PropertyHolderImpl::declareProperty( name, hndl, doc );
169 }
170
171 template <class T>
172 StatusCode declareTool( ToolHandle<T>& handle, std::string toolTypeAndName = "", bool createIf = true )
173 {
174 if ( handle.isPublic() ) {
175 return declarePublicTool( handle, toolTypeAndName, createIf );
176 } else {
177 return declarePrivateTool( handle, toolTypeAndName, createIf );
178 }
179 }
180
181 // ==========================================================================
182 // declare ToolHandleArrays to the AlgTool
183 template <class T>
184 Gaudi::Details::PropertyBase* declareProperty( const std::string& name, ToolHandleArray<T>& hndlArr,
185 const std::string& doc = "none" )
186 {
187 m_toolHandleArrays.push_back( &hndlArr );
188 return PropertyHolderImpl::declareProperty( name, hndlArr, doc );
189 }
190
191 public:
192
193 virtual void acceptDHVisitor( IDataHandleVisitor* ) const override;
194
195 void commitHandles() override;
196
197 public:
198 void registerTool( IAlgTool* tool ) const
199 {
200 if ( UNLIKELY( msgLevel( MSG::DEBUG ) ) ) debug() << "Registering tool " << tool->name() << endmsg;
201 m_tools.push_back( tool );
202 }
203
204 void deregisterTool( IAlgTool* tool ) const
205 {
206 auto it = std::find( m_tools.begin(), m_tools.end(), tool );
207 if ( it != m_tools.end() ) {
208 if ( UNLIKELY( msgLevel( MSG::DEBUG ) ) ) debug() << "De-Registering tool " << tool->name() << endmsg;
209 m_tools.erase( it );
210 } else {
211 if ( UNLIKELY( msgLevel( MSG::DEBUG ) ) ) debug() << "Could not de-register tool " << tool->name() << endmsg;
212 }
213 }
214
222 template <class T>
223 StatusCode declarePublicTool( ToolHandle<T>& handle, std::string toolTypeAndName = "", bool createIf = true )
224 {
225
226 if ( toolTypeAndName == "" ) toolTypeAndName = handle.typeAndName();
227
228 StatusCode sc = handle.initialize( toolTypeAndName, 0, createIf );
229 if ( UNLIKELY( !sc ) ) {
230 throw GaudiException{"Cannot create handle for public tool " + toolTypeAndName, name(), sc};
231 }
232
233 m_toolHandles.push_back( &handle );
234
235 return sc;
236 }
237
245 template <class T>
246 StatusCode declarePrivateTool( ToolHandle<T>& handle, std::string toolTypeAndName = "", bool createIf = true )
247 {
248
249 if ( toolTypeAndName == "" ) toolTypeAndName = handle.typeAndName();
250
251 StatusCode sc = handle.initialize( toolTypeAndName, this, createIf );
252 if ( UNLIKELY( !sc ) ) {
253 throw GaudiException{"Cannot create handle for private tool " + toolTypeAndName, name(), sc};
254 }
255
256 m_toolHandles.push_back( &handle );
257
258 return sc;
259 }
260
261 const std::vector<IAlgTool*>& tools() const;
262
263 protected:
264 std::vector<IAlgTool*>& tools();
265
267 std::unique_ptr<IDataHandleVisitor> m_updateDataHandles;
268
269 private:
270 // place IAlgTools defined via ToolHandles in m_tools
271 void initToolHandles() const;
272
273 public:
274 // ==========================================================================
276 IAuditorSvc* auditorSvc() const;
277
287 inline IMonitorSvc* monitorSvc() const
288 {
289 // If not already located try to locate it without forcing a creation
290 if ( !m_pMonitorSvc ) m_pMonitorSvc = service( m_monitorSvcName, false, true );
291 return m_pMonitorSvc.get();
292 }
293
299 template <class T>
300 void declareInfo( const std::string& name, const T& var, const std::string& desc ) const
301 {
302 IMonitorSvc* mS = monitorSvc();
303 if ( mS ) mS->declareInfo( name, var, desc, this );
304 }
305
313 void declareInfo( const std::string& name, const std::string& format, const void* var, int size,
314 const std::string& desc ) const
315 {
316 IMonitorSvc* mS = monitorSvc();
317 if ( mS ) mS->declareInfo( name, format, var, size, desc, this );
318 }
319
320 // Standard destructor.
321 ~AlgTool() override;
322
323 private:
324 typedef std::list<std::pair<InterfaceID, void*>> InterfaceList;
325
326 std::string m_type;
327 const std::string m_name;
328 const IInterface* m_parent = nullptr;
329
330 mutable SmartIF<ISvcLocator> m_svcLocator;
331 mutable SmartIF<IDataProviderSvc> m_evtSvc;
332 mutable SmartIF<IToolSvc> m_ptoolSvc;
333 mutable SmartIF<IMonitorSvc> m_pMonitorSvc;
334 mutable SmartIF<IAuditorSvc> m_pAuditorSvc;
335
336 InterfaceList m_interfaceList;
337
338 // Properties
339 Gaudi::Property<int> m_outputLevel{this, "OutputLevel", MSG::NIL, "output level"};
340
341 Gaudi::Property<std::string> m_monitorSvcName{this, "MonitorService", "MonitorSvc",
342 "name to use for Monitor Service"};
343
344 Gaudi::Property<bool> m_auditInit{this, "AuditTools", false, "[[deprecated]] unused"};
345 Gaudi::Property<bool> m_auditorInitialize{this, "AuditInitialize", false, "trigger auditor on initialize()"};
346 Gaudi::Property<bool> m_auditorStart{this, "AuditStart", false, "trigger auditor on start()"};
347 Gaudi::Property<bool> m_auditorStop{this, "AuditStop", false, "trigger auditor on stop()"};
348 Gaudi::Property<bool> m_auditorFinalize{this, "AuditFinalize", false, "trigger auditor on finalize()"};
349 Gaudi::Property<bool> m_auditorReinitialize{this, "AuditReinitialize", false, "trigger auditor on reinitialize()"};
350 Gaudi::Property<bool> m_auditorRestart{this, "AuditRestart", false, "trigger auditor on restart()"};
351
352 std::string m_threadID;
353
354 // tools used by tool
355 mutable std::vector<IAlgTool*> m_tools;
356 mutable std::vector<BaseToolHandle*> m_toolHandles;
357 mutable std::vector<GaudiHandleArrayBase*> m_toolHandleArrays;
358 mutable bool m_toolHandlesInit = false;
359
361 StatusCode service_i( const std::string& algName, bool createIf, const InterfaceID& iid, void** ppSvc ) const;
362 StatusCode service_i( const std::string& svcType, const std::string& svcName, const InterfaceID& iid,
363 void** ppS ) const;
364
365 Gaudi::StateMachine::State m_state = Gaudi::StateMachine::CONFIGURED;
366 Gaudi::StateMachine::State m_targetState = Gaudi::StateMachine::CONFIGURED;
367 };
368
369 #ifndef GAUDI_NEW_PLUGIN_SERVICE
370 template <class T>
371 struct ToolFactory {
372 template <typename S, typename... Args>
373 static typename S::ReturnType create( Args&&... args )
374 {
375 return new T( std::forward<Args>( args )... );
376 }
377 };
378
379 // Macros to declare component factories
380 #define DECLARE_TOOL_FACTORY( x ) DECLARE_FACTORY_WITH_CREATOR( x, ToolFactory<x>, AlgTool::Factory )
381 #define DECLARE_NAMESPACE_TOOL_FACTORY( n, x ) DECLARE_TOOL_FACTORY( n::x )
382
383 #else
384
385 // Macros to declare component factories
386 #define DECLARE_TOOL_FACTORY( x ) DECLARE_COMPONENT( x )
387 #define DECLARE_NAMESPACE_TOOL_FACTORY( n, x ) DECLARE_COMPONENT( n::x )
388
389 #endif
390
391 #endif // GAUDIKERNEL_ALGTOOL_H
IToolSvc
The interface implemented by the IToolSvc base class.
Definition: IToolSvc.h:19
AlgTool::targetFSMState
Gaudi::StateMachine::State targetFSMState() const override
Definition: AlgTool.h:78
ToolFactory
Definition: AlgTool.h:371
PropertyHolder.h
IDataHandleHolder.h
StatusCode::SUCCESS
Definition: StatusCode.h:30
AlgTool::m_pMonitorSvc
SmartIF< IMonitorSvc > m_pMonitorSvc
Online Monitoring Service.
Definition: AlgTool.h:333
SmartIF< ISvcLocator >
GaudiException
Define general base for Gaudi exception.
Definition: GaudiException.h:23
ISvcLocator
The ISvcLocator is the interface implemented by the Service Factory in the Application Manager to loc...
Definition: ISvcLocator.h:25
format
GAUDI_API std::string format(const char *,...)
MsgStream format utility "a la sprintf(...)".
Definition: MsgStream.cpp:119
AlgTool::m_type
std::string m_type
AlgTool type (concrete class name)
Definition: AlgTool.h:326
Gaudi::StateMachine::CONFIGURED
Definition: StateMachine.h:14
gaudiComponentHelp.name
name
Definition: gaudiComponentHelp.py:28
Gaudi::Property
Implementation of property with value of concrete type.
Definition: Property.h:314
AlgTool::declarePrivateTool
StatusCode declarePrivateTool(ToolHandle< T > &handle, std::string toolTypeAndName="", bool createIf=true)
Declare used private tool.
Definition: AlgTool.h:246
gaudirun.args
args
Definition: gaudirun.py:236
ToolHandleInfo::isPublic
bool isPublic() const noexcept
Definition: ToolHandle.h:38
AlgTool::deregisterTool
void deregisterTool(IAlgTool *tool) const
Definition: AlgTool.h:204
IAuditorSvc.h
AlgTool::InterfaceList
std::list< std::pair< InterfaceID, void * > > InterfaceList
Definition: AlgTool.h:324
MSG::DEBUG
Definition: IMessageSvc.h:18
UNLIKELY
#define UNLIKELY(x)
Definition: Kernel.h:126
ToolHandleArray
Array of Handles to be used in lieu of vector of naked pointers to tools.
Definition: PropertyHolder.h:34
AlgTool::declareProperty
Gaudi::Details::PropertyBase * declareProperty(const std::string &name, ToolHandle< T > &hndl, const std::string &doc="none")
Definition: AlgTool.h:164
Gaudi::StateMachine::State
State
Allowed states for classes implementing the state machine (ApplicationMgr, Algorithm, Service, AlgTool).
Definition: StateMachine.h:12
AlgTool::registerTool
void registerTool(IAlgTool *tool) const
Definition: AlgTool.h:198
IDataProviderSvc
Data provider interface definition.
Definition: IDataProviderSvc.h:45
AlgTool::m_tools
std::vector< IAlgTool * > m_tools
Definition: AlgTool.h:355
IToolSvc.h
std::string
STL class.
AlgTool::m_name
const std::string m_name
AlgTool full name.
Definition: AlgTool.h:327
DataObjectHandle
DataObjectHandle.h GaudiKernel/DataObjectHandle.h.
Definition: AlgTool.h:27
AlgTool::FSMState
Gaudi::StateMachine::State FSMState() const override
Definition: AlgTool.h:77
InterfaceID
Interface ID class.
Definition: IInterface.h:30
AlgTool::monitorSvc
IMonitorSvc * monitorSvc() const
Access the monitor service.
Definition: AlgTool.h:287
extends< BASE, IDataHandleHolder >::queryInterface
StatusCode queryInterface(const InterfaceID &ti, void **pp) override
Implementation of IInterface::queryInterface.
Definition: extends.h:28
IDataProviderSvc.h
ToolFactory::create
static S::ReturnType create(Args &&...args)
Definition: AlgTool.h:373
AlgTool::svcLoc
ISvcLocator * svcLoc() const
shortcut for the method service locator
Definition: AlgTool.h:110
IMonitorSvc
Definition of the IMonitorSvc interface, which publishes Gaudi variables to outside monitoring proces...
Definition: IMonitorSvc.h:21
AlgTool::declareInfo
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:313
StatusCode
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:26
declareProperty
Gaudi::Details::PropertyBase * declareProperty(const std::string &name, TYPE &value, const std::string &doc="none")
Declare a property (templated)
ToolHandle::typeAndName
std::string typeAndName() const override
Definition: ToolHandle.h:227
IInterface
Definition of the basic interface.
Definition: IInterface.h:234
gaudirun.type
type
Definition: gaudirun.py:125
DataHandleHolderBase
Definition: DataHandleHolderBase.h:25
CommonMessaging.h
AlgTool::configure
StatusCode configure() override
Definition: AlgTool.h:69
AlgTool::m_threadID
std::string m_threadID
Thread Id for Alg Tool.
Definition: AlgTool.h:352
IAlgTool.h
AlgTool::service
StatusCode service(const std::string &name, T *&svc, bool createIf=true) const
Access a service by name, creating it if it doesn&#39;t already exist.
Definition: AlgTool.h:131
MSG::NIL
Definition: IMessageSvc.h:16
IOTest.start
start
Definition: IOTest.py:88
IMessageSvc.h
Gaudi::Details::PropertyBase
PropertyBase base class allowing PropertyBase* collections to be "homogeneous".
Definition: Property.h:32
AlgTool::m_ptoolSvc
SmartIF< IToolSvc > m_ptoolSvc
Tool service.
Definition: AlgTool.h:332
IStateful.h
std::list
STL class.
DataObjIDProperty.h
IService.h
DataHandleHolderBase.h
AlgTool::m_pAuditorSvc
SmartIF< IAuditorSvc > m_pAuditorSvc
Auditor Service.
Definition: AlgTool.h:334
AlgTool::declareInfo
void declareInfo(const std::string &name, const T &var, const std::string &desc) const
Declare monitoring information.
Definition: AlgTool.h:300
IMonitorSvc.h
PluginService.h
IDataHandleVisitor
Definition: IDataHandleHolder.h:38
ToolHandle
Handle to be used in lieu of naked pointers to tools.
Definition: PropertyHolder.h:30
ToolHandle::initialize
StatusCode initialize(const std::string &toolTypeAndName, const IInterface *parent=nullptr, bool createIf=true)
Definition: ToolHandle.h:188
std::find
T find(T...args)
IProperty.h
std::vector< IAlgTool * >
AlgTool::declareTool
StatusCode declareTool(ToolHandle< T > &handle, std::string toolTypeAndName="", bool createIf=true)
Definition: AlgTool.h:172
IMonitorSvc::declareInfo
virtual void declareInfo(const std::string &name, const bool &var, const std::string &desc, const IInterface *owner)=0
Declare monitoring information.
std::unique_ptr< IDataHandleVisitor >
AlgTool::m_toolHandles
std::vector< BaseToolHandle * > m_toolHandles
Definition: AlgTool.h:356
Gaudi::PluginService::Factory
Class wrapping the signature for a factory with any number of arguments.
Definition: PluginService.h:47
AlgTool::service
StatusCode service(const std::string &type, const std::string &name, T *&svc) const
Access a service by name, type creating it if it doesn&#39;t already exist.
Definition: AlgTool.h:139
AlgTool::declareInterface
void declareInterface(I *i)
Definition: AlgTool.h:155
AlgTool::declarePublicTool
StatusCode declarePublicTool(ToolHandle< T > &handle, std::string toolTypeAndName="", bool createIf=true)
Declare used public tool.
Definition: AlgTool.h:223
AlgTool::service
SmartIF< T > service(const std::string &name, const bool createIf=true, const bool quiet=false) const
Definition: AlgTool.h:148
AlgTool
Base class from which all the concrete tool classes should be derived.
Definition: AlgTool.h:48
IAlgTool
The interface implemented by the AlgTool base class.
Definition: IAlgTool.h:23
ToolHandle.h
AlgTool::m_svcLocator
SmartIF< ISvcLocator > m_svcLocator
Pointer to Service Locator service.
Definition: AlgTool.h:330
AlgTool::m_evtSvc
SmartIF< IDataProviderSvc > m_evtSvc
Event data service.
Definition: AlgTool.h:331
AlgTool::m_interfaceList
InterfaceList m_interfaceList
Interface list.
Definition: AlgTool.h:336
AlgTool::terminate
StatusCode terminate() override
Definition: AlgTool.h:74
IAuditorSvc
The interface implemented by the IAuditorSvc base class.
Definition: IAuditorSvc.h:15
DataHandle.h
GAUDI_API
#define GAUDI_API
Definition: Kernel.h:107
ISvcLocator.h
endmsg
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:244
INamedInterface::name
virtual const std::string & name() const =0
Retrieve the name of the instance.
ToolHandleInfo
General info and helper functions for toolhandles and arrays.
Definition: ToolHandle.h:25
AlgTool::Factory
Gaudi::PluginService::Factory< IAlgTool *, const std::string &, const std::string &, const IInterface * > Factory
Definition: AlgTool.h:53
AlgTool::m_toolHandleArrays
std::vector< GaudiHandleArrayBase * > m_toolHandleArrays
Definition: AlgTool.h:357
AlgTool::m_updateDataHandles
std::unique_ptr< IDataHandleVisitor > m_updateDataHandles
Hook for for derived classes to provide a custom visitor for data handles.
Definition: AlgTool.h:267
AlgTool::declareProperty
Gaudi::Details::PropertyBase * declareProperty(const std::string &name, ToolHandleArray< T > &hndlArr, const std::string &doc="none")
Definition: AlgTool.h:184