| The Gaudi Framework v28r2p1 (f1a77ff4)

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

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 {
38 public:
39 #ifndef __REFLEX__
40 typedef Gaudi::PluginService::Factory<IService*, const std::string&, ISvcLocator*> Factory;
41 #endif
42 friend class ServiceManager;
43
45 const std::string& name() const override;
46
47 // State machine implementation
48 StatusCode configure() override { return StatusCode::SUCCESS; }
49 StatusCode initialize() override;
50 StatusCode start() override;
51 StatusCode stop() override;
52 StatusCode finalize() override;
53 StatusCode terminate() override { return StatusCode::SUCCESS; }
54 Gaudi::StateMachine::State FSMState() const override { return m_state; }
55 Gaudi::StateMachine::State targetFSMState() const override { return m_targetState; }
56 StatusCode reinitialize() override;
57 StatusCode restart() override;
58
60 StatusCode sysInitialize() override;
62 StatusCode sysStart() override;
64 StatusCode sysStop() override;
66 StatusCode sysFinalize() override;
68 StatusCode sysReinitialize() override;
70 StatusCode sysRestart() override;
71
73 Service( std::string name, ISvcLocator* svcloc );
75 SmartIF<ISvcLocator>& serviceLocator() const override;
76
80 StatusCode setProperties();
81
84 template <class T>
85 StatusCode service( const std::string& name, const T*& psvc, bool createIf = true ) const
86 {
87 ISvcLocator& svcLoc = *serviceLocator();
88 auto ptr = ServiceLocatorHelper( svcLoc, *this )
89 .service<T>( name, !createIf, // quiet
90 createIf );
91 if ( ptr ) {
92 psvc = ptr.get();
93 const_cast<T*>( psvc )->addRef();
94 return StatusCode::SUCCESS;
95 }
96 // else
97 psvc = nullptr;
98 return StatusCode::FAILURE;
99 }
100
101 template <class T>
102 StatusCode service( const std::string& name, T*& psvc, bool createIf = true ) const
103 {
104 auto ptr = service<T>( name, createIf );
105 psvc = ( ptr ? ptr.get() : nullptr );
106 if ( psvc ) {
107 psvc->addRef();
108 return StatusCode::SUCCESS;
109 }
110 return StatusCode::FAILURE;
111 }
112
113 template <typename IFace = IService>
114 SmartIF<IFace> service( const std::string& name, bool createIf = true ) const
115 {
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 {
126 return service( svcType + "/" + svcName, psvc );
127 }
128
136 template <class T>
137 StatusCode declarePrivateTool( ToolHandle<T>& handle, std::string toolTypeAndName = "", bool createIf = true )
138 {
139
140 if ( toolTypeAndName == "" ) toolTypeAndName = System::typeinfoName( typeid( T ) );
141
142 StatusCode sc = handle.initialize( toolTypeAndName, this, createIf );
143
144 if ( sc.isSuccess() ) {
145 if ( UNLIKELY( msgLevel( MSG::DEBUG ) ) )
146 debug() << "Handle for private tool" << toolTypeAndName << " successfully created and stored." << endmsg;
147 } else {
148
149 error() << "Handle for private tool" << toolTypeAndName << " could not be created." << endmsg;
150 }
151
152 return sc;
153 }
154
162 template <class T>
163 StatusCode declarePublicTool( ToolHandle<T>& handle, std::string toolTypeAndName = "", bool createIf = true )
164 {
165
166 if ( toolTypeAndName == "" ) toolTypeAndName = System::typeinfoName( typeid( T ) );
167
168 StatusCode sc = handle.initialize( toolTypeAndName, 0, createIf );
169
170 if ( sc.isSuccess() ) {
171 if ( UNLIKELY( msgLevel( MSG::DEBUG ) ) )
172 debug() << "Handle for public tool" << toolTypeAndName << " successfully created and stored." << endmsg;
173 } else {
174
175 error() << "Handle for public tool" << toolTypeAndName << " could not be created." << endmsg;
176 }
177
178 return sc;
179 }
180
181 // ==========================================================================
185 SmartIF<IAuditorSvc>& auditorSvc() const;
186
187 protected:
189 ~Service() override;
191 Gaudi::StateMachine::State m_state = Gaudi::StateMachine::OFFLINE;
193 Gaudi::StateMachine::State m_targetState = Gaudi::StateMachine::OFFLINE;
194
196 int outputLevel() const { return m_outputLevel.value(); }
197
198 private:
199 void sysInitialize_imp();
200 StatusCode m_initSC;
201 std::once_flag m_initFlag;
202
204 std::string m_name;
206 mutable SmartIF<ISvcLocator> m_svcLocator;
207 SmartIF<ISvcManager> m_svcManager;
208
209 void setServiceManager( ISvcManager* ism ) override;
210
211 protected:
212 // Properties
213
214 Gaudi::Property<int> m_outputLevel{this, "OutputLevel", MSG::NIL, "output level"};
215 Gaudi::Property<bool> m_auditInit{this, "AuditServices", false, "[[deprecated]] unused"};
216 Gaudi::Property<bool> m_auditorInitialize{this, "AuditInitialize", false, "trigger auditor on initialize()"};
217 Gaudi::Property<bool> m_auditorStart{this, "AuditStart", false, "trigger auditor on start()"};
218 Gaudi::Property<bool> m_auditorStop{this, "AuditStop", false, "trigger auditor on stop()"};
219 Gaudi::Property<bool> m_auditorFinalize{this, "AuditFinalize", false, "trigger auditor on finalize()"};
220 Gaudi::Property<bool> m_auditorReinitialize{this, "AuditReinitialize", false, "trigger auditor on reinitialize()"};
221 Gaudi::Property<bool> m_auditorRestart{this, "AuditRestart", false, "trigger auditor on restart()"};
222
224 mutable SmartIF<IAuditorSvc> m_pAuditorSvc;
225 };
226
227 #ifndef GAUDI_NEW_PLUGIN_SERVICE
228 template <class T>
229 class SvcFactory
230 {
231 public:
232 #ifndef __REFLEX__
233 template <typename S, typename... Args>
234 static typename S::ReturnType create( Args&&... args )
235 {
236 return new T( std::forward<Args>( args )... );
237 }
238 #endif
239 };
240
241 // Macros to declare component factories
242 #define DECLARE_SERVICE_FACTORY( x ) DECLARE_FACTORY_WITH_CREATOR( x, SvcFactory<x>, Service::Factory )
243 #define DECLARE_NAMED_SERVICE_FACTORY( x, n ) \
244 DECLARE_FACTORY_WITH_CREATOR_AND_ID( x, SvcFactory<x>, #n, Service::Factory )
245 #define DECLARE_NAMESPACE_SERVICE_FACTORY( n, x ) DECLARE_SERVICE_FACTORY( n::x )
246
247 #else
248
249 // macros to declare factories
250 #define DECLARE_SERVICE_FACTORY( x ) DECLARE_COMPONENT( x )
251 #define DECLARE_NAMED_SERVICE_FACTORY( x, n ) DECLARE_COMPONENT_WITH_ID( x, #n )
252 #define DECLARE_NAMESPACE_SERVICE_FACTORY( n, x ) DECLARE_COMPONENT( n::x )
253
254 #endif
255
256 #endif // GAUDIKERNEL_SERVICE_H
ServiceManager
The ServiceManager class is in charge of the creation of concrete instances of Services.
Definition: ServiceManager.h:36
PropertyHolder.h
ServiceLocatorHelper
an helper to share the implementation of service() among the various kernel base classes ...
Definition: ServiceLocatorHelper.h:17
StatusCode::SUCCESS
Definition: StatusCode.h:30
SmartIF< ISvcLocator >
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:413
Service::m_pAuditorSvc
SmartIF< IAuditorSvc > m_pAuditorSvc
Auditor Service.
Definition: Service.h:224
gaudiComponentHelp.name
name
Definition: gaudiComponentHelp.py:28
StatusCode::FAILURE
Definition: StatusCode.h:29
Gaudi::Property
Implementation of property with value of concrete type.
Definition: Property.h:314
System::typeinfoName
GAUDI_API const std::string typeinfoName(const std::type_info &)
Get platform independent information about the class type.
Definition: System.cpp:299
gaudirun.args
args
Definition: gaudirun.py:236
Service::m_initSC
StatusCode m_initSC
Definition: Service.h:200
ISvcManager
The ISvcManager is the interface implemented by the Service Factory in the Application Manager to sup...
Definition: ISvcManager.h:28
IAuditorSvc.h
StatusCode::isSuccess
bool isSuccess() const
Test for a status code of SUCCESS.
Definition: StatusCode.h:74
ServiceManager::serviceLocator
SmartIF< ISvcLocator > & serviceLocator() const override
Function needed by CommonMessaging.
Definition: ServiceManager.h:57
Service::outputLevel
int outputLevel() const
get the Service&#39;s output level
Definition: Service.h:196
MSG::DEBUG
Definition: IMessageSvc.h:18
UNLIKELY
#define UNLIKELY(x)
Definition: Kernel.h:126
Service::m_svcLocator
SmartIF< ISvcLocator > m_svcLocator
Service Locator reference.
Definition: Service.h:206
SvcFactory::create
static S::ReturnType create(Args &&...args)
Definition: Service.h:234
SvcFactory
Definition: Service.h:229
ServiceManager::restart
StatusCode restart() override
Initialization (from RUNNING to RUNNING, via INITIALIZED).
Definition: ServiceManager.cpp:392
Service::m_name
std::string m_name
Service Name.
Definition: Service.h:204
Gaudi::StateMachine::State
State
Allowed states for classes implementing the state machine (ApplicationMgr, Algorithm, Service, AlgTool).
Definition: StateMachine.h:12
std::once_flag
Service::configure
StatusCode configure() override
Definition: Service.h:48
std::string
STL class.
SmartIF::get
TYPE * get() const
Get interface pointer.
Definition: SmartIF.h:76
CommonMessagingBase::error
MsgStream & error() const
shortcut for the method msgStream(MSG::ERROR)
Definition: CommonMessaging.h:117
ServiceManager::stop
StatusCode stop() override
Stop (from RUNNING to INITIALIZED).
Definition: ServiceManager.cpp:336
StatusCode
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:26
Service::declarePrivateTool
StatusCode declarePrivateTool(ToolHandle< T > &handle, std::string toolTypeAndName="", bool createIf=true)
Declare used Private tool.
Definition: Service.h:137
SmartIF.h
CommonMessaging.h
IMessageSvc
The IMessage is the interface implemented by the message service.
Definition: IMessageSvc.h:57
MSG::NIL
Definition: IMessageSvc.h:16
IOTest.start
start
Definition: IOTest.py:88
IStateful.h
Service::m_initFlag
std::once_flag m_initFlag
Definition: Service.h:201
IService.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&#39;t already exist.
Definition: Service.h:124
Service::service
SmartIF< IFace > service(const std::string &name, bool createIf=true) const
Definition: Service.h:114
PluginService.h
ToolHandle
Handle to be used in lieu of naked pointers to tools.
Definition: PropertyHolder.h:30
ToolHandle::initialize
StatusCode initialize(const std::string &toolTypeAndName, const IInterface *parent=nullptr, bool createIf=true)
Definition: ToolHandle.h:188
Service::declarePublicTool
StatusCode declarePublicTool(ToolHandle< T > &handle, std::string toolTypeAndName="", bool createIf=true)
Declare used Public tool.
Definition: Service.h:163
IProperty.h
CommonMessagingBase::debug
MsgStream & debug() const
shortcut for the method msgStream(MSG::DEBUG)
Definition: CommonMessaging.h:126
Gaudi::PluginService::Factory
Class wrapping the signature for a factory with any number of arguments.
Definition: PluginService.h:47
ServiceManager::reinitialize
StatusCode reinitialize() override
Initialization (from INITIALIZED or RUNNING to INITIALIZED, via CONFIGURED).
Definition: ServiceManager.cpp:371
Property.h
ToolHandle.h
PropertyHolder
Helper class to implement the IProperty interface.
Definition: PropertyHolder.h:73
Service::terminate
StatusCode terminate() override
Definition: Service.h:53
Service::FSMState
Gaudi::StateMachine::State FSMState() const override
Definition: Service.h:54
ServiceLocatorHelper.h
Service::targetFSMState
Gaudi::StateMachine::State targetFSMState() const override
Definition: Service.h:55
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&#39;t already exist.
Definition: Service.h:85
ServiceLocatorHelper::service
SmartIF< IService > service(const std::string &name, const bool quiet=false, const bool createIf=true) const
Definition: ServiceLocatorHelper.cpp:63
Service::m_svcManager
SmartIF< ISvcManager > m_svcManager
Definition: Service.h:207
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:161
Gaudi::StateMachine::OFFLINE
Definition: StateMachine.h:13
Service
Base class for all services.
Definition: Service.h:36
CommonMessagingBase::msgLevel
MSG::Level msgLevel() const
get the output level from the embedded MsgStream
Definition: CommonMessaging.h:135
GAUDI_API
#define GAUDI_API
Definition: Kernel.h:107
Service::Factory
Gaudi::PluginService::Factory< IService *, const std::string &, ISvcLocator * > Factory
Definition: Service.h:40
ISvcLocator.h
Service::service
StatusCode service(const std::string &name, T *&psvc, bool createIf=true) const
Definition: Service.h:102
endmsg
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:244
ServiceManager::initialize
StatusCode initialize() override
Initialization (from CONFIGURED to INITIALIZED).
Definition: ServiceManager.cpp:266