The Gaudi Framework  master (37c0b60a)
ServiceManager.h
Go to the documentation of this file.
1 /***********************************************************************************\
2 * (c) Copyright 1998-2024 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 <algorithm>
24 #include <list>
25 #include <memory>
26 #include <mutex>
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, int p = 0, bool act = false ) : service( s ), priority( p ), active( act ) {}
51  int priority;
52  bool active;
53  bool operator==( std::string_view name ) const { return service->name() == name; }
54  bool operator==( const IService* ptr ) const { return service.get() == ptr; }
55  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( std::string_view 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( std::string_view name ) override;
85 
87  StatusCode declareSvcType( std::string svcname, 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( std::string_view name ) const override;
112  StatusCode setPriority( std::string_view 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  ListSvc::iterator find( std::string_view name ) {
144  auto lck = std::scoped_lock{ m_gLock };
145  return std::find( m_listsvc.begin(), m_listsvc.end(), name );
146  }
147  ListSvc::const_iterator find( std::string_view name ) const {
148  auto lck = std::scoped_lock{ m_gLock };
149  return std::find( m_listsvc.begin(), m_listsvc.end(), name );
150  }
151  ListSvc::iterator find( const IService* ptr ) {
152  auto lck = std::scoped_lock{ m_gLock };
153  return std::find( m_listsvc.begin(), m_listsvc.end(), ptr );
154  }
155  ListSvc::const_iterator find( const IService* ptr ) const {
156  auto lck = std::scoped_lock{ 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 
189 
190 private:
191  void dump() const;
192 };
193 #endif // GAUDISVC_ServiceManager_H
IService
Definition: IService.h:28
ServiceManager::find
ListSvc::const_iterator find(std::string_view name) const
Definition: ServiceManager.h:147
ServiceManager::loopCheckEnabled
bool loopCheckEnabled() const override
Get the value of the initialization loop check flag.
Definition: ServiceManager.cpp:504
ComponentManager.h
std::string
STL class.
IMessageSvc
Definition: IMessageSvc.h:47
std::list< ServiceItem >
ServiceManager::start
StatusCode start() override
Start (from INITIALIZED to RUNNING).
Definition: ServiceManager.cpp:301
ServiceManager::outputLevelUpdate
void outputLevelUpdate() override
Function to call to update the outputLevel of the components (after a change in MessageSvc).
Definition: ServiceManager.cpp:538
gaudirun.s
string s
Definition: gaudirun.py:346
std::find
T find(T... args)
ServiceManager::~ServiceManager
~ServiceManager() override
virtual destructor
Definition: ServiceManager.cpp:62
ServiceManager::ServiceItem
Definition: ServiceManager.h:48
ServiceManager::dump
void dump() const
Definition: ServiceManager.cpp:513
ServiceManager
Definition: ServiceManager.h:46
std::recursive_mutex
STL class.
ServiceManager::ServiceItem::ServiceItem
ServiceItem(IService *s, int p=0, bool act=false)
Definition: ServiceManager.h:49
ISvcManager::addService
virtual StatusCode addService(IService *svc, int prio=DEFAULT_SVC_PRIORITY)=0
Add a service to the "active" list of services of the factory.
ServiceManager::ServiceItem::service
SmartIF< IService > service
Definition: ServiceManager.h:50
ServiceManager::reinitialize
StatusCode reinitialize() override
Initialization (from INITIALIZED or RUNNING to INITIALIZED, via CONFIGURED).
Definition: ServiceManager.cpp:368
ServiceManager::find
ListSvc::const_iterator find(const IService *ptr) const
Definition: ServiceManager.h:155
ServiceManager::ServiceItem::active
bool active
Definition: ServiceManager.h:52
SmartIF.h
ServiceManager::m_appSvc
SmartIF< IService > m_appSvc
Pointer to the application IService interface.
Definition: ServiceManager.h:179
ServiceManager::ListSvc
std::list< ServiceItem > ListSvc
Definition: ServiceManager.h:59
Gaudi::Utils::TypeNameString
Helper class to parse a string of format "type/name".
Definition: TypeNameString.h:20
ServiceManager::addService
StatusCode addService(IService *svc, int prio=DEFAULT_SVC_PRIORITY) override
implementation of ISvcManager::addService
Definition: ServiceManager.cpp:119
StatusCode
Definition: StatusCode.h:65
ServiceManager::initialize
StatusCode initialize() override
Initialization (from CONFIGURED to INITIALIZED).
Definition: ServiceManager.cpp:267
ServiceManager::ServiceItem::priority
int priority
Definition: ServiceManager.h:51
ServiceManager::removeService
StatusCode removeService(IService *svc) override
implementation of ISvcManager::removeService
Definition: ServiceManager.cpp:239
ServiceManager::service
SmartIF< IService > & service(const Gaudi::Utils::TypeNameString &typeName, const bool createIf=true) override
Returns a smart pointer to a service.
Definition: ServiceManager.cpp:178
GaudiUtils::Map
Definition: Map.h:91
ServiceManager::m_listsvc
ListSvc m_listsvc
List of service maintained by ServiceManager This contains SmartIF<T> for all services – and because ...
Definition: ServiceManager.h:161
ServiceManager::declareSvcType
StatusCode declareSvcType(std::string svcname, std::string svctype) override
implementation of ISvcManager::declareSvcType
Definition: ServiceManager.cpp:259
ServiceManager::finalize
StatusCode finalize() override
Finalize (from INITIALIZED to CONFIGURED).
Definition: ServiceManager.cpp:410
IStateful.h
ServiceManager::createService
SmartIF< IService > & createService(const Gaudi::Utils::TypeNameString &nametype) override
implementation of ISvcManager::createService NOTE: as this returns a &, we must guarantee that once c...
Definition: ServiceManager.cpp:69
ServiceManager::m_loopCheck
bool m_loopCheck
Check for service initialization loops.
Definition: ServiceManager.h:176
ServiceManager::ServiceManager
ServiceManager(IInterface *application)
default creator
Definition: ServiceManager.cpp:54
SmartIF< IService >
std::map
STL class.
extends
Base class used to extend a class implementing other interfaces.
Definition: extends.h:20
ServiceManager::m_lockMap
std::map< std::string, std::recursive_mutex > m_lockMap
Definition: ServiceManager.h:188
ServiceManager::find
ListSvc::iterator find(std::string_view name)
Definition: ServiceManager.h:143
ServiceManager::ServiceItem::operator==
bool operator==(std::string_view name) const
Definition: ServiceManager.h:53
ServiceManager::m_listOfPtrs
std::list< IService * > m_listOfPtrs
List of pointers to the know services used to implement getServices()
Definition: ServiceManager.h:182
ServiceManager::setLoopCheckEnabled
void setLoopCheckEnabled(bool en) override
Set the value of the initialization loop check flag.
Definition: ServiceManager.cpp:508
ComponentManager::m_svcLocator
SmartIF< ISvcLocator > m_svcLocator
Service locator (needed to access the MessageSvc)
Definition: ComponentManager.h:86
ServiceManager::getPriority
int getPriority(std::string_view name) const override
manage priorities of services
Definition: ServiceManager.cpp:486
ServiceManager::find
ListSvc::iterator find(const IService *ptr)
Definition: ServiceManager.h:151
ServiceManager::MapType
std::map< std::string, std::string, std::less<> > MapType
Definition: ServiceManager.h:60
ServiceManager::m_defaultImplementations
GaudiUtils::Map< InterfaceID, SmartIF< IInterface > > m_defaultImplementations
Definition: ServiceManager.h:184
SmartIF::get
TYPE * get() const
Get interface pointer.
Definition: SmartIF.h:86
GaudiDict::typeName
std::string typeName(const std::type_info &typ)
Definition: Dictionary.cpp:31
std::list::begin
T begin(T... args)
ServiceManager::existsService
bool existsService(std::string_view name) const override
implementation of ISvcLocation::existsService
Definition: ServiceManager.cpp:232
Kernel.h
IInterface
Definition: IInterface.h:239
ServiceManager::restart
StatusCode restart() override
Initialization (from RUNNING to RUNNING, via INITIALIZED).
Definition: ServiceManager.cpp:389
ServiceManager::m_gLock
std::recursive_mutex m_gLock
Mutex to synchronize shared service initialization between threads.
Definition: ServiceManager.h:187
ServiceManager::ServiceItem::operator==
bool operator==(const IService *ptr) const
Definition: ServiceManager.h:54
ServiceManager::serviceLocator
SmartIF< ISvcLocator > & serviceLocator() const override
Function needed by CommonMessaging.
Definition: ServiceManager.h:66
std::list::end
T end(T... args)
Map.h
ServiceManager::ServiceItem::operator<
bool operator<(const ServiceItem &rhs) const
Definition: ServiceManager.h:55
ISvcManager::createService
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.
ServiceManager::getServices
const std::list< IService * > & getServices() const override
Return the list of Services.
Definition: ServiceManager.cpp:222
ISvcLocator.h
ServiceManager::name
const std::string & name() const override
Return the name of the manager (implementation of INamedInterface)
Definition: ServiceManager.h:120
ServiceManager::stop
StatusCode stop() override
Stop (from RUNNING to INITIALIZED).
Definition: ServiceManager.cpp:334
gaudirun.application
application
Definition: gaudirun.py:323
ServiceManager::m_maptype
MapType m_maptype
Map of service name and service type.
Definition: ServiceManager.h:175
ServiceManager::service
SmartIF< T > service(const Gaudi::Utils::TypeNameString &typeName, const bool createIf=true)
Returns a smart pointer to the requested interface of a service.
Definition: ServiceManager.h:130
ISvcManager.h
ServiceManager::setPriority
StatusCode setPriority(std::string_view name, int pri) override
Definition: ServiceManager.cpp:493