| The Gaudi Framework v32r1 (f65d50dc)

Service.h
Go to the documentation of this file.
1 #ifndef GAUDIKERNEL_SERVICE_H
2 #define GAUDIKERNEL_SERVICE_H
3 // ============================================================================
4 // Include files
5 // ============================================================================
6 #include "GaudiKernel/CommonMessaging.h"
7 #include "GaudiKernel/IAuditorSvc.h"
8 #include "GaudiKernel/IProperty.h"
9 #include "GaudiKernel/IService.h"
10 #include "GaudiKernel/IStateful.h"
11 #include "GaudiKernel/ISvcLocator.h"
12 #include "GaudiKernel/Property.h"
13 #include "GaudiKernel/PropertyHolder.h"
14 #include "GaudiKernel/ServiceLocatorHelper.h"
15 #include "GaudiKernel/SmartIF.h"
16 #include "GaudiKernel/ToolHandle.h"
17 #include <Gaudi/PluginService.h>
18
19 // ============================================================================
20 #include <mutex>
21 #include <vector>
22 // ============================================================================
23 // Forward declarations
24 // ============================================================================
25 class IMessageSvc;
26 class ISvcManager;
27 class ServiceManager;
28 // ============================================================================
36 class GAUDI_API Service : public PropertyHolder<CommonMessaging<implements<IService, IProperty, IStateful>>> {
37 public:
38 using Factory = Gaudi::PluginService::Factory<IService*( const std::string&, ISvcLocator* )>;
39
40 friend class ServiceManager;
41
43 const std::string& name() const override;
44
45 // State machine implementation
46 StatusCode configure() override { return StatusCode::SUCCESS; }
47 StatusCode initialize() override;
48 StatusCode start() override;
49 StatusCode stop() override;
50 StatusCode finalize() override;
51 StatusCode terminate() override { return StatusCode::SUCCESS; }
52 Gaudi::StateMachine::State FSMState() const override { return m_state; }
53 Gaudi::StateMachine::State targetFSMState() const override { return m_targetState; }
54 StatusCode reinitialize() override;
55 StatusCode restart() override;
56
58 StatusCode sysInitialize() override;
60 StatusCode sysStart() override;
62 StatusCode sysStop() override;
64 StatusCode sysFinalize() override;
66 StatusCode sysReinitialize() override;
68 StatusCode sysRestart() override;
69
71 Service( std::string name, ISvcLocator* svcloc );
73 SmartIF<ISvcLocator>& serviceLocator() const override;
74
78 StatusCode setProperties();
79
82 template <class T>
83 StatusCode service( const std::string& name, const T*& psvc, bool createIf = true ) const {
84 ISvcLocator& svcLoc = *serviceLocator();
85 auto ptr = ServiceLocatorHelper( svcLoc, *this )
86 .service<T>( name, !createIf, // quiet
87 createIf );
88 if ( ptr ) {
89 psvc = ptr.get();
90 const_cast<T*>( psvc )->addRef();
91 return StatusCode::SUCCESS;
92 }
93 // else
94 psvc = nullptr;
95 return StatusCode::FAILURE;
96 }
97
98 template <class T>
99 StatusCode service( const std::string& name, T*& psvc, bool createIf = true ) const {
100 auto ptr = service<T>( name, createIf );
101 psvc = ( ptr ? ptr.get() : nullptr );
102 if ( psvc ) {
103 psvc->addRef();
104 return StatusCode::SUCCESS;
105 }
106 return StatusCode::FAILURE;
107 }
108
109 template <typename IFace = IService>
110 SmartIF<IFace> service( const std::string& name, bool createIf = true ) const {
111 return ServiceLocatorHelper( *serviceLocator(), *this )
112 .service<IFace>( name, !createIf, // quiet
113 createIf );
114 }
115
118 template <class T>
119 StatusCode service( const std::string& svcType, const std::string& svcName, T*& psvc ) const {
120 return service( svcType + "/" + svcName, psvc );
121 }
122
130 template <class T>
131 StatusCode declareTool( ToolHandle<T>& handle, std::string toolTypeAndName, bool createIf = true ) {
132
133 StatusCode sc = handle.initialize( toolTypeAndName, handle.isPublic() ? nullptr : this, createIf );
134 if ( UNLIKELY( !sc ) ) {
135 throw GaudiException{std::string{"Cannot create handle for "} + ( handle.isPublic() ? "public" : "private" ) +
136 " tool " + toolTypeAndName,
137 name(), sc};
138 }
139
140 return sc;
141 }
142
143 // ==========================================================================
147 SmartIF<IAuditorSvc>& auditorSvc() const;
148
149 protected:
151 ~Service() override;
153 Gaudi::StateMachine::State m_state = Gaudi::StateMachine::OFFLINE;
155 Gaudi::StateMachine::State m_targetState = Gaudi::StateMachine::OFFLINE;
156
158 int outputLevel() const { return m_outputLevel.value(); }
159
160 private:
161 void sysInitialize_imp();
162 StatusCode m_initSC;
163 std::once_flag m_initFlag;
164
166 std::string m_name;
168 mutable SmartIF<ISvcLocator> m_svcLocator;
169 SmartIF<ISvcManager> m_svcManager;
170
171 void setServiceManager( ISvcManager* ism ) override;
172
173 protected:
174 // Properties
175
176 Gaudi::Property<int> m_outputLevel{this, "OutputLevel", MSG::NIL, "output level"};
177 Gaudi::Property<bool> m_auditInit{this, "AuditServices", false, "[[deprecated]] unused"};
178 Gaudi::Property<bool> m_auditorInitialize{this, "AuditInitialize", false, "trigger auditor on initialize()"};
179 Gaudi::Property<bool> m_auditorStart{this, "AuditStart", false, "trigger auditor on start()"};
180 Gaudi::Property<bool> m_auditorStop{this, "AuditStop", false, "trigger auditor on stop()"};
181 Gaudi::Property<bool> m_auditorFinalize{this, "AuditFinalize", false, "trigger auditor on finalize()"};
182 Gaudi::Property<bool> m_auditorReinitialize{this, "AuditReinitialize", false, "trigger auditor on reinitialize()"};
183 Gaudi::Property<bool> m_auditorRestart{this, "AuditRestart", false, "trigger auditor on restart()"};
184
186 mutable SmartIF<IAuditorSvc> m_pAuditorSvc;
187 };
188
189 #endif // GAUDIKERNEL_SERVICE_H
ServiceManager
The ServiceManager class is in charge of the creation of concrete instances of Services.
Definition: ServiceManager.h:36
UNLIKELY
#define UNLIKELY(x)
Definition: Kernel.h:96
PropertyHolder.h
ServiceLocatorHelper
an helper to share the implementation of service() among the various kernel base classes
Definition: ServiceLocatorHelper.h:17
Service::service
SmartIF< IFace > service(const std::string &name, bool createIf=true) const
Definition: Service.h:110
SmartIF< ISvcLocator >
GaudiException
Define general base for Gaudi exception.
Definition: GaudiException.h:21
ISvcLocator
The ISvcLocator is the interface implemented by the Service Factory in the Application Manager to loc...
Definition: ISvcLocator.h:25
ServiceManager::finalize
StatusCode finalize() override
Finalize (from INITIALIZED to CONFIGURED).
Definition: ServiceManager.cpp:394
Service::m_pAuditorSvc
SmartIF< IAuditorSvc > m_pAuditorSvc
Auditor Service.
Definition: Service.h:186
gaudiComponentHelp.name
name
Definition: gaudiComponentHelp.py:41
Gaudi::Property
Implementation of property with value of concrete type.
Definition: Property.h:352
Service::FSMState
Gaudi::StateMachine::State FSMState() const override
Definition: Service.h:52
Service::m_initSC
StatusCode m_initSC
Definition: Service.h:162
ISvcManager
The ISvcManager is the interface implemented by the Service Factory in the Application Manager to sup...
Definition: ISvcManager.h:28
IAuditorSvc.h
ServiceManager::serviceLocator
SmartIF< ISvcLocator > & serviceLocator() const override
Function needed by CommonMessaging.
Definition: ServiceManager.h:56
StatusCode::SUCCESS
constexpr static const auto SUCCESS
Definition: StatusCode.h:85
Service::m_svcLocator
SmartIF< ISvcLocator > m_svcLocator
Service Locator reference.
Definition: Service.h:168
ServiceManager::restart
StatusCode restart() override
Initialization (from RUNNING to RUNNING, via INITIALIZED).
Definition: ServiceManager.cpp:373
Service::m_name
std::string m_name
Service Name.
Definition: Service.h:166
Gaudi::StateMachine::State
State
Allowed states for classes implementing the state machine (ApplicationMgr, Algorithm,...
Definition: StateMachine.h:12
std::once_flag
SmartIF::get
TYPE * get() const
Get interface pointer.
Definition: SmartIF.h:76
IOTest.start
def start
Definition: IOTest.py:97
Service::configure
StatusCode configure() override
Definition: Service.h:46
std::string
STL class.
ServiceManager::stop
StatusCode stop() override
Stop (from RUNNING to INITIALIZED).
Definition: ServiceManager.cpp:318
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:119
IService
General service interface definition.
Definition: IService.h:18
StatusCode
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:50
ToolHandleInfo::isPublic
bool isPublic() const noexcept
Definition: ToolHandle.h:40
SmartIF.h
CommonMessaging.h
IMessageSvc
The IMessage is the interface implemented by the message service.
Definition: IMessageSvc.h:37
MSG::NIL
Definition: IMessageSvc.h:15
ServiceLocatorHelper::service
SmartIF< IService > service(const std::string &name, const bool quiet=false, const bool createIf=true) const
Definition: ServiceLocatorHelper.cpp:38
IStateful.h
Service::m_initFlag
std::once_flag m_initFlag
Definition: Service.h:163
IService.h
PluginService.h
Service::outputLevel
int outputLevel() const
get the Service's output level
Definition: Service.h:158
ToolHandle
Handle to be used in lieu of naked pointers to tools.
Definition: ToolHandle.h:125
ToolHandle::initialize
StatusCode initialize(const std::string &toolTypeAndName, const IInterface *parent=nullptr, bool createIf=true)
Definition: ToolHandle.h:200
IProperty.h
Service::Factory
Gaudi::PluginService::Factory< IService *(const std::string &, ISvcLocator *)> Factory
Definition: Service.h:38
Service::declareTool
StatusCode declareTool(ToolHandle< T > &handle, std::string toolTypeAndName, bool createIf=true)
Declare used tool.
Definition: Service.h:131
ServiceManager::reinitialize
StatusCode reinitialize() override
Initialization (from INITIALIZED or RUNNING to INITIALIZED, via CONFIGURED).
Definition: ServiceManager.cpp:352
Property.h
ToolHandle.h
PropertyHolder
Helper class to implement the IProperty interface.
Definition: PropertyHolder.h:71
Service::terminate
StatusCode terminate() override
Definition: Service.h:51
StatusCode::FAILURE
constexpr static const auto FAILURE
Definition: StatusCode.h:86
ServiceLocatorHelper.h
Service::service
StatusCode service(const std::string &name, T *&psvc, bool createIf=true) const
Definition: Service.h:99
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:83
Service::m_svcManager
SmartIF< ISvcManager > m_svcManager
Definition: Service.h:169
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:159
Gaudi::StateMachine::OFFLINE
Definition: StateMachine.h:13
Service
Base class for all services.
Definition: Service.h:36
GAUDI_API
#define GAUDI_API
Definition: Kernel.h:71
Service::targetFSMState
Gaudi::StateMachine::State targetFSMState() const override
Definition: Service.h:53
ISvcLocator.h
ServiceManager::initialize
StatusCode initialize() override
Initialization (from CONFIGURED to INITIALIZED).
Definition: ServiceManager.cpp:251