The Gaudi Framework  v30r3 (a5ef0a68)
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  using Factory = Gaudi::PluginService::Factory<IAlgTool*( const std::string&, const std::string&, const IInterface* )>;
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(), reinterpret_cast<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>
163  const std::string& doc = "none" )
164  {
165  this->declareTool( hndl, hndl.typeAndName() ).ignore();
166  return PropertyHolderImpl::declareProperty( name, hndl, doc );
167  }
168 
169  template <class T>
170  StatusCode declareTool( ToolHandle<T>& handle, bool createIf = true )
171  {
172  return this->declareTool( handle, handle.typeAndName(), createIf );
173  }
174 
175  template <class T>
176  StatusCode declareTool( ToolHandle<T>& handle, std::string toolTypeAndName, bool createIf = true )
177  {
178 
179  StatusCode sc = handle.initialize( toolTypeAndName, handle.isPublic() ? nullptr : this, createIf );
180  if ( UNLIKELY( !sc ) ) {
181  throw GaudiException{std::string{"Cannot create handle for "} + ( handle.isPublic() ? "public" : "private" ) +
182  " tool " + toolTypeAndName,
183  name(), sc};
184  }
185 
186  m_toolHandles.push_back( &handle );
187 
188  return sc;
189  }
190 
191  // ==========================================================================
192  // declare ToolHandleArrays to the AlgTool
193  template <class T>
195  const std::string& doc = "none" )
196  {
197  addToolsArray( hndlArr );
198  return PropertyHolderImpl::declareProperty( name, hndlArr, doc );
199  }
200 
201  template <class T>
203  {
204  m_toolHandleArrays.push_back( &hndlArr );
205  }
206 
207 public:
208  void acceptDHVisitor( IDataHandleVisitor* ) const override;
209 
210 public:
211  void registerTool( IAlgTool* tool ) const
212  {
213  if ( UNLIKELY( msgLevel( MSG::DEBUG ) ) ) debug() << "Registering tool " << tool->name() << endmsg;
214  m_tools.push_back( tool );
215  }
216 
217  void deregisterTool( IAlgTool* tool ) const
218  {
219  auto it = std::find( m_tools.begin(), m_tools.end(), tool );
220  if ( it != m_tools.end() ) {
221  if ( UNLIKELY( msgLevel( MSG::DEBUG ) ) ) debug() << "De-Registering tool " << tool->name() << endmsg;
222  m_tools.erase( it );
223  } else {
224  if ( UNLIKELY( msgLevel( MSG::DEBUG ) ) ) debug() << "Could not de-register tool " << tool->name() << endmsg;
225  }
226  }
227 
228  const std::vector<IAlgTool*>& tools() const;
229 
230 protected:
231  std::vector<IAlgTool*>& tools();
232 
235 
236 private:
237  // place IAlgTools defined via ToolHandles in m_tools
238  void initToolHandles() const;
239 
240 public:
241  // ==========================================================================
243  IAuditorSvc* auditorSvc() const;
244 
254  inline IMonitorSvc* monitorSvc() const
255  {
256  // If not already located try to locate it without forcing a creation
257  if ( !m_pMonitorSvc ) m_pMonitorSvc = service( m_monitorSvcName, false, true );
258  return m_pMonitorSvc.get();
259  }
260 
266  template <class T>
267  void declareInfo( const std::string& name, const T& var, const std::string& desc ) const
268  {
269  IMonitorSvc* mS = monitorSvc();
270  if ( mS ) mS->declareInfo( name, var, desc, this );
271  }
272 
280  void declareInfo( const std::string& name, const std::string& format, const void* var, int size,
281  const std::string& desc ) const
282  {
283  IMonitorSvc* mS = monitorSvc();
284  if ( mS ) mS->declareInfo( name, format, var, size, desc, this );
285  }
286 
287  // Standard destructor.
288  ~AlgTool() override;
289 
290 private:
292 
295  const IInterface* m_parent = nullptr;
296 
302 
303  InterfaceList m_interfaceList;
304 
305  // Properties
306  Gaudi::Property<int> m_outputLevel{this, "OutputLevel", MSG::NIL, "output level"};
307 
308  Gaudi::Property<std::string> m_monitorSvcName{this, "MonitorService", "MonitorSvc",
309  "name to use for Monitor Service"};
310 
311  Gaudi::Property<bool> m_auditInit{this, "AuditTools", false, "[[deprecated]] unused"};
312  Gaudi::Property<bool> m_auditorInitialize{this, "AuditInitialize", false, "trigger auditor on initialize()"};
313  Gaudi::Property<bool> m_auditorStart{this, "AuditStart", false, "trigger auditor on start()"};
314  Gaudi::Property<bool> m_auditorStop{this, "AuditStop", false, "trigger auditor on stop()"};
315  Gaudi::Property<bool> m_auditorFinalize{this, "AuditFinalize", false, "trigger auditor on finalize()"};
316  Gaudi::Property<bool> m_auditorReinitialize{this, "AuditReinitialize", false, "trigger auditor on reinitialize()"};
317  Gaudi::Property<bool> m_auditorRestart{this, "AuditRestart", false, "trigger auditor on restart()"};
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 #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:76
#define UNLIKELY(x)
Definition: Kernel.h:122
SmartIF< IMonitorSvc > m_pMonitorSvc
Online Monitoring Service.
Definition: AlgTool.h:300
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:293
Implementation of property with value of concrete type.
Definition: Property.h:381
void addToolsArray(ToolHandleArray< T > &hndlArr)
Definition: AlgTool.h:202
bool isPublic() const noexcept
Definition: ToolHandle.h:41
void deregisterTool(IAlgTool *tool) const
Definition: AlgTool.h:217
std::list< std::pair< InterfaceID, void * > > InterfaceList
Definition: AlgTool.h:291
StatusCode declareTool(ToolHandle< T > &handle, bool createIf=true)
Definition: AlgTool.h:170
Array of Handles to be used in lieu of vector of naked pointers to tools.
Definition: ToolHandle.h:385
Gaudi::Details::PropertyBase * declareProperty(const std::string &name, ToolHandle< T > &hndl, const std::string &doc="none")
Definition: AlgTool.h:162
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:211
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:294
DataObjectHandle.h GaudiKernel/DataObjectHandle.h.
Definition: AlgTool.h:26
Gaudi::StateMachine::State FSMState() const override
Definition: AlgTool.h:75
Interface ID class.
Definition: IInterface.h:29
IMonitorSvc * monitorSvc() const
Access the monitor service.
Definition: AlgTool.h:254
StatusCode queryInterface(const InterfaceID &ti, void **pp) override
Implementation of IInterface::queryInterface.
Definition: extends.h:30
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: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:280
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:51
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:281
Definition of the basic interface.
Definition: IInterface.h:277
StatusCode configure() override
Definition: AlgTool.h:67
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:99
Gaudi::PluginService::Factory< IAlgTool *(const std::string &, const std::string &, const IInterface *)> Factory
Definition: AlgTool.h:52
PropertyBase base class allowing PropertyBase* collections to be "homogeneous".
Definition: Property.h:32
SmartIF< IToolSvc > m_ptoolSvc
Tool service.
Definition: AlgTool.h:299
STL class.
StatusCode declareTool(ToolHandle< T > &handle, std::string toolTypeAndName, bool createIf=true)
Definition: AlgTool.h:176
constexpr static const auto SUCCESS
Definition: StatusCode.h:87
SmartIF< IAuditorSvc > m_pAuditorSvc
Auditor Service.
Definition: AlgTool.h:301
void declareInfo(const std::string &name, const T &var, const std::string &desc) const
Declare monitoring information.
Definition: AlgTool.h:267
Handle to be used in lieu of naked pointers to tools.
Definition: ToolHandle.h:130
StatusCode initialize(const std::string &toolTypeAndName, const IInterface *parent=nullptr, bool createIf=true)
Definition: ToolHandle.h:214
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
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
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: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:297
SmartIF< IDataProviderSvc > m_evtSvc
Event data service.
Definition: AlgTool.h:298
InterfaceList m_interfaceList
Interface list.
Definition: AlgTool.h:303
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:104
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:29
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:234
Gaudi::Details::PropertyBase * declareProperty(const std::string &name, ToolHandleArray< T > &hndlArr, const std::string &doc="none")
Definition: AlgTool.h:194