The Gaudi Framework  v28r3 (cc1cf868)
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  if ( toolTypeAndName == "" ) toolTypeAndName = handle.typeAndName();
141 
142  StatusCode sc = handle.initialize( toolTypeAndName, handle.isPublic() ? nullptr : this, createIf );
143  if ( UNLIKELY( !sc ) ) {
144  throw GaudiException{std::string{"Cannot create handle for "} + ( handle.isPublic() ? "public" : "private" ) +
145  " tool " + toolTypeAndName,
146  name(), sc};
147  }
148 
149  return sc;
150  }
151 
152  // ==========================================================================
156  SmartIF<IAuditorSvc>& auditorSvc() const;
157 
158 protected:
160  ~Service() override;
165 
167  int outputLevel() const { return m_outputLevel.value(); }
168 
169 private:
170  void sysInitialize_imp();
173 
179 
180  void setServiceManager( ISvcManager* ism ) override;
181 
182 protected:
183  // Properties
184 
185  Gaudi::Property<int> m_outputLevel{this, "OutputLevel", MSG::NIL, "output level"};
186  Gaudi::Property<bool> m_auditInit{this, "AuditServices", false, "[[deprecated]] unused"};
187  Gaudi::Property<bool> m_auditorInitialize{this, "AuditInitialize", false, "trigger auditor on initialize()"};
188  Gaudi::Property<bool> m_auditorStart{this, "AuditStart", false, "trigger auditor on start()"};
189  Gaudi::Property<bool> m_auditorStop{this, "AuditStop", false, "trigger auditor on stop()"};
190  Gaudi::Property<bool> m_auditorFinalize{this, "AuditFinalize", false, "trigger auditor on finalize()"};
191  Gaudi::Property<bool> m_auditorReinitialize{this, "AuditReinitialize", false, "trigger auditor on reinitialize()"};
192  Gaudi::Property<bool> m_auditorRestart{this, "AuditRestart", false, "trigger auditor on restart()"};
193 
196 };
197 
198 #ifndef GAUDI_NEW_PLUGIN_SERVICE
199 template <class T>
201 {
202 public:
203 #ifndef __REFLEX__
204  template <typename S, typename... Args>
205  static typename S::ReturnType create( Args&&... args )
206  {
207  return new T( std::forward<Args>( args )... );
208  }
209 #endif
210 };
211 
212 // Macros to declare component factories
213 #define DECLARE_SERVICE_FACTORY( x ) DECLARE_FACTORY_WITH_CREATOR( x, SvcFactory<x>, Service::Factory )
214 #define DECLARE_NAMED_SERVICE_FACTORY( x, n ) \
215  DECLARE_FACTORY_WITH_CREATOR_AND_ID( x, SvcFactory<x>, #n, Service::Factory )
216 #define DECLARE_NAMESPACE_SERVICE_FACTORY( n, x ) DECLARE_SERVICE_FACTORY( n::x )
217 
218 #else
219 
220 // macros to declare factories
221 #define DECLARE_SERVICE_FACTORY( x ) DECLARE_COMPONENT( x )
222 #define DECLARE_NAMED_SERVICE_FACTORY( x, n ) DECLARE_COMPONENT_WITH_ID( x, #n )
223 #define DECLARE_NAMESPACE_SERVICE_FACTORY( n, x ) DECLARE_COMPONENT( n::x )
224 
225 #endif
226 
227 #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 ...
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:195
Implementation of property with value of concrete type.
Definition: Property.h:319
StatusCode m_initSC
Definition: Service.h:171
bool isPublic() const noexcept
Definition: ToolHandle.h:38
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:167
#define UNLIKELY(x)
Definition: Kernel.h:126
SmartIF< ISvcLocator > m_svcLocator
Service Locator reference.
Definition: Service.h:177
static S::ReturnType create(Args &&...args)
Definition: Service.h:205
StatusCode restart() override
Initialization (from RUNNING to RUNNING, via INITIALIZED).
std::string m_name
Service Name.
Definition: Service.h:175
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
StatusCode stop() override
Stop (from RUNNING to INITIALIZED).
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:26
std::string typeAndName() const override
Definition: ToolHandle.h:249
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:172
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:210
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:178
StatusCode declareTool(ToolHandle< T > &handle, std::string toolTypeAndName="", bool createIf=true)
Declare used tool.
Definition: Service.h:137
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: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
StatusCode initialize() override
Initialization (from CONFIGURED to INITIALIZED).