Loading [MathJax]/extensions/tex2jax.js
The Gaudi Framework  v38r0 (2143aa4c)
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
Service.h
Go to the documentation of this file.
1 /***********************************************************************************\
2 * (c) Copyright 1998-2022 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 // ============================================================================
18 #include "GaudiKernel/IProperty.h"
19 #include "GaudiKernel/IService.h"
20 #include "GaudiKernel/IStateful.h"
24 #include "GaudiKernel/SmartIF.h"
25 #include "GaudiKernel/ToolHandle.h"
26 #include <Gaudi/PluginService.h>
27 #include <Gaudi/Property.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  StatusCode service( const std::string& name, const T*& psvc, bool createIf = true ) const {
89  ISvcLocator& svcLoc = *serviceLocator();
90  auto ptr = ServiceLocatorHelper( svcLoc, *this )
91  .service<T>( name, !createIf, // quiet
92  createIf );
93  if ( ptr ) {
94  psvc = ptr.get();
95  const_cast<T*>( psvc )->addRef();
96  return StatusCode::SUCCESS;
97  }
98  // else
99  psvc = nullptr;
100  return StatusCode::FAILURE;
101  }
102 
103  template <class T>
104  StatusCode service( const std::string& name, T*& psvc, bool createIf = true ) const {
105  auto ptr = service<T>( name, createIf );
106  psvc = ( ptr ? ptr.get() : nullptr );
107  if ( psvc ) {
108  psvc->addRef();
109  return StatusCode::SUCCESS;
110  }
111  return StatusCode::FAILURE;
112  }
113 
114  template <typename IFace = IService>
115  SmartIF<IFace> service( const std::string& name, bool createIf = true ) const {
116  return ServiceLocatorHelper( *serviceLocator(), *this )
117  .service<IFace>( name, !createIf, // quiet
118  createIf );
119  }
120 
123  template <class T>
124  StatusCode service( const std::string& svcType, const std::string& svcName, T*& psvc ) const {
125  return service( svcType + "/" + svcName, psvc );
126  }
127 
128  // ==========================================================================
129  // Tool handling
130 
132 
133  template <class T>
135  const std::string& doc = "none" ) {
136  this->declareTool( hndl, hndl.typeAndName() ).ignore();
137  return PropertyHolderImpl::declareProperty( name, hndl, doc );
138  }
139 
140  template <class T>
141  StatusCode declareTool( ToolHandle<T>& handle, bool createIf = true ) {
142  return this->declareTool( handle, handle.typeAndName(), createIf );
143  }
144 
152  template <class T>
153  StatusCode declareTool( ToolHandle<T>& handle, std::string toolTypeAndName, bool createIf = true ) {
154 
155  StatusCode sc = handle.initialize( toolTypeAndName, handle.isPublic() ? nullptr : this, createIf );
156  if ( !sc ) {
157  throw GaudiException{ std::string{ "Cannot create handle for " } + ( handle.isPublic() ? "public" : "private" ) +
158  " tool " + toolTypeAndName,
159  name(), sc };
160  }
161 
162  m_toolHandles.push_back( &handle );
163 
164  return sc;
165  }
166 
167  // declare ToolHandleArrays to the AlgTool
168  template <class T>
170  const std::string& doc = "none" ) {
171  addToolsArray( hndlArr );
172  return PropertyHolderImpl::declareProperty( name, hndlArr, doc );
173  }
174 
175  template <class T>
177  m_toolHandleArrays.push_back( &hndlArr );
178  }
179 
180  const std::vector<IAlgTool*>& tools() const;
181 
182 protected:
183  std::vector<IAlgTool*>& tools();
184 
185 private:
186  // place IAlgTools defined via ToolHandles in m_tools
187  void initToolHandles() const;
188 
189 public:
190  // ==========================================================================
194  SmartIF<IAuditorSvc>& auditorSvc() const;
195 
196 protected:
198  ~Service() override;
203 
205  int outputLevel() const { return m_outputLevel.value(); }
206 
207 private:
208  void sysInitialize_imp();
211 
217 
218  void setServiceManager( ISvcManager* ism ) override;
219 
220  // AlgTools used by Service
224  mutable bool m_toolHandlesInit = false;
225 
226 protected:
227  // Properties
228 
229  Gaudi::Property<int> m_outputLevel{ this, "OutputLevel", MSG::NIL, "output level" };
230  Gaudi::Property<bool> m_auditInit{ this, "AuditServices", false, "[[deprecated]] unused" };
231  Gaudi::Property<bool> m_auditorInitialize{ this, "AuditInitialize", false, "trigger auditor on initialize()" };
232  Gaudi::Property<bool> m_auditorStart{ this, "AuditStart", false, "trigger auditor on start()" };
233  Gaudi::Property<bool> m_auditorStop{ this, "AuditStop", false, "trigger auditor on stop()" };
234  Gaudi::Property<bool> m_auditorFinalize{ this, "AuditFinalize", false, "trigger auditor on finalize()" };
235  Gaudi::Property<bool> m_auditorReinitialize{ this, "AuditReinitialize", false, "trigger auditor on reinitialize()" };
236  Gaudi::Property<bool> m_auditorRestart{ this, "AuditRestart", false, "trigger auditor on restart()" };
237 
238  Gaudi::Property<bool> m_autoRetrieveTools{ this, "AutoRetrieveTools", true,
239  "retrieve all AlgTools during initialize" };
240  Gaudi::Property<bool> m_checkToolDeps{ this, "CheckToolDeps", true,
241  "check data dependencies of AlgTools (error if any found)" };
242 
245 };
246 
247 #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:244
Service::targetFSMState
Gaudi::StateMachine::State targetFSMState() const override
Definition: Service.h:63
Service::m_toolHandleArrays
std::vector< GaudiHandleArrayBase * > m_toolHandleArrays
Definition: Service.h:223
Service::declareProperty
Gaudi::Details::PropertyBase * declareProperty(const std::string &name, ToolHandleArray< T > &hndlArr, const std::string &doc="none")
Definition: Service.h:169
IService.h
std::string
STL class.
IMessageSvc
Definition: IMessageSvc.h:47
bug_34121.name
name
Definition: bug_34121.py:20
Service::m_initSC
StatusCode m_initSC
Definition: Service.h:209
Service::addToolsArray
void addToolsArray(ToolHandleArray< T > &hndlArr)
Definition: Service.h:176
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:215
Service::service
SmartIF< IFace > service(const std::string &name, bool createIf=true) const
Definition: Service.h:115
Service::declareProperty
Gaudi::Details::PropertyBase * declareProperty(const std::string &name, ToolHandle< T > &hndl, const std::string &doc="none")
Definition: Service.h:134
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:108
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:213
std::once_flag
Service::m_initFlag
std::once_flag m_initFlag
Definition: Service.h:210
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:221
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:124
SmartIF< ISvcLocator >
CommonMessaging.h
ToolHandleInfo::isPublic
bool isPublic() const noexcept
Definition: ToolHandle.h:51
Service::declareTool
StatusCode declareTool(ToolHandle< T > &handle, std::string toolTypeAndName, bool createIf=true)
Declare used tool.
Definition: Service.h:153
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
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:205
Service::m_svcManager
SmartIF< ISvcManager > m_svcManager
Definition: Service.h:216
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:104
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:141
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:88
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:222
Service::terminate
StatusCode terminate() override
Definition: Service.h:61