The Gaudi Framework  master (d98a2936)
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
ServiceManager.h
Go to the documentation of this file.
1 /***********************************************************************************\
2 * (c) Copyright 1998-2025 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 #pragma once
12 
14 #include <GaudiKernel/IStateful.h>
17 #include <GaudiKernel/Kernel.h>
18 #include <GaudiKernel/Map.h>
19 #include <GaudiKernel/SmartIF.h>
20 #include <algorithm>
21 #include <list>
22 #include <mutex>
23 #include <string>
24 
25 class IService;
26 class IMessageSvc;
27 
40 class ServiceManager : public extends<ComponentManager, ISvcManager, ISvcLocator> {
41 public:
42  struct ServiceItem final {
43  ServiceItem( IService* s, int p = 0, bool act = false ) : service( s ), priority( p ), active( act ) {}
45  int priority;
46  bool active;
47  bool operator==( std::string_view name ) const { return service->name() == name; }
48  bool operator==( const IService* ptr ) const { return service.get() == ptr; }
49  bool operator<( const ServiceItem& rhs ) const { return priority < rhs.priority; }
50  };
51 
52  // typedefs and classes
53  typedef std::list<ServiceItem> ListSvc;
54  typedef std::map<std::string, std::string, std::less<>> MapType;
55 
58 
60  inline SmartIF<ISvcLocator>& serviceLocator() const override { return m_svcLocator; }
61 
63  ~ServiceManager() override;
64 
66  const std::list<IService*>& getServices() const override;
67 
69  bool existsService( std::string_view name ) const override;
70 
72  StatusCode addService( IService* svc, int prio = DEFAULT_SVC_PRIORITY ) override;
74  StatusCode addService( const Gaudi::Utils::TypeNameString& typeName, int prio = DEFAULT_SVC_PRIORITY ) override;
76  StatusCode removeService( IService* svc ) override;
78  StatusCode removeService( std::string_view name ) override;
79 
81  StatusCode declareSvcType( std::string svcname, std::string svctype ) override;
82 
89 
91  StatusCode initialize() override;
93  StatusCode start() override;
95  StatusCode stop() override;
97  StatusCode finalize() override;
98 
100  StatusCode reinitialize() override;
102  StatusCode restart() override;
103 
105  int getPriority( std::string_view name ) const override;
106  StatusCode setPriority( std::string_view name, int pri ) override;
107 
109  bool loopCheckEnabled() const override;
111  void setLoopCheckEnabled( bool en ) override;
112 
114  const std::string& name() const override {
115  static std::string _name = "ServiceManager";
116  return _name;
117  }
118 
120  SmartIF<IService>& service( const Gaudi::Utils::TypeNameString& typeName, const bool createIf = true ) override;
121 
123  template <typename T>
124  inline SmartIF<T> service( const Gaudi::Utils::TypeNameString& typeName, const bool createIf = true ) {
125  return SmartIF<T>{ service( typeName, createIf ) };
126  }
127 
129  void outputLevelUpdate() override;
130 
131 private:
132  ListSvc::iterator find( std::string_view name ) {
133  auto lck = std::scoped_lock{ m_gLock };
134  return std::find( m_listsvc.begin(), m_listsvc.end(), name );
135  }
136  ListSvc::const_iterator find( std::string_view name ) const {
137  auto lck = std::scoped_lock{ m_gLock };
138  return std::find( m_listsvc.begin(), m_listsvc.end(), name );
139  }
140  ListSvc::iterator find( const IService* ptr ) {
141  auto lck = std::scoped_lock{ m_gLock };
142  return std::find( m_listsvc.begin(), m_listsvc.end(), ptr );
143  }
144  ListSvc::const_iterator find( const IService* ptr ) const {
145  auto lck = std::scoped_lock{ m_gLock };
146  return std::find( m_listsvc.begin(), m_listsvc.end(), ptr );
147  }
148 
149 private:
165  bool m_loopCheck = true;
166 
169 
171  mutable std::list<IService*> m_listOfPtrs;
172 
174 
176  mutable std::recursive_mutex m_gLock;
177  mutable std::map<std::string, std::recursive_mutex> m_lockMap;
178 
179 private:
180  void dump() const;
181 };
IService
Definition: IService.h:26
ServiceManager::find
ListSvc::const_iterator find(std::string_view name) const
Definition: ServiceManager.h:136
ServiceManager::loopCheckEnabled
bool loopCheckEnabled() const override
Get the value of the initialization loop check flag.
Definition: ServiceManager.cpp:443
ComponentManager.h
IMessageSvc
Definition: IMessageSvc.h:34
ServiceManager::start
StatusCode start() override
Start (from INITIALIZED to RUNNING).
Definition: ServiceManager.cpp:261
ServiceManager::outputLevelUpdate
void outputLevelUpdate() override
Function to call to update the outputLevel of the components (after a change in MessageSvc).
Definition: ServiceManager.cpp:471
gaudirun.s
string s
Definition: gaudirun.py:346
ServiceManager::~ServiceManager
~ServiceManager() override
virtual destructor
Definition: ServiceManager.cpp:57
ServiceManager::ServiceItem
Definition: ServiceManager.h:42
ServiceManager::dump
void dump() const
Definition: ServiceManager.cpp:446
ServiceManager
Definition: ServiceManager.h:40
ServiceManager::ServiceItem::ServiceItem
ServiceItem(IService *s, int p=0, bool act=false)
Definition: ServiceManager.h:43
ServiceManager::ServiceItem::service
SmartIF< IService > service
Definition: ServiceManager.h:44
ServiceManager::reinitialize
StatusCode reinitialize() override
Initialization (from INITIALIZED or RUNNING to INITIALIZED, via CONFIGURED).
Definition: ServiceManager.cpp:322
ServiceManager::find
ListSvc::const_iterator find(const IService *ptr) const
Definition: ServiceManager.h:144
ServiceManager::ServiceItem::active
bool active
Definition: ServiceManager.h:46
SmartIF.h
ServiceManager::m_appSvc
SmartIF< IService > m_appSvc
Pointer to the application IService interface.
Definition: ServiceManager.h:168
ServiceManager::ListSvc
std::list< ServiceItem > ListSvc
Definition: ServiceManager.h:53
Gaudi::Utils::TypeNameString
Helper class to parse a string of format "type/name".
Definition: TypeNameString.h:19
ServiceManager::addService
StatusCode addService(IService *svc, int prio=DEFAULT_SVC_PRIORITY) override
implementation of ISvcManager::addService
Definition: ServiceManager.cpp:108
StatusCode
Definition: StatusCode.h:64
ServiceManager::initialize
StatusCode initialize() override
Initialization (from CONFIGURED to INITIALIZED).
Definition: ServiceManager.cpp:230
ServiceManager::ServiceItem::priority
int priority
Definition: ServiceManager.h:45
ServiceManager::removeService
StatusCode removeService(IService *svc) override
implementation of ISvcManager::removeService
Definition: ServiceManager.cpp:211
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:159
GaudiUtils::Map
Definition: Map.h:82
ServiceManager::m_listsvc
ListSvc m_listsvc
List of service maintained by ServiceManager This contains SmartIF<T> for all services – and because ...
Definition: ServiceManager.h:150
ServiceManager::declareSvcType
StatusCode declareSvcType(std::string svcname, std::string svctype) override
implementation of ISvcManager::declareSvcType
Definition: ServiceManager.cpp:225
ServiceManager::finalize
StatusCode finalize() override
Finalize (from INITIALIZED to CONFIGURED).
Definition: ServiceManager.cpp:358
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:62
ServiceManager::m_loopCheck
bool m_loopCheck
Check for service initialization loops.
Definition: ServiceManager.h:165
ServiceManager::ServiceManager
ServiceManager(IInterface *application)
default creator
Definition: ServiceManager.cpp:50
SmartIF< IService >
extends
Base class used to extend a class implementing other interfaces.
Definition: extends.h:19
ServiceManager::m_lockMap
std::map< std::string, std::recursive_mutex > m_lockMap
Definition: ServiceManager.h:177
ServiceManager::find
ListSvc::iterator find(std::string_view name)
Definition: ServiceManager.h:132
ServiceManager::ServiceItem::operator==
bool operator==(std::string_view name) const
Definition: ServiceManager.h:47
ServiceManager::m_listOfPtrs
std::list< IService * > m_listOfPtrs
List of pointers to the know services used to implement getServices()
Definition: ServiceManager.h:171
ServiceManager::setLoopCheckEnabled
void setLoopCheckEnabled(bool en) override
Set the value of the initialization loop check flag.
Definition: ServiceManager.cpp:444
ComponentManager::m_svcLocator
SmartIF< ISvcLocator > m_svcLocator
Service locator (needed to access the MessageSvc)
Definition: ComponentManager.h:89
ServiceManager::getPriority
int getPriority(std::string_view name) const override
manage priorities of services
Definition: ServiceManager.cpp:431
ServiceManager::find
ListSvc::iterator find(const IService *ptr)
Definition: ServiceManager.h:140
ServiceManager::MapType
std::map< std::string, std::string, std::less<> > MapType
Definition: ServiceManager.h:54
ServiceManager::m_defaultImplementations
GaudiUtils::Map< InterfaceID, SmartIF< IInterface > > m_defaultImplementations
Definition: ServiceManager.h:173
SmartIF::get
TYPE * get() const
Get interface pointer.
Definition: SmartIF.h:82
GaudiDict::typeName
std::string typeName(const std::type_info &typ)
Definition: Dictionary.cpp:31
ServiceManager::existsService
bool existsService(std::string_view name) const override
implementation of ISvcLocation::existsService
Definition: ServiceManager.cpp:209
Kernel.h
IInterface
Definition: IInterface.h:225
ServiceManager::restart
StatusCode restart() override
Initialization (from RUNNING to RUNNING, via INITIALIZED).
Definition: ServiceManager.cpp:340
ServiceManager::m_gLock
std::recursive_mutex m_gLock
Mutex to synchronize shared service initialization between threads.
Definition: ServiceManager.h:176
ServiceManager::ServiceItem::operator==
bool operator==(const IService *ptr) const
Definition: ServiceManager.h:48
ServiceManager::serviceLocator
SmartIF< ISvcLocator > & serviceLocator() const override
Function needed by CommonMessaging.
Definition: ServiceManager.h:60
Map.h
ServiceManager::ServiceItem::operator<
bool operator<(const ServiceItem &rhs) const
Definition: ServiceManager.h:49
ServiceManager::getServices
const std::list< IService * > & getServices() const override
Return the list of Services.
Definition: ServiceManager.cpp:202
ISvcLocator.h
ServiceManager::name
const std::string & name() const override
Return the name of the manager (implementation of INamedInterface)
Definition: ServiceManager.h:114
ServiceManager::stop
StatusCode stop() override
Stop (from RUNNING to INITIALIZED).
Definition: ServiceManager.cpp:291
gaudirun.application
application
Definition: gaudirun.py:323
ServiceManager::m_maptype
MapType m_maptype
Map of service name and service type.
Definition: ServiceManager.h:164
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:124
ISvcManager.h
ServiceManager::setPriority
StatusCode setPriority(std::string_view name, int pri) override
Definition: ServiceManager.cpp:436