The Gaudi Framework  v33r1 (b1225454)
Service.h
Go to the documentation of this file.
1 /***********************************************************************************\
2 * (c) Copyright 1998-2019 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 GAUDIKERNEL_SERVICE_H
12 #define GAUDIKERNEL_SERVICE_H
13 // ============================================================================
14 // Include files
15 // ============================================================================
18 #include "GaudiKernel/IProperty.h"
19 #include "GaudiKernel/IService.h"
20 #include "GaudiKernel/IStateful.h"
22 #include "GaudiKernel/Property.h"
25 #include "GaudiKernel/SmartIF.h"
26 #include "GaudiKernel/ToolHandle.h"
27 #include <Gaudi/PluginService.h>
28 
29 // ============================================================================
30 #include <mutex>
31 #include <vector>
32 // ============================================================================
33 // Forward declarations
34 // ============================================================================
35 class IMessageSvc;
36 class ISvcManager;
37 class ServiceManager;
38 // ============================================================================
46 class GAUDI_API Service : public PropertyHolder<CommonMessaging<implements<IService, IProperty, IStateful>>> {
47 public:
48  using Factory = Gaudi::PluginService::Factory<IService*( const std::string&, ISvcLocator* )>;
49 
50  friend class ServiceManager;
51 
53  const std::string& name() const override;
54 
55  // State machine implementation
56  StatusCode configure() override { return StatusCode::SUCCESS; }
57  StatusCode initialize() override;
58  StatusCode start() override;
59  StatusCode stop() override;
60  StatusCode finalize() override;
61  StatusCode terminate() override { return StatusCode::SUCCESS; }
62  Gaudi::StateMachine::State FSMState() const override { return m_state; }
63  Gaudi::StateMachine::State targetFSMState() const override { return m_targetState; }
64  StatusCode reinitialize() override;
65  StatusCode restart() override;
66 
68  StatusCode sysInitialize() override;
70  StatusCode sysStart() override;
72  StatusCode sysStop() override;
74  StatusCode sysFinalize() override;
76  StatusCode sysReinitialize() override;
78  StatusCode sysRestart() override;
79 
81  Service( std::string name, ISvcLocator* svcloc );
83  SmartIF<ISvcLocator>& serviceLocator() const override;
84 
88  StatusCode setProperties();
89 
92  template <class T>
93  StatusCode service( const std::string& name, const T*& psvc, bool createIf = true ) const {
94  ISvcLocator& svcLoc = *serviceLocator();
95  auto ptr = ServiceLocatorHelper( svcLoc, *this )
96  .service<T>( name, !createIf, // quiet
97  createIf );
98  if ( ptr ) {
99  psvc = ptr.get();
100  const_cast<T*>( psvc )->addRef();
101  return StatusCode::SUCCESS;
102  }
103  // else
104  psvc = nullptr;
105  return StatusCode::FAILURE;
106  }
107 
108  template <class T>
109  StatusCode service( const std::string& name, T*& psvc, bool createIf = true ) const {
110  auto ptr = service<T>( name, createIf );
111  psvc = ( ptr ? ptr.get() : nullptr );
112  if ( psvc ) {
113  psvc->addRef();
114  return StatusCode::SUCCESS;
115  }
116  return StatusCode::FAILURE;
117  }
118 
119  template <typename IFace = IService>
120  SmartIF<IFace> service( const std::string& name, bool createIf = true ) const {
121  return ServiceLocatorHelper( *serviceLocator(), *this )
122  .service<IFace>( name, !createIf, // quiet
123  createIf );
124  }
125 
128  template <class T>
129  StatusCode service( const std::string& svcType, const std::string& svcName, T*& psvc ) const {
130  return service( svcType + "/" + svcName, psvc );
131  }
132 
140  template <class T>
141  StatusCode declareTool( ToolHandle<T>& handle, std::string toolTypeAndName, bool createIf = true ) {
142 
143  StatusCode sc = handle.initialize( toolTypeAndName, handle.isPublic() ? nullptr : this, createIf );
144  if ( UNLIKELY( !sc ) ) {
145  throw GaudiException{std::string{"Cannot create handle for "} + ( handle.isPublic() ? "public" : "private" ) +
146  " tool " + toolTypeAndName,
147  name(), sc};
148  }
149 
150  return sc;
151  }
152 
153  // ==========================================================================
157  SmartIF<IAuditorSvc>& auditorSvc() const;
158 
159 protected:
161  ~Service() override;
166 
168  int outputLevel() const { return m_outputLevel.value(); }
169 
170 private:
171  void sysInitialize_imp();
174 
180 
181  void setServiceManager( ISvcManager* ism ) override;
182 
183 protected:
184  // Properties
185 
186  Gaudi::Property<int> m_outputLevel{this, "OutputLevel", MSG::NIL, "output level"};
187  Gaudi::Property<bool> m_auditInit{this, "AuditServices", false, "[[deprecated]] unused"};
188  Gaudi::Property<bool> m_auditorInitialize{this, "AuditInitialize", false, "trigger auditor on initialize()"};
189  Gaudi::Property<bool> m_auditorStart{this, "AuditStart", false, "trigger auditor on start()"};
190  Gaudi::Property<bool> m_auditorStop{this, "AuditStop", false, "trigger auditor on stop()"};
191  Gaudi::Property<bool> m_auditorFinalize{this, "AuditFinalize", false, "trigger auditor on finalize()"};
192  Gaudi::Property<bool> m_auditorReinitialize{this, "AuditReinitialize", false, "trigger auditor on reinitialize()"};
193  Gaudi::Property<bool> m_auditorRestart{this, "AuditRestart", false, "trigger auditor on restart()"};
194 
197 };
198 
199 #endif // GAUDIKERNEL_SERVICE_H
The ServiceManager class is in charge of the creation of concrete instances of Services.
#define UNLIKELY(x)
Definition: Kernel.h:106
an helper to share the implementation of service() among the various kernel base classes
SmartIF< IFace > service(const std::string &name, bool createIf=true) const
Definition: Service.h:120
Define general base for Gaudi exception.
The ISvcLocator is the interface implemented by the Service Factory in the Application Manager to loc...
Definition: ISvcLocator.h:35
StatusCode finalize() override
Finalize (from INITIALIZED to CONFIGURED).
SmartIF< IAuditorSvc > m_pAuditorSvc
Auditor Service.
Definition: Service.h:196
Implementation of property with value of concrete type.
Definition: Property.h:370
Gaudi::StateMachine::State FSMState() const override
Definition: Service.h:62
StatusCode m_initSC
Definition: Service.h:172
The ISvcManager is the interface implemented by the Service Factory in the Application Manager to sup...
Definition: ISvcManager.h:38
SmartIF< IService > service(std::string_view name, const bool quiet=false, const bool createIf=true) const
SmartIF< ISvcLocator > & serviceLocator() const override
Function needed by CommonMessaging.
constexpr static const auto SUCCESS
Definition: StatusCode.h:100
SmartIF< ISvcLocator > m_svcLocator
Service Locator reference.
Definition: Service.h:178
StatusCode restart() override
Initialization (from RUNNING to RUNNING, via INITIALIZED).
std::string m_name
Service Name.
Definition: Service.h:176
State
Allowed states for classes implementing the state machine (ApplicationMgr, Algorithm,...
Definition: StateMachine.h:22
TYPE * get() const
Get interface pointer.
Definition: SmartIF.h:86
def start
Definition: IOTest.py:108
StatusCode configure() override
Definition: Service.h:56
STL class.
StatusCode stop() override
Stop (from RUNNING to INITIALIZED).
StatusCode service(const std::string &svcType, const std::string &svcName, T *&psvc) const
Access a service by name and type, creating it if it doesn't already exist.
Definition: Service.h:129
General service interface definition.
Definition: IService.h:28
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:61
bool isPublic() const noexcept
Definition: ToolHandle.h:50
The IMessage is the interface implemented by the message service.
Definition: IMessageSvc.h:47
std::once_flag m_initFlag
Definition: Service.h:173
int outputLevel() const
get the Service's output level
Definition: Service.h:168
Handle to be used in lieu of naked pointers to tools.
Definition: ToolHandle.h:135
StatusCode initialize(const std::string &toolTypeAndName, const IInterface *parent=nullptr, bool createIf=true)
Definition: ToolHandle.h:210
Gaudi::PluginService::Factory< IService *(const std::string &, ISvcLocator *)> Factory
Definition: Service.h:48
StatusCode declareTool(ToolHandle< T > &handle, std::string toolTypeAndName, bool createIf=true)
Declare used tool.
Definition: Service.h:141
StatusCode reinitialize() override
Initialization (from INITIALIZED or RUNNING to INITIALIZED, via CONFIGURED).
Helper class to implement the IProperty interface.
StatusCode terminate() override
Definition: Service.h:61
constexpr static const auto FAILURE
Definition: StatusCode.h:101
StatusCode service(const std::string &name, T *&psvc, bool createIf=true) const
Definition: Service.h:109
StatusCode service(const std::string &name, const T *&psvc, bool createIf=true) const
Access a service by name, creating it if it doesn't already exist.
Definition: Service.h:93
SmartIF< ISvcManager > m_svcManager
Definition: Service.h:179
SmartIF< IService > & service(const Gaudi::Utils::TypeNameString &typeName, const bool createIf=true) override
Returns a smart pointer to a service.
Base class for all services.
Definition: Service.h:46
#define GAUDI_API
Definition: Kernel.h:81
Gaudi::StateMachine::State targetFSMState() const override
Definition: Service.h:63
StatusCode initialize() override
Initialization (from CONFIGURED to INITIALIZED).