File indexing completed on 2025-01-18 09:57:42
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011 #ifndef GAUDIKERNEL_SERVICE_H
0012 #define GAUDIKERNEL_SERVICE_H
0013
0014
0015
0016 #include "GaudiKernel/CommonMessaging.h"
0017 #include "GaudiKernel/IAuditorSvc.h"
0018 #include "GaudiKernel/IProperty.h"
0019 #include "GaudiKernel/IService.h"
0020 #include "GaudiKernel/IStateful.h"
0021 #include "GaudiKernel/ISvcLocator.h"
0022 #include "GaudiKernel/PropertyHolder.h"
0023 #include "GaudiKernel/ServiceLocatorHelper.h"
0024 #include "GaudiKernel/SmartIF.h"
0025 #include "GaudiKernel/ToolHandle.h"
0026 #include <Gaudi/PluginService.h>
0027 #include <Gaudi/Property.h>
0028
0029
0030 #include <mutex>
0031 #include <vector>
0032
0033
0034
0035 class IMessageSvc;
0036 class ISvcManager;
0037 class ServiceManager;
0038
0039
0040
0041
0042
0043
0044
0045
0046 class GAUDI_API Service : public PropertyHolder<CommonMessaging<implements<IService, IProperty, IStateful>>> {
0047 public:
0048 using Factory = Gaudi::PluginService::Factory<IService*( const std::string&, ISvcLocator* )>;
0049
0050 friend class ServiceManager;
0051
0052
0053 const std::string& name() const override;
0054
0055
0056 StatusCode configure() override { return StatusCode::SUCCESS; }
0057 StatusCode initialize() override;
0058 StatusCode start() override;
0059 StatusCode stop() override;
0060 StatusCode finalize() override;
0061 StatusCode terminate() override { return StatusCode::SUCCESS; }
0062 Gaudi::StateMachine::State FSMState() const override { return m_state; }
0063 Gaudi::StateMachine::State targetFSMState() const override { return m_targetState; }
0064 StatusCode reinitialize() override;
0065 StatusCode restart() override;
0066
0067
0068 StatusCode sysInitialize() override;
0069
0070 StatusCode sysStart() override;
0071
0072 StatusCode sysStop() override;
0073
0074 StatusCode sysFinalize() override;
0075
0076 StatusCode sysReinitialize() override;
0077
0078 StatusCode sysRestart() override;
0079
0080
0081 Service( std::string name, ISvcLocator* svcloc );
0082
0083 SmartIF<ISvcLocator>& serviceLocator() const override;
0084
0085
0086
0087 template <class T>
0088 StatusCode service( const std::string& name, const T*& psvc, bool createIf = true ) const {
0089 ISvcLocator& svcLoc = *serviceLocator();
0090 auto ptr = ServiceLocatorHelper( svcLoc, *this )
0091 .service<T>( name, !createIf,
0092 createIf );
0093 if ( ptr ) {
0094 psvc = ptr.get();
0095 const_cast<T*>( psvc )->addRef();
0096 return StatusCode::SUCCESS;
0097 }
0098
0099 psvc = nullptr;
0100 return StatusCode::FAILURE;
0101 }
0102
0103 template <class T>
0104 StatusCode service( const std::string& name, T*& psvc, bool createIf = true ) const {
0105 auto ptr = service<T>( name, createIf );
0106 psvc = ( ptr ? ptr.get() : nullptr );
0107 if ( psvc ) {
0108 psvc->addRef();
0109 return StatusCode::SUCCESS;
0110 }
0111 return StatusCode::FAILURE;
0112 }
0113
0114 template <typename IFace = IService>
0115 SmartIF<IFace> service( const std::string& name, bool createIf = true ) const {
0116 return ServiceLocatorHelper( *serviceLocator(), *this )
0117 .service<IFace>( name, !createIf,
0118 createIf );
0119 }
0120
0121
0122
0123 template <class T>
0124 StatusCode service( const std::string& svcType, const std::string& svcName, T*& psvc ) const {
0125 return service( svcType + "/" + svcName, psvc );
0126 }
0127
0128
0129
0130
0131 using PropertyHolderImpl::declareProperty;
0132
0133 template <class T>
0134 Gaudi::Details::PropertyBase* declareProperty( const std::string& name, ToolHandle<T>& hndl,
0135 const std::string& doc = "none" ) {
0136 this->declareTool( hndl, hndl.typeAndName() ).ignore();
0137 return PropertyHolderImpl::declareProperty( name, hndl, doc );
0138 }
0139
0140 template <class T>
0141 StatusCode declareTool( ToolHandle<T>& handle, bool createIf = true ) {
0142 return this->declareTool( handle, handle.typeAndName(), createIf );
0143 }
0144
0145
0146
0147
0148
0149
0150
0151
0152 template <class T>
0153 StatusCode declareTool( ToolHandle<T>& handle, std::string toolTypeAndName, bool createIf = true ) {
0154
0155 StatusCode sc = handle.initialize( toolTypeAndName, handle.isPublic() ? nullptr : this, createIf );
0156 if ( !sc ) {
0157 throw GaudiException{ std::string{ "Cannot create handle for " } + ( handle.isPublic() ? "public" : "private" ) +
0158 " tool " + toolTypeAndName,
0159 name(), sc };
0160 }
0161
0162 m_toolHandles.push_back( &handle );
0163
0164 return sc;
0165 }
0166
0167
0168 template <class T>
0169 Gaudi::Details::PropertyBase* declareProperty( const std::string& name, ToolHandleArray<T>& hndlArr,
0170 const std::string& doc = "none" ) {
0171 addToolsArray( hndlArr );
0172 return PropertyHolderImpl::declareProperty( name, hndlArr, doc );
0173 }
0174
0175 template <class T>
0176 void addToolsArray( ToolHandleArray<T>& hndlArr ) {
0177 m_toolHandleArrays.push_back( &hndlArr );
0178 }
0179
0180 const std::vector<IAlgTool*>& tools() const;
0181
0182 protected:
0183 std::vector<IAlgTool*>& tools();
0184
0185 private:
0186
0187 void initToolHandles() const;
0188
0189 public:
0190
0191
0192
0193
0194 SmartIF<IAuditorSvc>& auditorSvc() const;
0195
0196 protected:
0197
0198 ~Service() override;
0199
0200 Gaudi::StateMachine::State m_state = Gaudi::StateMachine::OFFLINE;
0201
0202 Gaudi::StateMachine::State m_targetState = Gaudi::StateMachine::OFFLINE;
0203
0204
0205 int outputLevel() const { return m_outputLevel.value(); }
0206
0207 private:
0208 void sysInitialize_imp();
0209 StatusCode m_initSC;
0210 std::once_flag m_initFlag;
0211
0212
0213 std::string m_name;
0214
0215 mutable SmartIF<ISvcLocator> m_svcLocator;
0216 SmartIF<ISvcManager> m_svcManager;
0217
0218 void setServiceManager( ISvcManager* ism ) override;
0219
0220
0221 mutable std::vector<IAlgTool*> m_tools;
0222 mutable std::vector<BaseToolHandle*> m_toolHandles;
0223 mutable std::vector<GaudiHandleArrayBase*> m_toolHandleArrays;
0224 mutable bool m_toolHandlesInit = false;
0225
0226 protected:
0227
0228
0229 Gaudi::Property<int> m_outputLevel{ this, "OutputLevel", MSG::NIL, "output level" };
0230 Gaudi::Property<bool> m_auditInit{ this, "AuditServices", false, "[[deprecated]] unused" };
0231 Gaudi::Property<bool> m_auditorInitialize{ this, "AuditInitialize", false, "trigger auditor on initialize()" };
0232 Gaudi::Property<bool> m_auditorStart{ this, "AuditStart", false, "trigger auditor on start()" };
0233 Gaudi::Property<bool> m_auditorStop{ this, "AuditStop", false, "trigger auditor on stop()" };
0234 Gaudi::Property<bool> m_auditorFinalize{ this, "AuditFinalize", false, "trigger auditor on finalize()" };
0235 Gaudi::Property<bool> m_auditorReinitialize{ this, "AuditReinitialize", false, "trigger auditor on reinitialize()" };
0236 Gaudi::Property<bool> m_auditorRestart{ this, "AuditRestart", false, "trigger auditor on restart()" };
0237
0238 Gaudi::Property<bool> m_autoRetrieveTools{ this, "AutoRetrieveTools", true,
0239 "retrieve all AlgTools during initialize" };
0240 Gaudi::Property<bool> m_checkToolDeps{ this, "CheckToolDeps", true,
0241 "check data dependencies of AlgTools (error if any found)" };
0242
0243
0244 mutable SmartIF<IAuditorSvc> m_pAuditorSvc;
0245 };
0246
0247 #endif