All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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__
36  typedef Gaudi::PluginService::Factory<IService*,
37  const std::string&,
38  ISvcLocator*> Factory;
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
50  virtual StatusCode configure() { return StatusCode::SUCCESS; }
51  virtual StatusCode initialize();
52  virtual StatusCode start();
53  virtual StatusCode stop();
54  virtual StatusCode finalize();
55  virtual StatusCode terminate() { return StatusCode::SUCCESS; }
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 
123  template <class TYPE>
125  ( const std::string& name ,
126  const TYPE& value )
127  { return Gaudi::Utils::setProperty ( m_propertyMgr , name , value ) ; }
128 
130  Service( const std::string& name, ISvcLocator* svcloc);
133 
137  StatusCode setProperties();
138 
141  template <class T>
142  StatusCode service( const std::string& name, const T*& psvc, bool createIf = true ) const {
143  ISvcLocator& svcLoc = *serviceLocator();
144  SmartIF<T> ptr(
145  ServiceLocatorHelper(svcLoc, *this).service(name, !createIf, // quiet
146  createIf));
147  if (ptr.isValid()) {
148  psvc = ptr.get();
149  const_cast<T*>(psvc)->addRef();
150  return StatusCode::SUCCESS;
151  }
152  // else
153  psvc = 0;
154  return StatusCode::FAILURE;
155  }
156 
157  template <class T>
158  StatusCode service( const std::string& name, T*& psvc, bool createIf = true ) const {
159  ISvcLocator& svcLoc = *serviceLocator();
160  SmartIF<T> ptr(
161  ServiceLocatorHelper(svcLoc, *this).service(name, !createIf, // quiet
162  createIf));
163  if (ptr.isValid()) {
164  psvc = ptr.get();
165  psvc->addRef();
166  return StatusCode::SUCCESS;
167  }
168  // else
169  psvc = 0;
170  return StatusCode::FAILURE;
171  }
172 
175  template <class T>
176  StatusCode service( const std::string& svcType, const std::string& svcName,
177  T*& psvc) const {
178  return service(svcType + "/" + svcName, psvc);
179  }
180  // ==========================================================================
209  template <class T>
210  Property* declareProperty
211  ( const std::string& name ,
212  T& property ,
213  const std::string& doc = "none" ) const
214  {
215  return m_propertyMgr -> declareProperty ( name , property , doc ) ;
216  }
217  // ==========================================================================
226  Property* declareRemoteProperty
227  ( const std::string& name ,
228  IProperty* rsvc ,
229  const std::string& rname = "" ) const
230  {
231  return m_propertyMgr -> declareRemoteProperty ( name , rsvc , rname ) ;
232  }
233  // ==========================================================================
237  SmartIF<IAuditorSvc>& auditorSvc() const;
238 
239 protected:
241  virtual ~Service();
243  IntegerProperty m_outputLevel;
247  Gaudi::StateMachine::State m_targetState;
248 
250  int outputLevel() const { return m_outputLevel.value(); }
251 
252 private:
254  std::string m_name;
257  SmartIF<ISvcManager> m_svcManager;
259  PropertyMgr* m_propertyMgr;
260 
261  void setServiceManager(ISvcManager* ism);
262 
264  mutable SmartIF<IAuditorSvc> m_pAuditorSvc;
265  BooleanProperty m_auditInit;
266  bool m_auditorInitialize;
267  bool m_auditorStart;
268  bool m_auditorStop;
269  bool m_auditorFinalize;
270  bool m_auditorReinitialize;
271  bool m_auditorRestart;
272 
274  void initOutputLevel(Property& prop);
275 };
276 
277 #ifndef GAUDI_NEW_PLUGIN_SERVICE
278 template <class T>
279 class SvcFactory {
280 public:
281 #ifndef __REFLEX__
282  template <typename S, typename... Args>
283  static typename S::ReturnType create(Args... args) {
284  return new T(args...);
285  }
286 #endif
287 };
288 
289 // Macros to declare component factories
290 #define DECLARE_SERVICE_FACTORY(x) \
291  DECLARE_FACTORY_WITH_CREATOR(x, SvcFactory< x >, Service::Factory)
292 #define DECLARE_NAMED_SERVICE_FACTORY(x, n) \
293  DECLARE_FACTORY_WITH_CREATOR_AND_ID(x, SvcFactory< x >, #n, Service::Factory)
294 #define DECLARE_NAMESPACE_SERVICE_FACTORY(n, x) \
295  DECLARE_SERVICE_FACTORY(n::x)
296 
297 #else
298 
299 // macros to declare factories
300 #define DECLARE_SERVICE_FACTORY(x) DECLARE_COMPONENT(x)
301 #define DECLARE_NAMED_SERVICE_FACTORY(x, n) DECLARE_COMPONENT_WITH_ID(x, #n)
302 #define DECLARE_NAMESPACE_SERVICE_FACTORY(n, x) DECLARE_COMPONENT(n::x)
303 
304 #endif
305 
306 #endif // GAUDIKERNEL_SERVICE_H