The Gaudi Framework  master (37c0b60a)
AlgTool.h
Go to the documentation of this file.
1 /***********************************************************************************\
2 * (c) Copyright 1998-2024 CERN for the benefit of the LHCb and ATLAS collaborations *
3 * *
4 * This software is distributed under the terms of the Apache version 2 licence, *
5 * copied verbatim in the file "LICENSE". *
6 * *
7 * In applying this licence, CERN does not waive the privileges and immunities *
8 * granted to it by virtue of its status as an Intergovernmental Organization *
9 * or submit itself to any jurisdiction. *
10 \***********************************************************************************/
11 #ifndef GAUDIKERNEL_ALGTOOL_H
12 #define GAUDIKERNEL_ALGTOOL_H
13 // ============================================================================
14 // Include files
15 #include <Gaudi/PluginService.h>
17 #include <GaudiKernel/DataObjID.h>
18 #include <GaudiKernel/IAlgTool.h>
23 #include <GaudiKernel/IProperty.h>
24 #include <GaudiKernel/IService.h>
25 #include <GaudiKernel/IStateful.h>
27 #include <GaudiKernel/IToolSvc.h>
29 #include <GaudiKernel/ToolHandle.h>
30 
31 #include <GaudiKernel/DataHandle.h>
34 
35 template <class T>
37 
38 class ToolHandleInfo;
39 
40 #include <list>
41 #include <vector>
42 
43 // Forward declarations
44 class ToolSvc;
45 
46 class ToolVisitor;
47 
61  : public DataHandleHolderBase<
62  PropertyHolder<CommonMessaging<implements<IAlgTool, IDataHandleHolder, IProperty, IStateful>>>> {
63  friend ToolSvc;
64  friend class ToolVisitor;
65 
66 public:
67  using Factory = Gaudi::PluginService::Factory<IAlgTool*( const std::string&, const std::string&, const IInterface* )>;
68 
70  StatusCode queryInterface( const InterfaceID& riid, void** ppvUnknown ) override;
71 
73  const std::string& name() const override;
74 
76  const std::string& type() const override;
77 
79  const IInterface* parent() const override;
80 
81  // State machine implementation
82  StatusCode configure() override { return StatusCode::SUCCESS; }
83  StatusCode initialize() override;
84  StatusCode start() override;
85  StatusCode stop() override;
86  StatusCode finalize() override;
87  StatusCode terminate() override { return StatusCode::SUCCESS; }
88  StatusCode reinitialize() override;
89  StatusCode restart() override;
90  Gaudi::StateMachine::State FSMState() const override { return m_state; }
91  Gaudi::StateMachine::State targetFSMState() const override { return m_targetState; }
92 
94  StatusCode sysInitialize() override;
95 
97  StatusCode sysStart() override;
98 
100  StatusCode sysStop() override;
101 
103  StatusCode sysFinalize() override;
104 
106  StatusCode sysReinitialize() override;
107 
109  StatusCode sysRestart() override;
110 
111 public:
117  AlgTool( std::string type, std::string name, const IInterface* parent );
118 
120  SmartIF<ISvcLocator>& serviceLocator() const override;
121 
123  ISvcLocator* svcLoc() const { return serviceLocator(); }
124 
128  IDataProviderSvc* evtSvc() const;
129 
131  IToolSvc* toolSvc() const;
132 
136  template <class T>
137  [[deprecated( "use service<T>(name, createIf) -> SmartIF<T>" )]] StatusCode service( std::string_view name, T*& svc,
138  bool createIf = true ) const {
139  return service_i( name, createIf, T::interfaceID(), (void**)&svc );
140  }
141 
144  template <class T>
145  [[deprecated( "use service<T>(name, createIf) -> SmartIF<T>" )]] StatusCode
146  service( std::string_view type, std::string_view name, T*& svc ) const {
147  return service_i( type, name, T::interfaceID(), reinterpret_cast<void**>( &svc ) );
148  }
149 
151  SmartIF<IService> service( std::string_view name, const bool createIf = true, const bool quiet = false ) const;
152 
153  template <typename T>
154  SmartIF<T> service( std::string_view name, const bool createIf = true, const bool quiet = false ) const {
155  return SmartIF<T>( service( name, createIf, quiet ) );
156  }
157 
158 protected:
159  template <typename I>
160  void declareInterface( I* i ) {
161  m_interfaceList.emplace_back( I::interfaceID(), i );
162  }
163 
164 public:
165  using PropertyHolderImpl::declareProperty;
166 
167  template <class T>
169  const std::string& doc = "none" ) {
170  this->declareTool( hndl, hndl.typeAndName() ).ignore();
171  return PropertyHolderImpl::declareProperty( name, hndl, doc );
172  }
173 
174  template <class T>
175  StatusCode declareTool( ToolHandle<T>& handle, bool createIf = true ) {
176  return this->declareTool( handle, handle.typeAndName(), createIf );
177  }
178 
179  template <class T>
180  StatusCode declareTool( ToolHandle<T>& handle, const std::string& toolTypeAndName, bool createIf = true ) {
181 
182  StatusCode sc = handle.initialize( toolTypeAndName, handle.isPublic() ? nullptr : this, createIf );
183  if ( !sc ) {
184  throw GaudiException{ std::string{ "Cannot create handle for " } + ( handle.isPublic() ? "public" : "private" ) +
185  " tool " + toolTypeAndName,
186  name(), sc };
187  }
188 
189  m_toolHandles.push_back( &handle );
190 
191  return sc;
192  }
193 
194  // ==========================================================================
195  // declare ToolHandleArrays to the AlgTool
196  template <class T>
198  const std::string& doc = "none" ) {
199  addToolsArray( hndlArr );
200  return PropertyHolderImpl::declareProperty( name, hndlArr, doc );
201  }
202 
203  template <class T>
205  m_toolHandleArrays.push_back( &hndlArr );
206  }
207 
208 public:
209  void acceptDHVisitor( IDataHandleVisitor* ) const override;
210 
211 public:
212  void registerTool( IAlgTool* tool ) const {
213  if ( msgLevel( MSG::DEBUG ) ) debug() << "Registering tool " << tool->name() << endmsg;
214  m_tools.push_back( tool );
215  }
216 
217  void deregisterTool( IAlgTool* tool ) const {
218  auto it = std::find( m_tools.begin(), m_tools.end(), tool );
219  if ( it != m_tools.end() ) {
220  if ( msgLevel( MSG::DEBUG ) ) debug() << "De-Registering tool " << tool->name() << endmsg;
221  m_tools.erase( it );
222  } else {
223  if ( 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  // 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  IMonitorSvc* mS = monitorSvc();
267  if ( mS ) mS->declareInfo( name, var, desc, this );
268  }
269 
277  void declareInfo( const std::string& name, const std::string& format, const void* var, int size,
278  const std::string& desc ) const {
279  IMonitorSvc* mS = monitorSvc();
280  if ( mS ) mS->declareInfo( name, format, var, size, desc, this );
281  }
282 
283  // Standard destructor.
284  ~AlgTool() override;
285 
286 private:
288 
291  const IInterface* m_parent = nullptr;
292 
298 
300 
301  // Properties
302  // initialize output level from MessageSvc and initialize messaging (before enabling update handler)
303  Gaudi::Property<int> m_outputLevel{
304  this, "OutputLevel", setUpMessaging(),
305  [this]( Gaudi::Details::PropertyBase& ) { this->updateMsgStreamOutputLevel( this->m_outputLevel ); },
306  "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  [[deprecated]] StatusCode service_i( std::string_view algName, bool createIf, const InterfaceID& iid,
327  void** ppSvc ) const;
328  [[deprecated]] StatusCode service_i( std::string_view svcType, std::string_view svcName, const InterfaceID& iid,
329  void** ppS ) const;
330 
333 };
334 
335 #endif // GAUDIKERNEL_ALGTOOL_H
MSG::DEBUG
@ DEBUG
Definition: IMessageSvc.h:25
Gaudi::Details::PropertyBase
PropertyBase base class allowing PropertyBase* collections to be "homogeneous".
Definition: PropertyBase.h:35
AlgTool::service
SmartIF< T > service(std::string_view name, const bool createIf=true, const bool quiet=false) const
Definition: AlgTool.h:154
AlgTool::declareProperty
Gaudi::Details::PropertyBase * declareProperty(const std::string &name, ToolHandleArray< T > &hndlArr, const std::string &doc="none")
Definition: AlgTool.h:197
DataHandleHolderBase
Definition: DataHandleHolderBase.h:34
AlgTool::svcLoc
ISvcLocator * svcLoc() const
shortcut for the method service locator
Definition: AlgTool.h:123
IService.h
std::string
STL class.
IAlgTool
Definition: IAlgTool.h:33
details::size
constexpr auto size(const T &, Args &&...) noexcept
Definition: AnyDataWrapper.h:23
AlgTool::m_pMonitorSvc
SmartIF< IMonitorSvc > m_pMonitorSvc
Online Monitoring Service.
Definition: AlgTool.h:296
std::list
STL class.
AlgTool::m_toolHandles
std::vector< BaseToolHandle * > m_toolHandles
Definition: AlgTool.h:321
AlgTool::targetFSMState
Gaudi::StateMachine::State targetFSMState() const override
Definition: AlgTool.h:91
AlgTool::addToolsArray
void addToolsArray(ToolHandleArray< T > &hndlArr)
Definition: AlgTool.h:204
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:233
AlgTool::InterfaceList
std::list< std::pair< InterfaceID, void * > > InterfaceList
Definition: AlgTool.h:287
AlgTool::declareInterface
void declareInterface(I *i)
Definition: AlgTool.h:160
AlgTool::declareTool
StatusCode declareTool(ToolHandle< T > &handle, bool createIf=true)
Definition: AlgTool.h:175
AlgTool::registerTool
void registerTool(IAlgTool *tool) const
Definition: AlgTool.h:212
std::vector< IAlgTool * >
std::find
T find(T... args)
AlgTool::m_svcLocator
SmartIF< ISvcLocator > m_svcLocator
Pointer to Service Locator service.
Definition: AlgTool.h:293
ISvcLocator
Definition: ISvcLocator.h:46
GaudiException
Definition: GaudiException.h:31
AlgTool::m_evtSvc
SmartIF< IDataProviderSvc > m_evtSvc
Event data service.
Definition: AlgTool.h:294
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:277
DataObjID.h
AlgTool::m_tools
std::vector< IAlgTool * > m_tools
Definition: AlgTool.h:320
ToolSvc
Definition: ToolSvc.cpp:48
IMonitorSvc
Definition: IMonitorSvc.h:33
IOTest.start
start
Definition: IOTest.py:110
IMessageSvc.h
IAuditorSvc.h
IDataProviderSvc.h
AlgTool::FSMState
Gaudi::StateMachine::State FSMState() const override
Definition: AlgTool.h:90
DataObjectHandle
DataObjectHandle.h GaudiKernel/DataObjectHandle.h.
Definition: AlgTool.h:36
AlgTool::ToolSvc
friend ToolSvc
Definition: AlgTool.h:63
IToolSvc.h
AlgTool::declareInfo
void declareInfo(const std::string &name, const T &var, const std::string &desc) const
Declare monitoring information.
Definition: AlgTool.h:265
Gaudi::StateMachine::State
State
Allowed states for classes implementing the state machine (ApplicationMgr, Algorithm,...
Definition: StateMachine.h:22
PluginService.h
ToolHandle
Definition: ToolHandle.h:132
bug_34121.tool
tool
Definition: bug_34121.py:18
Gaudi::StateMachine::CONFIGURED
@ CONFIGURED
Definition: StateMachine.h:24
AlgTool::declareProperty
Gaudi::Details::PropertyBase * declareProperty(const std::string &name, ToolHandle< T > &hndl, const std::string &doc="none")
Definition: AlgTool.h:168
StatusCode
Definition: StatusCode.h:65
AlgTool::monitorSvc
IMonitorSvc * monitorSvc() const
Access the monitor service.
Definition: AlgTool.h:253
IMonitorSvc::declareInfo
virtual void declareInfo(const std::string &name, const bool &var, const std::string &desc, const IInterface *owner)=0
Declare monitoring information.
AlgTool::terminate
StatusCode terminate() override
Definition: AlgTool.h:87
ToolHandleInfo
General info and helper functions for toolhandles and arrays.
Definition: ToolHandle.h:42
IAlgTool.h
ToolHandleArray
Definition: ToolHandle.h:399
AlgTool::m_type
std::string m_type
AlgTool type (concrete class name)
Definition: AlgTool.h:289
AlgTool::Factory
Gaudi::PluginService::Factory< IAlgTool *(const std::string &, const std::string &, const IInterface *)> Factory
Definition: AlgTool.h:67
IStateful.h
AlgTool::m_ptoolSvc
SmartIF< IToolSvc > m_ptoolSvc
Tool service.
Definition: AlgTool.h:295
AlgTool::m_toolHandleArrays
std::vector< GaudiHandleArrayBase * > m_toolHandleArrays
Definition: AlgTool.h:322
SmartIF< ISvcLocator >
CommonMessaging.h
format
GAUDI_API std::string format(const char *,...)
MsgStream format utility "a la sprintf(...)".
Definition: MsgStream.cpp:119
endmsg
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:202
IDataHandleVisitor
Definition: IDataHandleHolder.h:46
ToolHandleInfo::isPublic
bool isPublic() const noexcept
Definition: ToolHandle.h:51
IMonitorSvc.h
AlgTool::m_interfaceList
InterfaceList m_interfaceList
Interface list.
Definition: AlgTool.h:299
gaudirun.type
type
Definition: gaudirun.py:160
StatusCode::SUCCESS
constexpr static const auto SUCCESS
Definition: StatusCode.h:100
ConditionsStallTest.name
name
Definition: ConditionsStallTest.py:77
AlgTool::service
StatusCode service(std::string_view name, T *&svc, bool createIf=true) const
Access a service by name, creating it if it doesn't already exist.
Definition: AlgTool.h:137
IAuditorSvc
Definition: IAuditorSvc.h:25
AlgTool
Definition: AlgTool.h:62
IDataHandleHolder.h
DataHandleHolderBase.h
ToolHandle.h
AlgTool::deregisterTool
void deregisterTool(IAlgTool *tool) const
Definition: AlgTool.h:217
extends< BASE, IDataHandleHolder >::queryInterface
StatusCode queryInterface(const InterfaceID &ti, void **pp) override
Implementation of IInterface::queryInterface.
Definition: extends.h:38
AlgTool::declareTool
StatusCode declareTool(ToolHandle< T > &handle, const std::string &toolTypeAndName, bool createIf=true)
Definition: AlgTool.h:180
IInterface
Definition: IInterface.h:239
DataHandle.h
AlgTool::m_name
const std::string m_name
AlgTool full name.
Definition: AlgTool.h:290
AlgTool::m_pAuditorSvc
SmartIF< IAuditorSvc > m_pAuditorSvc
Auditor Service.
Definition: AlgTool.h:297
ToolHandle::initialize
StatusCode initialize(const std::string &toolTypeAndName, const IInterface *parent=nullptr, bool createIf=true)
Definition: ToolHandle.h:213
IProperty.h
ToolHandle::typeAndName
std::string typeAndName() const override
Definition: ToolHandle.h:273
IDataProviderSvc
Definition: IDataProviderSvc.h:53
InterfaceID
Definition: IInterface.h:39
ISvcLocator.h
std::unique_ptr< IDataHandleVisitor >
IToolSvc
Definition: IToolSvc.h:29
PropertyHolder.h
AlgTool::configure
StatusCode configure() override
Definition: AlgTool.h:82
GAUDI_API
#define GAUDI_API
Definition: Kernel.h:81
Gaudi::Property< int >
AlgTool::service
StatusCode service(std::string_view type, std::string_view name, T *&svc) const
Access a service by name, type creating it if it doesn't already exist.
Definition: AlgTool.h:146