Service.h
Go to the documentation of this file.
1 #ifndef GAUDIKERNEL_SERVICE_H
2 #define GAUDIKERNEL_SERVICE_H
3 // ============================================================================
4 // Include files
5 // ============================================================================
6 #include "GaudiKernel/IService.h"
7 #include "GaudiKernel/ISvcLocator.h"
8 #include "GaudiKernel/ServiceLocatorHelper.h"
9 #include "GaudiKernel/IProperty.h"
10 #include "GaudiKernel/IStateful.h"
11 #include "GaudiKernel/PropertyMgr.h"
12 #include "GaudiKernel/Property.h"
13 #include "GaudiKernel/IAuditorSvc.h"
14 #include "GaudiKernel/CommonMessaging.h"
15 #include "GaudiKernel/SmartIF.h"
16 #include <Gaudi/PluginService.h>
17 #include "GaudiKernel/ToolHandle.h"
18 
19 // ============================================================================
20 #include <vector>
21 // ============================================================================
22 // Forward declarations
23 // ============================================================================
24 class IMessageSvc;
25 class ISvcManager;
26 class ServiceManager;
27 // ============================================================================
35 class GAUDI_API Service: public CommonMessaging<implements3<IService, IProperty, IStateful> > {
36 public:
37 #ifndef __REFLEX__
39  const std::string&,
40  ISvcLocator*> Factory;
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 
72  // Default implementations for ISetProperty
73  StatusCode setProperty(const Property& p) override;
74  StatusCode setProperty( const std::string& s ) override;
75  StatusCode setProperty( const std::string& n, const std::string& v) override;
76  StatusCode getProperty(Property* p) const override;
77  const Property& getProperty( const std::string& name) const override;
78  StatusCode getProperty( const std::string& n, std::string& v ) const override;
79  const std::vector<Property*>& getProperties( ) const override;
80  bool hasProperty(const std::string& name) const override;
81 
122  template <class TYPE>
124  ( const std::string& name ,
125  const TYPE& value )
126  { return Gaudi::Utils::setProperty ( m_propertyMgr.get() , name , value ) ; }
127 
129  Service( std::string name, ISvcLocator* svcloc);
131  SmartIF<ISvcLocator>& serviceLocator() const override;
132 
136  StatusCode setProperties();
137 
140  template <class T>
141  StatusCode service( const std::string& name, const T*& psvc, bool createIf = true ) const {
142  ISvcLocator& svcLoc = *serviceLocator();
143  auto ptr =
144  ServiceLocatorHelper(svcLoc, *this).service<T>(name, !createIf, // quiet
145  createIf);
146  if (ptr) {
147  psvc = ptr.get();
148  const_cast<T*>(psvc)->addRef();
149  return StatusCode::SUCCESS;
150  }
151  // else
152  psvc = nullptr;
153  return StatusCode::FAILURE;
154  }
155 
156  template <class T>
157  StatusCode service( const std::string& name, T*& psvc, bool createIf = true ) const {
158  auto ptr = service<T>(name,createIf);
159  psvc = ( ptr ? ptr.get() : nullptr );
160  if (psvc) {
161  psvc->addRef();
162  return StatusCode::SUCCESS;
163  }
164  return StatusCode::FAILURE;
165  }
166 
167  template <typename IFace = IService>
168  SmartIF<IFace> service(const std::string& name, bool createIf = true) const {
169  return ServiceLocatorHelper(*serviceLocator(), *this).
170  service<IFace>(name, !createIf, // quiet
171  createIf);
172  }
173 
176  template <class T>
177  StatusCode service( const std::string& svcType, const std::string& svcName,
178  T*& psvc) const {
179  return service(svcType + "/" + svcName, psvc);
180  }
181  // ==========================================================================
210  template <class T>
211  Property* declareProperty
212  ( const std::string& name ,
213  T& property ,
214  const std::string& doc = "none" ) const
215  {
216  return m_propertyMgr -> declareProperty ( name , property , doc ) ;
217  }
218  // ==========================================================================
227  Property* declareRemoteProperty
228  ( const std::string& name ,
229  IProperty* rsvc ,
230  const std::string& rname = "" ) const
231  {
232  return m_propertyMgr -> declareRemoteProperty ( name , rsvc , rname ) ;
233  }
234 
242  template<class T>
243  StatusCode declarePrivateTool(ToolHandle<T> & handle, std::string toolTypeAndName =
244  "", bool createIf = true) {
245 
246  if (toolTypeAndName == "")
247  toolTypeAndName = System::typeinfoName(typeid(T));
248 
249  StatusCode sc = handle.initialize(toolTypeAndName, this, createIf);
250 
251  MsgStream log(msgSvc(), name());
252 
253  if (sc.isSuccess()) {
254  log << MSG::DEBUG << "Handle for private tool" << toolTypeAndName
255  << " successfully created and stored." << endmsg;
256  } else {
257 
258  log << MSG::ERROR << "Handle for private tool" << toolTypeAndName
259  << " could not be created." << endmsg;
260  }
261 
262  return sc;
263 
264  }
265 
273  template<class T>
274  StatusCode declarePublicTool(ToolHandle<T> & handle, std::string toolTypeAndName =
275  "", bool createIf = true) {
276 
277  if (toolTypeAndName == "")
278  toolTypeAndName = System::typeinfoName(typeid(T));
279 
280  StatusCode sc = handle.initialize(toolTypeAndName, 0, createIf);
281 
282  MsgStream log(msgSvc(), name());
283 
284  if (sc.isSuccess()) {
285  log << MSG::DEBUG << "Handle for public tool" << toolTypeAndName
286  << " successfully created and stored." << endmsg;
287  } else {
288 
289  log << MSG::ERROR << "Handle for public tool" << toolTypeAndName
290  << " could not be created." << endmsg;
291  }
292 
293  return sc;
294 
295  }
296 
297  // ==========================================================================
301  SmartIF<IAuditorSvc>& auditorSvc() const;
302 
303 protected:
305  ~Service() override;
307  IntegerProperty m_outputLevel = MSG::NIL;
312 
314  int outputLevel() const { return m_outputLevel.value(); }
315 
316 private:
318  std::string m_name;
320  mutable SmartIF<ISvcLocator> m_svcLocator;
321  SmartIF<ISvcManager> m_svcManager;
323  SmartIF<PropertyMgr> m_propertyMgr;
324 
325  void setServiceManager(ISvcManager* ism) override;
326 
328  mutable SmartIF<IAuditorSvc> m_pAuditorSvc;
329  BooleanProperty m_auditInit;
330  bool m_auditorInitialize;
331  bool m_auditorStart;
332  bool m_auditorStop;
333  bool m_auditorFinalize;
334  bool m_auditorReinitialize;
335  bool m_auditorRestart;
336 
338  void initOutputLevel(Property& prop);
339 };
340 
341 #ifndef GAUDI_NEW_PLUGIN_SERVICE
342 template <class T>
343 class SvcFactory {
344 public:
345 #ifndef __REFLEX__
346  template <typename S, typename... Args>
347  static typename S::ReturnType create(Args&&... args) {
348  return new T(std::forward<Args>(args)...);
349  }
350 #endif
351 };
352 
353 // Macros to declare component factories
354 #define DECLARE_SERVICE_FACTORY(x) \
355  DECLARE_FACTORY_WITH_CREATOR(x, SvcFactory< x >, Service::Factory)
356 #define DECLARE_NAMED_SERVICE_FACTORY(x, n) \
357  DECLARE_FACTORY_WITH_CREATOR_AND_ID(x, SvcFactory< x >, #n, Service::Factory)
358 #define DECLARE_NAMESPACE_SERVICE_FACTORY(n, x) \
359  DECLARE_SERVICE_FACTORY(n::x)
360 
361 #else
362 
363 // macros to declare factories
364 #define DECLARE_SERVICE_FACTORY(x) DECLARE_COMPONENT(x)
365 #define DECLARE_NAMED_SERVICE_FACTORY(x, n) DECLARE_COMPONENT_WITH_ID(x, #n)
366 #define DECLARE_NAMESPACE_SERVICE_FACTORY(n, x) DECLARE_COMPONENT(n::x)
367 
368 #endif
369 
370 #endif // GAUDIKERNEL_SERVICE_H
The ServiceManager class is in charge of the creation of concrete instances of Services.
StatusCode setProperty(IProperty *component, const std::string &name, const TYPE &value, const std::string &doc)
simple function to set the property of the given object from the value
Definition: Property.h:1187
Definition of the MsgStream class used to transmit messages.
Definition: MsgStream.h:24
an helper to share the implementation of service() among the various kernel base classes ...
def initialize()
Definition: AnalysisTest.py:12
The ISvcLocator is the interface implemented by the Service Factory in the Application Manager to loc...
Definition: ISvcLocator.h:25
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:244
StatusCode finalize() override
Finalize (from INITIALIZED to CONFIGURED).
#define GAUDI_API
Definition: Kernel.h:107
The ISvcManager is the interface implemented by the Service Factory in the Application Manager to sup...
Definition: ISvcManager.h:28
SmartIF< IMessageSvc > & msgSvc() const
The standard message service.
bool isSuccess() const
Test for a status code of SUCCESS.
Definition: StatusCode.h:76
SmartIF< ISvcLocator > & serviceLocator() const override
Function needed by CommonMessaging.
GAUDI_API const std::string typeinfoName(const std::type_info &)
Get platform independent information about the class type.
Definition: System.cpp:297
GAUDI_API bool hasProperty(const IProperty *p, const std::string &name)
simple function which check the existence of the property with the given name.
Definition: Property.cpp:159
static S::ReturnType create(Args &&...args)
Definition: Service.h:347
StatusCode restart() override
Initialization (from RUNNING to RUNNING, via INITIALIZED).
State
Allowed states for classes implementing the state machine (ApplicationMgr, Algorithm, Service, AlgTool).
Definition: StateMachine.h:12
const char *PyHelper() getProperty(IInterface *p, char *name)
Definition: Bootstrap.cpp:258
TYPE * get() const
Get interface pointer.
Definition: SmartIF.h:76
StatusCode stop() override
Stop (from RUNNING to INITIALIZED).
General service interface definition.
Definition: IService.h:18
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:26
The IMessage is the interface implemented by the message service.
Definition: IMessageSvc.h:57
bool PyHelper() setProperty(IInterface *p, char *name, char *value)
Definition: Bootstrap.cpp:254
const TYPE & value() const
explicit conversion
Definition: Property.h:341
Handle to be used in lieu of naked pointers to tools.
Definition: PropertyMgr.h:21
StatusCode initialize(const std::string &toolTypeAndName, const IInterface *parent=nullptr, bool createIf=true)
Definition: ToolHandle.h:156
Property base class allowing Property* collections to be "homogeneous".
Definition: Property.h:38
list args
Definition: gaudirun.py:290
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).
string s
Definition: gaudirun.py:245
SmartIF< IService > service(const std::string &name, const bool quiet=false, const bool createIf=true) const
SmartIF< IService > & service(const Gaudi::Utils::TypeNameString &typeName, const bool createIf=true) override
Returns a smart pointer to a service.
The IProperty is the basic interface for all components which have properties that can be set or get...
Definition: IProperty.h:21
Base class for all services.
Definition: Service.h:35
tuple start
Definition: IOTest.py:88