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 // ============================================================================
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&,
39 #endif
40  friend class ServiceManager;
41 
44  virtual unsigned long release();
45 
47  virtual const std::string& name() const;
48 
49  // State machine implementation
51  virtual StatusCode initialize();
52  virtual StatusCode start();
53  virtual StatusCode stop();
54  virtual StatusCode finalize();
56  virtual Gaudi::StateMachine::State FSMState() const { return m_state; }
57  virtual Gaudi::StateMachine::State targetFSMState() const { return m_targetState; }
58  virtual StatusCode reinitialize();
59  virtual StatusCode restart();
60 
62  virtual StatusCode sysInitialize();
64  virtual StatusCode sysStart();
66  virtual StatusCode sysStop();
68  virtual StatusCode sysFinalize();
70  virtual StatusCode sysReinitialize();
72  virtual StatusCode sysRestart();
73 
74  // Default implementations for ISetProperty
75  virtual StatusCode setProperty(const Property& p);
76  virtual StatusCode setProperty( const std::string& s );
77  virtual StatusCode setProperty( const std::string& n, const std::string& v);
78  virtual StatusCode getProperty(Property* p) const;
79  virtual const Property& getProperty( const std::string& name) const;
80  virtual StatusCode getProperty( const std::string& n, std::string& v ) const;
81  virtual const std::vector<Property*>& getProperties( ) const;
82  virtual bool hasProperty(const std::string& name) const;
83 
124  template <class TYPE>
126  ( const std::string& name ,
127  const TYPE& value )
128  { return Gaudi::Utils::setProperty ( m_propertyMgr , name , value ) ; }
129 
131  Service( const std::string& name, ISvcLocator* svcloc);
134 
138  StatusCode setProperties();
139 
142  template <class T>
143  StatusCode service( const std::string& name, const T*& psvc, bool createIf = true ) const {
144  ISvcLocator& svcLoc = *serviceLocator();
145  SmartIF<T> ptr(
146  ServiceLocatorHelper(svcLoc, *this).service(name, !createIf, // quiet
147  createIf));
148  if (ptr.isValid()) {
149  psvc = ptr.get();
150  const_cast<T*>(psvc)->addRef();
151  return StatusCode::SUCCESS;
152  }
153  // else
154  psvc = 0;
155  return StatusCode::FAILURE;
156  }
157 
158  template <class T>
159  StatusCode service( const std::string& name, T*& psvc, bool createIf = true ) const {
160  ISvcLocator& svcLoc = *serviceLocator();
161  SmartIF<T> ptr(
162  ServiceLocatorHelper(svcLoc, *this).service(name, !createIf, // quiet
163  createIf));
164  if (ptr.isValid()) {
165  psvc = ptr.get();
166  psvc->addRef();
167  return StatusCode::SUCCESS;
168  }
169  // else
170  psvc = 0;
171  return StatusCode::FAILURE;
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  // ==========================================================================
238  SmartIF<IAuditorSvc>& auditorSvc() const;
239 
240 protected:
242  virtual ~Service();
249 
251  int outputLevel() const { return m_outputLevel.value(); }
252 
253 private:
255  std::string m_name;
261 
262  void setServiceManager(ISvcManager* ism);
263 
273 
275  void initOutputLevel(Property& prop);
276 };
277 
278 #ifndef GAUDI_NEW_PLUGIN_SERVICE
279 template <class T>
280 class SvcFactory {
281 public:
282 #ifndef __REFLEX__
283  template <typename S, typename... Args>
284  static typename S::ReturnType create(Args... args) {
285  return new T(args...);
286  }
287 #endif
288 };
289 
290 // Macros to declare component factories
291 #define DECLARE_SERVICE_FACTORY(x) \
292  DECLARE_FACTORY_WITH_CREATOR(x, SvcFactory< x >, Service::Factory)
293 #define DECLARE_NAMED_SERVICE_FACTORY(x, n) \
294  DECLARE_FACTORY_WITH_CREATOR_AND_ID(x, SvcFactory< x >, #n, Service::Factory)
295 #define DECLARE_NAMESPACE_SERVICE_FACTORY(n, x) \
296  DECLARE_SERVICE_FACTORY(n::x)
297 
298 #else
299 
300 // macros to declare factories
301 #define DECLARE_SERVICE_FACTORY(x) DECLARE_COMPONENT(x)
302 #define DECLARE_NAMED_SERVICE_FACTORY(x, n) DECLARE_COMPONENT_WITH_ID(x, #n)
303 #define DECLARE_NAMESPACE_SERVICE_FACTORY(n, x) DECLARE_COMPONENT(n::x)
304 
305 #endif
306 
307 #endif // GAUDIKERNEL_SERVICE_H
The ServiceManager class is in charge of the creation of concrete instances of Services.
virtual StatusCode configure()
Configuration (from OFFLINE to CONFIGURED).
Definition: Service.h:50
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:1212
BooleanProperty m_auditInit
Definition: Service.h:266
Gaudi::StateMachine::State m_targetState
Service state.
Definition: Service.h:248
an helper to share the implementation of service() among the various kernel base classes $Id: ...
IntegerProperty m_outputLevel
Service output level.
Definition: Service.h:244
virtual StatusCode stop()
Stop (from RUNNING to INITIALIZED).
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:26
bool m_auditorInitialize
Definition: Service.h:267
const std::string & name() const
Return the name of the manager (implementation of INamedInterface)
Gaudi::StateMachine::State m_state
Service state.
Definition: Service.h:246
SmartIF< IAuditorSvc > m_pAuditorSvc
Auditor Service.
Definition: Service.h:265
virtual SmartIF< ISvcLocator > & serviceLocator() const =0
Needed to locate the message service.
The ISvcManager is the interface implemented by the Service Factory in the Application Manager to sup...
Definition: ISvcManager.h:29
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:232
int outputLevel() const
get the Service's output level
Definition: Service.h:251
SmartIF< ISvcLocator > m_svcLocator
Service Locator reference.
Definition: Service.h:257
virtual StatusCode reinitialize()
Initialization (from INITIALIZED or RUNNING to INITIALIZED, via CONFIGURED).
std::string m_name
Service Name.
Definition: Service.h:255
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:297
Property manager helper class.
Definition: PropertyMgr.h:34
static S::ReturnType create(Args...args)
Definition: Service.h:284
TYPE * get() const
Get interface pointer.
Definition: SmartIF.h:62
General service interface definition.
Definition: IService.h:19
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:30
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:290
bool m_auditorFinalize
Definition: Service.h:270
virtual StatusCode finalize()
Finalize (from INITIALIZED to CONFIGURED).
bool m_auditorReinitialize
Definition: Service.h:271
virtual StatusCode terminate()
Initialization (from CONFIGURED to OFFLINE).
Definition: Service.h:55
virtual Gaudi::StateMachine::State FSMState() const
Get the current state.
Definition: Service.h:56
const TYPE & value() const
explicit conversion
Definition: Property.h:355
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:177
PropertyMgr * m_propertyMgr
Property Manager.
Definition: Service.h:260
Property base class allowing Property* collections to be "homogeneous".
Definition: Property.h:43
virtual unsigned long release()=0
Release Interface instance.
list args
Definition: gaudirun.py:262
bool m_auditorStart
Definition: Service.h:268
Class wrapping the signature for a factory with any number of arguments.
Definition: PluginService.h:47
bool isValid() const
Allow for check if smart pointer is valid.
Definition: SmartIF.h:51
virtual const std::string & name() const =0
This is needed to avoid ambiguous calls to name()
string s
Definition: gaudirun.py:217
Templated class to add the standard messaging functionalities.
virtual unsigned long addRef()=0
Increment the reference count of Interface instance.
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:143
virtual SmartIF< IService > & service(const Gaudi::Utils::TypeNameString &typeName, const bool createIf=true)
Returns a smart pointer to a service.
SmartIF< ISvcManager > m_svcManager
Definition: Service.h:258
virtual Gaudi::StateMachine::State targetFSMState() const
When we are in the middle of a transition, get the state where the transition is leading us...
Definition: Service.h:57
virtual StatusCode restart()
Initialization (from RUNNING to RUNNING, via INITIALIZED).
The IProperty is the basic interface for all components which have properties that can be set or get...
Definition: IProperty.h:22
Base class for all services.
Definition: Service.h:33
#define GAUDI_API
Definition: Kernel.h:108
Gaudi::PluginService::Factory< IService *, const std::string &, ISvcLocator * > Factory
Definition: Service.h:38
StatusCode service(const std::string &name, T *&psvc, bool createIf=true) const
Definition: Service.h:159
bool m_auditorRestart
Definition: Service.h:272
bool m_auditorStop
Definition: Service.h:269
tuple start
Definition: IOTest.py:88