The Gaudi Framework  v30r3 (a5ef0a68)
ServiceManager.h
Go to the documentation of this file.
1 #ifndef GAUDISVC_ServiceManager_H
2 #define GAUDISVC_ServiceManager_H
3 
4 // Include files
9 #include "GaudiKernel/Kernel.h"
10 #include "GaudiKernel/Map.h"
11 #include "GaudiKernel/SmartIF.h"
12 
13 #include "boost/thread.hpp"
14 #include <algorithm>
15 #include <list>
16 #include <memory>
17 #include <string>
18 #include <vector>
19 
20 // Forward declarations
21 class IService;
22 class IMessageSvc;
23 
36 class ServiceManager : public extends<ComponentManager, ISvcManager, ISvcLocator>
37 {
38 public:
39  struct ServiceItem final {
40  ServiceItem( IService* s, long p = 0, bool act = false ) : service( s ), priority( p ), active( act ) {}
42  long priority;
43  bool active;
44  inline bool operator==( const std::string& name ) const { return service->name() == name; }
45  inline bool operator==( const IService* ptr ) const { return service.get() == ptr; }
46  inline bool operator<( const ServiceItem& rhs ) const { return priority < rhs.priority; }
47  };
48 
49  // typedefs and classes
52 
54  ServiceManager( IInterface* application );
55 
57  inline SmartIF<ISvcLocator>& serviceLocator() const override { return m_svcLocator; }
58 
60  ~ServiceManager() override;
61 
63  const std::list<IService*>& getServices() const override;
64 
66  bool existsService( const std::string& name ) const override;
67 
69  StatusCode addService( IService* svc, int prio = DEFAULT_SVC_PRIORITY ) override;
71  StatusCode addService( const Gaudi::Utils::TypeNameString& typeName, int prio = DEFAULT_SVC_PRIORITY ) override;
73  StatusCode removeService( IService* svc ) override;
75  StatusCode removeService( const std::string& name ) override;
76 
78  StatusCode declareSvcType( const std::string& svcname, const std::string& svctype ) override;
79 
86 
88  StatusCode initialize() override;
90  StatusCode start() override;
92  StatusCode stop() override;
94  StatusCode finalize() override;
95 
97  StatusCode reinitialize() override;
99  StatusCode restart() override;
100 
102  int getPriority( const std::string& name ) const override;
103  StatusCode setPriority( const std::string& name, int pri ) override;
104 
106  bool loopCheckEnabled() const override;
108  void setLoopCheckEnabled( bool en ) override;
109 
111  const std::string& name() const override
112  {
113  static std::string _name = "ServiceManager";
114  return _name;
115  }
116 
118  SmartIF<IService>& service( const Gaudi::Utils::TypeNameString& typeName, const bool createIf = true ) override;
119 
121  template <typename T>
122  inline SmartIF<T> service( const Gaudi::Utils::TypeNameString& typeName, const bool createIf = true )
123  {
124  return SmartIF<T>{service( typeName, createIf )};
125  }
126 
127 #if !defined( GAUDI_V22_API ) || defined( G22_NEW_SVCLOCATOR )
130 #endif
131 
133  void outputLevelUpdate() override;
134 
135 private:
136  inline ListSvc::iterator find( const std::string& name )
137  {
138  boost::lock_guard<boost::recursive_mutex> lck( m_gLock );
139  return std::find( m_listsvc.begin(), m_listsvc.end(), name );
140  }
141  inline ListSvc::const_iterator find( const std::string& name ) const
142  {
143  boost::lock_guard<boost::recursive_mutex> lck( m_gLock );
144  return std::find( m_listsvc.begin(), m_listsvc.end(), name );
145  }
146  inline ListSvc::iterator find( const IService* ptr )
147  {
148  boost::lock_guard<boost::recursive_mutex> lck( m_gLock );
149  return std::find( m_listsvc.begin(), m_listsvc.end(), ptr );
150  }
151  inline ListSvc::const_iterator find( const IService* ptr ) const
152  {
153  boost::lock_guard<boost::recursive_mutex> lck( m_gLock );
154  return std::find( m_listsvc.begin(), m_listsvc.end(), ptr );
155  }
156 
157 private:
158  ListSvc m_listsvc;
159  MapType m_maptype;
173  bool m_loopCheck = true;
174 
177 
180 
182 
184  typedef boost::recursive_mutex Mutex_t;
185  typedef boost::lock_guard<Mutex_t> LockGuard_t;
186 
187  mutable Mutex_t m_gLock;
189 
190 private:
191  void dump() const;
192 };
193 #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).
SmartIF< ISvcLocator > m_svcLocator
Service locator (needed to access the MessageSvc)
bool m_loopCheck
Check for service initialization loops.
SmartIF< ISvcLocator > & serviceLocator() const override
Function needed by CommonMessaging.
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)
MapType m_maptype
Map of service name and service type.
STL class.
TYPE * get() const
Get interface pointer.
Definition: SmartIF.h:82
bool existsService(const std::string &name) const override
implementation of ISvcLocation::existsService
StatusCode stop() override
Stop (from RUNNING to INITIALIZED).
Helper class to parse a string of format "type/name".
bool loopCheckEnabled() const override
Get the value of the initialization loop check flag.
void dump() const
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:51
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:277
~ServiceManager() override
virtual destructor
const std::string & name() const override
Return the name of the manager (implementation of INamedInterface)
The IMessage is the interface implemented by the message service.
Definition: IMessageSvc.h:38
const std::list< IService * > & getServices() const override
Return the list of Services.
StatusCode start() override
Start (from INITIALIZED to RUNNING).
bool operator==(const std::string &name) const
int getPriority(const std::string &name) const override
manage priorities of services
T find(T...args)
void outputLevelUpdate() override
Function to call to update the outputLevel of the components (after a change in MessageSvc).
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:253
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
std::string typeName(const std::type_info &typ)
Definition: Dictionary.cpp:23
std::list< ServiceItem > ListSvc
ListSvc m_listsvc
List of service maintained by ServiceManager This contains SmartIF<T> 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
std::map< std::string, Mutex_t > m_lockMap
StatusCode addService(IService *svc, int prio=DEFAULT_SVC_PRIORITY) override
implementation of ISvcManager::addService
StatusCode initialize() override
Initialization (from CONFIGURED to INITIALIZED).