All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
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"
6 #include "GaudiKernel/ISvcManager.h"
7 #include "GaudiKernel/ISvcLocator.h"
8 #include "GaudiKernel/IStateful.h"
9 #include "GaudiKernel/SmartIF.h"
10 #include "GaudiKernel/ComponentManager.h"
11 #include "GaudiKernel/Map.h"
12 #include <string>
13 #include <list>
14 #include <vector>
15 #include <algorithm>
16 #include <mutex>
17 
18 // Forward declarations
19 class IService;
20 class IMessageSvc;
21 class Property;
22 
35 class ServiceManager : public extends2<ComponentManager, ISvcManager, ISvcLocator>{
36 public:
37 
38  struct ServiceItem final {
39  ServiceItem(IService *s, long p = 0, bool act = false):
40  service(s), priority(p), active(act) {}
42  long priority;
43  bool active;
44  inline bool operator==(const std::string &name) const {
45  return service->name() == name;
46  }
47  inline bool operator==(const IService *ptr) const {
48  return service.get() == ptr;
49  }
50  inline bool operator<(const ServiceItem& rhs) const {
51  return priority < rhs.priority;
52  }
53  };
54 
55  // typedefs and classes
56  typedef std::list<ServiceItem> ListSvc;
58 
60  ServiceManager(IInterface* application);
61 
63  inline SmartIF<ISvcLocator>& serviceLocator() const override {
64  return m_svcLocator;
65  }
66 
68  ~ServiceManager() override;
69 
71  const std::list<IService*>& getServices() const override;
72 
74  bool existsService(const std::string& name) const override;
75 
77  StatusCode addService(IService* svc, int prio = DEFAULT_SVC_PRIORITY) override;
79  StatusCode addService(const Gaudi::Utils::TypeNameString& typeName, int prio = DEFAULT_SVC_PRIORITY) override;
81  StatusCode removeService(IService* svc) override;
83  StatusCode removeService(const std::string& name) override;
84 
86  StatusCode declareSvcType(const std::string& svcname, const std::string& svctype) override;
87 
94 
96  StatusCode initialize() override;
98  StatusCode start() override;
100  StatusCode stop() override;
102  StatusCode finalize() override;
103 
105  StatusCode reinitialize() override;
107  StatusCode restart() override;
108 
110  int getPriority(const std::string& name) const override;
111  StatusCode setPriority(const std::string& name, int pri) override;
112 
114  bool loopCheckEnabled() const override;
116  void setLoopCheckEnabled(bool en) override;
117 
119  const std::string &name() const override {
120  static std::string _name = "ServiceManager";
121  return _name;
122  }
123 
125  SmartIF<IService> &service(const Gaudi::Utils::TypeNameString &typeName, const bool createIf = true) override;
126 
128  template <typename T>
129  inline SmartIF<T> service(const Gaudi::Utils::TypeNameString &typeName, const bool createIf = true) {
130  return SmartIF<T>{service(typeName, createIf)};
131  }
132 
133 #if !defined(GAUDI_V22_API) || defined(G22_NEW_SVCLOCATOR)
136 #endif
137 
138 private:
139 
140  inline ListSvc::iterator find(const std::string &name) {
141  return std::find(m_listsvc.begin(), m_listsvc.end(), name);
142  }
143  inline ListSvc::const_iterator find(const std::string &name) const {
144  return std::find(m_listsvc.begin(), m_listsvc.end(), name);
145  }
146  inline ListSvc::iterator find(const IService *ptr) {
147  return std::find(m_listsvc.begin(), m_listsvc.end(), ptr);
148  }
149  inline ListSvc::const_iterator find(const IService *ptr) const {
150  return std::find(m_listsvc.begin(), m_listsvc.end(), ptr);
151  }
152 
153 private:
154  ListSvc m_listsvc;
155  MapType m_maptype;
169  bool m_loopCheck = true;
170 
173 
175  mutable std::list<IService*> m_listOfPtrs;
176 
178 
180  std::recursive_mutex m_svcinitmutex;
181 
182 private:
183  void dump() const;
184 
185 };
186 #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.
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.
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
StatusCode restart() override
Initialization (from RUNNING to RUNNING, via INITIALIZED).
MapType m_maptype
Map of service name and service type.
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
std::recursive_mutex m_svcinitmutex
Mutex to synchronize shared service initialization between threads.
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
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
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).