The Gaudi Framework  v30r3 (a5ef0a68)
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  using Factory = Gaudi::PluginService::Factory<IService*( const std::string&, ISvcLocator* )>;
40 
41  friend class ServiceManager;
42 
44  const std::string& name() const override;
45 
46  // State machine implementation
47  StatusCode configure() override { return StatusCode::SUCCESS; }
48  StatusCode initialize() override;
49  StatusCode start() override;
50  StatusCode stop() override;
51  StatusCode finalize() override;
52  StatusCode terminate() override { return StatusCode::SUCCESS; }
53  Gaudi::StateMachine::State FSMState() const override { return m_state; }
54  Gaudi::StateMachine::State targetFSMState() const override { return m_targetState; }
55  StatusCode reinitialize() override;
56  StatusCode restart() override;
57 
59  StatusCode sysInitialize() override;
61  StatusCode sysStart() override;
63  StatusCode sysStop() override;
65  StatusCode sysFinalize() override;
67  StatusCode sysReinitialize() override;
69  StatusCode sysRestart() override;
70 
72  Service( std::string name, ISvcLocator* svcloc );
74  SmartIF<ISvcLocator>& serviceLocator() const override;
75 
79  StatusCode setProperties();
80 
83  template <class T>
84  StatusCode service( const std::string& name, const T*& psvc, bool createIf = true ) const
85  {
86  ISvcLocator& svcLoc = *serviceLocator();
87  auto ptr = ServiceLocatorHelper( svcLoc, *this )
88  .service<T>( name, !createIf, // quiet
89  createIf );
90  if ( ptr ) {
91  psvc = ptr.get();
92  const_cast<T*>( psvc )->addRef();
93  return StatusCode::SUCCESS;
94  }
95  // else
96  psvc = nullptr;
97  return StatusCode::FAILURE;
98  }
99 
100  template <class T>
101  StatusCode service( const std::string& name, T*& psvc, bool createIf = true ) const
102  {
103  auto ptr = service<T>( name, createIf );
104  psvc = ( ptr ? ptr.get() : nullptr );
105  if ( psvc ) {
106  psvc->addRef();
107  return StatusCode::SUCCESS;
108  }
109  return StatusCode::FAILURE;
110  }
111 
112  template <typename IFace = IService>
113  SmartIF<IFace> service( const std::string& name, bool createIf = true ) const
114  {
115  return ServiceLocatorHelper( *serviceLocator(), *this )
116  .service<IFace>( name, !createIf, // quiet
117  createIf );
118  }
119 
122  template <class T>
123  StatusCode service( const std::string& svcType, const std::string& svcName, T*& psvc ) const
124  {
125  return service( svcType + "/" + svcName, psvc );
126  }
127 
135  template <class T>
136  StatusCode declareTool( ToolHandle<T>& handle, std::string toolTypeAndName, bool createIf = true )
137  {
138 
139  StatusCode sc = handle.initialize( toolTypeAndName, handle.isPublic() ? nullptr : this, createIf );
140  if ( UNLIKELY( !sc ) ) {
141  throw GaudiException{std::string{"Cannot create handle for "} + ( handle.isPublic() ? "public" : "private" ) +
142  " tool " + toolTypeAndName,
143  name(), sc};
144  }
145 
146  return sc;
147  }
148 
149  // ==========================================================================
153  SmartIF<IAuditorSvc>& auditorSvc() const;
154 
155 protected:
157  ~Service() override;
162 
164  int outputLevel() const { return m_outputLevel.value(); }
165 
166 private:
167  void sysInitialize_imp();
170 
176 
177  void setServiceManager( ISvcManager* ism ) override;
178 
179 protected:
180  // Properties
181 
182  Gaudi::Property<int> m_outputLevel{this, "OutputLevel", MSG::NIL, "output level"};
183  Gaudi::Property<bool> m_auditInit{this, "AuditServices", false, "[[deprecated]] unused"};
184  Gaudi::Property<bool> m_auditorInitialize{this, "AuditInitialize", false, "trigger auditor on initialize()"};
185  Gaudi::Property<bool> m_auditorStart{this, "AuditStart", false, "trigger auditor on start()"};
186  Gaudi::Property<bool> m_auditorStop{this, "AuditStop", false, "trigger auditor on stop()"};
187  Gaudi::Property<bool> m_auditorFinalize{this, "AuditFinalize", false, "trigger auditor on finalize()"};
188  Gaudi::Property<bool> m_auditorReinitialize{this, "AuditReinitialize", false, "trigger auditor on reinitialize()"};
189  Gaudi::Property<bool> m_auditorRestart{this, "AuditRestart", false, "trigger auditor on restart()"};
190 
193 };
194 
195 #endif // GAUDIKERNEL_SERVICE_H
The ServiceManager class is in charge of the creation of concrete instances of Services.
#define UNLIKELY(x)
Definition: Kernel.h:122
constexpr static const auto FAILURE
Definition: StatusCode.h:88
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:192
Implementation of property with value of concrete type.
Definition: Property.h:381
StatusCode m_initSC
Definition: Service.h:168
bool isPublic() const noexcept
Definition: ToolHandle.h:41
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:164
SmartIF< ISvcLocator > m_svcLocator
Service Locator reference.
Definition: Service.h:174
StatusCode restart() override
Initialization (from RUNNING to RUNNING, via INITIALIZED).
std::string m_name
Service Name.
Definition: Service.h:172
State
Allowed states for classes implementing the state machine (ApplicationMgr, Algorithm, Service, AlgTool).
Definition: StateMachine.h:14
StatusCode configure() override
Definition: Service.h:47
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:51
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:169
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:123
constexpr static const auto SUCCESS
Definition: StatusCode.h:87
SmartIF< IFace > service(const std::string &name, bool createIf=true) const
Definition: Service.h:113
Handle to be used in lieu of naked pointers to tools.
Definition: ToolHandle.h:130
StatusCode initialize(const std::string &toolTypeAndName, const IInterface *parent=nullptr, bool createIf=true)
Definition: ToolHandle.h:214
Gaudi::PluginService::Factory< IService *(const std::string &, ISvcLocator *)> Factory
Definition: Service.h:39
StatusCode declareTool(ToolHandle< T > &handle, std::string toolTypeAndName, bool createIf=true)
Declare used tool.
Definition: Service.h:136
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:52
Gaudi::StateMachine::State FSMState() const override
Definition: Service.h:53
Gaudi::StateMachine::State targetFSMState() const override
Definition: Service.h:54
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:84
SmartIF< IService > service(const std::string &name, const bool quiet=false, const bool createIf=true) const
SmartIF< ISvcManager > m_svcManager
Definition: Service.h:175
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:104
StatusCode service(const std::string &name, T *&psvc, bool createIf=true) const
Definition: Service.h:101
StatusCode initialize() override
Initialization (from CONFIGURED to INITIALIZED).