The Gaudi Framework  master (82fdf313)
Loading...
Searching...
No Matches
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
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
25class IService;
26class IMessageSvc;
27
40class ServiceManager : public extends<ComponentManager, ISvcManager, ISvcLocator> {
41public:
42 struct ServiceItem final {
43 ServiceItem( IService* s, int p = 0, bool act = false ) : service( s ), priority( p ), active( act ) {}
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
57 ServiceManager( IInterface* application );
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
131private:
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
149private:
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
179private:
180 void dump() const;
181};
SmartIF< ISvcLocator > m_svcLocator
Service locator (needed to access the MessageSvc)
Helper class to parse a string of format "type/name".
Extension of the STL map.
Definition Map.h:83
Definition of the basic interface.
Definition IInterface.h:225
The IMessage is the interface implemented by the message service.
Definition IMessageSvc.h:34
General service interface definition.
Definition IService.h:26
StatusCode declareSvcType(std::string svcname, std::string svctype) override
implementation of ISvcManager::declareSvcType
ListSvc::const_iterator find(const IService *ptr) const
const std::list< IService * > & getServices() const override
Return the list of Services.
ListSvc::iterator find(const IService *ptr)
const std::string & name() const override
Return the name of the manager (implementation of INamedInterface)
ListSvc::const_iterator find(std::string_view name) const
bool loopCheckEnabled() const override
Get the value of the initialization loop check flag.
StatusCode start() override
Start (from INITIALIZED to RUNNING).
void outputLevelUpdate() override
Function to call to update the outputLevel of the components (after a change in MessageSvc).
StatusCode reinitialize() override
Initialization (from INITIALIZED or RUNNING to INITIALIZED, via CONFIGURED).
StatusCode addService(IService *svc, int prio=DEFAULT_SVC_PRIORITY) override
implementation of ISvcManager::addService
StatusCode finalize() override
Finalize (from INITIALIZED to CONFIGURED).
bool existsService(std::string_view name) const override
implementation of ISvcLocation::existsService
std::list< IService * > m_listOfPtrs
List of pointers to the know services used to implement getServices()
std::map< std::string, std::string, std::less<> > MapType
StatusCode removeService(IService *svc) override
implementation of ISvcManager::removeService
StatusCode initialize() override
Initialization (from CONFIGURED to INITIALIZED).
StatusCode restart() override
Initialization (from RUNNING to RUNNING, via INITIALIZED).
SmartIF< IService > & createService(const Gaudi::Utils::TypeNameString &nametype) override
implementation of ISvcManager::createService NOTE: as this returns a &, we must guarantee that once c...
SmartIF< IService > m_appSvc
Pointer to the application IService interface.
GaudiUtils::Map< InterfaceID, SmartIF< IInterface > > m_defaultImplementations
SmartIF< IService > & service(const Gaudi::Utils::TypeNameString &typeName, const bool createIf=true) override
Returns a smart pointer to a service.
SmartIF< ISvcLocator > & serviceLocator() const override
Function needed by CommonMessaging.
~ServiceManager() override
virtual destructor
std::map< std::string, std::recursive_mutex > m_lockMap
ListSvc m_listsvc
List of service maintained by ServiceManager This contains SmartIF<T> for all services – and because ...
ServiceManager(IInterface *application)
default creator
ListSvc::iterator find(std::string_view name)
void setLoopCheckEnabled(bool en) override
Set the value of the initialization loop check flag.
std::list< ServiceItem > ListSvc
bool m_loopCheck
Check for service initialization loops.
int getPriority(std::string_view name) const override
manage priorities of services
std::recursive_mutex m_gLock
Mutex to synchronize shared service initialization between threads.
StatusCode stop() override
Stop (from RUNNING to INITIALIZED).
void dump() const
StatusCode setPriority(std::string_view name, int pri) override
SmartIF< T > service(const Gaudi::Utils::TypeNameString &typeName, const bool createIf=true)
Returns a smart pointer to the requested interface of a service.
MapType m_maptype
Map of service name and service type.
Small smart pointer class with automatic reference counting for IInterface.
Definition SmartIF.h:28
This class is used for returning status codes from appropriate routines.
Definition StatusCode.h:64
Base class used to extend a class implementing other interfaces.
Definition extends.h:19
ServiceItem(IService *s, int p=0, bool act=false)
bool operator<(const ServiceItem &rhs) const
SmartIF< IService > service
bool operator==(std::string_view name) const
bool operator==(const IService *ptr) const