The Gaudi Framework  v32r2 (46d42edc)
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 public:
38  using Factory = Gaudi::PluginService::Factory<IService*( const std::string&, ISvcLocator* )>;
39 
40  friend class ServiceManager;
41 
43  const std::string& name() const override;
44 
45  // State machine implementation
46  StatusCode configure() override { return StatusCode::SUCCESS; }
47  StatusCode initialize() override;
48  StatusCode start() override;
49  StatusCode stop() override;
50  StatusCode finalize() override;
51  StatusCode terminate() override { return StatusCode::SUCCESS; }
52  Gaudi::StateMachine::State FSMState() const override { return m_state; }
53  Gaudi::StateMachine::State targetFSMState() const override { return m_targetState; }
54  StatusCode reinitialize() override;
55  StatusCode restart() override;
56 
58  StatusCode sysInitialize() override;
60  StatusCode sysStart() override;
62  StatusCode sysStop() override;
64  StatusCode sysFinalize() override;
66  StatusCode sysReinitialize() override;
68  StatusCode sysRestart() override;
69 
71  Service( std::string name, ISvcLocator* svcloc );
73  SmartIF<ISvcLocator>& serviceLocator() const override;
74 
78  StatusCode setProperties();
79 
82  template <class T>
83  StatusCode service( const std::string& name, const T*& psvc, bool createIf = true ) const {
84  ISvcLocator& svcLoc = *serviceLocator();
85  auto ptr = ServiceLocatorHelper( svcLoc, *this )
86  .service<T>( name, !createIf, // quiet
87  createIf );
88  if ( ptr ) {
89  psvc = ptr.get();
90  const_cast<T*>( psvc )->addRef();
91  return StatusCode::SUCCESS;
92  }
93  // else
94  psvc = nullptr;
95  return StatusCode::FAILURE;
96  }
97 
98  template <class T>
99  StatusCode service( const std::string& name, T*& psvc, bool createIf = true ) const {
100  auto ptr = service<T>( name, createIf );
101  psvc = ( ptr ? ptr.get() : nullptr );
102  if ( psvc ) {
103  psvc->addRef();
104  return StatusCode::SUCCESS;
105  }
106  return StatusCode::FAILURE;
107  }
108 
109  template <typename IFace = IService>
110  SmartIF<IFace> service( const std::string& name, bool createIf = true ) const {
111  return ServiceLocatorHelper( *serviceLocator(), *this )
112  .service<IFace>( name, !createIf, // quiet
113  createIf );
114  }
115 
118  template <class T>
119  StatusCode service( const std::string& svcType, const std::string& svcName, T*& psvc ) const {
120  return service( svcType + "/" + svcName, psvc );
121  }
122 
130  template <class T>
131  StatusCode declareTool( ToolHandle<T>& handle, std::string toolTypeAndName, bool createIf = true ) {
132 
133  StatusCode sc = handle.initialize( toolTypeAndName, handle.isPublic() ? nullptr : this, createIf );
134  if ( UNLIKELY( !sc ) ) {
135  throw GaudiException{std::string{"Cannot create handle for "} + ( handle.isPublic() ? "public" : "private" ) +
136  " tool " + toolTypeAndName,
137  name(), sc};
138  }
139 
140  return sc;
141  }
142 
143  // ==========================================================================
147  SmartIF<IAuditorSvc>& auditorSvc() const;
148 
149 protected:
151  ~Service() override;
156 
158  int outputLevel() const { return m_outputLevel.value(); }
159 
160 private:
161  void sysInitialize_imp();
164 
170 
171  void setServiceManager( ISvcManager* ism ) override;
172 
173 protected:
174  // Properties
175 
176  Gaudi::Property<int> m_outputLevel{this, "OutputLevel", MSG::NIL, "output level"};
177  Gaudi::Property<bool> m_auditInit{this, "AuditServices", false, "[[deprecated]] unused"};
178  Gaudi::Property<bool> m_auditorInitialize{this, "AuditInitialize", false, "trigger auditor on initialize()"};
179  Gaudi::Property<bool> m_auditorStart{this, "AuditStart", false, "trigger auditor on start()"};
180  Gaudi::Property<bool> m_auditorStop{this, "AuditStop", false, "trigger auditor on stop()"};
181  Gaudi::Property<bool> m_auditorFinalize{this, "AuditFinalize", false, "trigger auditor on finalize()"};
182  Gaudi::Property<bool> m_auditorReinitialize{this, "AuditReinitialize", false, "trigger auditor on reinitialize()"};
183  Gaudi::Property<bool> m_auditorRestart{this, "AuditRestart", false, "trigger auditor on restart()"};
184 
187 };
188 
189 #endif // GAUDIKERNEL_SERVICE_H
The ServiceManager class is in charge of the creation of concrete instances of Services.
#define UNLIKELY(x)
Definition: Kernel.h:96
an helper to share the implementation of service() among the various kernel base classes
SmartIF< IFace > service(const std::string &name, bool createIf=true) const
Definition: Service.h:110
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:186
Implementation of property with value of concrete type.
Definition: Property.h:352
Gaudi::StateMachine::State FSMState() const override
Definition: Service.h:52
StatusCode m_initSC
Definition: Service.h:162
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.
constexpr static const auto SUCCESS
Definition: StatusCode.h:85
SmartIF< ISvcLocator > m_svcLocator
Service Locator reference.
Definition: Service.h:168
StatusCode restart() override
Initialization (from RUNNING to RUNNING, via INITIALIZED).
std::string m_name
Service Name.
Definition: Service.h:166
State
Allowed states for classes implementing the state machine (ApplicationMgr, Algorithm,...
Definition: StateMachine.h:12
TYPE * get() const
Get interface pointer.
Definition: SmartIF.h:76
def start
Definition: IOTest.py:98
StatusCode configure() override
Definition: Service.h:46
STL class.
StatusCode stop() override
Stop (from RUNNING to INITIALIZED).
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't already exist.
Definition: Service.h:119
General service interface definition.
Definition: IService.h:18
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:50
bool isPublic() const noexcept
Definition: ToolHandle.h:40
The IMessage is the interface implemented by the message service.
Definition: IMessageSvc.h:37
SmartIF< IService > service(const std::string &name, const bool quiet=false, const bool createIf=true) const
std::once_flag m_initFlag
Definition: Service.h:163
int outputLevel() const
get the Service's output level
Definition: Service.h:158
Handle to be used in lieu of naked pointers to tools.
Definition: ToolHandle.h:125
StatusCode initialize(const std::string &toolTypeAndName, const IInterface *parent=nullptr, bool createIf=true)
Definition: ToolHandle.h:200
Gaudi::PluginService::Factory< IService *(const std::string &, ISvcLocator *)> Factory
Definition: Service.h:38
StatusCode declareTool(ToolHandle< T > &handle, std::string toolTypeAndName, bool createIf=true)
Declare used tool.
Definition: Service.h:131
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:51
constexpr static const auto FAILURE
Definition: StatusCode.h:86
StatusCode service(const std::string &name, T *&psvc, bool createIf=true) const
Definition: Service.h:99
StatusCode service(const std::string &name, const T *&psvc, bool createIf=true) const
Access a service by name, creating it if it doesn't already exist.
Definition: Service.h:83
SmartIF< ISvcManager > m_svcManager
Definition: Service.h:169
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:71
Gaudi::StateMachine::State targetFSMState() const override
Definition: Service.h:53
StatusCode initialize() override
Initialization (from CONFIGURED to INITIALIZED).