The Gaudi Framework  v33r0 (d5ea422b)
AlgTool.h
Go to the documentation of this file.
1 /***********************************************************************************\
2 * (c) Copyright 1998-2019 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
16 #include "GaudiKernel/DataObjID.h"
17 #include "GaudiKernel/IAlgTool.h"
22 #include "GaudiKernel/IProperty.h"
23 #include "GaudiKernel/IService.h"
24 #include "GaudiKernel/IStateful.h"
26 #include "GaudiKernel/IToolSvc.h"
28 #include "GaudiKernel/ToolHandle.h"
29 #include <Gaudi/PluginService.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 
58  : public DataHandleHolderBase<
59  PropertyHolder<CommonMessaging<implements<IAlgTool, IDataHandleHolder, IProperty, IStateful>>>> {
60 public:
61  using Factory = Gaudi::PluginService::Factory<IAlgTool*( const std::string&, const std::string&, const IInterface* )>;
62 
64  StatusCode queryInterface( const InterfaceID& riid, void** ppvUnknown ) override;
65 
67  const std::string& name() const override;
68 
70  const std::string& type() const override;
71 
73  const IInterface* parent() const override;
74 
75  // State machine implementation
76  StatusCode configure() override { return StatusCode::SUCCESS; }
77  StatusCode initialize() override;
78  StatusCode start() override;
79  StatusCode stop() override;
80  StatusCode finalize() override;
81  StatusCode terminate() override { return StatusCode::SUCCESS; }
82  StatusCode reinitialize() override;
83  StatusCode restart() override;
84  Gaudi::StateMachine::State FSMState() const override { return m_state; }
85  Gaudi::StateMachine::State targetFSMState() const override { return m_targetState; }
86 
88  StatusCode sysInitialize() override;
89 
91  StatusCode sysStart() override;
92 
94  StatusCode sysStop() override;
95 
97  StatusCode sysFinalize() override;
98 
100  StatusCode sysReinitialize() override;
101 
103  StatusCode sysRestart() override;
104 
105 public:
111  AlgTool( const std::string& type, const std::string& name, const IInterface* parent );
112 
114  SmartIF<ISvcLocator>& serviceLocator() const override;
115 
117  ISvcLocator* svcLoc() const { return serviceLocator(); }
118 
122  IDataProviderSvc* evtSvc() const;
123 
125  IToolSvc* toolSvc() const;
126 
132  StatusCode setProperties();
133 
137  template <class T>
138  StatusCode service( const std::string& name, T*& svc, bool createIf = true ) const {
139  return service_i( name, createIf, T::interfaceID(), (void**)&svc );
140  }
141 
144  template <class T>
145  StatusCode service( const std::string& type, const std::string& name, T*& svc ) const {
146  return service_i( type, name, T::interfaceID(), reinterpret_cast<void**>( &svc ) );
147  }
148 
150  SmartIF<IService> service( const std::string& name, const bool createIf = true, const bool quiet = false ) const;
151 
152  template <typename T>
153  SmartIF<T> service( const std::string& name, const bool createIf = true, const bool quiet = false ) const {
154  return SmartIF<T>( service( name, createIf, quiet ) );
155  }
156 
157 protected:
158  template <typename I>
159  void declareInterface( I* i ) {
160  m_interfaceList.emplace_back( I::interfaceID(), i );
161  }
162 
163 public:
164  using PropertyHolderImpl::declareProperty;
165 
166  template <class T>
168  const std::string& doc = "none" ) {
169  this->declareTool( hndl, hndl.typeAndName() ).ignore();
170  return PropertyHolderImpl::declareProperty( name, hndl, doc );
171  }
172 
173  template <class T>
174  StatusCode declareTool( ToolHandle<T>& handle, bool createIf = true ) {
175  return this->declareTool( handle, handle.typeAndName(), createIf );
176  }
177 
178  template <class T>
179  StatusCode declareTool( ToolHandle<T>& handle, std::string toolTypeAndName, bool createIf = true ) {
180 
181  StatusCode sc = handle.initialize( toolTypeAndName, handle.isPublic() ? nullptr : this, createIf );
182  if ( UNLIKELY( !sc ) ) {
183  throw GaudiException{std::string{"Cannot create handle for "} + ( handle.isPublic() ? "public" : "private" ) +
184  " tool " + toolTypeAndName,
185  name(), sc};
186  }
187 
188  m_toolHandles.push_back( &handle );
189 
190  return sc;
191  }
192 
193  // ==========================================================================
194  // declare ToolHandleArrays to the AlgTool
195  template <class T>
197  const std::string& doc = "none" ) {
198  addToolsArray( hndlArr );
199  return PropertyHolderImpl::declareProperty( name, hndlArr, doc );
200  }
201 
202  template <class T>
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  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  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  // If not already located try to locate it without forcing a creation
254  if ( !m_pMonitorSvc ) m_pMonitorSvc = service( m_monitorSvcName, false, true );
255  return m_pMonitorSvc.get();
256  }
257 
263  template <class T>
264  void declareInfo( const std::string& name, const T& var, const std::string& desc ) const {
265  IMonitorSvc* mS = monitorSvc();
266  if ( mS ) mS->declareInfo( name, var, desc, this );
267  }
268 
276  void declareInfo( const std::string& name, const std::string& format, const void* var, int size,
277  const std::string& desc ) const {
278  IMonitorSvc* mS = monitorSvc();
279  if ( mS ) mS->declareInfo( name, format, var, size, desc, this );
280  }
281 
282  // Standard destructor.
283  ~AlgTool() override;
284 
285 private:
287 
290  const IInterface* m_parent = nullptr;
291 
297 
299 
300  // Properties
301  // initialize output level from MessageSvc and initialize messaging (before enabling update handler)
302  Gaudi::Property<int> m_outputLevel{
303  this, "OutputLevel", setUpMessaging(),
304  [this]( Gaudi::Details::PropertyBase& ) { this->updateMsgStreamOutputLevel( this->m_outputLevel ); },
305  "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 
318  // tools used by tool
322  mutable bool m_toolHandlesInit = false;
323 
325  StatusCode service_i( const std::string& algName, bool createIf, const InterfaceID& iid, void** ppSvc ) const;
326  StatusCode service_i( const std::string& svcType, const std::string& svcName, const InterfaceID& iid,
327  void** ppS ) const;
328 
331 };
332 
333 #endif // GAUDIKERNEL_ALGTOOL_H
std::string typeAndName() const override
Definition: ToolHandle.h:270
void registerTool(IAlgTool *tool) const
Definition: AlgTool.h:211
The interface implemented by the IToolSvc base class.
Definition: IToolSvc.h:29
#define UNLIKELY(x)
Definition: Kernel.h:106
constexpr auto size(const T &, Args &&...) noexcept
SmartIF< IMonitorSvc > m_pMonitorSvc
Online Monitoring Service.
Definition: AlgTool.h:295
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:35
GAUDI_API std::string format(const char *,...)
MsgStream format utility "a la sprintf(...)".
Definition: MsgStream.cpp:119
ISvcLocator * svcLoc() const
shortcut for the method service locator
Definition: AlgTool.h:117
std::string m_type
AlgTool type (concrete class name)
Definition: AlgTool.h:288
Implementation of property with value of concrete type.
Definition: Property.h:370
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:276
void addToolsArray(ToolHandleArray< T > &hndlArr)
Definition: AlgTool.h:203
std::list< std::pair< InterfaceID, void * > > InterfaceList
Definition: AlgTool.h:286
StatusCode declareTool(ToolHandle< T > &handle, bool createIf=true)
Definition: AlgTool.h:174
constexpr static const auto SUCCESS
Definition: StatusCode.h:96
Array of Handles to be used in lieu of vector of naked pointers to tools.
Definition: ToolHandle.h:345
Gaudi::Details::PropertyBase * declareProperty(const std::string &name, ToolHandle< T > &hndl, const std::string &doc="none")
Definition: AlgTool.h:167
State
Allowed states for classes implementing the state machine (ApplicationMgr, Algorithm,...
Definition: StateMachine.h:22
Data provider interface definition.
std::vector< IAlgTool * > m_tools
Definition: AlgTool.h:319
def start
Definition: IOTest.py:108
Gaudi::StateMachine::State FSMState() const override
Definition: AlgTool.h:84
SmartIF< T > service(const std::string &name, const bool createIf=true, const bool quiet=false) const
Definition: AlgTool.h:153
virtual const std::string & name() const =0
Retrieve the name of the instance.
STL class.
const std::string m_name
AlgTool full name.
Definition: AlgTool.h:289
DataObjectHandle.h GaudiKernel/DataObjectHandle.h.
Definition: AlgTool.h:36
IMonitorSvc * monitorSvc() const
Access the monitor service.
Definition: AlgTool.h:252
Interface ID class.
Definition: IInterface.h:39
StatusCode queryInterface(const InterfaceID &ti, void **pp) override
Implementation of IInterface::queryInterface.
Definition: extends.h:38
Definition of the IMonitorSvc interface, which publishes Gaudi variables to outside monitoring proces...
Definition: IMonitorSvc.h:33
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:61
bool isPublic() const noexcept
Definition: ToolHandle.h:50
Definition of the basic interface.
Definition: IInterface.h:254
Gaudi::StateMachine::State targetFSMState() const override
Definition: AlgTool.h:85
StatusCode configure() override
Definition: AlgTool.h:76
Gaudi::PluginService::Factory< IAlgTool *(const std::string &, const std::string &, const IInterface *)> Factory
Definition: AlgTool.h:61
StatusCode service(const std::string &name, T *&svc, bool createIf=true) const
Access a service by name, creating it if it doesn't already exist.
Definition: AlgTool.h:138
PropertyBase base class allowing PropertyBase* collections to be "homogeneous".
Definition: Property.h:42
SmartIF< IToolSvc > m_ptoolSvc
Tool service.
Definition: AlgTool.h:294
STL class.
StatusCode declareTool(ToolHandle< T > &handle, std::string toolTypeAndName, bool createIf=true)
Definition: AlgTool.h:179
SmartIF< IAuditorSvc > m_pAuditorSvc
Auditor Service.
Definition: AlgTool.h:296
Handle to be used in lieu of naked pointers to tools.
Definition: ToolHandle.h:135
StatusCode initialize(const std::string &toolTypeAndName, const IInterface *parent=nullptr, bool createIf=true)
Definition: ToolHandle.h:210
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:320
void declareInterface(I *i)
Definition: AlgTool.h:159
Base class from which all the concrete tool classes should be derived.
Definition: AlgTool.h:57
The interface implemented by the AlgTool base class.
Definition: IAlgTool.h:33
SmartIF< ISvcLocator > m_svcLocator
Pointer to Service Locator service.
Definition: AlgTool.h:292
SmartIF< IDataProviderSvc > m_evtSvc
Event data service.
Definition: AlgTool.h:293
InterfaceList m_interfaceList
Interface list.
Definition: AlgTool.h:298
StatusCode terminate() override
Definition: AlgTool.h:81
The interface implemented by the IAuditorSvc base class.
Definition: IAuditorSvc.h:25
#define GAUDI_API
Definition: Kernel.h:81
void deregisterTool(IAlgTool *tool) const
Definition: AlgTool.h:216
StatusCode service(const std::string &type, const std::string &name, T *&svc) const
Access a service by name, type creating it if it doesn't already exist.
Definition: AlgTool.h:145
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:202
General info and helper functions for toolhandles and arrays.
Definition: ToolHandle.h:41
void declareInfo(const std::string &name, const T &var, const std::string &desc) const
Declare monitoring information.
Definition: AlgTool.h:264
std::vector< GaudiHandleArrayBase * > m_toolHandleArrays
Definition: AlgTool.h:321
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:196