The Gaudi Framework  v33r0 (d5ea422b)
ServiceManager.h
Go to the documentation of this file.
1 /***********************************************************************************\
2 * (c) Copyright 1998-2019 CERN for the benefit of the LHCb and ATLAS collaborations *
3 * *
4 * This software is distributed under the terms of the Apache version 2 licence, *
5 * copied verbatim in the file "LICENSE". *
6 * *
7 * In applying this licence, CERN does not waive the privileges and immunities *
8 * granted to it by virtue of its status as an Intergovernmental Organization *
9 * or submit itself to any jurisdiction. *
10 \***********************************************************************************/
11 #ifndef GAUDISVC_ServiceManager_H
12 #define GAUDISVC_ServiceManager_H
13 
14 // Include files
16 #include "GaudiKernel/IStateful.h"
19 #include "GaudiKernel/Kernel.h"
20 #include "GaudiKernel/Map.h"
21 #include "GaudiKernel/SmartIF.h"
22 
23 #include "boost/thread.hpp"
24 #include <algorithm>
25 #include <list>
26 #include <memory>
27 #include <string>
28 #include <vector>
29 
30 // Forward declarations
31 class IService;
32 class IMessageSvc;
33 
46 class ServiceManager : public extends<ComponentManager, ISvcManager, ISvcLocator> {
47 public:
48  struct ServiceItem final {
49  ServiceItem( IService* s, long p = 0, bool act = false ) : service( s ), priority( p ), active( act ) {}
51  long priority;
52  bool active;
53  inline bool operator==( const std::string& name ) const { return service->name() == name; }
54  inline bool operator==( const IService* ptr ) const { return service.get() == ptr; }
55  inline bool operator<( const ServiceItem& rhs ) const { return priority < rhs.priority; }
56  };
57 
58  // typedefs and classes
61 
64 
66  inline SmartIF<ISvcLocator>& serviceLocator() const override { return m_svcLocator; }
67 
69  ~ServiceManager() override;
70 
72  const std::list<IService*>& getServices() const override;
73 
75  bool existsService( const std::string& name ) const override;
76 
78  StatusCode addService( IService* svc, int prio = DEFAULT_SVC_PRIORITY ) override;
80  StatusCode addService( const Gaudi::Utils::TypeNameString& typeName, int prio = DEFAULT_SVC_PRIORITY ) override;
82  StatusCode removeService( IService* svc ) override;
84  StatusCode removeService( const std::string& name ) override;
85 
87  StatusCode declareSvcType( const std::string& svcname, const std::string& svctype ) override;
88 
95 
97  StatusCode initialize() override;
99  StatusCode start() override;
101  StatusCode stop() override;
103  StatusCode finalize() override;
104 
106  StatusCode reinitialize() override;
108  StatusCode restart() override;
109 
111  int getPriority( const std::string& name ) const override;
112  StatusCode setPriority( const std::string& name, int pri ) override;
113 
115  bool loopCheckEnabled() const override;
117  void setLoopCheckEnabled( bool en ) override;
118 
120  const std::string& name() const override {
121  static std::string _name = "ServiceManager";
122  return _name;
123  }
124 
126  SmartIF<IService>& service( const Gaudi::Utils::TypeNameString& typeName, const bool createIf = true ) override;
127 
129  template <typename T>
130  inline SmartIF<T> service( const Gaudi::Utils::TypeNameString& typeName, const bool createIf = true ) {
131  return SmartIF<T>{service( typeName, createIf )};
132  }
133 
134 #if !defined( GAUDI_V22_API ) || defined( G22_NEW_SVCLOCATOR )
137 #endif
138 
140  void outputLevelUpdate() override;
141 
142 private:
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:
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 private:
194  void dump() const;
195 };
196 #endif // GAUDISVC_ServiceManager_H
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.
bool operator==(const std::string &name) const
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)
TYPE * get() const
Get interface pointer.
Definition: SmartIF.h:86
MapType m_maptype
Map of service name and service type.
STL class.
StatusCode stop() override
Stop (from RUNNING to INITIALIZED).
Helper class to parse a string of format "type/name".
bool operator==(const IService *ptr) const
bool loopCheckEnabled() const override
Get the value of the initialization loop check flag.
General service interface definition.
Definition: IService.h:28
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:61
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:254
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:47
const std::string & name() const override
Return the name of the manager (implementation of INamedInterface)
void dump() const
StatusCode start() override
Start (from INITIALIZED to RUNNING).
bool operator<(const ServiceItem &rhs) const
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:20
T begin(T... args)
StatusCode reinitialize() override
Initialization (from INITIALIZED or RUNNING to INITIALIZED, via CONFIGURED).
ListSvc::const_iterator find(const std::string &name) const
GaudiUtils::Map< std::string, std::string > MapType
string s
Definition: gaudirun.py:328
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:31
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)
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
ListSvc::const_iterator find(const IService *ptr) const
StatusCode initialize() override
Initialization (from CONFIGURED to INITIALIZED).