| The Gaudi Framework v27r1

- 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/IService.h"
7 #include "GaudiKernel/ISvcLocator.h"
8 #include "GaudiKernel/ServiceLocatorHelper.h"
9 #include "GaudiKernel/IProperty.h"
10 #include "GaudiKernel/IStateful.h"
11 #include "GaudiKernel/PropertyMgr.h"
12 #include "GaudiKernel/Property.h"
13 #include "GaudiKernel/IAuditorSvc.h"
14 #include "GaudiKernel/CommonMessaging.h"
15 #include "GaudiKernel/SmartIF.h"
16 #include <Gaudi/PluginService.h>
17 #include "GaudiKernel/ToolHandle.h"
18
19 // ============================================================================
20 #include <vector>
21 #include <mutex>
22 // ============================================================================
23 // Forward declarations
24 // ============================================================================
25 class IMessageSvc;
26 class ISvcManager;
27 class ServiceManager;
28 // ============================================================================
36 class GAUDI_API Service: public CommonMessaging<implements<IService,
37 IProperty,
38 IStateful> > {
39 public:
40 #ifndef __REFLEX__
41 typedef Gaudi::PluginService::Factory<IService*,
42 const std::string&,
43 ISvcLocator*> Factory;
44 #endif
45 friend class ServiceManager;
46
48 const std::string& name() const override;
49
50 // State machine implementation
51 StatusCode configure() override { return StatusCode::SUCCESS; }
52 StatusCode initialize() override;
53 StatusCode start() override;
54 StatusCode stop() override;
55 StatusCode finalize() override;
56 StatusCode terminate() override { return StatusCode::SUCCESS; }
57 Gaudi::StateMachine::State FSMState() const override { return m_state; }
58 Gaudi::StateMachine::State targetFSMState() const override { return m_targetState; }
59 StatusCode reinitialize() override;
60 StatusCode restart() override;
61
63 StatusCode sysInitialize() override;
65 StatusCode sysStart() override;
67 StatusCode sysStop() override;
69 StatusCode sysFinalize() override;
71 StatusCode sysReinitialize() override;
73 StatusCode sysRestart() override;
74
75 // Default implementations for ISetProperty
76 StatusCode setProperty(const Property& p) override;
77 StatusCode setProperty( const std::string& s ) override;
78 StatusCode setProperty( const std::string& n, const std::string& v) override;
79 StatusCode getProperty(Property* p) const override;
80 const Property& getProperty( const std::string& name) const override;
81 StatusCode getProperty( const std::string& n, std::string& v ) const override;
82 const std::vector<Property*>& getProperties( ) const override;
83 bool hasProperty(const std::string& name) const override;
84
125 template <class TYPE>
126 StatusCode setProperty
127 ( const std::string& name ,
128 const TYPE& value )
129 { return Gaudi::Utils::setProperty ( m_propertyMgr.get() , name , value ) ; }
130
132 Service( std::string name, ISvcLocator* svcloc);
134 SmartIF<ISvcLocator>& serviceLocator() const override;
135
139 StatusCode setProperties();
140
143 template <class T>
144 StatusCode service( const std::string& name, const T*& psvc, bool createIf = true ) const {
145 ISvcLocator& svcLoc = *serviceLocator();
146 auto ptr =
147 ServiceLocatorHelper(svcLoc, *this).service<T>(name, !createIf, // quiet
148 createIf);
149 if (ptr) {
150 psvc = ptr.get();
151 const_cast<T*>(psvc)->addRef();
152 return StatusCode::SUCCESS;
153 }
154 // else
155 psvc = nullptr;
156 return StatusCode::FAILURE;
157 }
158
159 template <class T>
160 StatusCode service( const std::string& name, T*& psvc, bool createIf = true ) const {
161 auto ptr = service<T>(name,createIf);
162 psvc = ( ptr ? ptr.get() : nullptr );
163 if (psvc) {
164 psvc->addRef();
165 return StatusCode::SUCCESS;
166 }
167 return StatusCode::FAILURE;
168 }
169
170 template <typename IFace = IService>
171 SmartIF<IFace> service(const std::string& name, bool createIf = true) const {
172 return ServiceLocatorHelper(*serviceLocator(), *this).
173 service<IFace>(name, !createIf, // quiet
174 createIf);
175 }
176
179 template <class T>
180 StatusCode service( const std::string& svcType, const std::string& svcName,
181 T*& psvc) const {
182 return service(svcType + "/" + svcName, psvc);
183 }
184 // ==========================================================================
213 template <class T>
214 Property* declareProperty
215 ( const std::string& name ,
216 T& property ,
217 const std::string& doc = "none" ) const
218 {
219 return m_propertyMgr -> declareProperty ( name , property , doc ) ;
220 }
221 // ==========================================================================
230 Property* declareRemoteProperty
231 ( const std::string& name ,
232 IProperty* rsvc ,
233 const std::string& rname = "" ) const
234 {
235 return m_propertyMgr -> declareRemoteProperty ( name , rsvc , rname ) ;
236 }
237
245 template<class T>
246 StatusCode declarePrivateTool(ToolHandle<T> & handle, std::string toolTypeAndName =
247 "", bool createIf = true) {
248
249 if (toolTypeAndName == "")
250 toolTypeAndName = System::typeinfoName(typeid(T));
251
252 StatusCode sc = handle.initialize(toolTypeAndName, this, createIf);
253
254 if (sc.isSuccess()) {
255 if (UNLIKELY(msgLevel(MSG::DEBUG)))
256 debug() << "Handle for private tool" << toolTypeAndName
257 << " successfully created and stored." << endmsg;
258 } else {
259
260 error() << "Handle for private tool" << toolTypeAndName
261 << " could not be created." << endmsg;
262 }
263
264 return sc;
265
266 }
267
275 template<class T>
276 StatusCode declarePublicTool(ToolHandle<T> & handle, std::string toolTypeAndName =
277 "", bool createIf = true) {
278
279 if (toolTypeAndName == "")
280 toolTypeAndName = System::typeinfoName(typeid(T));
281
282 StatusCode sc = handle.initialize(toolTypeAndName, 0, createIf);
283
284
285 if (sc.isSuccess()) {
286 if (UNLIKELY(msgLevel(MSG::DEBUG)))
287 debug() << "Handle for public tool" << toolTypeAndName
288 << " successfully created and stored." << endmsg;
289 } else {
290
291 error() << "Handle for public tool" << toolTypeAndName
292 << " could not be created." << endmsg;
293 }
294
295 return sc;
296
297 }
298
299 // ==========================================================================
303 SmartIF<IAuditorSvc>& auditorSvc() const;
304
305 protected:
307 ~Service() override;
309 IntegerProperty m_outputLevel = MSG::NIL;
311 Gaudi::StateMachine::State m_state = Gaudi::StateMachine::OFFLINE;
313 Gaudi::StateMachine::State m_targetState = Gaudi::StateMachine::OFFLINE;
314
316 int outputLevel() const { return m_outputLevel.value(); }
317
318 private:
319
320 void sysInitialize_imp();
321 StatusCode m_initSC;
322 std::once_flag m_initFlag;
323
325 std::string m_name;
327 mutable SmartIF<ISvcLocator> m_svcLocator;
328 SmartIF<ISvcManager> m_svcManager;
330 SmartIF<PropertyMgr> m_propertyMgr;
331
332 void setServiceManager(ISvcManager* ism) override;
333
335 mutable SmartIF<IAuditorSvc> m_pAuditorSvc;
336 BooleanProperty m_auditInit;
337 bool m_auditorInitialize;
338 bool m_auditorStart;
339 bool m_auditorStop;
340 bool m_auditorFinalize;
341 bool m_auditorReinitialize;
342 bool m_auditorRestart;
343
345 void initOutputLevel(Property& prop);
346 };
347
348 #ifndef GAUDI_NEW_PLUGIN_SERVICE
349 template <class T>
350 class SvcFactory {
351 public:
352 #ifndef __REFLEX__
353 template <typename S, typename... Args>
354 static typename S::ReturnType create(Args&&... args) {
355 return new T(std::forward<Args>(args)...);
356 }
357 #endif
358 };
359
360 // Macros to declare component factories
361 #define DECLARE_SERVICE_FACTORY(x) \
362 DECLARE_FACTORY_WITH_CREATOR(x, SvcFactory< x >, Service::Factory)
363 #define DECLARE_NAMED_SERVICE_FACTORY(x, n) \
364 DECLARE_FACTORY_WITH_CREATOR_AND_ID(x, SvcFactory< x >, #n, Service::Factory)
365 #define DECLARE_NAMESPACE_SERVICE_FACTORY(n, x) \
366 DECLARE_SERVICE_FACTORY(n::x)
367
368 #else
369
370 // macros to declare factories
371 #define DECLARE_SERVICE_FACTORY(x) DECLARE_COMPONENT(x)
372 #define DECLARE_NAMED_SERVICE_FACTORY(x, n) DECLARE_COMPONENT_WITH_ID(x, #n)
373 #define DECLARE_NAMESPACE_SERVICE_FACTORY(n, x) DECLARE_COMPONENT(n::x)
374
375 #endif
376
377 #endif // GAUDIKERNEL_SERVICE_H
ServiceManager
The ServiceManager class is in charge of the creation of concrete instances of Services.
Definition: ServiceManager.h:36
Gaudi::Utils::setProperty
StatusCode setProperty(IProperty *component, const std::string &name, const TYPE &value, const std::string &doc)
simple function to set the property of the given object from the value
Definition: Property.h:1187
UNLIKELY
#define UNLIKELY(x)
Definition: Kernel.h:126
Service::m_auditInit
BooleanProperty m_auditInit
Definition: Service.h:336
ServiceLocatorHelper
an helper to share the implementation of service() among the various kernel base classes ...
Definition: ServiceLocatorHelper.h:17
SmartIF< ISvcLocator >
AnalysisTest.initialize
def initialize()
Definition: AnalysisTest.py:12
ISvcLocator
The ISvcLocator is the interface implemented by the Service Factory in the Application Manager to loc...
Definition: ISvcLocator.h:25
Service::m_auditorInitialize
bool m_auditorInitialize
Definition: Service.h:337
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:335
gaudiComponentHelp.name
name
Definition: gaudiComponentHelp.py:28
Service::FSMState
Gaudi::StateMachine::State FSMState() const override
Definition: Service.h:57
System::typeinfoName
GAUDI_API const std::string typeinfoName(const std::type_info &)
Get platform independent information about the class type.
Definition: System.cpp:297
Service::m_initSC
StatusCode m_initSC
Definition: Service.h:321
ISvcManager
The ISvcManager is the interface implemented by the Service Factory in the Application Manager to sup...
Definition: ISvcManager.h:28
IAuditorSvc.h
Gaudi::Utils::hasProperty
GAUDI_API bool hasProperty(const IProperty *p, const std::string &name)
simple function which check the existence of the property with the given name.
Definition: Property.cpp:159
StatusCode::isSuccess
bool isSuccess() const
Test for a status code of SUCCESS.
Definition: StatusCode.h:76
ServiceManager::serviceLocator
SmartIF< ISvcLocator > & serviceLocator() const override
Function needed by CommonMessaging.
Definition: ServiceManager.h:66
Service::outputLevel
int outputLevel() const
get the Service's output level
Definition: Service.h:316
MSG::DEBUG
Definition: IMessageSvc.h:18
Service::m_svcLocator
SmartIF< ISvcLocator > m_svcLocator
Service Locator reference.
Definition: Service.h:327
SvcFactory::create
static S::ReturnType create(Args &&...args)
Definition: Service.h:354
SvcFactory
Definition: Service.h:350
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:325
Gaudi::StateMachine::State
State
Allowed states for classes implementing the state machine (ApplicationMgr, Algorithm, Service, AlgTool).
Definition: StateMachine.h:12
getProperty
const char *PyHelper() getProperty(IInterface *p, char *name)
Definition: Bootstrap.cpp:258
merge_files.sc
int sc
Definition: merge_files.py:167
std::once_flag
GaudiPluginService.cpluginsvc.n
list n
Definition: cpluginsvc.py:217
Service::configure
StatusCode configure() override
Definition: Service.h:51
std::string
STL class.
SimpleProperty< int >
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:118
ServiceManager::stop
StatusCode stop() override
Stop (from RUNNING to INITIALIZED).
Definition: ServiceManager.cpp:336
Service::m_propertyMgr
SmartIF< PropertyMgr > m_propertyMgr
Property Manager.
Definition: Service.h:330
StatusCode::FAILURE
Definition: StatusCode.h:29
IService
General service interface definition.
Definition: IService.h:18
StatusCode::SUCCESS
Definition: StatusCode.h:30
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:246
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
setProperty
bool PyHelper() setProperty(IInterface *p, char *name, char *value)
Definition: Bootstrap.cpp:254
dump_component_properties.value
list value
Definition: dump_component_properties.py:12
Service::m_auditorFinalize
bool m_auditorFinalize
Definition: Service.h:340
Service::m_auditorReinitialize
bool m_auditorReinitialize
Definition: Service.h:341
IStateful.h
Service::m_initFlag
std::once_flag m_initFlag
Definition: Service.h:322
PropertyWithValue::value
const TYPE & value() const
explicit conversion
Definition: Property.h:341
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't already exist.
Definition: Service.h:180
Service::service
SmartIF< IFace > service(const std::string &name, bool createIf=true) const
Definition: Service.h:171
PluginService.h
ToolHandle
Handle to be used in lieu of naked pointers to tools.
Definition: PropertyMgr.h:22
ToolHandle::initialize
StatusCode initialize(const std::string &toolTypeAndName, const IInterface *parent=nullptr, bool createIf=true)
Definition: ToolHandle.h:157
Service::declarePublicTool
StatusCode declarePublicTool(ToolHandle< T > &handle, std::string toolTypeAndName="", bool createIf=true)
Declare used Public tool.
Definition: Service.h:276
IProperty.h
Property
Property base class allowing Property* collections to be "homogeneous".
Definition: Property.h:38
std::vector< Property * >
gaudirun.args
list args
Definition: gaudirun.py:290
Service::m_auditorStart
bool m_auditorStart
Definition: Service.h:338
CommonMessagingBase::debug
MsgStream & debug() const
shortcut for the method msgStream(MSG::DEBUG)
Definition: CommonMessaging.h:127
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
PropertyMgr.h
ToolHandle.h
Service::terminate
StatusCode terminate() override
Definition: Service.h:56
gaudirun.s
string s
Definition: gaudirun.py:245
CommonMessaging
Definition: CommonMessaging.h:58
ServiceLocatorHelper.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:144
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:328
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
IProperty
The IProperty is the basic interface for all components which have properties that can be set or get...
Definition: IProperty.h:21
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:136
GAUDI_API
#define GAUDI_API
Definition: Kernel.h:107
Service::Factory
Gaudi::PluginService::Factory< IService *, const std::string &, ISvcLocator * > Factory
Definition: Service.h:43
Service::targetFSMState
Gaudi::StateMachine::State targetFSMState() const override
Definition: Service.h:58
ISvcLocator.h
Service::service
StatusCode service(const std::string &name, T *&psvc, bool createIf=true) const
Definition: Service.h:160
Service::m_auditorRestart
bool m_auditorRestart
Definition: Service.h:342
endmsg
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:244
Service::m_auditorStop
bool m_auditorStop
Definition: Service.h:339
IOTest.start
tuple start
Definition: IOTest.py:88