Loading [MathJax]/extensions/tex2jax.js
The Gaudi Framework  master (42b00024)
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
AlgTool.h
Go to the documentation of this file.
1 /***********************************************************************************\
2 * (c) Copyright 1998-2025 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 
134  SmartIF<IService> service( std::string_view name, const bool createIf = true, const bool quiet = false ) const;
135 
136  template <typename T>
137  SmartIF<T> service( std::string_view name, const bool createIf = true, const bool quiet = false ) const {
138  return SmartIF<T>( service( name, createIf, quiet ) );
139  }
140 
141 protected:
142  template <typename I>
143  void declareInterface( I* i ) {
144  m_interfaceList.emplace_back( I::interfaceID(), i );
145  }
146 
147 public:
148  using PropertyHolderImpl::declareProperty;
149 
150  template <class T>
152  const std::string& doc = "none" ) {
153  this->declareTool( hndl, hndl.typeAndName() ).ignore();
154  return PropertyHolderImpl::declareProperty( name, hndl, doc );
155  }
156 
157  template <class T>
158  StatusCode declareTool( ToolHandle<T>& handle, bool createIf = true ) {
159  return this->declareTool( handle, handle.typeAndName(), createIf );
160  }
161 
162  template <class T>
163  StatusCode declareTool( ToolHandle<T>& handle, const std::string& toolTypeAndName, bool createIf = true ) {
164 
165  StatusCode sc = handle.initialize( toolTypeAndName, handle.isPublic() ? nullptr : this, createIf );
166  if ( !sc ) {
167  throw GaudiException{ std::string{ "Cannot create handle for " } + ( handle.isPublic() ? "public" : "private" ) +
168  " tool " + toolTypeAndName,
169  name(), sc };
170  }
171 
172  m_toolHandles.push_back( &handle );
173 
174  return sc;
175  }
176 
177  // ==========================================================================
178  // declare ToolHandleArrays to the AlgTool
179  template <class T>
181  const std::string& doc = "none" ) {
182  addToolsArray( hndlArr );
183  return PropertyHolderImpl::declareProperty( name, hndlArr, doc );
184  }
185 
186  template <class T>
188  m_toolHandleArrays.push_back( &hndlArr );
189  }
190 
191 public:
192  void acceptDHVisitor( IDataHandleVisitor* ) const override;
193 
194 public:
195  void registerTool( IAlgTool* tool ) const {
196  if ( msgLevel( MSG::DEBUG ) ) debug() << "Registering tool " << tool->name() << endmsg;
197  m_tools.push_back( tool );
198  }
199 
200  void deregisterTool( IAlgTool* tool ) const {
201  auto it = std::find( m_tools.begin(), m_tools.end(), tool );
202  if ( it != m_tools.end() ) {
203  if ( msgLevel( MSG::DEBUG ) ) debug() << "De-Registering tool " << tool->name() << endmsg;
204  m_tools.erase( it );
205  } else {
206  if ( msgLevel( MSG::DEBUG ) ) debug() << "Could not de-register tool " << tool->name() << endmsg;
207  }
208  }
209 
210  const std::vector<IAlgTool*>& tools() const;
211 
212 protected:
213  std::vector<IAlgTool*>& tools();
214 
216  std::unique_ptr<IDataHandleVisitor> m_updateDataHandles;
217 
218 private:
219  // place IAlgTools defined via ToolHandles in m_tools
220  void initToolHandles() const;
221 
222 public:
223  // ==========================================================================
225  IAuditorSvc* auditorSvc() const;
226 
236  inline IMonitorSvc* monitorSvc() const {
237  // If not already located try to locate it without forcing a creation
238  if ( !m_pMonitorSvc ) m_pMonitorSvc = service( m_monitorSvcName, false, true );
239  return m_pMonitorSvc.get();
240  }
241 
247  template <class T>
248  void declareInfo( const std::string& name, const T& var, const std::string& desc ) const {
249  IMonitorSvc* mS = monitorSvc();
250  if ( mS ) mS->declareInfo( name, var, desc, this );
251  }
252 
260  void declareInfo( const std::string& name, const std::string& format, const void* var, int size,
261  const std::string& desc ) const {
262  IMonitorSvc* mS = monitorSvc();
263  if ( mS ) mS->declareInfo( name, format, var, size, desc, this );
264  }
265 
266  // Standard destructor.
267  ~AlgTool() override;
268 
269 private:
270  typedef std::list<std::pair<InterfaceID, void*>> InterfaceList;
271 
272  std::string m_type;
273  const std::string m_name;
274  const IInterface* m_parent = nullptr;
275 
281 
283 
284  // Properties
285  // initialize output level from MessageSvc and initialize messaging (before enabling update handler)
286  Gaudi::Property<int> m_outputLevel{
287  this, "OutputLevel", setUpMessaging(),
288  [this]( Gaudi::Details::PropertyBase& ) { this->updateMsgStreamOutputLevel( this->m_outputLevel ); },
289  "output level" };
290 
291  Gaudi::Property<std::string> m_monitorSvcName{ this, "MonitorService", "MonitorSvc",
292  "name to use for Monitor Service" };
293 
294  Gaudi::Property<bool> m_auditorInitialize{ this, "AuditInitialize", false, "trigger auditor on initialize()" };
295  Gaudi::Property<bool> m_auditorStart{ this, "AuditStart", false, "trigger auditor on start()" };
296  Gaudi::Property<bool> m_auditorStop{ this, "AuditStop", false, "trigger auditor on stop()" };
297  Gaudi::Property<bool> m_auditorFinalize{ this, "AuditFinalize", false, "trigger auditor on finalize()" };
298  Gaudi::Property<bool> m_auditorReinitialize{ this, "AuditReinitialize", false, "trigger auditor on reinitialize()" };
299  Gaudi::Property<bool> m_auditorRestart{ this, "AuditRestart", false, "trigger auditor on restart()" };
300 
301  // tools used by tool
302  mutable std::vector<IAlgTool*> m_tools;
303  mutable std::vector<BaseToolHandle*> m_toolHandles;
304  mutable std::vector<GaudiHandleArrayBase*> m_toolHandleArrays;
305  mutable bool m_toolHandlesInit = false;
306 
309 };
310 
311 #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:137
AlgTool::declareProperty
Gaudi::Details::PropertyBase * declareProperty(const std::string &name, ToolHandleArray< T > &hndlArr, const std::string &doc="none")
Definition: AlgTool.h:180
DataHandleHolderBase
Definition: DataHandleHolderBase.h:34
AlgTool::svcLoc
ISvcLocator * svcLoc() const
shortcut for the method service locator
Definition: AlgTool.h:123
IService.h
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:279
AlgTool::m_toolHandles
std::vector< BaseToolHandle * > m_toolHandles
Definition: AlgTool.h:303
AlgTool::targetFSMState
Gaudi::StateMachine::State targetFSMState() const override
Definition: AlgTool.h:91
AlgTool::addToolsArray
void addToolsArray(ToolHandleArray< T > &hndlArr)
Definition: AlgTool.h:187
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:216
AlgTool::InterfaceList
std::list< std::pair< InterfaceID, void * > > InterfaceList
Definition: AlgTool.h:270
AlgTool::declareInterface
void declareInterface(I *i)
Definition: AlgTool.h:143
AlgTool::declareTool
StatusCode declareTool(ToolHandle< T > &handle, bool createIf=true)
Definition: AlgTool.h:158
AlgTool::registerTool
void registerTool(IAlgTool *tool) const
Definition: AlgTool.h:195
AlgTool::m_svcLocator
SmartIF< ISvcLocator > m_svcLocator
Pointer to Service Locator service.
Definition: AlgTool.h:276
ISvcLocator
Definition: ISvcLocator.h:46
GaudiException
Definition: GaudiException.h:32
AlgTool::m_evtSvc
SmartIF< IDataProviderSvc > m_evtSvc
Event data service.
Definition: AlgTool.h:277
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:260
DataObjID.h
AlgTool::m_tools
std::vector< IAlgTool * > m_tools
Definition: AlgTool.h:302
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:248
Gaudi::StateMachine::State
State
Allowed states for classes implementing the state machine (ApplicationMgr, Algorithm,...
Definition: StateMachine.h:22
PluginService.h
ToolHandle
Definition: ToolHandle.h:136
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:151
StatusCode
Definition: StatusCode.h:65
AlgTool::monitorSvc
IMonitorSvc * monitorSvc() const
Access the monitor service.
Definition: AlgTool.h:236
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:46
IAlgTool.h
ToolHandleArray
Definition: ToolHandle.h:403
AlgTool::m_type
std::string m_type
AlgTool type (concrete class name)
Definition: AlgTool.h:272
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:278
AlgTool::m_toolHandleArrays
std::vector< GaudiHandleArrayBase * > m_toolHandleArrays
Definition: AlgTool.h:304
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:55
IMonitorSvc.h
AlgTool::m_interfaceList
InterfaceList m_interfaceList
Interface list.
Definition: AlgTool.h:282
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
IAuditorSvc
The interface implemented by the IAuditorSvc base class.
Definition: IAuditorSvc.h:24
AlgTool
Definition: AlgTool.h:62
IDataHandleHolder.h
DataHandleHolderBase.h
ToolHandle.h
AlgTool::deregisterTool
void deregisterTool(IAlgTool *tool) const
Definition: AlgTool.h:200
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:163
IInterface
Definition: IInterface.h:226
DataHandle.h
AlgTool::m_name
const std::string m_name
AlgTool full name.
Definition: AlgTool.h:273
AlgTool::m_pAuditorSvc
SmartIF< IAuditorSvc > m_pAuditorSvc
Auditor Service.
Definition: AlgTool.h:280
ToolHandle::initialize
StatusCode initialize(const std::string &toolTypeAndName, const IInterface *parent=nullptr, bool createIf=true)
Definition: ToolHandle.h:217
IProperty.h
ToolHandle::typeAndName
std::string typeAndName() const override
Definition: ToolHandle.h:277
IDataProviderSvc
Definition: IDataProviderSvc.h:53
InterfaceID
Definition: IInterface.h:39
ISvcLocator.h
IToolSvc
Definition: IToolSvc.h:29
PropertyHolder.h
AlgTool::configure
StatusCode configure() override
Definition: AlgTool.h:82
GAUDI_API
#define GAUDI_API
Definition: Kernel.h:84
Gaudi::Property< int >