Loading [MathJax]/extensions/tex2jax.js
The Gaudi Framework  v28r2p1 (f1a77ff4)
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
Service.h
Go to the documentation of this file.
1 #ifndef GAUDIKERNEL_SERVICE_H
2 #define GAUDIKERNEL_SERVICE_H
3 // ============================================================================
4 // Include files
5 // ============================================================================
9 #include "GaudiKernel/IService.h"
10 #include "GaudiKernel/IStateful.h"
12 #include "GaudiKernel/Property.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__
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;
194 
196  int outputLevel() const { return m_outputLevel.value(); }
197 
198 private:
199  void sysInitialize_imp();
202 
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 
225 };
226 
227 #ifndef GAUDI_NEW_PLUGIN_SERVICE
228 template <class T>
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
The ServiceManager class is in charge of the creation of concrete instances of Services.
an helper to share the implementation of service() among the various kernel base classes ...
The ISvcLocator is the interface implemented by the Service Factory in the Application Manager to loc...
Definition: ISvcLocator.h:25
StatusCode finalize() override
Finalize (from INITIALIZED to CONFIGURED).
SmartIF< IAuditorSvc > m_pAuditorSvc
Auditor Service.
Definition: Service.h:224
Implementation of property with value of concrete type.
Definition: Property.h:314
GAUDI_API const std::string typeinfoName(const std::type_info &)
Get platform independent information about the class type.
Definition: System.cpp:299
StatusCode m_initSC
Definition: Service.h:200
The ISvcManager is the interface implemented by the Service Factory in the Application Manager to sup...
Definition: ISvcManager.h:28
bool isSuccess() const
Test for a status code of SUCCESS.
Definition: StatusCode.h:74
SmartIF< ISvcLocator > & serviceLocator() const override
Function needed by CommonMessaging.
int outputLevel() const
get the Service&#39;s output level
Definition: Service.h:196
#define UNLIKELY(x)
Definition: Kernel.h:126
SmartIF< ISvcLocator > m_svcLocator
Service Locator reference.
Definition: Service.h:206
static S::ReturnType create(Args &&...args)
Definition: Service.h:234
StatusCode restart() override
Initialization (from RUNNING to RUNNING, via INITIALIZED).
std::string m_name
Service Name.
Definition: Service.h:204
State
Allowed states for classes implementing the state machine (ApplicationMgr, Algorithm, Service, AlgTool).
Definition: StateMachine.h:12
StatusCode configure() override
Definition: Service.h:48
STL class.
TYPE * get() const
Get interface pointer.
Definition: SmartIF.h:76
MsgStream & error() const
shortcut for the method msgStream(MSG::ERROR)
StatusCode stop() override
Stop (from RUNNING to INITIALIZED).
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:26
StatusCode declarePrivateTool(ToolHandle< T > &handle, std::string toolTypeAndName="", bool createIf=true)
Declare used Private tool.
Definition: Service.h:137
The IMessage is the interface implemented by the message service.
Definition: IMessageSvc.h:57
start
Definition: IOTest.py:88
std::once_flag m_initFlag
Definition: Service.h:201
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
SmartIF< IFace > service(const std::string &name, bool createIf=true) const
Definition: Service.h:114
Handle to be used in lieu of naked pointers to tools.
StatusCode initialize(const std::string &toolTypeAndName, const IInterface *parent=nullptr, bool createIf=true)
Definition: ToolHandle.h:188
StatusCode declarePublicTool(ToolHandle< T > &handle, std::string toolTypeAndName="", bool createIf=true)
Declare used Public tool.
Definition: Service.h:163
MsgStream & debug() const
shortcut for the method msgStream(MSG::DEBUG)
Class wrapping the signature for a factory with any number of arguments.
Definition: PluginService.h:47
StatusCode reinitialize() override
Initialization (from INITIALIZED or RUNNING to INITIALIZED, via CONFIGURED).
Helper class to implement the IProperty interface.
StatusCode terminate() override
Definition: Service.h:53
Gaudi::StateMachine::State FSMState() const override
Definition: Service.h:54
Gaudi::StateMachine::State targetFSMState() const override
Definition: Service.h:55
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
SmartIF< IService > service(const std::string &name, const bool quiet=false, const bool createIf=true) const
SmartIF< ISvcManager > m_svcManager
Definition: Service.h:207
SmartIF< IService > & service(const Gaudi::Utils::TypeNameString &typeName, const bool createIf=true) override
Returns a smart pointer to a service.
Base class for all services.
Definition: Service.h:36
MSG::Level msgLevel() const
get the output level from the embedded MsgStream
#define GAUDI_API
Definition: Kernel.h:107
Gaudi::PluginService::Factory< IService *, const std::string &, ISvcLocator * > Factory
Definition: Service.h:40
StatusCode service(const std::string &name, T *&psvc, bool createIf=true) const
Definition: Service.h:102
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:244
StatusCode initialize() override
Initialization (from CONFIGURED to INITIALIZED).