File indexing completed on 2025-11-03 09:47:18
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 <Gaudi/PluginService.h>
0017 #include <Gaudi/Property.h>
0018 #include <GaudiKernel/CommonMessaging.h>
0019 #include <GaudiKernel/IAuditorSvc.h>
0020 #include <GaudiKernel/IProperty.h>
0021 #include <GaudiKernel/IService.h>
0022 #include <GaudiKernel/IStateful.h>
0023 #include <GaudiKernel/ISvcLocator.h>
0024 #include <GaudiKernel/PropertyHolder.h>
0025 #include <GaudiKernel/ServiceLocatorHelper.h>
0026 #include <GaudiKernel/SmartIF.h>
0027 #include <GaudiKernel/ToolHandle.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   [[deprecated( "use service<T>(name, createIf) -> SmartIF<T>" )]] StatusCode
0089   service( const std::string& name, const T*& psvc, bool createIf = true ) const {
0090     ISvcLocator& svcLoc = *serviceLocator();
0091     auto         ptr    = ServiceLocatorHelper( svcLoc, *this )
0092                    .service<T>( name, !createIf, 
0093                                 createIf );
0094     if ( ptr ) {
0095       psvc = ptr.get();
0096       const_cast<T*>( psvc )->addRef();
0097       return StatusCode::SUCCESS;
0098     }
0099     
0100     psvc = nullptr;
0101     return StatusCode::FAILURE;
0102   }
0103 
0104   template <class T>
0105   [[deprecated( "use service<T>(name, createIf) -> SmartIF<T>" )]] StatusCode
0106   service( const std::string& name, T*& psvc, bool createIf = true ) const {
0107     auto ptr = service<T>( name, createIf );
0108     psvc     = ( ptr ? ptr.get() : nullptr );
0109     if ( psvc ) {
0110       psvc->addRef();
0111       return StatusCode::SUCCESS;
0112     }
0113     return StatusCode::FAILURE;
0114   }
0115 
0116   template <typename IFace = IService>
0117   SmartIF<IFace> service( const std::string& name, bool createIf = true ) const {
0118     return ServiceLocatorHelper( *serviceLocator(), *this )
0119         .service<IFace>( name, !createIf, 
0120                          createIf );
0121   }
0122 
0123   
0124 
0125   template <class T>
0126   [[deprecated( "use service<T>(name, createIf) -> SmartIF<T>" )]] StatusCode
0127   service( const std::string& svcType, const std::string& svcName, T*& psvc ) const {
0128     return service( svcType + "/" + svcName, psvc );
0129   }
0130 
0131   
0132   
0133 
0134   using PropertyHolderImpl::declareProperty;
0135 
0136   template <class T>
0137   Gaudi::Details::PropertyBase* declareProperty( const std::string& name, ToolHandle<T>& hndl,
0138                                                  const std::string& doc = "none" ) {
0139     this->declareTool( hndl, hndl.typeAndName() ).ignore();
0140     return PropertyHolderImpl::declareProperty( name, hndl, doc );
0141   }
0142 
0143   template <class T>
0144   StatusCode declareTool( ToolHandle<T>& handle, bool createIf = true ) {
0145     return this->declareTool( handle, handle.typeAndName(), createIf );
0146   }
0147 
0148   
0149 
0150 
0151 
0152 
0153 
0154 
0155   template <class T>
0156   StatusCode declareTool( ToolHandle<T>& handle, const std::string& toolTypeAndName, bool createIf = true ) {
0157 
0158     StatusCode sc = handle.initialize( toolTypeAndName, handle.isPublic() ? nullptr : this, createIf );
0159     if ( !sc ) {
0160       throw GaudiException{ std::string{ "Cannot create handle for " } + ( handle.isPublic() ? "public" : "private" ) +
0161                                 " tool " + toolTypeAndName,
0162                             name(), sc };
0163     }
0164 
0165     m_toolHandles.push_back( &handle );
0166 
0167     return sc;
0168   }
0169 
0170   
0171   template <class T>
0172   Gaudi::Details::PropertyBase* declareProperty( const std::string& name, ToolHandleArray<T>& hndlArr,
0173                                                  const std::string& doc = "none" ) {
0174     addToolsArray( hndlArr );
0175     return PropertyHolderImpl::declareProperty( name, hndlArr, doc );
0176   }
0177 
0178   template <class T>
0179   void addToolsArray( ToolHandleArray<T>& hndlArr ) {
0180     m_toolHandleArrays.push_back( &hndlArr );
0181   }
0182 
0183   const std::vector<IAlgTool*>& tools() const;
0184 
0185 protected:
0186   std::vector<IAlgTool*>& tools();
0187 
0188 private:
0189   
0190   void initToolHandles() const;
0191 
0192 public:
0193   
0194   
0195 
0196 
0197   SmartIF<IAuditorSvc>& auditorSvc() const;
0198 
0199 protected:
0200   
0201   ~Service() override;
0202   
0203   Gaudi::StateMachine::State m_state = Gaudi::StateMachine::OFFLINE;
0204   
0205   Gaudi::StateMachine::State m_targetState = Gaudi::StateMachine::OFFLINE;
0206 
0207   
0208   int outputLevel() const { return m_outputLevel.value(); }
0209 
0210 private:
0211   void           sysInitialize_imp();
0212   StatusCode     m_initSC;
0213   std::once_flag m_initFlag;
0214 
0215   
0216   std::string m_name;
0217   
0218   mutable SmartIF<ISvcLocator> m_svcLocator;
0219   SmartIF<ISvcManager>         m_svcManager;
0220 
0221   void setServiceManager( ISvcManager* ism ) override;
0222 
0223   
0224   mutable std::vector<IAlgTool*>             m_tools;
0225   mutable std::vector<BaseToolHandle*>       m_toolHandles;
0226   mutable std::vector<GaudiHandleArrayBase*> m_toolHandleArrays;
0227   mutable bool m_toolHandlesInit = false; 
0228 
0229 protected:
0230   
0231 
0232   Gaudi::Property<int>  m_outputLevel{ this, "OutputLevel", MSG::NIL, "output level" };
0233   Gaudi::Property<bool> m_auditInit{ this, "AuditServices", false, "[[deprecated]] unused" };
0234   Gaudi::Property<bool> m_auditorInitialize{ this, "AuditInitialize", false, "trigger auditor on initialize()" };
0235   Gaudi::Property<bool> m_auditorStart{ this, "AuditStart", false, "trigger auditor on start()" };
0236   Gaudi::Property<bool> m_auditorStop{ this, "AuditStop", false, "trigger auditor on stop()" };
0237   Gaudi::Property<bool> m_auditorFinalize{ this, "AuditFinalize", false, "trigger auditor on finalize()" };
0238   Gaudi::Property<bool> m_auditorReinitialize{ this, "AuditReinitialize", false, "trigger auditor on reinitialize()" };
0239   Gaudi::Property<bool> m_auditorRestart{ this, "AuditRestart", false, "trigger auditor on restart()" };
0240 
0241   Gaudi::Property<bool> m_autoRetrieveTools{ this, "AutoRetrieveTools", true,
0242                                              "retrieve all AlgTools during initialize" };
0243   Gaudi::Property<bool> m_checkToolDeps{ this, "CheckToolDeps", true,
0244                                          "check data dependencies of AlgTools (error if any found)" };
0245 
0246   
0247   mutable SmartIF<IAuditorSvc> m_pAuditorSvc;
0248 };
0249 
0250 #endif