The Gaudi Framework  v36r1 (3e2fb5a8)
Service.h
Go to the documentation of this file.
1 /***********************************************************************************\
2 * (c) Copyright 1998-2019 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 
85  [[deprecated( "not to be called explicitly, will be removed in v36r0" )]] StatusCode setProperties() {
86  if ( !serviceLocator() ) return StatusCode::FAILURE;
87  bindPropertiesTo( serviceLocator()->getOptsSvc() );
88  return StatusCode::SUCCESS;
89  }
90 
93  template <class T>
94  StatusCode service( const std::string& name, const T*& psvc, bool createIf = true ) const {
95  ISvcLocator& svcLoc = *serviceLocator();
96  auto ptr = ServiceLocatorHelper( svcLoc, *this )
97  .service<T>( name, !createIf, // quiet
98  createIf );
99  if ( ptr ) {
100  psvc = ptr.get();
101  const_cast<T*>( psvc )->addRef();
102  return StatusCode::SUCCESS;
103  }
104  // else
105  psvc = nullptr;
106  return StatusCode::FAILURE;
107  }
108 
109  template <class T>
110  StatusCode service( const std::string& name, T*& psvc, bool createIf = true ) const {
111  auto ptr = service<T>( name, createIf );
112  psvc = ( ptr ? ptr.get() : nullptr );
113  if ( psvc ) {
114  psvc->addRef();
115  return StatusCode::SUCCESS;
116  }
117  return StatusCode::FAILURE;
118  }
119 
120  template <typename IFace = IService>
121  SmartIF<IFace> service( const std::string& name, bool createIf = true ) const {
122  return ServiceLocatorHelper( *serviceLocator(), *this )
123  .service<IFace>( name, !createIf, // quiet
124  createIf );
125  }
126 
129  template <class T>
130  StatusCode service( const std::string& svcType, const std::string& svcName, T*& psvc ) const {
131  return service( svcType + "/" + svcName, psvc );
132  }
133 
134  // ==========================================================================
135  // Tool handling
136 
138 
139  template <class T>
141  const std::string& doc = "none" ) {
142  this->declareTool( hndl, hndl.typeAndName() ).ignore();
143  return PropertyHolderImpl::declareProperty( name, hndl, doc );
144  }
145 
146  template <class T>
147  StatusCode declareTool( ToolHandle<T>& handle, bool createIf = true ) {
148  return this->declareTool( handle, handle.typeAndName(), createIf );
149  }
150 
158  template <class T>
159  StatusCode declareTool( ToolHandle<T>& handle, std::string toolTypeAndName, bool createIf = true ) {
160 
161  StatusCode sc = handle.initialize( toolTypeAndName, handle.isPublic() ? nullptr : this, createIf );
162  if ( UNLIKELY( !sc ) ) {
163  throw GaudiException{std::string{"Cannot create handle for "} + ( handle.isPublic() ? "public" : "private" ) +
164  " tool " + toolTypeAndName,
165  name(), sc};
166  }
167 
168  m_toolHandles.push_back( &handle );
169 
170  return sc;
171  }
172 
173  // declare ToolHandleArrays to the AlgTool
174  template <class T>
176  const std::string& doc = "none" ) {
177  addToolsArray( hndlArr );
178  return PropertyHolderImpl::declareProperty( name, hndlArr, doc );
179  }
180 
181  template <class T>
183  m_toolHandleArrays.push_back( &hndlArr );
184  }
185 
186  const std::vector<IAlgTool*>& tools() const;
187 
188 protected:
189  std::vector<IAlgTool*>& tools();
190 
191 private:
192  // place IAlgTools defined via ToolHandles in m_tools
193  void initToolHandles() const;
194 
195 public:
196  // ==========================================================================
200  SmartIF<IAuditorSvc>& auditorSvc() const;
201 
202 protected:
204  ~Service() override;
209 
211  int outputLevel() const { return m_outputLevel.value(); }
212 
213 private:
214  void sysInitialize_imp();
217 
223 
224  void setServiceManager( ISvcManager* ism ) override;
225 
226  // AlgTools used by Service
230  mutable bool m_toolHandlesInit = false;
231 
232 protected:
233  // Properties
234 
235  Gaudi::Property<int> m_outputLevel{this, "OutputLevel", MSG::NIL, "output level"};
236  Gaudi::Property<bool> m_auditInit{this, "AuditServices", false, "[[deprecated]] unused"};
237  Gaudi::Property<bool> m_auditorInitialize{this, "AuditInitialize", false, "trigger auditor on initialize()"};
238  Gaudi::Property<bool> m_auditorStart{this, "AuditStart", false, "trigger auditor on start()"};
239  Gaudi::Property<bool> m_auditorStop{this, "AuditStop", false, "trigger auditor on stop()"};
240  Gaudi::Property<bool> m_auditorFinalize{this, "AuditFinalize", false, "trigger auditor on finalize()"};
241  Gaudi::Property<bool> m_auditorReinitialize{this, "AuditReinitialize", false, "trigger auditor on reinitialize()"};
242  Gaudi::Property<bool> m_auditorRestart{this, "AuditRestart", false, "trigger auditor on restart()"};
243 
244  Gaudi::Property<bool> m_autoRetrieveTools{this, "AutoRetrieveTools", true, "retrieve all AlgTools during initialize"};
245  Gaudi::Property<bool> m_checkToolDeps{this, "CheckToolDeps", true,
246  "check data dependencies of AlgTools (error if any found)"};
247 
250 };
251 
252 #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:249
Service::targetFSMState
Gaudi::StateMachine::State targetFSMState() const override
Definition: Service.h:63
Service::m_toolHandleArrays
std::vector< GaudiHandleArrayBase * > m_toolHandleArrays
Definition: Service.h:229
Service::declareProperty
Gaudi::Details::PropertyBase * declareProperty(const std::string &name, ToolHandleArray< T > &hndlArr, const std::string &doc="none")
Definition: Service.h:175
IService.h
std::string
STL class.
IMessageSvc
Definition: IMessageSvc.h:47
Service::m_initSC
StatusCode m_initSC
Definition: Service.h:215
Service::addToolsArray
void addToolsArray(ToolHandleArray< T > &hndlArr)
Definition: Service.h:182
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:221
Service::service
SmartIF< IFace > service(const std::string &name, bool createIf=true) const
Definition: Service.h:121
Service::declareProperty
Gaudi::Details::PropertyBase * declareProperty(const std::string &name, ToolHandle< T > &hndl, const std::string &doc="none")
Definition: Service.h:140
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:367
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
PropertyHolder::bindPropertiesTo
void bindPropertiesTo(Gaudi::Interfaces::IOptionsSvc &optsSvc)
Definition: PropertyHolder.h:252
Service::m_name
std::string m_name
Service Name
Definition: Service.h:219
std::once_flag
Service::m_initFlag
std::once_flag m_initFlag
Definition: Service.h:216
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:135
TimingHistograms.name
name
Definition: TimingHistograms.py:23
StatusCode
Definition: StatusCode.h:65
ServiceManager::initialize
StatusCode initialize() override
Initialization (from CONFIGURED to INITIALIZED).
Definition: ServiceManager.cpp:266
Service::m_tools
std::vector< IAlgTool * > m_tools
Definition: Service.h:227
Service::configure
StatusCode configure() override
Definition: Service.h:56
Gaudi::StateMachine::OFFLINE
@ OFFLINE
Definition: StateMachine.h:23
ToolHandleArray
Definition: ToolHandle.h:345
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:177
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:409
GaudiPython.Bindings.nullptr
nullptr
Definition: Bindings.py:74
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:130
SmartIF< ISvcLocator >
CommonMessaging.h
Service::setProperties
StatusCode setProperties()
Definition: Service.h:85
ToolHandleInfo::isPublic
bool isPublic() const noexcept
Definition: ToolHandle.h:50
Service::declareTool
StatusCode declareTool(ToolHandle< T > &handle, std::string toolTypeAndName, bool createIf=true)
Declare used tool.
Definition: Service.h:159
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:211
Service::m_svcManager
SmartIF< ISvcManager > m_svcManager
Definition: Service.h:222
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:388
ToolHandle::initialize
StatusCode initialize(const std::string &toolTypeAndName, const IInterface *parent=nullptr, bool createIf=true)
Definition: ToolHandle.h:210
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:110
IProperty.h
ToolHandle::typeAndName
std::string typeAndName() const override
Definition: ToolHandle.h:270
Service::declareTool
StatusCode declareTool(ToolHandle< T > &handle, bool createIf=true)
Definition: Service.h:147
StatusCode::FAILURE
constexpr static const auto FAILURE
Definition: StatusCode.h:101
UNLIKELY
#define UNLIKELY(x)
Definition: Kernel.h:106
ISvcLocator.h
ServiceManager::stop
StatusCode stop() override
Stop (from RUNNING to INITIALIZED).
Definition: ServiceManager.cpp:333
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:94
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:228
Service::terminate
StatusCode terminate() override
Definition: Service.h:61