The Gaudi Framework  master (d98a2936)
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
Service.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 
13 #include <Gaudi/PluginService.h>
14 #include <Gaudi/Property.h>
17 #include <GaudiKernel/IProperty.h>
18 #include <GaudiKernel/IService.h>
19 #include <GaudiKernel/IStateful.h>
23 #include <GaudiKernel/SmartIF.h>
24 #include <GaudiKernel/ToolHandle.h>
25 #include <mutex>
26 #include <vector>
27 
28 class IMessageSvc;
29 class ISvcManager;
30 class ServiceManager;
31 
39 class GAUDI_API Service : public PropertyHolder<CommonMessaging<implements<IService, IProperty, IStateful>>> {
40 public:
41  using Factory = Gaudi::PluginService::Factory<IService*( const std::string&, ISvcLocator* )>;
42 
43  friend class ServiceManager;
44 
46  const std::string& name() const override;
47 
48  // State machine implementation
49  StatusCode configure() override { return StatusCode::SUCCESS; }
50  StatusCode initialize() override;
51  StatusCode start() override;
52  StatusCode stop() override;
53  StatusCode finalize() override;
54  StatusCode terminate() override { return StatusCode::SUCCESS; }
55  Gaudi::StateMachine::State FSMState() const override { return m_state; }
56  Gaudi::StateMachine::State targetFSMState() const override { return m_targetState; }
57  StatusCode reinitialize() override;
58  StatusCode restart() override;
59 
61  StatusCode sysInitialize() override;
63  StatusCode sysStart() override;
65  StatusCode sysStop() override;
67  StatusCode sysFinalize() override;
69  StatusCode sysReinitialize() override;
71  StatusCode sysRestart() override;
72 
74  Service( std::string name, ISvcLocator* svcloc );
76  SmartIF<ISvcLocator>& serviceLocator() const override;
77 
78  template <typename IFace = IService>
79  SmartIF<IFace> service( const std::string& name, bool createIf = true ) const {
80  return ServiceLocatorHelper( *serviceLocator(), *this )
81  .service<IFace>( name, !createIf, // quiet
82  createIf );
83  }
84 
85  // ==========================================================================
86  // Tool handling
87 
89 
90  template <class T>
92  const std::string& doc = "none" ) {
93  this->declareTool( hndl, hndl.typeAndName() ).ignore();
94  return PropertyHolderImpl::declareProperty( name, hndl, doc );
95  }
96 
97  template <class T>
98  StatusCode declareTool( ToolHandle<T>& handle, bool createIf = true ) {
99  return this->declareTool( handle, handle.typeAndName(), createIf );
100  }
101 
109  template <class T>
110  StatusCode declareTool( ToolHandle<T>& handle, const std::string& toolTypeAndName, bool createIf = true ) {
111 
112  StatusCode sc = handle.initialize( toolTypeAndName, handle.isPublic() ? nullptr : this, createIf );
113  if ( !sc ) {
114  throw GaudiException{ std::string{ "Cannot create handle for " } + ( handle.isPublic() ? "public" : "private" ) +
115  " tool " + toolTypeAndName,
116  name(), sc };
117  }
118 
119  m_toolHandles.push_back( &handle );
120 
121  return sc;
122  }
123 
124  // declare ToolHandleArrays to the AlgTool
125  template <class T>
127  const std::string& doc = "none" ) {
128  addToolsArray( hndlArr );
129  return PropertyHolderImpl::declareProperty( name, hndlArr, doc );
130  }
131 
132  template <class T>
134  m_toolHandleArrays.push_back( &hndlArr );
135  }
136 
137  const std::vector<IAlgTool*>& tools() const;
138 
139 protected:
140  std::vector<IAlgTool*>& tools();
141 
142 private:
143  // place IAlgTools defined via ToolHandles in m_tools
144  void initToolHandles() const;
145 
146 public:
150  SmartIF<IAuditorSvc>& auditorSvc() const;
151 
152 protected:
153  ~Service() override;
158 
160  int outputLevel() const { return m_outputLevel.value(); }
161 
162 private:
163  void sysInitialize_imp();
165  std::once_flag m_initFlag;
166 
168  std::string m_name;
172 
173  void setServiceManager( ISvcManager* ism ) override;
174 
175  // AlgTools used by Service
176  mutable std::vector<IAlgTool*> m_tools;
177  mutable std::vector<BaseToolHandle*> m_toolHandles;
178  mutable std::vector<GaudiHandleArrayBase*> m_toolHandleArrays;
179  mutable bool m_toolHandlesInit = false;
180 
181 protected:
182  // Properties
183 
184  Gaudi::Property<int> m_outputLevel{ this, "OutputLevel", MSG::NIL, "output level" };
185  Gaudi::Property<bool> m_auditorInitialize{ this, "AuditInitialize", false, "trigger auditor on initialize()" };
186  Gaudi::Property<bool> m_auditorStart{ this, "AuditStart", false, "trigger auditor on start()" };
187  Gaudi::Property<bool> m_auditorStop{ this, "AuditStop", false, "trigger auditor on stop()" };
188  Gaudi::Property<bool> m_auditorFinalize{ this, "AuditFinalize", false, "trigger auditor on finalize()" };
189  Gaudi::Property<bool> m_auditorReinitialize{ this, "AuditReinitialize", false, "trigger auditor on reinitialize()" };
190  Gaudi::Property<bool> m_auditorRestart{ this, "AuditRestart", false, "trigger auditor on restart()" };
191 
192  Gaudi::Property<bool> m_autoRetrieveTools{ this, "AutoRetrieveTools", true,
193  "retrieve all AlgTools during initialize" };
194  Gaudi::Property<bool> m_checkToolDeps{ this, "CheckToolDeps", true,
195  "check data dependencies of AlgTools (error if any found)" };
196 
199 };
ServiceLocatorHelper::service
SmartIF< IService > service(std::string_view name, const bool quiet=false, const bool createIf=true) const
Definition: ServiceLocatorHelper.cpp:50
IService
Definition: IService.h:26
Gaudi::Details::PropertyBase
PropertyBase base class allowing PropertyBase* collections to be "homogeneous".
Definition: PropertyBase.h:34
Service::m_pAuditorSvc
SmartIF< IAuditorSvc > m_pAuditorSvc
Auditor Service
Definition: Service.h:198
Service::targetFSMState
Gaudi::StateMachine::State targetFSMState() const override
Definition: Service.h:56
Service::m_toolHandleArrays
std::vector< GaudiHandleArrayBase * > m_toolHandleArrays
Definition: Service.h:178
Service::declareProperty
Gaudi::Details::PropertyBase * declareProperty(const std::string &name, ToolHandleArray< T > &hndlArr, const std::string &doc="none")
Definition: Service.h:126
IService.h
IMessageSvc
Definition: IMessageSvc.h:34
Service::m_initSC
StatusCode m_initSC
Definition: Service.h:164
Service::addToolsArray
void addToolsArray(ToolHandleArray< T > &hndlArr)
Definition: Service.h:133
ISvcLocator
Definition: ISvcLocator.h:42
GaudiException
Definition: GaudiException.h:29
ServiceManager
Definition: ServiceManager.h:40
Service::m_svcLocator
SmartIF< ISvcLocator > m_svcLocator
Service Locator reference
Definition: Service.h:170
Service::service
SmartIF< IFace > service(const std::string &name, bool createIf=true) const
Definition: Service.h:79
Service::declareProperty
Gaudi::Details::PropertyBase * declareProperty(const std::string &name, ToolHandle< T > &hndl, const std::string &doc="none")
Definition: Service.h:91
PropertyHolder
Helper class to implement the IProperty interface.
Definition: PropertyHolder.h:68
ServiceManager::reinitialize
StatusCode reinitialize() override
Initialization (from INITIALIZED or RUNNING to INITIALIZED, via CONFIGURED).
Definition: ServiceManager.cpp:322
IOTest.start
start
Definition: IOTest.py:110
ISvcManager
Definition: ISvcManager.h:30
IAuditorSvc.h
Service::FSMState
Gaudi::StateMachine::State FSMState() const override
Definition: Service.h:55
Service
Definition: Service.h:39
Service::m_name
std::string m_name
Service Name
Definition: Service.h:168
Service::m_initFlag
std::once_flag m_initFlag
Definition: Service.h:165
Gaudi::StateMachine::State
State
Allowed states for classes implementing the state machine (ApplicationMgr, Algorithm,...
Definition: StateMachine.h:21
SmartIF.h
PluginService.h
ToolHandle
Definition: ToolHandle.h:130
StatusCode
Definition: StatusCode.h:64
ServiceManager::initialize
StatusCode initialize() override
Initialization (from CONFIGURED to INITIALIZED).
Definition: ServiceManager.cpp:230
Service::m_tools
std::vector< IAlgTool * > m_tools
Definition: Service.h:176
Service::configure
StatusCode configure() override
Definition: Service.h:49
Gaudi::StateMachine::OFFLINE
@ OFFLINE
Definition: StateMachine.h:22
ToolHandleArray
Definition: ToolHandle.h:397
ServiceLocatorHelper
an helper to share the implementation of service() among the various kernel base classes
Definition: ServiceLocatorHelper.h:26
ServiceManager::finalize
StatusCode finalize() override
Finalize (from INITIALIZED to CONFIGURED).
Definition: ServiceManager.cpp:358
IStateful.h
Service::declareTool
StatusCode declareTool(ToolHandle< T > &handle, const std::string &toolTypeAndName, bool createIf=true)
Declare used tool.
Definition: Service.h:110
SmartIF< ISvcLocator >
CommonMessaging.h
ToolHandleInfo::isPublic
bool isPublic() const noexcept
Definition: ToolHandle.h:49
PropertyHolder::declareProperty
Gaudi::Details::PropertyBase & declareProperty(Gaudi::Details::PropertyBase &prop)
Declare a property.
Definition: PropertyHolder.h:92
StatusCode::SUCCESS
constexpr static const auto SUCCESS
Definition: StatusCode.h:99
ConditionsStallTest.name
name
Definition: ConditionsStallTest.py:77
ServiceLocatorHelper.h
ToolHandle.h
MSG::NIL
@ NIL
Definition: IMessageSvc.h:22
Service::outputLevel
int outputLevel() const
get the Service's output level
Definition: Service.h:160
Service::m_svcManager
SmartIF< ISvcManager > m_svcManager
Definition: Service.h:171
Service::Factory
Gaudi::PluginService::Factory< IService *(const std::string &, ISvcLocator *)> Factory
Definition: Service.h:41
ServiceManager::restart
StatusCode restart() override
Initialization (from RUNNING to RUNNING, via INITIALIZED).
Definition: ServiceManager.cpp:340
ToolHandle::initialize
StatusCode initialize(const std::string &toolTypeAndName, const IInterface *parent=nullptr, bool createIf=true)
Definition: ToolHandle.h:211
ServiceManager::serviceLocator
SmartIF< ISvcLocator > & serviceLocator() const override
Function needed by CommonMessaging.
Definition: ServiceManager.h:60
IProperty.h
ToolHandle::typeAndName
std::string typeAndName() const override
Definition: ToolHandle.h:271
Service::declareTool
StatusCode declareTool(ToolHandle< T > &handle, bool createIf=true)
Definition: Service.h:98
ISvcLocator.h
ServiceManager::stop
StatusCode stop() override
Stop (from RUNNING to INITIALIZED).
Definition: ServiceManager.cpp:291
PropertyHolder.h
GAUDI_API
#define GAUDI_API
Definition: Kernel.h:49
Gaudi::Property< int >
Property.h
Service::m_toolHandles
std::vector< BaseToolHandle * > m_toolHandles
Definition: Service.h:177
Service::terminate
StatusCode terminate() override
Definition: Service.h:54