The Gaudi Framework  v29r0 (ff2e7097)
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 
21 
22 #include "GaudiKernel/DataHandle.h"
25 
26 template <class T>
28 
29 class ToolHandleInfo;
30 
31 #include <list>
32 #include <vector>
33 
34 // Forward declarations
35 
49  : public DataHandleHolderBase<
50  PropertyHolder<CommonMessaging<implements<IAlgTool, IDataHandleHolder, IProperty, IStateful>>>>
51 {
52 public:
53 #ifndef __REFLEX__
55 #endif
56 
58  StatusCode queryInterface( const InterfaceID& riid, void** ppvUnknown ) override;
59 
61  const std::string& name() const override;
62 
64  const std::string& type() const override;
65 
67  const IInterface* parent() const override;
68 
69  // State machine implementation
70  StatusCode configure() override { return StatusCode::SUCCESS; }
71  StatusCode initialize() override;
72  StatusCode start() override;
73  StatusCode stop() override;
74  StatusCode finalize() override;
75  StatusCode terminate() override { return StatusCode::SUCCESS; }
76  StatusCode reinitialize() override;
77  StatusCode restart() override;
78  Gaudi::StateMachine::State FSMState() const override { return m_state; }
79  Gaudi::StateMachine::State targetFSMState() const override { return m_targetState; }
80 
82  StatusCode sysInitialize() override;
83 
85  StatusCode sysStart() override;
86 
88  StatusCode sysStop() override;
89 
91  StatusCode sysFinalize() override;
92 
94  StatusCode sysReinitialize() override;
95 
97  StatusCode sysRestart() override;
98 
99 public:
105  AlgTool( const std::string& type, const std::string& name, const IInterface* parent );
106 
108  SmartIF<ISvcLocator>& serviceLocator() const override;
109 
111  ISvcLocator* svcLoc() const { return serviceLocator(); }
112 
116  IDataProviderSvc* evtSvc() const;
117 
119  IToolSvc* toolSvc() const;
120 
126  StatusCode setProperties();
127 
131  template <class T>
132  StatusCode service( const std::string& name, T*& svc, bool createIf = true ) const
133  {
134  return service_i( name, createIf, T::interfaceID(), (void**)&svc );
135  }
136 
139  template <class T>
140  StatusCode service( const std::string& type, const std::string& name, T*& svc ) const
141  {
142  return service_i( type, name, T::interfaceID(), (void**)&svc );
143  }
144 
146  SmartIF<IService> service( const std::string& name, const bool createIf = true, const bool quiet = false ) const;
147 
148  template <typename T>
149  SmartIF<T> service( const std::string& name, const bool createIf = true, const bool quiet = false ) const
150  {
151  return SmartIF<T>( service( name, createIf, quiet ) );
152  }
153 
154 protected:
155  template <typename I>
156  void declareInterface( I* i )
157  {
158  m_interfaceList.emplace_back( I::interfaceID(), i );
159  }
160 
161 public:
163 
164  template <class T>
166  const std::string& doc = "none" )
167  {
168  this->declareTool( hndl, hndl.typeAndName() ).ignore();
169  return PropertyHolderImpl::declareProperty( name, hndl, doc );
170  }
171 
172  template <class T>
173  StatusCode declareTool( ToolHandle<T>& handle, std::string toolTypeAndName, bool createIf = true )
174  {
175 
176  StatusCode sc = handle.initialize( toolTypeAndName, handle.isPublic() ? nullptr : this, createIf );
177  if ( UNLIKELY( !sc ) ) {
178  throw GaudiException{std::string{"Cannot create handle for "} + ( handle.isPublic() ? "public" : "private" ) +
179  " tool " + toolTypeAndName,
180  name(), sc};
181  }
182 
183  m_toolHandles.push_back( &handle );
184 
185  return sc;
186  }
187 
188  // ==========================================================================
189  // declare ToolHandleArrays to the AlgTool
190  template <class T>
192  const std::string& doc = "none" )
193  {
194  addToolsArray( hndlArr );
195  return PropertyHolderImpl::declareProperty( name, hndlArr, doc );
196  }
197 
198  template <class T>
200  {
201  m_toolHandleArrays.push_back( &hndlArr );
202  }
203 
204 public:
205  virtual void acceptDHVisitor( IDataHandleVisitor* ) const override;
206 
207  void commitHandles() override;
208 
209 public:
210  void registerTool( IAlgTool* tool ) const
211  {
212  if ( UNLIKELY( msgLevel( MSG::DEBUG ) ) ) debug() << "Registering tool " << tool->name() << endmsg;
213  m_tools.push_back( tool );
214  }
215 
216  void deregisterTool( IAlgTool* tool ) const
217  {
218  auto it = std::find( m_tools.begin(), m_tools.end(), tool );
219  if ( it != m_tools.end() ) {
220  if ( UNLIKELY( msgLevel( MSG::DEBUG ) ) ) debug() << "De-Registering tool " << tool->name() << endmsg;
221  m_tools.erase( it );
222  } else {
223  if ( UNLIKELY( msgLevel( MSG::DEBUG ) ) ) debug() << "Could not de-register tool " << tool->name() << endmsg;
224  }
225  }
226 
227  const std::vector<IAlgTool*>& tools() const;
228 
229 protected:
230  std::vector<IAlgTool*>& tools();
231 
234 
235 private:
236  // place IAlgTools defined via ToolHandles in m_tools
237  void initToolHandles() const;
238 
239 public:
240  // ==========================================================================
242  IAuditorSvc* auditorSvc() const;
243 
253  inline IMonitorSvc* monitorSvc() const
254  {
255  // If not already located try to locate it without forcing a creation
256  if ( !m_pMonitorSvc ) m_pMonitorSvc = service( m_monitorSvcName, false, true );
257  return m_pMonitorSvc.get();
258  }
259 
265  template <class T>
266  void declareInfo( const std::string& name, const T& var, const std::string& desc ) const
267  {
268  IMonitorSvc* mS = monitorSvc();
269  if ( mS ) mS->declareInfo( name, var, desc, this );
270  }
271 
279  void declareInfo( const std::string& name, const std::string& format, const void* var, int size,
280  const std::string& desc ) const
281  {
282  IMonitorSvc* mS = monitorSvc();
283  if ( mS ) mS->declareInfo( name, format, var, size, desc, this );
284  }
285 
286  // Standard destructor.
287  ~AlgTool() override;
288 
289 private:
291 
294  const IInterface* m_parent = nullptr;
295 
301 
302  InterfaceList m_interfaceList;
303 
304  // Properties
305  Gaudi::Property<int> m_outputLevel{this, "OutputLevel", MSG::NIL, "output level"};
306 
307  Gaudi::Property<std::string> m_monitorSvcName{this, "MonitorService", "MonitorSvc",
308  "name to use for Monitor Service"};
309 
310  Gaudi::Property<bool> m_auditInit{this, "AuditTools", false, "[[deprecated]] unused"};
311  Gaudi::Property<bool> m_auditorInitialize{this, "AuditInitialize", false, "trigger auditor on initialize()"};
312  Gaudi::Property<bool> m_auditorStart{this, "AuditStart", false, "trigger auditor on start()"};
313  Gaudi::Property<bool> m_auditorStop{this, "AuditStop", false, "trigger auditor on stop()"};
314  Gaudi::Property<bool> m_auditorFinalize{this, "AuditFinalize", false, "trigger auditor on finalize()"};
315  Gaudi::Property<bool> m_auditorReinitialize{this, "AuditReinitialize", false, "trigger auditor on reinitialize()"};
316  Gaudi::Property<bool> m_auditorRestart{this, "AuditRestart", false, "trigger auditor on restart()"};
317 
319 
320  // tools used by tool
324  mutable bool m_toolHandlesInit = false;
325 
327  StatusCode service_i( const std::string& algName, bool createIf, const InterfaceID& iid, void** ppSvc ) const;
328  StatusCode service_i( const std::string& svcType, const std::string& svcName, const InterfaceID& iid,
329  void** ppS ) const;
330 
333 };
334 
335 #ifndef GAUDI_NEW_PLUGIN_SERVICE
336 template <class T>
337 struct ToolFactory {
338  template <typename S, typename... Args>
339  static typename S::ReturnType create( Args&&... args )
340  {
341  return new T( std::forward<Args>( args )... );
342  }
343 };
344 
345 // Macros to declare component factories
346 #define DECLARE_TOOL_FACTORY( x ) DECLARE_FACTORY_WITH_CREATOR( x, ToolFactory<x>, AlgTool::Factory )
347 #define DECLARE_NAMESPACE_TOOL_FACTORY( n, x ) DECLARE_TOOL_FACTORY( n::x )
348 
349 #else
350 
351 // Macros to declare component factories
352 #define DECLARE_TOOL_FACTORY( x ) DECLARE_COMPONENT( x )
353 #define DECLARE_NAMESPACE_TOOL_FACTORY( n, x ) DECLARE_COMPONENT( n::x )
354 
355 #endif
356 
357 #endif // GAUDIKERNEL_ALGTOOL_H
The interface implemented by the IToolSvc base class.
Definition: IToolSvc.h:19
Gaudi::StateMachine::State targetFSMState() const override
Definition: AlgTool.h:79
#define UNLIKELY(x)
Definition: Kernel.h:128
SmartIF< IMonitorSvc > m_pMonitorSvc
Online Monitoring Service.
Definition: AlgTool.h:299
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:120
std::string m_type
AlgTool type (concrete class name)
Definition: AlgTool.h:292
Implementation of property with value of concrete type.
Definition: Property.h:319
void addToolsArray(ToolHandleArray< T > &hndlArr)
Definition: AlgTool.h:199
bool isPublic() const noexcept
Definition: ToolHandle.h:37
void deregisterTool(IAlgTool *tool) const
Definition: AlgTool.h:216
std::list< std::pair< InterfaceID, void * > > InterfaceList
Definition: AlgTool.h:290
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:165
State
Allowed states for classes implementing the state machine (ApplicationMgr, Algorithm, Service, AlgTool).
Definition: StateMachine.h:14
void registerTool(IAlgTool *tool) const
Definition: AlgTool.h:210
Data provider interface definition.
std::vector< IAlgTool * > m_tools
Definition: AlgTool.h:321
STL class.
const std::string m_name
AlgTool full name.
Definition: AlgTool.h:293
DataObjectHandle.h GaudiKernel/DataObjectHandle.h.
Definition: AlgTool.h:27
Gaudi::StateMachine::State FSMState() const override
Definition: AlgTool.h:78
Interface ID class.
Definition: IInterface.h:29
IMonitorSvc * monitorSvc() const
Access the monitor service.
Definition: AlgTool.h:253
StatusCode queryInterface(const InterfaceID &ti, void **pp) override
Implementation of IInterface::queryInterface.
Definition: extends.h:30
static S::ReturnType create(Args &&...args)
Definition: AlgTool.h:339
ISvcLocator * svcLoc() const
shortcut for the method service locator
Definition: AlgTool.h:111
Definition of the IMonitorSvc interface, which publishes Gaudi variables to outside monitoring proces...
Definition: IMonitorSvc.h:24
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:279
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:28
Gaudi::Details::PropertyBase * declareProperty(const std::string &name, TYPE &value, const std::string &doc="none")
Declare a property (templated)
std::string typeAndName() const override
Definition: ToolHandle.h:223
Definition of the basic interface.
Definition: IInterface.h:277
StatusCode configure() override
Definition: AlgTool.h:70
std::string m_threadID
Thread Id for Alg Tool.
Definition: AlgTool.h:318
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:132
start
Definition: IOTest.py:99
PropertyBase base class allowing PropertyBase* collections to be "homogeneous".
Definition: Property.h:32
SmartIF< IToolSvc > m_ptoolSvc
Tool service.
Definition: AlgTool.h:298
STL class.
StatusCode declareTool(ToolHandle< T > &handle, std::string toolTypeAndName, bool createIf=true)
Definition: AlgTool.h:173
SmartIF< IAuditorSvc > m_pAuditorSvc
Auditor Service.
Definition: AlgTool.h:300
void declareInfo(const std::string &name, const T &var, const std::string &desc) const
Declare monitoring information.
Definition: AlgTool.h:266
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:185
T find(T...args)
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:322
Class wrapping the signature for a factory with any number of arguments.
Definition: PluginService.h:45
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:140
void declareInterface(I *i)
Definition: AlgTool.h:156
SmartIF< T > service(const std::string &name, const bool createIf=true, const bool quiet=false) const
Definition: AlgTool.h:149
Base class from which all the concrete tool classes should be derived.
Definition: AlgTool.h:48
The interface implemented by the AlgTool base class.
Definition: IAlgTool.h:23
SmartIF< ISvcLocator > m_svcLocator
Pointer to Service Locator service.
Definition: AlgTool.h:296
SmartIF< IDataProviderSvc > m_evtSvc
Event data service.
Definition: AlgTool.h:297
InterfaceList m_interfaceList
Interface list.
Definition: AlgTool.h:302
StatusCode terminate() override
Definition: AlgTool.h:75
The interface implemented by the IAuditorSvc base class.
Definition: IAuditorSvc.h:15
#define GAUDI_API
Definition: Kernel.h:110
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:209
virtual const std::string & name() const =0
Retrieve the name of the instance.
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:54
std::vector< GaudiHandleArrayBase * > m_toolHandleArrays
Definition: AlgTool.h:323
std::unique_ptr< IDataHandleVisitor > m_updateDataHandles
Hook for for derived classes to provide a custom visitor for data handles.
Definition: AlgTool.h:233
Gaudi::Details::PropertyBase * declareProperty(const std::string &name, ToolHandleArray< T > &hndlArr, const std::string &doc="none")
Definition: AlgTool.h:191