All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
AlgTool.h
Go to the documentation of this file.
1 #ifndef GAUDIKERNEL_ALGTOOL_H
2 #define GAUDIKERNEL_ALGTOOL_H
3 // ============================================================================
4 // Include files
6 #include "GaudiKernel/IAlgTool.h"
11 #include "GaudiKernel/IProperty.h"
12 #include "GaudiKernel/IService.h"
13 #include "GaudiKernel/IStateful.h"
15 #include "GaudiKernel/IToolSvc.h"
17 #include "GaudiKernel/ToolHandle.h"
18 #include <Gaudi/PluginService.h>
19 
20 #include "GaudiKernel/DataHandle.h"
23 
24 template <class T>
26 
27 class ToolHandleInfo;
28 
29 #include <list>
30 #include <vector>
31 
32 // Forward declarations
33 
47  : public PropertyHolder<CommonMessaging<implements<IAlgTool, IDataHandleHolder, IProperty, IStateful>>>
48 {
49 public:
50 #ifndef __REFLEX__
52 #endif
53 
55  StatusCode queryInterface( const InterfaceID& riid, void** ppvUnknown ) override;
56 
58  const std::string& name() const override;
59 
61  const std::string& type() const override;
62 
64  const IInterface* parent() const override;
65 
66  // State machine implementation
67  StatusCode configure() override { return StatusCode::SUCCESS; }
68  StatusCode initialize() override;
69  StatusCode start() override;
70  StatusCode stop() override;
71  StatusCode finalize() override;
72  StatusCode terminate() override { return StatusCode::SUCCESS; }
73  StatusCode reinitialize() override;
74  StatusCode restart() override;
75  Gaudi::StateMachine::State FSMState() const override { return m_state; }
76  Gaudi::StateMachine::State targetFSMState() const override { return m_targetState; }
77 
79  StatusCode sysInitialize() override;
80 
82  StatusCode sysStart() override;
83 
85  StatusCode sysStop() override;
86 
88  StatusCode sysFinalize() override;
89 
91  StatusCode sysReinitialize() override;
92 
94  StatusCode sysRestart() override;
95 
96 public:
102  AlgTool( const std::string& type, const std::string& name, const IInterface* parent );
103 
105  SmartIF<ISvcLocator>& serviceLocator() const override;
106 
108  ISvcLocator* svcLoc() const { return serviceLocator(); }
109 
113  IDataProviderSvc* evtSvc() const;
114 
116  IToolSvc* toolSvc() const;
117 
123  StatusCode setProperties();
124 
128  template <class T>
129  StatusCode service( const std::string& name, T*& svc, bool createIf = true ) const
130  {
131  return service_i( name, createIf, T::interfaceID(), (void**)&svc );
132  }
133 
136  template <class T>
137  StatusCode service( const std::string& type, const std::string& name, T*& svc ) const
138  {
139  return service_i( type, name, T::interfaceID(), (void**)&svc );
140  }
141 
143  SmartIF<IService> service( const std::string& name, const bool createIf = true, const bool quiet = false ) const;
144 
145  template <typename T>
146  SmartIF<T> service( const std::string& name, const bool createIf = true, const bool quiet = false ) const
147  {
148  return SmartIF<T>( service( name, createIf, quiet ) );
149  }
150 
151 protected:
152  template <typename I>
153  void declareInterface( I* i )
154  {
155  m_interfaceList.emplace_back( I::interfaceID(), i );
156  }
157 
158 public:
160 
161  template <class T>
162 
164  const std::string& doc = "none" )
165  {
166  this->declareTool( hndl ).ignore();
167  return PropertyHolderImpl::declareProperty( name, hndl, doc );
168  }
169 
170  template <class T>
171  StatusCode declareTool( ToolHandle<T>& handle, std::string toolTypeAndName = "", bool createIf = true )
172  {
173  if ( handle.isPublic() ) {
174  return declarePublicTool( handle, toolTypeAndName, createIf );
175  } else {
176  return declarePrivateTool( handle, toolTypeAndName, createIf );
177  }
178  }
179 
180  // ==========================================================================
181  // declare ToolHandleArrays to the AlgTool
182  template <class T>
184  const std::string& doc = "none" )
185  {
186  m_toolHandleArrays.push_back( &hndlArr );
187  return PropertyHolderImpl::declareProperty( name, hndlArr, doc );
188  }
189 
190 protected:
191  virtual void declareInput( Gaudi::DataHandle* im ) override { m_inputHandles.push_back( im ); }
192 
193  virtual void declareOutput( Gaudi::DataHandle* im ) override { m_outputHandles.push_back( im ); }
194 public:
195  virtual std::vector<Gaudi::DataHandle*> inputHandles() const override { return m_inputHandles; }
196  virtual std::vector<Gaudi::DataHandle*> outputHandles() const override { return m_outputHandles; }
197 
198  virtual const DataObjIDColl& extraInputDeps() const override { return m_extInputDataObjs; }
199  virtual const DataObjIDColl& extraOutputDeps() const override { return m_extOutputDataObjs; }
200 
201  virtual void acceptDHVisitor( IDataHandleVisitor* ) const override;
202 
203  const DataObjIDColl& inputDataObjs() const override { return m_inputDataObjs; }
204  const DataObjIDColl& outputDataObjs() const override { return m_outputDataObjs; }
205 
206  void commitHandles() override;
207 
208 private:
210  DataObjIDColl m_inputDataObjs, m_outputDataObjs;
211 
212 public:
213  void registerTool( IAlgTool* tool ) const
214  {
215  if ( UNLIKELY( msgLevel( MSG::DEBUG ) ) ) debug() << "Registering tool " << tool->name() << endmsg;
216  m_tools.push_back( tool );
217  }
218 
219  void deregisterTool( IAlgTool* tool ) const
220  {
221  auto it = std::find( m_tools.begin(), m_tools.end(), tool );
222  if ( it != m_tools.end() ) {
223  if ( UNLIKELY( msgLevel( MSG::DEBUG ) ) ) debug() << "De-Registering tool " << tool->name() << endmsg;
224  m_tools.erase( it );
225  } else {
226  if ( UNLIKELY( msgLevel( MSG::DEBUG ) ) ) debug() << "Could not de-register tool " << tool->name() << endmsg;
227  }
228  }
229 
237  template <class T>
238  StatusCode declarePublicTool( ToolHandle<T>& handle, std::string toolTypeAndName = "", bool createIf = true )
239  {
240 
241  if ( toolTypeAndName == "" ) toolTypeAndName = handle.typeAndName();
242 
243  StatusCode sc = handle.initialize( toolTypeAndName, 0, createIf );
244  if ( UNLIKELY( !sc ) ) {
245  throw GaudiException{"Cannot create handle for public tool " + toolTypeAndName, name(), sc};
246  }
247 
248  m_toolHandles.push_back( &handle );
249 
250  return sc;
251  }
252 
260  template <class T>
261  StatusCode declarePrivateTool( ToolHandle<T>& handle, std::string toolTypeAndName = "", bool createIf = true )
262  {
263 
264  if ( toolTypeAndName == "" ) toolTypeAndName = handle.typeAndName();
265 
266  StatusCode sc = handle.initialize( toolTypeAndName, this, createIf );
267  if ( UNLIKELY( !sc ) ) {
268  throw GaudiException{"Cannot create handle for private tool " + toolTypeAndName, name(), sc};
269  }
270 
271  m_toolHandles.push_back( &handle );
272 
273  return sc;
274  }
275 
276  const std::vector<IAlgTool*>& tools() const;
277 
278 protected:
279  std::vector<IAlgTool*>& tools();
280 
283 
284 private:
285  // place IAlgTools defined via ToolHandles in m_tools
286  void initToolHandles() const;
287 
288 public:
289  // ==========================================================================
291  IAuditorSvc* auditorSvc() const;
292 
302  inline IMonitorSvc* monitorSvc() const
303  {
304  // If not already located try to locate it without forcing a creation
305  if ( !m_pMonitorSvc ) m_pMonitorSvc = service( m_monitorSvcName, false, true );
306  return m_pMonitorSvc.get();
307  }
308 
314  template <class T>
315  void declareInfo( const std::string& name, const T& var, const std::string& desc ) const
316  {
317  IMonitorSvc* mS = monitorSvc();
318  if ( mS ) mS->declareInfo( name, var, desc, this );
319  }
320 
328  void declareInfo( const std::string& name, const std::string& format, const void* var, int size,
329  const std::string& desc ) const
330  {
331  IMonitorSvc* mS = monitorSvc();
332  if ( mS ) mS->declareInfo( name, format, var, size, desc, this );
333  }
334 
335  // Standard destructor.
336  ~AlgTool() override;
337 
338 private:
340 
343  const IInterface* m_parent = nullptr;
344 
350 
351  InterfaceList m_interfaceList;
352 
353  // Properties
354  Gaudi::Property<int> m_outputLevel{this, "OutputLevel", MSG::NIL, "output level"};
355 
356  Gaudi::Property<std::string> m_monitorSvcName{this, "MonitorService", "MonitorSvc",
357  "name to use for Monitor Service"};
358 
359  Gaudi::Property<bool> m_auditInit{this, "AuditTools", false, "[[deprecated]] unused"};
360  Gaudi::Property<bool> m_auditorInitialize{this, "AuditInitialize", false, "trigger auditor on initialize()"};
361  Gaudi::Property<bool> m_auditorStart{this, "AuditStart", false, "trigger auditor on start()"};
362  Gaudi::Property<bool> m_auditorStop{this, "AuditStop", false, "trigger auditor on stop()"};
363  Gaudi::Property<bool> m_auditorFinalize{this, "AuditFinalize", false, "trigger auditor on finalize()"};
364  Gaudi::Property<bool> m_auditorReinitialize{this, "AuditReinitialize", false, "trigger auditor on reinitialize()"};
365  Gaudi::Property<bool> m_auditorRestart{this, "AuditRestart", false, "trigger auditor on restart()"};
366 
367  Gaudi::Property<DataObjIDColl> m_extInputDataObjs{this, "ExtraInputs", DataObjIDColl{}, "[[deprecated]]"};
368  Gaudi::Property<DataObjIDColl> m_extOutputDataObjs{this, "ExtraOutputs", DataObjIDColl{}, "[[deprecated]]"};
369 
371 
372  // tools used by tool
376  mutable bool m_toolHandlesInit = false;
377 
379  StatusCode service_i( const std::string& algName, bool createIf, const InterfaceID& iid, void** ppSvc ) const;
380  StatusCode service_i( const std::string& svcType, const std::string& svcName, const InterfaceID& iid,
381  void** ppS ) const;
382 
385 };
386 
387 #ifndef GAUDI_NEW_PLUGIN_SERVICE
388 template <class T>
389 struct ToolFactory {
390  template <typename S, typename... Args>
391  static typename S::ReturnType create( Args&&... args )
392  {
393  return new T( std::forward<Args>( args )... );
394  }
395 };
396 
397 // Macros to declare component factories
398 #define DECLARE_TOOL_FACTORY( x ) DECLARE_FACTORY_WITH_CREATOR( x, ToolFactory<x>, AlgTool::Factory )
399 #define DECLARE_NAMESPACE_TOOL_FACTORY( n, x ) DECLARE_TOOL_FACTORY( n::x )
400 
401 #else
402 
403 // Macros to declare component factories
404 #define DECLARE_TOOL_FACTORY( x ) DECLARE_COMPONENT( x )
405 #define DECLARE_NAMESPACE_TOOL_FACTORY( n, x ) DECLARE_COMPONENT( n::x )
406 
407 #endif
408 
409 #endif // GAUDIKERNEL_ALGTOOL_H
The interface implemented by the IToolSvc base class.
Definition: IToolSvc.h:18
Gaudi::StateMachine::State targetFSMState() const override
Definition: AlgTool.h:76
virtual std::vector< Gaudi::DataHandle * > inputHandles() const override
Definition: AlgTool.h:195
virtual const DataObjIDColl & extraOutputDeps() const override
Definition: AlgTool.h:199
SmartIF< IMonitorSvc > m_pMonitorSvc
Online Monitoring Service.
Definition: AlgTool.h:348
Define general base for Gaudi exception.
The ISvcLocator is the interface implemented by the Service Factory in the Application Manager to loc...
Definition: ISvcLocator.h:25
GAUDI_API std::string format(const char *,...)
MsgStream format utility "a la sprintf(...)".
Definition: MsgStream.cpp:119
std::string m_type
AlgTool type (concrete class name)
Definition: AlgTool.h:341
const DataObjIDColl & outputDataObjs() const override
Definition: AlgTool.h:204
Implementation of property with value of concrete type.
Definition: Property.h:313
StatusCode declarePrivateTool(ToolHandle< T > &handle, std::string toolTypeAndName="", bool createIf=true)
Declare used private tool.
Definition: AlgTool.h:261
bool isPublic() const noexcept
Definition: ToolHandle.h:38
void deregisterTool(IAlgTool *tool) const
Definition: AlgTool.h:219
std::list< std::pair< InterfaceID, void * > > InterfaceList
Definition: AlgTool.h:339
virtual std::vector< Gaudi::DataHandle * > outputHandles() const override
Definition: AlgTool.h:196
#define UNLIKELY(x)
Definition: Kernel.h:126
Gaudi::Details::PropertyBase & declareProperty(Gaudi::Details::PropertyBase &prop)
Declare a property.
Array of Handles to be used in lieu of vector of naked pointers to tools.
Gaudi::Details::PropertyBase * declareProperty(const std::string &name, ToolHandle< T > &hndl, const std::string &doc="none")
Definition: AlgTool.h:163
State
Allowed states for classes implementing the state machine (ApplicationMgr, Algorithm, Service, AlgTool).
Definition: StateMachine.h:12
void registerTool(IAlgTool *tool) const
Definition: AlgTool.h:213
Data provider interface definition.
std::vector< IAlgTool * > m_tools
Definition: AlgTool.h:373
STL class.
const std::string m_name
AlgTool full name.
Definition: AlgTool.h:342
DataObjectHandle.h GaudiKernel/DataObjectHandle.h.
Definition: AlgTool.h:25
Gaudi::StateMachine::State FSMState() const override
Definition: AlgTool.h:75
Interface ID class.
Definition: IInterface.h:30
IMonitorSvc * monitorSvc() const
Access the monitor service.
Definition: AlgTool.h:302
static S::ReturnType create(Args &&...args)
Definition: AlgTool.h:391
ISvcLocator * svcLoc() const
shortcut for the method service locator
Definition: AlgTool.h:108
Definition of the IMonitorSvc interface, which publishes Gaudi variables to outside monitoring proces...
Definition: IMonitorSvc.h:21
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:328
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:26
const DataObjIDColl & inputDataObjs() const override
Definition: AlgTool.h:203
std::string typeAndName() const override
Definition: ToolHandle.h:227
Definition of the basic interface.
Definition: IInterface.h:234
StatusCode configure() override
Definition: AlgTool.h:67
std::string m_threadID
Thread Id for Alg Tool.
Definition: AlgTool.h:370
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:129
start
Definition: IOTest.py:88
PropertyBase base class allowing PropertyBase* collections to be "homogeneous".
Definition: Property.h:32
SmartIF< IToolSvc > m_ptoolSvc
Tool service.
Definition: AlgTool.h:347
STL class.
virtual void declareInput(Gaudi::DataHandle *im) override
Definition: AlgTool.h:191
SmartIF< IAuditorSvc > m_pAuditorSvc
Auditor Service.
Definition: AlgTool.h:349
void declareInfo(const std::string &name, const T &var, const std::string &desc) const
Declare monitoring information.
Definition: AlgTool.h:315
Handle to be used in lieu of naked pointers to tools.
StatusCode initialize(const std::string &toolTypeAndName, const IInterface *parent=nullptr, bool createIf=true)
Definition: ToolHandle.h:188
T find(T...args)
StatusCode declareTool(ToolHandle< T > &handle, std::string toolTypeAndName="", bool createIf=true)
Definition: AlgTool.h:171
virtual void declareInfo(const std::string &name, const bool &var, const std::string &desc, const IInterface *owner)=0
Declare monitoring information.
std::vector< BaseToolHandle * > m_toolHandles
Definition: AlgTool.h:374
std::vector< Gaudi::DataHandle * > m_outputHandles
Definition: AlgTool.h:209
DataObjIDColl m_outputDataObjs
Definition: AlgTool.h:210
Class wrapping the signature for a factory with any number of arguments.
Definition: PluginService.h:47
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:137
void declareInterface(I *i)
Definition: AlgTool.h:153
StatusCode declarePublicTool(ToolHandle< T > &handle, std::string toolTypeAndName="", bool createIf=true)
Declare used public tool.
Definition: AlgTool.h:238
SmartIF< T > service(const std::string &name, const bool createIf=true, const bool quiet=false) const
Definition: AlgTool.h:146
Base class from which all the concrete tool classes should be derived.
Definition: AlgTool.h:46
The interface implemented by the AlgTool base class.
Definition: IAlgTool.h:23
SmartIF< ISvcLocator > m_svcLocator
Pointer to Service Locator service.
Definition: AlgTool.h:345
Helper class to implement the IProperty interface.
SmartIF< IDataProviderSvc > m_evtSvc
Event data service.
Definition: AlgTool.h:346
InterfaceList m_interfaceList
Interface list.
Definition: AlgTool.h:351
StatusCode queryInterface(const InterfaceID &iid, void **pinterface) override
StatusCode terminate() override
Definition: AlgTool.h:72
The interface implemented by the IAuditorSvc base class.
Definition: IAuditorSvc.h:15
#define GAUDI_API
Definition: Kernel.h:107
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:244
virtual const std::string & name() const =0
Retrieve the name of the instance.
virtual void declareOutput(Gaudi::DataHandle *im) override
Definition: AlgTool.h:193
General info and helper functions for toolhandles and arrays.
Definition: ToolHandle.h:25
Gaudi::PluginService::Factory< IAlgTool *, const std::string &, const std::string &, const IInterface * > Factory
Definition: AlgTool.h:51
std::vector< GaudiHandleArrayBase * > m_toolHandleArrays
Definition: AlgTool.h:375
std::unique_ptr< IDataHandleVisitor > m_updateDataHandles
Hook for for derived classes to provide a custom visitor for data handles.
Definition: AlgTool.h:282
virtual const DataObjIDColl & extraInputDeps() const override
Definition: AlgTool.h:198
Gaudi::Details::PropertyBase * declareProperty(const std::string &name, ToolHandleArray< T > &hndlArr, const std::string &doc="none")
Definition: AlgTool.h:183