The Gaudi Framework  v29r0 (ff2e7097)
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 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;
163 
165  int outputLevel() const { return m_outputLevel.value(); }
166 
167 private:
168  void sysInitialize_imp();
171 
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 
194 };
195 
196 #ifndef GAUDI_NEW_PLUGIN_SERVICE
197 template <class T>
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
The ServiceManager class is in charge of the creation of concrete instances of Services.
#define UNLIKELY(x)
Definition: Kernel.h:128
an helper to share the implementation of service() among the various kernel base classes ...
Define general base for Gaudi exception.
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:193
Implementation of property with value of concrete type.
Definition: Property.h:319
StatusCode m_initSC
Definition: Service.h:169
bool isPublic() const noexcept
Definition: ToolHandle.h:37
The ISvcManager is the interface implemented by the Service Factory in the Application Manager to sup...
Definition: ISvcManager.h:28
SmartIF< ISvcLocator > & serviceLocator() const override
Function needed by CommonMessaging.
int outputLevel() const
get the Service&#39;s output level
Definition: Service.h:165
SmartIF< ISvcLocator > m_svcLocator
Service Locator reference.
Definition: Service.h:175
static S::ReturnType create(Args &&...args)
Definition: Service.h:203
StatusCode restart() override
Initialization (from RUNNING to RUNNING, via INITIALIZED).
std::string m_name
Service Name.
Definition: Service.h:173
State
Allowed states for classes implementing the state machine (ApplicationMgr, Algorithm, Service, AlgTool).
Definition: StateMachine.h:14
StatusCode configure() override
Definition: Service.h:48
STL class.
TYPE * get() const
Get interface pointer.
Definition: SmartIF.h:82
StatusCode stop() override
Stop (from RUNNING to INITIALIZED).
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:28
The IMessage is the interface implemented by the message service.
Definition: IMessageSvc.h:38
start
Definition: IOTest.py:99
std::once_flag m_initFlag
Definition: Service.h:170
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:185
Class wrapping the signature for a factory with any number of arguments.
Definition: PluginService.h:45
StatusCode declareTool(ToolHandle< T > &handle, std::string toolTypeAndName, bool createIf=true)
Declare used tool.
Definition: Service.h:137
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:176
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
#define GAUDI_API
Definition: Kernel.h:110
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
StatusCode initialize() override
Initialization (from CONFIGURED to INITIALIZED).