The Gaudi Framework  master (ff829712)
Loading...
Searching...
No Matches
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>
23#include <GaudiKernel/SmartIF.h>
25#include <mutex>
26#include <vector>
27
28class IMessageSvc;
29class ISvcManager;
30class ServiceManager;
31
39class GAUDI_API Service : public PropertyHolder<CommonMessaging<implements<IService, IProperty, IStateful>>> {
40public:
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
50 StatusCode initialize() override;
51 StatusCode start() override;
52 StatusCode stop() override;
53 StatusCode finalize() override;
55 Gaudi::StateMachine::State FSMState() const override { return m_state; }
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
88 using PropertyHolderImpl::declareProperty;
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
139protected:
140 std::vector<IAlgTool*>& tools();
141
142private:
143 // place IAlgTools defined via ToolHandles in m_tools
144 void initToolHandles() const;
145
146public:
150 SmartIF<IAuditorSvc>& auditorSvc() const;
151
152protected:
153 ~Service() override;
158
160 int outputLevel() const { return m_outputLevel.value(); }
161
162private:
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
181protected:
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};
#define GAUDI_API
Definition Kernel.h:49
PropertyBase base class allowing PropertyBase* collections to be "homogeneous".
Implementation of property with value of concrete type.
Definition PropertyFwd.h:27
Define general base for Gaudi exception.
The IMessage is the interface implemented by the message service.
Definition IMessageSvc.h:34
General service interface definition.
Definition IService.h:26
The ISvcLocator is the interface implemented by the Service Factory in the Application Manager to loc...
Definition ISvcLocator.h:42
The ISvcManager is the interface implemented by the Service Factory in the Application Manager to sup...
Definition ISvcManager.h:30
Gaudi::Details::PropertyBase & declareProperty(Gaudi::Details::PropertyBase &prop)
Declare a property.
Base class for all services.
Definition Service.h:39
Gaudi::StateMachine::State FSMState() const override
Definition Service.h:55
SmartIF< ISvcLocator > & serviceLocator() const override
Retrieve pointer to service locator.
Definition Service.cpp:336
Gaudi::Property< bool > m_auditorStart
Definition Service.h:186
Gaudi::StateMachine::State targetFSMState() const override
Definition Service.h:56
Gaudi::Property< bool > m_auditorFinalize
Definition Service.h:188
StatusCode declareTool(ToolHandle< T > &handle, bool createIf=true)
Definition Service.h:98
std::vector< GaudiHandleArrayBase * > m_toolHandleArrays
Definition Service.h:178
SmartIF< ISvcManager > m_svcManager
Definition Service.h:171
StatusCode configure() override
Definition Service.h:49
std::once_flag m_initFlag
Definition Service.h:165
Gaudi::Property< int > m_outputLevel
flag indicating whether ToolHandle tools have been added to m_tools
Definition Service.h:184
Gaudi::Property< bool > m_auditorStop
Definition Service.h:187
const std::string & name() const override
Retrieve name of the service.
Definition Service.cpp:333
Gaudi::Details::PropertyBase * declareProperty(const std::string &name, ToolHandleArray< T > &hndlArr, const std::string &doc="none")
Definition Service.h:126
Gaudi::StateMachine::State m_targetState
Service state.
Definition Service.h:157
StatusCode terminate() override
Definition Service.h:54
int outputLevel() const
get the Service's output level
Definition Service.h:160
std::vector< IAlgTool * > m_tools
Definition Service.h:176
Gaudi::Property< bool > m_auditorRestart
Definition Service.h:190
std::vector< BaseToolHandle * > m_toolHandles
Definition Service.h:177
Gaudi::Details::PropertyBase * declareProperty(const std::string &name, ToolHandle< T > &hndl, const std::string &doc="none")
Definition Service.h:91
Gaudi::StateMachine::State m_state
Service state.
Definition Service.h:155
Gaudi::Property< bool > m_checkToolDeps
Definition Service.h:194
SmartIF< IAuditorSvc > m_pAuditorSvc
Auditor Service.
Definition Service.h:198
void setServiceManager(ISvcManager *ism) override
Definition Service.cpp:368
bool m_toolHandlesInit
Definition Service.h:179
Gaudi::Property< bool > m_autoRetrieveTools
Definition Service.h:192
StatusCode m_initSC
Definition Service.h:164
SmartIF< ISvcLocator > m_svcLocator
Service Locator reference.
Definition Service.h:170
SmartIF< IFace > service(const std::string &name, bool createIf=true) const
Definition Service.h:79
Gaudi::Property< bool > m_auditorInitialize
Definition Service.h:185
void addToolsArray(ToolHandleArray< T > &hndlArr)
Definition Service.h:133
Gaudi::Property< bool > m_auditorReinitialize
Definition Service.h:189
Gaudi::PluginService::Factory< IService *(const std::string &, ISvcLocator *)> Factory
Definition Service.h:41
std::string m_name
Service Name.
Definition Service.h:168
Service(std::string name, ISvcLocator *svcloc)
Standard Constructor.
Definition Service.cpp:340
StatusCode declareTool(ToolHandle< T > &handle, const std::string &toolTypeAndName, bool createIf=true)
Declare used tool.
Definition Service.h:110
friend class ServiceManager
Definition Service.h:43
an helper to share the implementation of service() among the various kernel base classes
SmartIF< IService > service(std::string_view name, const bool quiet=false, const bool createIf=true) const
The ServiceManager class is in charge of the creation of concrete instances of Services.
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
constexpr static const auto SUCCESS
Definition StatusCode.h:99
Array of Handles to be used in lieu of vector of naked pointers to tools.
Definition ToolHandle.h:399
Handle to be used in lieu of naked pointers to tools.
Definition ToolHandle.h:132
std::string typeAndName() const override
Definition ToolHandle.h:273
StatusCode initialize(const std::string &toolTypeAndName, const IInterface *parent=nullptr, bool createIf=true)
Definition ToolHandle.h:213
bool isPublic() const noexcept
Definition ToolHandle.h:49
State
Allowed states for classes implementing the state machine (ApplicationMgr, Algorithm,...
@ NIL
Definition IMessageSvc.h:22