Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-11-03 09:47:18

0001 /***********************************************************************************\
0002 * (c) Copyright 1998-2024 CERN for the benefit of the LHCb and ATLAS collaborations *
0003 *                                                                                   *
0004 * This software is distributed under the terms of the Apache version 2 licence,     *
0005 * copied verbatim in the file "LICENSE".                                            *
0006 *                                                                                   *
0007 * In applying this licence, CERN does not waive the privileges and immunities       *
0008 * granted to it by virtue of its status as an Intergovernmental Organization        *
0009 * or submit itself to any jurisdiction.                                             *
0010 \***********************************************************************************/
0011 #ifndef GAUDIKERNEL_SERVICE_H
0012 #define GAUDIKERNEL_SERVICE_H
0013 // ============================================================================
0014 // Include files
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 // Forward declarations
0034 // ============================================================================
0035 class IMessageSvc;
0036 class ISvcManager;
0037 class ServiceManager;
0038 // ============================================================================
0039 /** @class Service GaudiKernel/Service.h
0040  *
0041  *  Base class for all services. It implements the IService and IProperty interfaces.
0042  *
0043  *  @author Pere Mato
0044  *  @author Marco Clemencic
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   /** Retrieve name of the service               */
0053   const std::string& name() const override;
0054 
0055   // State machine implementation
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   /** Initialize Service                          */
0068   StatusCode sysInitialize() override;
0069   /** Initialize Service                          */
0070   StatusCode sysStart() override;
0071   /** Initialize Service                          */
0072   StatusCode sysStop() override;
0073   /** Finalize Service                           */
0074   StatusCode sysFinalize() override;
0075   /// Re-initialize the Service
0076   StatusCode sysReinitialize() override;
0077   /// Re-initialize the Service
0078   StatusCode sysRestart() override;
0079 
0080   /** Standard Constructor                       */
0081   Service( std::string name, ISvcLocator* svcloc );
0082   /** Retrieve pointer to service locator        */
0083   SmartIF<ISvcLocator>& serviceLocator() const override;
0084 
0085   /** Access a service by name, creating it if it doesn't already exist.
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, // quiet
0093                                 createIf );
0094     if ( ptr ) {
0095       psvc = ptr.get();
0096       const_cast<T*>( psvc )->addRef();
0097       return StatusCode::SUCCESS;
0098     }
0099     // else
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, // quiet
0120                          createIf );
0121   }
0122 
0123   /** Access a service by name and type, creating it if it doesn't already exist.
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   // Tool handling
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   /** Declare used tool
0149    *
0150    *  @param handle ToolHandle<T>
0151    *  @param toolTypeAndName
0152    *  @param parent, default public tool
0153    *  @param create if necessary, default true
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   // declare ToolHandleArrays to the AlgTool
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   // place IAlgTools defined via ToolHandles in m_tools
0190   void initToolHandles() const;
0191 
0192 public:
0193   // ==========================================================================
0194   /** The standard auditor service.May not be invoked before sysInitialize()
0195    *  has been invoked.
0196    */
0197   SmartIF<IAuditorSvc>& auditorSvc() const;
0198 
0199 protected:
0200   /** Standard Destructor                        */
0201   ~Service() override;
0202   /** Service state                              */
0203   Gaudi::StateMachine::State m_state = Gaudi::StateMachine::OFFLINE;
0204   /** Service state                              */
0205   Gaudi::StateMachine::State m_targetState = Gaudi::StateMachine::OFFLINE;
0206 
0207   /// get the @c Service's output level
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   /** Service Name  */
0216   std::string m_name;
0217   /** Service Locator reference                  */
0218   mutable SmartIF<ISvcLocator> m_svcLocator;
0219   SmartIF<ISvcManager>         m_svcManager;
0220 
0221   void setServiceManager( ISvcManager* ism ) override;
0222 
0223   // AlgTools used by Service
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; /// flag indicating whether ToolHandle tools have been added to m_tools
0228 
0229 protected:
0230   // Properties
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   /** Auditor Service                            */
0247   mutable SmartIF<IAuditorSvc> m_pAuditorSvc;
0248 };
0249 
0250 #endif // GAUDIKERNEL_SERVICE_H