ServiceManager.h
Go to the documentation of this file.
1 #ifndef GAUDISVC_ServiceManager_H
2 #define GAUDISVC_ServiceManager_H
3 
4 // Include files
5 #include "GaudiKernel/Kernel.h"
9 #include "GaudiKernel/SmartIF.h"
11 #include "GaudiKernel/Map.h"
12 #include <string>
13 #include <list>
14 #include <vector>
15 #include <algorithm>
16 #include <memory>
17 #include "boost/thread.hpp"
18 
19 // Forward declarations
20 class IService;
21 class IMessageSvc;
22 class Property;
23 
36 class ServiceManager : public extends<ComponentManager,
37  ISvcManager,
38  ISvcLocator> {
39 public:
40 
41  struct ServiceItem final {
42  ServiceItem(IService *s, long p = 0, bool act = false):
43  service(s), priority(p), active(act) {}
45  long priority;
46  bool active;
47  inline bool operator==(const std::string &name) const {
48  return service->name() == name;
49  }
50  inline bool operator==(const IService *ptr) const {
51  return service.get() == ptr;
52  }
53  inline bool operator<(const ServiceItem& rhs) const {
54  return priority < rhs.priority;
55  }
56  };
57 
58  // typedefs and classes
61 
63  ServiceManager(IInterface* application);
64 
66  inline SmartIF<ISvcLocator>& serviceLocator() const override {
67  return m_svcLocator;
68  }
69 
71  ~ServiceManager() override;
72 
74  const std::list<IService*>& getServices() const override;
75 
77  bool existsService(const std::string& name) const override;
78 
80  StatusCode addService(IService* svc, int prio = DEFAULT_SVC_PRIORITY) override;
82  StatusCode addService(const Gaudi::Utils::TypeNameString& typeName, int prio = DEFAULT_SVC_PRIORITY) override;
84  StatusCode removeService(IService* svc) override;
86  StatusCode removeService(const std::string& name) override;
87 
89  StatusCode declareSvcType(const std::string& svcname, const std::string& svctype) override;
90 
97 
99  StatusCode initialize() override;
101  StatusCode start() override;
103  StatusCode stop() override;
105  StatusCode finalize() override;
106 
108  StatusCode reinitialize() override;
110  StatusCode restart() override;
111 
113  int getPriority(const std::string& name) const override;
114  StatusCode setPriority(const std::string& name, int pri) override;
115 
117  bool loopCheckEnabled() const override;
119  void setLoopCheckEnabled(bool en) override;
120 
122  const std::string &name() const override {
123  static std::string _name = "ServiceManager";
124  return _name;
125  }
126 
128  SmartIF<IService> &service(const Gaudi::Utils::TypeNameString &typeName, const bool createIf = true) override;
129 
131  template <typename T>
132  inline SmartIF<T> service(const Gaudi::Utils::TypeNameString &typeName, const bool createIf = true) {
133  return SmartIF<T>{service(typeName, createIf)};
134  }
135 
136 #if !defined(GAUDI_V22_API) || defined(G22_NEW_SVCLOCATOR)
139 #endif
140 
141 private:
142 
143  inline ListSvc::iterator find(const std::string &name) {
144  boost::lock_guard<boost::recursive_mutex> lck(m_gLock);
145  return std::find(m_listsvc.begin(), m_listsvc.end(), name);
146  }
147  inline ListSvc::const_iterator find(const std::string &name) const {
148  boost::lock_guard<boost::recursive_mutex> lck(m_gLock);
149  return std::find(m_listsvc.begin(), m_listsvc.end(), name);
150  }
151  inline ListSvc::iterator find(const IService *ptr) {
152  boost::lock_guard<boost::recursive_mutex> lck(m_gLock);
153  return std::find(m_listsvc.begin(), m_listsvc.end(), ptr);
154  }
155  inline ListSvc::const_iterator find(const IService *ptr) const {
156  boost::lock_guard<boost::recursive_mutex> lck(m_gLock);
157  return std::find(m_listsvc.begin(), m_listsvc.end(), ptr);
158  }
159 
160 private:
161  ListSvc m_listsvc;
162  MapType m_maptype;
176  bool m_loopCheck = true;
177 
180 
183 
185 
187  typedef boost::recursive_mutex Mutex_t;
188  typedef boost::lock_guard<Mutex_t> LockGuard_t;
189 
190  mutable Mutex_t m_gLock;
192 
193 
194 private:
195  void dump() const;
196 
197 };
198 #endif // GAUDISVC_ServiceManager_H
ListSvc::const_iterator find(const IService *ptr) const
The ServiceManager class is in charge of the creation of concrete instances of Services.
virtual StatusCode addService(IService *svc, int prio=DEFAULT_SVC_PRIORITY)=0
Add a service to the "active" list of services of the factory.
std::list< IService * > m_listOfPtrs
List of pointers to the know services used to implement getServices()
void setLoopCheckEnabled(bool en) override
Set the value of the initialization loop check flag.
SmartIF< IService > m_appSvc
Pointer to the application IService interface.
boost::lock_guard< Mutex_t > LockGuard_t
ListSvc::iterator find(const IService *ptr)
GaudiUtils::Map< InterfaceID, SmartIF< IInterface > > m_defaultImplementations
StatusCode finalize() override
Finalize (from INITIALIZED to CONFIGURED).
int getPriority(const std::string &name) const override
manage priorities of services
SmartIF< ISvcLocator > & serviceLocator() const override
Function needed by CommonMessaging.
SmartIF< ISvcLocator > m_svcLocator
Service locator (needed to access the MessageSvc)
bool m_loopCheck
Check for service initialization loops.
bool existsService(const std::string &name) const override
implementation of ISvcLocation::existsService
ServiceManager(IInterface *application)
default creator
boost::recursive_mutex Mutex_t
Mutex to synchronize shared service initialization between threads.
StatusCode restart() override
Initialization (from RUNNING to RUNNING, via INITIALIZED).
T end(T...args)
STL class.
MapType m_maptype
Map of service name and service type.
STL class.
TYPE * get() const
Get interface pointer.
Definition: SmartIF.h:76
StatusCode stop() override
Stop (from RUNNING to INITIALIZED).
Helper class to parse a string of format "type/name".
Definition: TypeNameString.h:9
void dump() const
bool loopCheckEnabled() const override
Get the value of the initialization loop check flag.
General service interface definition.
Definition: IService.h:18
SmartIF< T > service(const Gaudi::Utils::TypeNameString &typeName, const bool createIf=true)
Returns a smart pointer to the requested interface of a service.
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:26
virtual SmartIF< IService > & createService(const Gaudi::Utils::TypeNameString &nametype)=0
Creates and instance of a service type that has been declared beforehand and assigns it a name...
Definition of the basic interface.
Definition: IInterface.h:234
const std::list< IService * > & getServices() const override
Return the list of Services.
~ServiceManager() override
virtual destructor
The IMessage is the interface implemented by the message service.
Definition: IMessageSvc.h:57
const std::string & name() const override
Return the name of the manager (implementation of INamedInterface)
StatusCode start() override
Start (from INITIALIZED to RUNNING).
bool operator==(const std::string &name) const
std::map< std::string, std::unique_ptr< Mutex_t > > m_lockMap
T find(T...args)
Property base class allowing Property* collections to be "homogeneous".
Definition: Property.h:38
Base class used to extend a class implementing other interfaces.
Definition: extends.h:10
T begin(T...args)
StatusCode reinitialize() override
Initialization (from INITIALIZED or RUNNING to INITIALIZED, via CONFIGURED).
GaudiUtils::Map< std::string, std::string > MapType
ListSvc::const_iterator find(const std::string &name) const
string s
Definition: gaudirun.py:245
bool operator<(const ServiceItem &rhs) const
ListSvc::iterator find(const std::string &name)
StatusCode declareSvcType(const std::string &svcname, const std::string &svctype) override
implementation of ISvcManager::declareSvcType
StatusCode setPriority(const std::string &name, int pri) override
StatusCode removeService(IService *svc) override
implementation of ISvcManager::removeService
SmartIF< IService > & service(const Gaudi::Utils::TypeNameString &typeName, const bool createIf=true) override
Returns a smart pointer to a service.
std::string typeName(const std::type_info &typ)
Definition: Dictionary.cpp:21
std::list< ServiceItem > ListSvc
ListSvc m_listsvc
List of service maintained by ServiceManager This contains SmartIF for all services – and because...
SmartIF< IService > & createService(const Gaudi::Utils::TypeNameString &nametype) override
implementation of ISvcManager::createService NOTE: as this returns a &, we must guarantee that once c...
ServiceItem(IService *s, long p=0, bool act=false)
bool operator==(const IService *ptr) const
SmartIF< IService > service
StatusCode addService(IService *svc, int prio=DEFAULT_SVC_PRIORITY) override
implementation of ISvcManager::addService
StatusCode initialize() override
Initialization (from CONFIGURED to INITIALIZED).