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 // ============================================================================
18 #include <vector>
19 // ============================================================================
20 // Forward declarations
21 // ============================================================================
22 class IMessageSvc;
23 class ISvcManager;
24 class ServiceManager;
25 // ============================================================================
33 class GAUDI_API Service: public CommonMessaging<implements3<IService, IProperty, IStateful> > {
34 public:
35 #ifndef __REFLEX__
37  const std::string&,
38  ISvcLocator*> Factory;
39 #endif
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 
70  // Default implementations for ISetProperty
71  StatusCode setProperty(const Property& p) override;
72  StatusCode setProperty( const std::string& s ) override;
73  StatusCode setProperty( const std::string& n, const std::string& v) override;
74  StatusCode getProperty(Property* p) const override;
75  const Property& getProperty( const std::string& name) const override;
76  StatusCode getProperty( const std::string& n, std::string& v ) const override;
77  const std::vector<Property*>& getProperties( ) const override;
78  bool hasProperty(const std::string& name) const override;
79 
120  template <class TYPE>
122  ( const std::string& name ,
123  const TYPE& value )
124  { return Gaudi::Utils::setProperty ( m_propertyMgr.get() , name , value ) ; }
125 
127  Service( std::string name, ISvcLocator* svcloc);
129  SmartIF<ISvcLocator>& serviceLocator() const override;
130 
134  StatusCode setProperties();
135 
138  template <class T>
139  StatusCode service( const std::string& name, const T*& psvc, bool createIf = true ) const {
140  ISvcLocator& svcLoc = *serviceLocator();
141  auto ptr =
142  ServiceLocatorHelper(svcLoc, *this).service<T>(name, !createIf, // quiet
143  createIf);
144  if (ptr) {
145  psvc = ptr.get();
146  const_cast<T*>(psvc)->addRef();
147  return StatusCode::SUCCESS;
148  }
149  // else
150  psvc = nullptr;
151  return StatusCode::FAILURE;
152  }
153 
154  template <class T>
155  StatusCode service( const std::string& name, T*& psvc, bool createIf = true ) const {
156  auto ptr = service<T>(name,createIf);
157  psvc = ( ptr ? ptr.get() : nullptr );
158  if (psvc) {
159  psvc->addRef();
160  return StatusCode::SUCCESS;
161  }
162  return StatusCode::FAILURE;
163  }
164 
165  template <typename IFace = IService>
166  SmartIF<IFace> service(const std::string& name, bool createIf = true) const {
167  return ServiceLocatorHelper(*serviceLocator(), *this).
168  service<IFace>(name, !createIf, // quiet
169  createIf);
170  }
171 
174  template <class T>
175  StatusCode service( const std::string& svcType, const std::string& svcName,
176  T*& psvc) const {
177  return service(svcType + "/" + svcName, psvc);
178  }
179  // ==========================================================================
208  template <class T>
209  Property* declareProperty
210  ( const std::string& name ,
211  T& property ,
212  const std::string& doc = "none" ) const
213  {
214  return m_propertyMgr -> declareProperty ( name , property , doc ) ;
215  }
216  // ==========================================================================
225  Property* declareRemoteProperty
226  ( const std::string& name ,
227  IProperty* rsvc ,
228  const std::string& rname = "" ) const
229  {
230  return m_propertyMgr -> declareRemoteProperty ( name , rsvc , rname ) ;
231  }
232  // ==========================================================================
236  SmartIF<IAuditorSvc>& auditorSvc() const;
237 
238 protected:
240  ~Service() override;
242  IntegerProperty m_outputLevel = MSG::NIL;
247 
249  int outputLevel() const { return m_outputLevel.value(); }
250 
251 private:
253  std::string m_name;
255  mutable SmartIF<ISvcLocator> m_svcLocator;
256  SmartIF<ISvcManager> m_svcManager;
258  SmartIF<PropertyMgr> m_propertyMgr;
259 
260  void setServiceManager(ISvcManager* ism) override;
261 
263  mutable SmartIF<IAuditorSvc> m_pAuditorSvc;
264  BooleanProperty m_auditInit;
265  bool m_auditorInitialize;
266  bool m_auditorStart;
267  bool m_auditorStop;
268  bool m_auditorFinalize;
269  bool m_auditorReinitialize;
270  bool m_auditorRestart;
271 
273  void initOutputLevel(Property& prop);
274 };
275 
276 #ifndef GAUDI_NEW_PLUGIN_SERVICE
277 template <class T>
278 class SvcFactory {
279 public:
280 #ifndef __REFLEX__
281  template <typename S, typename... Args>
282  static typename S::ReturnType create(Args&&... args) {
283  return new T(std::forward<Args>(args)...);
284  }
285 #endif
286 };
287 
288 // Macros to declare component factories
289 #define DECLARE_SERVICE_FACTORY(x) \
290  DECLARE_FACTORY_WITH_CREATOR(x, SvcFactory< x >, Service::Factory)
291 #define DECLARE_NAMED_SERVICE_FACTORY(x, n) \
292  DECLARE_FACTORY_WITH_CREATOR_AND_ID(x, SvcFactory< x >, #n, Service::Factory)
293 #define DECLARE_NAMESPACE_SERVICE_FACTORY(n, x) \
294  DECLARE_SERVICE_FACTORY(n::x)
295 
296 #else
297 
298 // macros to declare factories
299 #define DECLARE_SERVICE_FACTORY(x) DECLARE_COMPONENT(x)
300 #define DECLARE_NAMED_SERVICE_FACTORY(x, n) DECLARE_COMPONENT_WITH_ID(x, #n)
301 #define DECLARE_NAMESPACE_SERVICE_FACTORY(n, x) DECLARE_COMPONENT(n::x)
302 
303 #endif
304 
305 #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
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
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
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:157
static S::ReturnType create(Args &&...args)
Definition: Service.h:282
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:259
SmartIF< ISvcLocator > & serviceLocator() const
Function needed by CommonMessaging.
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:255
const TYPE & value() const
explicit conversion
Definition: Property.h:341
Property base class allowing Property* collections to be "homogeneous".
Definition: Property.h:38
list args
Definition: gaudirun.py:291
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:246
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:33
tuple start
Definition: IOTest.py:88