The Gaudi Framework  master (37c0b60a)
Service.h
Go to the documentation of this file.
1 /***********************************************************************************\
2 * (c) Copyright 1998-2024 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_SERVICE_H
12 #define GAUDIKERNEL_SERVICE_H
13 // ============================================================================
14 // Include files
15 // ============================================================================
16 #include <Gaudi/PluginService.h>
17 #include <Gaudi/Property.h>
20 #include <GaudiKernel/IProperty.h>
21 #include <GaudiKernel/IService.h>
22 #include <GaudiKernel/IStateful.h>
26 #include <GaudiKernel/SmartIF.h>
27 #include <GaudiKernel/ToolHandle.h>
28 
29 // ============================================================================
30 #include <mutex>
31 #include <vector>
32 // ============================================================================
33 // Forward declarations
34 // ============================================================================
35 class IMessageSvc;
36 class ISvcManager;
37 class ServiceManager;
38 // ============================================================================
46 class GAUDI_API Service : public PropertyHolder<CommonMessaging<implements<IService, IProperty, IStateful>>> {
47 public:
48  using Factory = Gaudi::PluginService::Factory<IService*( const std::string&, ISvcLocator* )>;
49 
50  friend class ServiceManager;
51 
53  const std::string& name() const override;
54 
55  // State machine implementation
56  StatusCode configure() override { return StatusCode::SUCCESS; }
57  StatusCode initialize() override;
58  StatusCode start() override;
59  StatusCode stop() override;
60  StatusCode finalize() override;
61  StatusCode terminate() override { return StatusCode::SUCCESS; }
62  Gaudi::StateMachine::State FSMState() const override { return m_state; }
63  Gaudi::StateMachine::State targetFSMState() const override { return m_targetState; }
64  StatusCode reinitialize() override;
65  StatusCode restart() override;
66 
68  StatusCode sysInitialize() override;
70  StatusCode sysStart() override;
72  StatusCode sysStop() override;
74  StatusCode sysFinalize() override;
76  StatusCode sysReinitialize() override;
78  StatusCode sysRestart() override;
79 
81  Service( std::string name, ISvcLocator* svcloc );
83  SmartIF<ISvcLocator>& serviceLocator() const override;
84 
87  template <class T>
88  [[deprecated( "use service<T>(name, createIf) -> SmartIF<T>" )]] StatusCode
89  service( const std::string& name, const T*& psvc, bool createIf = true ) const {
90  ISvcLocator& svcLoc = *serviceLocator();
91  auto ptr = ServiceLocatorHelper( svcLoc, *this )
92  .service<T>( name, !createIf, // quiet
93  createIf );
94  if ( ptr ) {
95  psvc = ptr.get();
96  const_cast<T*>( psvc )->addRef();
97  return StatusCode::SUCCESS;
98  }
99  // else
100  psvc = nullptr;
101  return StatusCode::FAILURE;
102  }
103 
104  template <class T>
105  [[deprecated( "use service<T>(name, createIf) -> SmartIF<T>" )]] StatusCode
106  service( const std::string& name, T*& psvc, bool createIf = true ) const {
107  auto ptr = service<T>( name, createIf );
108  psvc = ( ptr ? ptr.get() : nullptr );
109  if ( psvc ) {
110  psvc->addRef();
111  return StatusCode::SUCCESS;
112  }
113  return StatusCode::FAILURE;
114  }
115 
116  template <typename IFace = IService>
117  SmartIF<IFace> service( const std::string& name, bool createIf = true ) const {
118  return ServiceLocatorHelper( *serviceLocator(), *this )
119  .service<IFace>( name, !createIf, // quiet
120  createIf );
121  }
122 
125  template <class T>
126  [[deprecated( "use service<T>(name, createIf) -> SmartIF<T>" )]] StatusCode
127  service( const std::string& svcType, const std::string& svcName, T*& psvc ) const {
128  return service( svcType + "/" + svcName, psvc );
129  }
130 
131  // ==========================================================================
132  // Tool handling
133 
135 
136  template <class T>
138  const std::string& doc = "none" ) {
139  this->declareTool( hndl, hndl.typeAndName() ).ignore();
140  return PropertyHolderImpl::declareProperty( name, hndl, doc );
141  }
142 
143  template <class T>
144  StatusCode declareTool( ToolHandle<T>& handle, bool createIf = true ) {
145  return this->declareTool( handle, handle.typeAndName(), createIf );
146  }
147 
155  template <class T>
156  StatusCode declareTool( ToolHandle<T>& handle, const std::string& toolTypeAndName, bool createIf = true ) {
157 
158  StatusCode sc = handle.initialize( toolTypeAndName, handle.isPublic() ? nullptr : this, createIf );
159  if ( !sc ) {
160  throw GaudiException{ std::string{ "Cannot create handle for " } + ( handle.isPublic() ? "public" : "private" ) +
161  " tool " + toolTypeAndName,
162  name(), sc };
163  }
164 
165  m_toolHandles.push_back( &handle );
166 
167  return sc;
168  }
169 
170  // declare ToolHandleArrays to the AlgTool
171  template <class T>
173  const std::string& doc = "none" ) {
174  addToolsArray( hndlArr );
175  return PropertyHolderImpl::declareProperty( name, hndlArr, doc );
176  }
177 
178  template <class T>
180  m_toolHandleArrays.push_back( &hndlArr );
181  }
182 
183  const std::vector<IAlgTool*>& tools() const;
184 
185 protected:
186  std::vector<IAlgTool*>& tools();
187 
188 private:
189  // place IAlgTools defined via ToolHandles in m_tools
190  void initToolHandles() const;
191 
192 public:
193  // ==========================================================================
197  SmartIF<IAuditorSvc>& auditorSvc() const;
198 
199 protected:
201  ~Service() override;
206 
208  int outputLevel() const { return m_outputLevel.value(); }
209 
210 private:
211  void sysInitialize_imp();
214 
220 
221  void setServiceManager( ISvcManager* ism ) override;
222 
223  // AlgTools used by Service
227  mutable bool m_toolHandlesInit = false;
228 
229 protected:
230  // Properties
231 
232  Gaudi::Property<int> m_outputLevel{ this, "OutputLevel", MSG::NIL, "output level" };
233  Gaudi::Property<bool> m_auditInit{ this, "AuditServices", false, "[[deprecated]] unused" };
234  Gaudi::Property<bool> m_auditorInitialize{ this, "AuditInitialize", false, "trigger auditor on initialize()" };
235  Gaudi::Property<bool> m_auditorStart{ this, "AuditStart", false, "trigger auditor on start()" };
236  Gaudi::Property<bool> m_auditorStop{ this, "AuditStop", false, "trigger auditor on stop()" };
237  Gaudi::Property<bool> m_auditorFinalize{ this, "AuditFinalize", false, "trigger auditor on finalize()" };
238  Gaudi::Property<bool> m_auditorReinitialize{ this, "AuditReinitialize", false, "trigger auditor on reinitialize()" };
239  Gaudi::Property<bool> m_auditorRestart{ this, "AuditRestart", false, "trigger auditor on restart()" };
240 
241  Gaudi::Property<bool> m_autoRetrieveTools{ this, "AutoRetrieveTools", true,
242  "retrieve all AlgTools during initialize" };
243  Gaudi::Property<bool> m_checkToolDeps{ this, "CheckToolDeps", true,
244  "check data dependencies of AlgTools (error if any found)" };
245 
248 };
249 
250 #endif // GAUDIKERNEL_SERVICE_H
ServiceLocatorHelper::service
SmartIF< IService > service(std::string_view name, const bool quiet=false, const bool createIf=true) const
Definition: ServiceLocatorHelper.cpp:50
IService
Definition: IService.h:28
Gaudi::Details::PropertyBase
PropertyBase base class allowing PropertyBase* collections to be "homogeneous".
Definition: PropertyBase.h:35
Service::m_pAuditorSvc
SmartIF< IAuditorSvc > m_pAuditorSvc
Auditor Service
Definition: Service.h:247
Service::targetFSMState
Gaudi::StateMachine::State targetFSMState() const override
Definition: Service.h:63
Service::m_toolHandleArrays
std::vector< GaudiHandleArrayBase * > m_toolHandleArrays
Definition: Service.h:226
Service::declareProperty
Gaudi::Details::PropertyBase * declareProperty(const std::string &name, ToolHandleArray< T > &hndlArr, const std::string &doc="none")
Definition: Service.h:172
IService.h
std::string
STL class.
IMessageSvc
Definition: IMessageSvc.h:47
Service::m_initSC
StatusCode m_initSC
Definition: Service.h:212
Service::addToolsArray
void addToolsArray(ToolHandleArray< T > &hndlArr)
Definition: Service.h:179
std::vector< IAlgTool * >
ISvcLocator
Definition: ISvcLocator.h:46
GaudiException
Definition: GaudiException.h:31
ServiceManager
Definition: ServiceManager.h:46
Service::m_svcLocator
SmartIF< ISvcLocator > m_svcLocator
Service Locator reference
Definition: Service.h:218
Service::service
SmartIF< IFace > service(const std::string &name, bool createIf=true) const
Definition: Service.h:117
Service::declareProperty
Gaudi::Details::PropertyBase * declareProperty(const std::string &name, ToolHandle< T > &hndl, const std::string &doc="none")
Definition: Service.h:137
PropertyHolder
Helper class to implement the IProperty interface.
Definition: PropertyHolder.h:82
ServiceManager::reinitialize
StatusCode reinitialize() override
Initialization (from INITIALIZED or RUNNING to INITIALIZED, via CONFIGURED).
Definition: ServiceManager.cpp:368
IOTest.start
start
Definition: IOTest.py:110
ISvcManager
Definition: ISvcManager.h:38
IAuditorSvc.h
Service::FSMState
Gaudi::StateMachine::State FSMState() const override
Definition: Service.h:62
Service
Definition: Service.h:46
Service::m_name
std::string m_name
Service Name
Definition: Service.h:216
std::once_flag
Service::m_initFlag
std::once_flag m_initFlag
Definition: Service.h:213
Gaudi::StateMachine::State
State
Allowed states for classes implementing the state machine (ApplicationMgr, Algorithm,...
Definition: StateMachine.h:22
SmartIF.h
PluginService.h
ToolHandle
Definition: ToolHandle.h:132
StatusCode
Definition: StatusCode.h:65
ServiceManager::initialize
StatusCode initialize() override
Initialization (from CONFIGURED to INITIALIZED).
Definition: ServiceManager.cpp:267
Service::m_tools
std::vector< IAlgTool * > m_tools
Definition: Service.h:224
Service::configure
StatusCode configure() override
Definition: Service.h:56
Gaudi::StateMachine::OFFLINE
@ OFFLINE
Definition: StateMachine.h:23
ToolHandleArray
Definition: ToolHandle.h:399
ServiceManager::service
SmartIF< IService > & service(const Gaudi::Utils::TypeNameString &typeName, const bool createIf=true) override
Returns a smart pointer to a service.
Definition: ServiceManager.cpp:178
ServiceLocatorHelper
an helper to share the implementation of service() among the various kernel base classes
Definition: ServiceLocatorHelper.h:27
ServiceManager::finalize
StatusCode finalize() override
Finalize (from INITIALIZED to CONFIGURED).
Definition: ServiceManager.cpp:410
GaudiPython.Bindings.nullptr
nullptr
Definition: Bindings.py:87
IStateful.h
Service::service
StatusCode service(const std::string &svcType, const std::string &svcName, T *&psvc) const
Access a service by name and type, creating it if it doesn't already exist.
Definition: Service.h:127
Service::declareTool
StatusCode declareTool(ToolHandle< T > &handle, const std::string &toolTypeAndName, bool createIf=true)
Declare used tool.
Definition: Service.h:156
SmartIF< ISvcLocator >
CommonMessaging.h
ToolHandleInfo::isPublic
bool isPublic() const noexcept
Definition: ToolHandle.h:51
PropertyHolder::declareProperty
Gaudi::Details::PropertyBase & declareProperty(Gaudi::Details::PropertyBase &prop)
Declare a property.
Definition: PropertyHolder.h:106
StatusCode::SUCCESS
constexpr static const auto SUCCESS
Definition: StatusCode.h:100
ConditionsStallTest.name
name
Definition: ConditionsStallTest.py:77
SmartIF::get
TYPE * get() const
Get interface pointer.
Definition: SmartIF.h:86
ServiceLocatorHelper.h
ToolHandle.h
MSG::NIL
@ NIL
Definition: IMessageSvc.h:25
Service::outputLevel
int outputLevel() const
get the Service's output level
Definition: Service.h:208
Service::m_svcManager
SmartIF< ISvcManager > m_svcManager
Definition: Service.h:219
Service::Factory
Gaudi::PluginService::Factory< IService *(const std::string &, ISvcLocator *)> Factory
Definition: Service.h:48
ServiceManager::restart
StatusCode restart() override
Initialization (from RUNNING to RUNNING, via INITIALIZED).
Definition: ServiceManager.cpp:389
ToolHandle::initialize
StatusCode initialize(const std::string &toolTypeAndName, const IInterface *parent=nullptr, bool createIf=true)
Definition: ToolHandle.h:213
ServiceManager::serviceLocator
SmartIF< ISvcLocator > & serviceLocator() const override
Function needed by CommonMessaging.
Definition: ServiceManager.h:66
Service::service
StatusCode service(const std::string &name, T *&psvc, bool createIf=true) const
Definition: Service.h:106
IProperty.h
ToolHandle::typeAndName
std::string typeAndName() const override
Definition: ToolHandle.h:273
Service::declareTool
StatusCode declareTool(ToolHandle< T > &handle, bool createIf=true)
Definition: Service.h:144
StatusCode::FAILURE
constexpr static const auto FAILURE
Definition: StatusCode.h:101
ISvcLocator.h
ServiceManager::stop
StatusCode stop() override
Stop (from RUNNING to INITIALIZED).
Definition: ServiceManager.cpp:334
PropertyHolder.h
Service::service
StatusCode service(const std::string &name, const T *&psvc, bool createIf=true) const
Access a service by name, creating it if it doesn't already exist.
Definition: Service.h:89
GAUDI_API
#define GAUDI_API
Definition: Kernel.h:81
Gaudi::Property< int >
Property.h
Service::m_toolHandles
std::vector< BaseToolHandle * > m_toolHandles
Definition: Service.h:225
Service::terminate
StatusCode terminate() override
Definition: Service.h:61