File indexing completed on 2025-01-30 10:07:10
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011 #ifndef GAUDIKERNEL_ALGTOOL_H
0012 #define GAUDIKERNEL_ALGTOOL_H
0013
0014
0015 #include "GaudiKernel/CommonMessaging.h"
0016 #include "GaudiKernel/DataObjID.h"
0017 #include "GaudiKernel/IAlgTool.h"
0018 #include "GaudiKernel/IAuditorSvc.h"
0019 #include "GaudiKernel/IDataProviderSvc.h"
0020 #include "GaudiKernel/IMessageSvc.h"
0021 #include "GaudiKernel/IMonitorSvc.h"
0022 #include "GaudiKernel/IProperty.h"
0023 #include "GaudiKernel/IService.h"
0024 #include "GaudiKernel/IStateful.h"
0025 #include "GaudiKernel/ISvcLocator.h"
0026 #include "GaudiKernel/IToolSvc.h"
0027 #include "GaudiKernel/PropertyHolder.h"
0028 #include "GaudiKernel/ToolHandle.h"
0029 #include <Gaudi/PluginService.h>
0030
0031 #include "GaudiKernel/DataHandle.h"
0032 #include "GaudiKernel/DataHandleHolderBase.h"
0033 #include "GaudiKernel/IDataHandleHolder.h"
0034
0035 template <class T>
0036 class DataObjectHandle;
0037
0038 class ToolHandleInfo;
0039
0040 #include <list>
0041 #include <vector>
0042
0043
0044 class ToolSvc;
0045
0046 class ToolVisitor;
0047
0048
0049
0050
0051
0052
0053
0054
0055
0056
0057
0058
0059
0060 class GAUDI_API AlgTool
0061 : public DataHandleHolderBase<
0062 PropertyHolder<CommonMessaging<implements<IAlgTool, IDataHandleHolder, IProperty, IStateful>>>> {
0063 friend ToolSvc;
0064 friend class ToolVisitor;
0065
0066 public:
0067 using Factory = Gaudi::PluginService::Factory<IAlgTool*( const std::string&, const std::string&, const IInterface* )>;
0068
0069
0070 StatusCode queryInterface( const InterfaceID& riid, void** ppvUnknown ) override;
0071
0072
0073 const std::string& name() const override;
0074
0075
0076 const std::string& type() const override;
0077
0078
0079 const IInterface* parent() const override;
0080
0081
0082 StatusCode configure() override { return StatusCode::SUCCESS; }
0083 StatusCode initialize() override;
0084 StatusCode start() override;
0085 StatusCode stop() override;
0086 StatusCode finalize() override;
0087 StatusCode terminate() override { return StatusCode::SUCCESS; }
0088 StatusCode reinitialize() override;
0089 StatusCode restart() override;
0090 Gaudi::StateMachine::State FSMState() const override { return m_state; }
0091 Gaudi::StateMachine::State targetFSMState() const override { return m_targetState; }
0092
0093
0094 StatusCode sysInitialize() override;
0095
0096
0097 StatusCode sysStart() override;
0098
0099
0100 StatusCode sysStop() override;
0101
0102
0103 StatusCode sysFinalize() override;
0104
0105
0106 StatusCode sysReinitialize() override;
0107
0108
0109 StatusCode sysRestart() override;
0110
0111 public:
0112
0113
0114
0115
0116
0117 AlgTool( std::string type, std::string name, const IInterface* parent );
0118
0119
0120 SmartIF<ISvcLocator>& serviceLocator() const override;
0121
0122
0123 ISvcLocator* svcLoc() const { return serviceLocator(); }
0124
0125
0126
0127
0128 IDataProviderSvc* evtSvc() const;
0129
0130
0131 IToolSvc* toolSvc() const;
0132
0133
0134
0135
0136 template <class T>
0137 StatusCode service( std::string_view name, T*& svc, bool createIf = true ) const {
0138 return service_i( name, createIf, T::interfaceID(), (void**)&svc );
0139 }
0140
0141
0142
0143 template <class T>
0144 StatusCode service( std::string_view type, std::string_view name, T*& svc ) const {
0145 return service_i( type, name, T::interfaceID(), reinterpret_cast<void**>( &svc ) );
0146 }
0147
0148
0149 SmartIF<IService> service( std::string_view name, const bool createIf = true, const bool quiet = false ) const;
0150
0151 template <typename T>
0152 SmartIF<T> service( std::string_view name, const bool createIf = true, const bool quiet = false ) const {
0153 return SmartIF<T>( service( name, createIf, quiet ) );
0154 }
0155
0156 protected:
0157 template <typename I>
0158 void declareInterface( I* i ) {
0159 m_interfaceList.emplace_back( I::interfaceID(), i );
0160 }
0161
0162 public:
0163 using PropertyHolderImpl::declareProperty;
0164
0165 template <class T>
0166 Gaudi::Details::PropertyBase* declareProperty( const std::string& name, ToolHandle<T>& hndl,
0167 const std::string& doc = "none" ) {
0168 this->declareTool( hndl, hndl.typeAndName() ).ignore();
0169 return PropertyHolderImpl::declareProperty( name, hndl, doc );
0170 }
0171
0172 template <class T>
0173 StatusCode declareTool( ToolHandle<T>& handle, bool createIf = true ) {
0174 return this->declareTool( handle, handle.typeAndName(), createIf );
0175 }
0176
0177 template <class T>
0178 StatusCode declareTool( ToolHandle<T>& handle, std::string toolTypeAndName, bool createIf = true ) {
0179
0180 StatusCode sc = handle.initialize( toolTypeAndName, handle.isPublic() ? nullptr : this, createIf );
0181 if ( !sc ) {
0182 throw GaudiException{ std::string{ "Cannot create handle for " } + ( handle.isPublic() ? "public" : "private" ) +
0183 " tool " + toolTypeAndName,
0184 name(), sc };
0185 }
0186
0187 m_toolHandles.push_back( &handle );
0188
0189 return sc;
0190 }
0191
0192
0193
0194 template <class T>
0195 Gaudi::Details::PropertyBase* declareProperty( const std::string& name, ToolHandleArray<T>& hndlArr,
0196 const std::string& doc = "none" ) {
0197 addToolsArray( hndlArr );
0198 return PropertyHolderImpl::declareProperty( name, hndlArr, doc );
0199 }
0200
0201 template <class T>
0202 void addToolsArray( ToolHandleArray<T>& hndlArr ) {
0203 m_toolHandleArrays.push_back( &hndlArr );
0204 }
0205
0206 public:
0207 void acceptDHVisitor( IDataHandleVisitor* ) const override;
0208
0209 public:
0210 void registerTool( IAlgTool* tool ) const {
0211 if ( msgLevel( MSG::DEBUG ) ) debug() << "Registering tool " << tool->name() << endmsg;
0212 m_tools.push_back( tool );
0213 }
0214
0215 void deregisterTool( IAlgTool* tool ) const {
0216 auto it = std::find( m_tools.begin(), m_tools.end(), tool );
0217 if ( it != m_tools.end() ) {
0218 if ( msgLevel( MSG::DEBUG ) ) debug() << "De-Registering tool " << tool->name() << endmsg;
0219 m_tools.erase( it );
0220 } else {
0221 if ( msgLevel( MSG::DEBUG ) ) debug() << "Could not de-register tool " << tool->name() << endmsg;
0222 }
0223 }
0224
0225 const std::vector<IAlgTool*>& tools() const;
0226
0227 protected:
0228 std::vector<IAlgTool*>& tools();
0229
0230
0231 std::unique_ptr<IDataHandleVisitor> m_updateDataHandles;
0232
0233 private:
0234
0235 void initToolHandles() const;
0236
0237 public:
0238
0239
0240 IAuditorSvc* auditorSvc() const;
0241
0242
0243
0244
0245
0246
0247
0248
0249
0250
0251 inline IMonitorSvc* monitorSvc() const {
0252
0253 if ( !m_pMonitorSvc ) m_pMonitorSvc = service( m_monitorSvcName, false, true );
0254 return m_pMonitorSvc.get();
0255 }
0256
0257
0258
0259
0260
0261
0262 template <class T>
0263 void declareInfo( const std::string& name, const T& var, const std::string& desc ) const {
0264 IMonitorSvc* mS = monitorSvc();
0265 if ( mS ) mS->declareInfo( name, var, desc, this );
0266 }
0267
0268
0269
0270
0271
0272
0273
0274
0275 void declareInfo( const std::string& name, const std::string& format, const void* var, int size,
0276 const std::string& desc ) const {
0277 IMonitorSvc* mS = monitorSvc();
0278 if ( mS ) mS->declareInfo( name, format, var, size, desc, this );
0279 }
0280
0281
0282 ~AlgTool() override;
0283
0284 private:
0285 typedef std::list<std::pair<InterfaceID, void*>> InterfaceList;
0286
0287 std::string m_type;
0288 const std::string m_name;
0289 const IInterface* m_parent = nullptr;
0290
0291 mutable SmartIF<ISvcLocator> m_svcLocator;
0292 mutable SmartIF<IDataProviderSvc> m_evtSvc;
0293 mutable SmartIF<IToolSvc> m_ptoolSvc;
0294 mutable SmartIF<IMonitorSvc> m_pMonitorSvc;
0295 mutable SmartIF<IAuditorSvc> m_pAuditorSvc;
0296
0297 InterfaceList m_interfaceList;
0298
0299
0300
0301 Gaudi::Property<int> m_outputLevel{
0302 this, "OutputLevel", setUpMessaging(),
0303 [this]( Gaudi::Details::PropertyBase& ) { this->updateMsgStreamOutputLevel( this->m_outputLevel ); },
0304 "output level" };
0305
0306 Gaudi::Property<std::string> m_monitorSvcName{ this, "MonitorService", "MonitorSvc",
0307 "name to use for Monitor Service" };
0308
0309 Gaudi::Property<bool> m_auditInit{ this, "AuditTools", false, "[[deprecated]] unused" };
0310 Gaudi::Property<bool> m_auditorInitialize{ this, "AuditInitialize", false, "trigger auditor on initialize()" };
0311 Gaudi::Property<bool> m_auditorStart{ this, "AuditStart", false, "trigger auditor on start()" };
0312 Gaudi::Property<bool> m_auditorStop{ this, "AuditStop", false, "trigger auditor on stop()" };
0313 Gaudi::Property<bool> m_auditorFinalize{ this, "AuditFinalize", false, "trigger auditor on finalize()" };
0314 Gaudi::Property<bool> m_auditorReinitialize{ this, "AuditReinitialize", false, "trigger auditor on reinitialize()" };
0315 Gaudi::Property<bool> m_auditorRestart{ this, "AuditRestart", false, "trigger auditor on restart()" };
0316
0317
0318 mutable std::vector<IAlgTool*> m_tools;
0319 mutable std::vector<BaseToolHandle*> m_toolHandles;
0320 mutable std::vector<GaudiHandleArrayBase*> m_toolHandleArrays;
0321 mutable bool m_toolHandlesInit = false;
0322
0323
0324 StatusCode service_i( std::string_view algName, bool createIf, const InterfaceID& iid, void** ppSvc ) const;
0325 StatusCode service_i( std::string_view svcType, std::string_view svcName, const InterfaceID& iid, void** ppS ) const;
0326
0327 Gaudi::StateMachine::State m_state = Gaudi::StateMachine::CONFIGURED;
0328 Gaudi::StateMachine::State m_targetState = Gaudi::StateMachine::CONFIGURED;
0329 };
0330
0331 #endif