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"
10 #include "GaudiKernel/IStateful.h"
12 #include "GaudiKernel/Property.h"
15 #include "GaudiKernel/SmartIF.h"
16 #include <Gaudi/PluginService.h>
17 #include "GaudiKernel/ToolHandle.h"
18 
19 // ============================================================================
20 #include <vector>
21 #include <mutex>
22 // ============================================================================
23 // Forward declarations
24 // ============================================================================
25 class IMessageSvc;
26 class ISvcManager;
27 class ServiceManager;
28 // ============================================================================
36 class GAUDI_API Service: public CommonMessaging<implements<IService,
37  IProperty,
38  IStateful> > {
39 public:
40 #ifndef __REFLEX__
42  const std::string&,
44 #endif
45  friend class ServiceManager;
46 
48  const std::string& name() const override;
49 
50  // State machine implementation
51  StatusCode configure() override { return StatusCode::SUCCESS; }
52  StatusCode initialize() override;
53  StatusCode start() override;
54  StatusCode stop() override;
55  StatusCode finalize() override;
56  StatusCode terminate() override { return StatusCode::SUCCESS; }
57  Gaudi::StateMachine::State FSMState() const override { return m_state; }
58  Gaudi::StateMachine::State targetFSMState() const override { return m_targetState; }
59  StatusCode reinitialize() override;
60  StatusCode restart() override;
61 
63  StatusCode sysInitialize() override;
65  StatusCode sysStart() override;
67  StatusCode sysStop() override;
69  StatusCode sysFinalize() override;
71  StatusCode sysReinitialize() override;
73  StatusCode sysRestart() override;
74 
75  // Default implementations for ISetProperty
76  StatusCode setProperty(const Property& p) override;
77  StatusCode setProperty( const std::string& s ) override;
78  StatusCode setProperty( const std::string& n, const std::string& v) override;
79  StatusCode getProperty(Property* p) const override;
80  const Property& getProperty( const std::string& name) const override;
81  StatusCode getProperty( const std::string& n, std::string& v ) const override;
82  const std::vector<Property*>& getProperties( ) const override;
83  bool hasProperty(const std::string& name) const override;
84 
125  template <class TYPE>
127  ( const std::string& name ,
128  const TYPE& value )
129  { return Gaudi::Utils::setProperty ( m_propertyMgr.get() , name , value ) ; }
130 
132  Service( std::string name, ISvcLocator* svcloc);
134  SmartIF<ISvcLocator>& serviceLocator() const override;
135 
139  StatusCode setProperties();
140 
143  template <class T>
144  StatusCode service( const std::string& name, const T*& psvc, bool createIf = true ) const {
145  ISvcLocator& svcLoc = *serviceLocator();
146  auto ptr =
147  ServiceLocatorHelper(svcLoc, *this).service<T>(name, !createIf, // quiet
148  createIf);
149  if (ptr) {
150  psvc = ptr.get();
151  const_cast<T*>(psvc)->addRef();
152  return StatusCode::SUCCESS;
153  }
154  // else
155  psvc = nullptr;
156  return StatusCode::FAILURE;
157  }
158 
159  template <class T>
160  StatusCode service( const std::string& name, T*& psvc, bool createIf = true ) const {
161  auto ptr = service<T>(name,createIf);
162  psvc = ( ptr ? ptr.get() : nullptr );
163  if (psvc) {
164  psvc->addRef();
165  return StatusCode::SUCCESS;
166  }
167  return StatusCode::FAILURE;
168  }
169 
170  template <typename IFace = IService>
171  SmartIF<IFace> service(const std::string& name, bool createIf = true) const {
172  return ServiceLocatorHelper(*serviceLocator(), *this).
173  service<IFace>(name, !createIf, // quiet
174  createIf);
175  }
176 
179  template <class T>
180  StatusCode service( const std::string& svcType, const std::string& svcName,
181  T*& psvc) const {
182  return service(svcType + "/" + svcName, psvc);
183  }
184  // ==========================================================================
213  template <class T>
214  Property* declareProperty
215  ( const std::string& name ,
216  T& property ,
217  const std::string& doc = "none" ) const
218  {
219  return m_propertyMgr -> declareProperty ( name , property , doc ) ;
220  }
221  // ==========================================================================
230  Property* declareRemoteProperty
231  ( const std::string& name ,
232  IProperty* rsvc ,
233  const std::string& rname = "" ) const
234  {
235  return m_propertyMgr -> declareRemoteProperty ( name , rsvc , rname ) ;
236  }
237 
245  template<class T>
246  StatusCode declarePrivateTool(ToolHandle<T> & handle, std::string toolTypeAndName =
247  "", bool createIf = true) {
248 
249  if (toolTypeAndName == "")
250  toolTypeAndName = System::typeinfoName(typeid(T));
251 
252  StatusCode sc = handle.initialize(toolTypeAndName, this, createIf);
253 
254  if (sc.isSuccess()) {
256  debug() << "Handle for private tool" << toolTypeAndName
257  << " successfully created and stored." << endmsg;
258  } else {
259 
260  error() << "Handle for private tool" << toolTypeAndName
261  << " could not be created." << endmsg;
262  }
263 
264  return sc;
265 
266  }
267 
275  template<class T>
276  StatusCode declarePublicTool(ToolHandle<T> & handle, std::string toolTypeAndName =
277  "", bool createIf = true) {
278 
279  if (toolTypeAndName == "")
280  toolTypeAndName = System::typeinfoName(typeid(T));
281 
282  StatusCode sc = handle.initialize(toolTypeAndName, 0, createIf);
283 
284 
285  if (sc.isSuccess()) {
287  debug() << "Handle for public tool" << toolTypeAndName
288  << " successfully created and stored." << endmsg;
289  } else {
290 
291  error() << "Handle for public tool" << toolTypeAndName
292  << " could not be created." << endmsg;
293  }
294 
295  return sc;
296 
297  }
298 
299  // ==========================================================================
303  SmartIF<IAuditorSvc>& auditorSvc() const;
304 
305 protected:
307  ~Service() override;
309  IntegerProperty m_outputLevel = MSG::NIL;
314 
316  int outputLevel() const { return m_outputLevel.value(); }
317 
318 private:
319 
320  void sysInitialize_imp();
323 
325  std::string m_name;
331 
332  void setServiceManager(ISvcManager* ism) override;
333 
343 
345  void initOutputLevel(Property& prop);
346 };
347 
348 #ifndef GAUDI_NEW_PLUGIN_SERVICE
349 template <class T>
350 class SvcFactory {
351 public:
352 #ifndef __REFLEX__
353  template <typename S, typename... Args>
354  static typename S::ReturnType create(Args&&... args) {
355  return new T(std::forward<Args>(args)...);
356  }
357 #endif
358 };
359 
360 // Macros to declare component factories
361 #define DECLARE_SERVICE_FACTORY(x) \
362  DECLARE_FACTORY_WITH_CREATOR(x, SvcFactory< x >, Service::Factory)
363 #define DECLARE_NAMED_SERVICE_FACTORY(x, n) \
364  DECLARE_FACTORY_WITH_CREATOR_AND_ID(x, SvcFactory< x >, #n, Service::Factory)
365 #define DECLARE_NAMESPACE_SERVICE_FACTORY(n, x) \
366  DECLARE_SERVICE_FACTORY(n::x)
367 
368 #else
369 
370 // macros to declare factories
371 #define DECLARE_SERVICE_FACTORY(x) DECLARE_COMPONENT(x)
372 #define DECLARE_NAMED_SERVICE_FACTORY(x, n) DECLARE_COMPONENT_WITH_ID(x, #n)
373 #define DECLARE_NAMESPACE_SERVICE_FACTORY(n, x) DECLARE_COMPONENT(n::x)
374 
375 #endif
376 
377 #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
#define UNLIKELY(x)
Definition: Kernel.h:126
BooleanProperty m_auditInit
Definition: Service.h:336
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
bool m_auditorInitialize
Definition: Service.h:337
StatusCode finalize() override
Finalize (from INITIALIZED to CONFIGURED).
SmartIF< IAuditorSvc > m_pAuditorSvc
Auditor Service.
Definition: Service.h:335
Gaudi::StateMachine::State FSMState() const override
Definition: Service.h:57
GAUDI_API const std::string typeinfoName(const std::type_info &)
Get platform independent information about the class type.
Definition: System.cpp:297
StatusCode m_initSC
Definition: Service.h:321
The ISvcManager is the interface implemented by the Service Factory in the Application Manager to sup...
Definition: ISvcManager.h:28
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
bool isSuccess() const
Test for a status code of SUCCESS.
Definition: StatusCode.h:76
SmartIF< ISvcLocator > & serviceLocator() const override
Function needed by CommonMessaging.
int outputLevel() const
get the Service's output level
Definition: Service.h:316
SmartIF< ISvcLocator > m_svcLocator
Service Locator reference.
Definition: Service.h:327
static S::ReturnType create(Args &&...args)
Definition: Service.h:354
StatusCode restart() override
Initialization (from RUNNING to RUNNING, via INITIALIZED).
std::string m_name
Service Name.
Definition: Service.h:325
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
StatusCode configure() override
Definition: Service.h:51
STL class.
TYPE * get() const
Get interface pointer.
Definition: SmartIF.h:76
MsgStream & error() const
shortcut for the method msgStream(MSG::ERROR)
StatusCode stop() override
Stop (from RUNNING to INITIALIZED).
SmartIF< PropertyMgr > m_propertyMgr
Property Manager.
Definition: Service.h:330
General service interface definition.
Definition: IService.h:18
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:26
StatusCode declarePrivateTool(ToolHandle< T > &handle, std::string toolTypeAndName="", bool createIf=true)
Declare used Private tool.
Definition: Service.h:246
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
bool m_auditorFinalize
Definition: Service.h:340
bool m_auditorReinitialize
Definition: Service.h:341
std::once_flag m_initFlag
Definition: Service.h:322
const TYPE & value() const
explicit conversion
Definition: Property.h:341
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:180
SmartIF< IFace > service(const std::string &name, bool createIf=true) const
Definition: Service.h:171
Handle to be used in lieu of naked pointers to tools.
Definition: PropertyMgr.h:22
StatusCode initialize(const std::string &toolTypeAndName, const IInterface *parent=nullptr, bool createIf=true)
Definition: ToolHandle.h:157
StatusCode declarePublicTool(ToolHandle< T > &handle, std::string toolTypeAndName="", bool createIf=true)
Declare used Public tool.
Definition: Service.h:276
Property base class allowing Property* collections to be "homogeneous".
Definition: Property.h:38
list args
Definition: gaudirun.py:290
bool m_auditorStart
Definition: Service.h:338
MsgStream & debug() const
shortcut for the method msgStream(MSG::DEBUG)
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).
StatusCode terminate() override
Definition: Service.h:56
string s
Definition: gaudirun.py:245
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:144
SmartIF< IService > service(const std::string &name, const bool quiet=false, const bool createIf=true) const
SmartIF< ISvcManager > m_svcManager
Definition: Service.h:328
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:36
MSG::Level msgLevel() const
get the output level from the embedded MsgStream
#define GAUDI_API
Definition: Kernel.h:107
Gaudi::PluginService::Factory< IService *, const std::string &, ISvcLocator * > Factory
Definition: Service.h:43
Gaudi::StateMachine::State targetFSMState() const override
Definition: Service.h:58
StatusCode service(const std::string &name, T *&psvc, bool createIf=true) const
Definition: Service.h:160
bool m_auditorRestart
Definition: Service.h:342
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:244
bool m_auditorStop
Definition: Service.h:339
tuple start
Definition: IOTest.py:88