| The Gaudi Framework v29r1 (5cd95fa2)

- 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 declareTool( ToolHandle<T>& handle, std::string toolTypeAndName, bool createIf = true )
138 {
139
140 StatusCode sc = handle.initialize( toolTypeAndName, handle.isPublic() ? nullptr : this, createIf );
141 if ( UNLIKELY( !sc ) ) {
142 throw GaudiException{std::string{"Cannot create handle for "} + ( handle.isPublic() ? "public" : "private" ) +
143 " tool " + toolTypeAndName,
144 name(), sc};
145 }
146
147 return sc;
148 }
149
150 // ==========================================================================
154 SmartIF<IAuditorSvc>& auditorSvc() const;
155
156 protected:
158 ~Service() override;
160 Gaudi::StateMachine::State m_state = Gaudi::StateMachine::OFFLINE;
162 Gaudi::StateMachine::State m_targetState = Gaudi::StateMachine::OFFLINE;
163
165 int outputLevel() const { return m_outputLevel.value(); }
166
167 private:
168 void sysInitialize_imp();
169 StatusCode m_initSC;
170 std::once_flag m_initFlag;
171
173 std::string m_name;
175 mutable SmartIF<ISvcLocator> m_svcLocator;
176 SmartIF<ISvcManager> m_svcManager;
177
178 void setServiceManager( ISvcManager* ism ) override;
179
180 protected:
181 // Properties
182
183 Gaudi::Property<int> m_outputLevel{this, "OutputLevel", MSG::NIL, "output level"};
184 Gaudi::Property<bool> m_auditInit{this, "AuditServices", false, "[[deprecated]] unused"};
185 Gaudi::Property<bool> m_auditorInitialize{this, "AuditInitialize", false, "trigger auditor on initialize()"};
186 Gaudi::Property<bool> m_auditorStart{this, "AuditStart", false, "trigger auditor on start()"};
187 Gaudi::Property<bool> m_auditorStop{this, "AuditStop", false, "trigger auditor on stop()"};
188 Gaudi::Property<bool> m_auditorFinalize{this, "AuditFinalize", false, "trigger auditor on finalize()"};
189 Gaudi::Property<bool> m_auditorReinitialize{this, "AuditReinitialize", false, "trigger auditor on reinitialize()"};
190 Gaudi::Property<bool> m_auditorRestart{this, "AuditRestart", false, "trigger auditor on restart()"};
191
193 mutable SmartIF<IAuditorSvc> m_pAuditorSvc;
194 };
195
196 #ifndef GAUDI_NEW_PLUGIN_SERVICE
197 template <class T>
198 class SvcFactory
199 {
200 public:
201 #ifndef __REFLEX__
202 template <typename S, typename... Args>
203 static typename S::ReturnType create( Args&&... args )
204 {
205 return new T( std::forward<Args>( args )... );
206 }
207 #endif
208 };
209
210 // Macros to declare component factories
211 #define DECLARE_SERVICE_FACTORY( x ) DECLARE_FACTORY_WITH_CREATOR( x, SvcFactory<x>, Service::Factory )
212 #define DECLARE_NAMED_SERVICE_FACTORY( x, n ) \
213 DECLARE_FACTORY_WITH_CREATOR_AND_ID( x, SvcFactory<x>, #n, Service::Factory )
214 #define DECLARE_NAMESPACE_SERVICE_FACTORY( n, x ) DECLARE_SERVICE_FACTORY( n::x )
215
216 #else
217
218 // macros to declare factories
219 #define DECLARE_SERVICE_FACTORY( x ) DECLARE_COMPONENT( x )
220 #define DECLARE_NAMED_SERVICE_FACTORY( x, n ) DECLARE_COMPONENT_WITH_ID( x, #n )
221 #define DECLARE_NAMESPACE_SERVICE_FACTORY( n, x ) DECLARE_COMPONENT( n::x )
222
223 #endif
224
225 #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:128
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:31
SmartIF< ISvcLocator >
GaudiException
Define general base for Gaudi exception.
Definition: GaudiException.h:22
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:404
Service::m_pAuditorSvc
SmartIF< IAuditorSvc > m_pAuditorSvc
Auditor Service.
Definition: Service.h:193
gaudiComponentHelp.name
name
Definition: gaudiComponentHelp.py:32
StatusCode::FAILURE
Definition: StatusCode.h:31
Gaudi::Property
Implementation of property with value of concrete type.
Definition: Property.h:319
gaudirun.args
args
Definition: gaudirun.py:244
Service::m_initSC
StatusCode m_initSC
Definition: Service.h:169
ToolHandleInfo::isPublic
bool isPublic() const noexcept
Definition: ToolHandle.h:37
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:57
Service::outputLevel
int outputLevel() const
get the Service&#39;s output level
Definition: Service.h:165
Service::m_svcLocator
SmartIF< ISvcLocator > m_svcLocator
Service Locator reference.
Definition: Service.h:175
SvcFactory::create
static S::ReturnType create(Args &&...args)
Definition: Service.h:203
SvcFactory
Definition: Service.h:198
ServiceManager::restart
StatusCode restart() override
Initialization (from RUNNING to RUNNING, via INITIALIZED).
Definition: ServiceManager.cpp:383
Service::m_name
std::string m_name
Service Name.
Definition: Service.h:173
Gaudi::StateMachine::State
State
Allowed states for classes implementing the state machine (ApplicationMgr, Algorithm, Service, AlgTool).
Definition: StateMachine.h:14
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:82
ServiceManager::stop
StatusCode stop() override
Stop (from RUNNING to INITIALIZED).
Definition: ServiceManager.cpp:328
StatusCode
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:28
SmartIF.h
CommonMessaging.h
IMessageSvc
The IMessage is the interface implemented by the message service.
Definition: IMessageSvc.h:38
MSG::NIL
Definition: IMessageSvc.h:16
IOTest.start
start
Definition: IOTest.py:99
IStateful.h
Service::m_initFlag
std::once_flag m_initFlag
Definition: Service.h:170
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:185
IProperty.h
Gaudi::PluginService::Factory
Class wrapping the signature for a factory with any number of arguments.
Definition: PluginService.h:45
Service::declareTool
StatusCode declareTool(ToolHandle< T > &handle, std::string toolTypeAndName, bool createIf=true)
Declare used tool.
Definition: Service.h:137
ServiceManager::reinitialize
StatusCode reinitialize() override
Initialization (from INITIALIZED or RUNNING to INITIALIZED, via CONFIGURED).
Definition: ServiceManager.cpp:362
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:50
Service::m_svcManager
SmartIF< ISvcManager > m_svcManager
Definition: Service.h:176
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:165
Gaudi::StateMachine::OFFLINE
Definition: StateMachine.h:15
Service
Base class for all services.
Definition: Service.h:36
GAUDI_API
#define GAUDI_API
Definition: Kernel.h:110
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
ServiceManager::initialize
StatusCode initialize() override
Initialization (from CONFIGURED to INITIALIZED).
Definition: ServiceManager.cpp:261