The Gaudi Framework  v30r1 (5d4f4ae2)
AlgTool.h
Go to the documentation of this file.
1 #ifndef GAUDIKERNEL_ALGTOOL_H
2 #define GAUDIKERNEL_ALGTOOL_H
3 // ============================================================================
4 // Include files
7 #include "GaudiKernel/IAlgTool.h"
12 #include "GaudiKernel/IProperty.h"
13 #include "GaudiKernel/IService.h"
14 #include "GaudiKernel/IStateful.h"
16 #include "GaudiKernel/IToolSvc.h"
18 #include "GaudiKernel/ToolHandle.h"
19 #include <Gaudi/PluginService.h>
20 
21 #include "GaudiKernel/DataHandle.h"
24 
25 template <class T>
27 
28 class ToolHandleInfo;
29 
30 #include <list>
31 #include <vector>
32 
33 // Forward declarations
34 
48  : public DataHandleHolderBase<
49  PropertyHolder<CommonMessaging<implements<IAlgTool, IDataHandleHolder, IProperty, IStateful>>>>
50 {
51 public:
52 #ifndef __REFLEX__
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:
162 
163  template <class T>
165  const std::string& doc = "none" )
166  {
167  this->declareTool( hndl, hndl.typeAndName() ).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 
175  StatusCode sc = handle.initialize( toolTypeAndName, handle.isPublic() ? nullptr : this, createIf );
176  if ( UNLIKELY( !sc ) ) {
177  throw GaudiException{std::string{"Cannot create handle for "} + ( handle.isPublic() ? "public" : "private" ) +
178  " tool " + toolTypeAndName,
179  name(), sc};
180  }
181 
182  m_toolHandles.push_back( &handle );
183 
184  return sc;
185  }
186 
187  // ==========================================================================
188  // declare ToolHandleArrays to the AlgTool
189  template <class T>
191  const std::string& doc = "none" )
192  {
193  addToolsArray( hndlArr );
194  return PropertyHolderImpl::declareProperty( name, hndlArr, doc );
195  }
196 
197  template <class T>
199  {
200  m_toolHandleArrays.push_back( &hndlArr );
201  }
202 
203 public:
204  virtual void acceptDHVisitor( IDataHandleVisitor* ) const override;
205 
206  void commitHandles() override;
207 
208 public:
209  void registerTool( IAlgTool* tool ) const
210  {
211  if ( UNLIKELY( msgLevel( MSG::DEBUG ) ) ) debug() << "Registering tool " << tool->name() << endmsg;
212  m_tools.push_back( tool );
213  }
214 
215  void deregisterTool( IAlgTool* tool ) const
216  {
217  auto it = std::find( m_tools.begin(), m_tools.end(), tool );
218  if ( it != m_tools.end() ) {
219  if ( UNLIKELY( msgLevel( MSG::DEBUG ) ) ) debug() << "De-Registering tool " << tool->name() << endmsg;
220  m_tools.erase( it );
221  } else {
222  if ( UNLIKELY( msgLevel( MSG::DEBUG ) ) ) debug() << "Could not de-register tool " << tool->name() << endmsg;
223  }
224  }
225 
226  const std::vector<IAlgTool*>& tools() const;
227 
228 protected:
229  std::vector<IAlgTool*>& tools();
230 
233 
234 private:
235  // place IAlgTools defined via ToolHandles in m_tools
236  void initToolHandles() const;
237 
238 public:
239  // ==========================================================================
241  IAuditorSvc* auditorSvc() const;
242 
252  inline IMonitorSvc* monitorSvc() const
253  {
254  // If not already located try to locate it without forcing a creation
255  if ( !m_pMonitorSvc ) m_pMonitorSvc = service( m_monitorSvcName, false, true );
256  return m_pMonitorSvc.get();
257  }
258 
264  template <class T>
265  void declareInfo( const std::string& name, const T& var, const std::string& desc ) const
266  {
267  IMonitorSvc* mS = monitorSvc();
268  if ( mS ) mS->declareInfo( name, var, desc, this );
269  }
270 
278  void declareInfo( const std::string& name, const std::string& format, const void* var, int size,
279  const std::string& desc ) const
280  {
281  IMonitorSvc* mS = monitorSvc();
282  if ( mS ) mS->declareInfo( name, format, var, size, desc, this );
283  }
284 
285  // Standard destructor.
286  ~AlgTool() override;
287 
288 private:
290 
293  const IInterface* m_parent = nullptr;
294 
300 
301  InterfaceList m_interfaceList;
302 
303  // Properties
304  Gaudi::Property<int> m_outputLevel{this, "OutputLevel", MSG::NIL, "output level"};
305 
306  Gaudi::Property<std::string> m_monitorSvcName{this, "MonitorService", "MonitorSvc",
307  "name to use for Monitor Service"};
308 
309  Gaudi::Property<bool> m_auditInit{this, "AuditTools", false, "[[deprecated]] unused"};
310  Gaudi::Property<bool> m_auditorInitialize{this, "AuditInitialize", false, "trigger auditor on initialize()"};
311  Gaudi::Property<bool> m_auditorStart{this, "AuditStart", false, "trigger auditor on start()"};
312  Gaudi::Property<bool> m_auditorStop{this, "AuditStop", false, "trigger auditor on stop()"};
313  Gaudi::Property<bool> m_auditorFinalize{this, "AuditFinalize", false, "trigger auditor on finalize()"};
314  Gaudi::Property<bool> m_auditorReinitialize{this, "AuditReinitialize", false, "trigger auditor on reinitialize()"};
315  Gaudi::Property<bool> m_auditorRestart{this, "AuditRestart", false, "trigger auditor on restart()"};
316 
318 
319  // tools used by tool
323  mutable bool m_toolHandlesInit = false;
324 
326  StatusCode service_i( const std::string& algName, bool createIf, const InterfaceID& iid, void** ppSvc ) const;
327  StatusCode service_i( const std::string& svcType, const std::string& svcName, const InterfaceID& iid,
328  void** ppS ) const;
329 
332 };
333 
334 #ifndef GAUDI_NEW_PLUGIN_SERVICE
335 template <class T>
336 struct ToolFactory {
337  template <typename S, typename... Args>
338  static typename S::ReturnType create( Args&&... args )
339  {
340  return new T( std::forward<Args>( args )... );
341  }
342 };
343 
344 // Macros to declare component factories
345 #define DECLARE_TOOL_FACTORY( x ) DECLARE_FACTORY_WITH_CREATOR( x, ToolFactory<x>, AlgTool::Factory )
346 #define DECLARE_NAMESPACE_TOOL_FACTORY( n, x ) DECLARE_TOOL_FACTORY( n::x )
347 
348 #else
349 
350 // Macros to declare component factories
351 #define DECLARE_TOOL_FACTORY( x ) DECLARE_COMPONENT( x )
352 #define DECLARE_NAMESPACE_TOOL_FACTORY( n, x ) DECLARE_COMPONENT( n::x )
353 
354 #endif
355 
356 #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:78
#define UNLIKELY(x)
Definition: Kernel.h:128
SmartIF< IMonitorSvc > m_pMonitorSvc
Online Monitoring Service.
Definition: AlgTool.h:298
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:291
Implementation of property with value of concrete type.
Definition: Property.h:319
void addToolsArray(ToolHandleArray< T > &hndlArr)
Definition: AlgTool.h:198
bool isPublic() const noexcept
Definition: ToolHandle.h:37
void deregisterTool(IAlgTool *tool) const
Definition: AlgTool.h:215
std::list< std::pair< InterfaceID, void * > > InterfaceList
Definition: AlgTool.h:289
Array of Handles to be used in lieu of vector of naked pointers to tools.
Definition: ToolHandle.h:351
Gaudi::Details::PropertyBase * declareProperty(const std::string &name, ToolHandle< T > &hndl, const std::string &doc="none")
Definition: AlgTool.h:164
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:209
Data provider interface definition.
std::vector< IAlgTool * > m_tools
Definition: AlgTool.h:320
constexpr auto size(const C &c) noexcept(noexcept(c.size())) -> decltype(c.size())
STL class.
const std::string m_name
AlgTool full name.
Definition: AlgTool.h:292
DataObjectHandle.h GaudiKernel/DataObjectHandle.h.
Definition: AlgTool.h:26
Gaudi::StateMachine::State FSMState() const override
Definition: AlgTool.h:77
Interface ID class.
Definition: IInterface.h:29
IMonitorSvc * monitorSvc() const
Access the monitor service.
Definition: AlgTool.h:252
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:338
ISvcLocator * svcLoc() const
shortcut for the method service locator
Definition: AlgTool.h:110
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:278
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:26
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:246
Definition of the basic interface.
Definition: IInterface.h:277
StatusCode configure() override
Definition: AlgTool.h:69
std::string m_threadID
Thread Id for Alg Tool.
Definition: AlgTool.h:317
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
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:297
STL class.
StatusCode declareTool(ToolHandle< T > &handle, std::string toolTypeAndName, bool createIf=true)
Definition: AlgTool.h:172
SmartIF< IAuditorSvc > m_pAuditorSvc
Auditor Service.
Definition: AlgTool.h:299
void declareInfo(const std::string &name, const T &var, const std::string &desc) const
Declare monitoring information.
Definition: AlgTool.h:265
Handle to be used in lieu of naked pointers to tools.
Definition: ToolHandle.h:124
StatusCode initialize(const std::string &toolTypeAndName, const IInterface *parent=nullptr, bool createIf=true)
Definition: ToolHandle.h:208
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:321
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:139
void declareInterface(I *i)
Definition: AlgTool.h:155
SmartIF< T > service(const std::string &name, const bool createIf=true, const bool quiet=false) const
Definition: AlgTool.h:148
Base class from which all the concrete tool classes should be derived.
Definition: AlgTool.h:47
The interface implemented by the AlgTool base class.
Definition: IAlgTool.h:23
SmartIF< ISvcLocator > m_svcLocator
Pointer to Service Locator service.
Definition: AlgTool.h:295
SmartIF< IDataProviderSvc > m_evtSvc
Event data service.
Definition: AlgTool.h:296
InterfaceList m_interfaceList
Interface list.
Definition: AlgTool.h:301
StatusCode terminate() override
Definition: AlgTool.h:74
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:53
std::vector< GaudiHandleArrayBase * > m_toolHandleArrays
Definition: AlgTool.h:322
std::unique_ptr< IDataHandleVisitor > m_updateDataHandles
Hook for for derived classes to provide a custom visitor for data handles.
Definition: AlgTool.h:232
Gaudi::Details::PropertyBase * declareProperty(const std::string &name, ToolHandleArray< T > &hndlArr, const std::string &doc="none")
Definition: AlgTool.h:190