The Gaudi Framework  v36r13 (995e4364)
ISvcLocator.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_ISVCLOCATOR_H
12 #define GAUDIKERNEL_ISVCLOCATOR_H 1
13 
14 // Include files
15 #include "GaudiKernel/IInterface.h"
17 #include "GaudiKernel/SmartIF.h"
19 
20 #include <list>
21 #include <string>
22 
23 // Forward class declaration
24 class IService;
25 namespace Gaudi::Monitoring {
26  struct Hub;
27 }
28 
29 namespace Gaudi {
30  namespace Interfaces {
31  struct IOptionsSvc;
32  }
33 } // namespace Gaudi
34 #define GAUDI_HAS_IOPTIONS_SVC
35 
46 class GAUDI_API ISvcLocator : virtual public IInterface {
47 public:
50 
51 #if !defined( GAUDI_V22_API ) || defined( G22_NEW_SVCLOCATOR )
52 
57  const bool createIf = true ) {
58  SmartIF<IService>& s = service( typeName, createIf );
59  svc = s.get();
60  if ( svc ) {
61  svc->addRef(); // Needed to maintain the correct reference counting.
62  return StatusCode::SUCCESS;
63  }
64  return StatusCode::FAILURE;
65  }
66 
73  IInterface*& pinterface ) {
74  auto svc = service( typeName, false );
75  return svc ? svc->queryInterface( iid, (void**)&pinterface ) : StatusCode::FAILURE;
76  }
77 
83 // virtual StatusCode getService( const Gaudi::Utils::TypeNameString& name,
84 // IService*& svc,
85 // bool createIf ) = 0;
86 #endif
87 
89  virtual const std::list<IService*>& getServices() const = 0;
90 
92  virtual bool existsService( std::string_view name ) const = 0;
93 
94 #if !defined( GAUDI_V22_API ) || defined( G22_NEW_SVCLOCATOR )
95  template <class T>
97  StatusCode service( const Gaudi::Utils::TypeNameString& name, T*& svc, bool createIf = true ) {
98  if ( createIf ) {
99  IService* s;
100  StatusCode sc = getService( name, s, true );
101  if ( !sc.isSuccess() ) return sc; // Must check if initialization was OK!
102  }
103  return getService( name, T::interfaceID(), (IInterface*&)svc );
104  }
105 
107  template <class T>
108  StatusCode service( std::string_view type, std::string_view name, T*& svc, bool createIf = true ) {
109  return service( std::string{ type }.append( "/" ).append( name ), svc, createIf );
110  }
111 #endif
112 
114  virtual SmartIF<IService>& service( const Gaudi::Utils::TypeNameString& typeName, const bool createIf = true ) = 0;
115 
117  template <typename T>
118  inline SmartIF<T> service( const Gaudi::Utils::TypeNameString& typeName, const bool createIf = true ) {
119  return SmartIF<T>{ service( typeName, createIf ) };
120  }
121 
122  // try to access a different interface of the _current_ serviceLocator...
123  template <typename IFace>
125  return SmartIF<IFace>{ this };
126  }
127 
129  Gaudi::Interfaces::IOptionsSvc& getOptsSvc();
130 
131  Gaudi::Monitoring::Hub& monitoringHub();
132 };
133 
134 #endif // GAUDI_ISVCLOCATOR_H
IService
Definition: IService.h:28
Gaudi::InterfaceId
Class to handle automatically the versioning of the interfaces when they are inheriting from other in...
Definition: IInterface.h:187
ISvcLocator::getServices
virtual const std::list< IService * > & getServices() const =0
Get a reference to a service and create it if it does not exists.
std::string
STL class.
std::list< IService * >
StatusCode::isSuccess
bool isSuccess() const
Definition: StatusCode.h:314
ISvcLocator::DeclareInterfaceID
DeclareInterfaceID(ISvcLocator, 3, 0)
InterfaceID.
ISvcLocator::getService
virtual StatusCode getService(const Gaudi::Utils::TypeNameString &typeName, const InterfaceID &iid, IInterface *&pinterface)
Get a specific interface pointer given a service name and interface id.
Definition: ISvcLocator.h:72
gaudirun.s
string s
Definition: gaudirun.py:348
ISvcLocator
Definition: ISvcLocator.h:46
Gaudi::Monitoring
Definition: JSONSink.cpp:19
getService
IInterface *PyHelper() getService(IInterface *app, char *name)
Definition: Bootstrap.cpp:240
ISvcLocator::getService
virtual StatusCode getService(const Gaudi::Utils::TypeNameString &typeName, IService *&svc, const bool createIf=true)
Get a reference to the service given a service name.
Definition: ISvcLocator.h:56
ISvcLocator::service
virtual SmartIF< IService > & service(const Gaudi::Utils::TypeNameString &typeName, const bool createIf=true)=0
Returns a smart pointer to a service.
SmartIF.h
Gaudi::Utils::TypeNameString
Helper class to parse a string of format "type/name".
Definition: TypeNameString.h:20
TimingHistograms.name
name
Definition: TimingHistograms.py:25
StatusCode
Definition: StatusCode.h:65
IInterface.h
ISvcLocator::service
StatusCode service(const Gaudi::Utils::TypeNameString &name, T *&svc, bool createIf=true)
Templated method to access a service by name.
Definition: ISvcLocator.h:97
SmartIF< IService >
TypeNameString.h
Gaudi
Header file for std:chrono::duration-based Counters.
Definition: __init__.py:1
gaudirun.type
type
Definition: gaudirun.py:162
StatusCode::SUCCESS
constexpr static const auto SUCCESS
Definition: StatusCode.h:100
GaudiDict::typeName
std::string typeName(const std::type_info &typ)
Definition: Dictionary.cpp:31
ISvcLocator::as
SmartIF< IFace > as()
Definition: ISvcLocator.h:124
IInterface
Definition: IInterface.h:237
InterfaceID
Definition: IInterface.h:39
Gaudi::Monitoring::Hub
Central entity in a Gaudi application that manages monitoring objects (i.e.
Definition: MonitoringHub.h:73
StatusCode::FAILURE
constexpr static const auto FAILURE
Definition: StatusCode.h:101
Gaudi::Interfaces::IOptionsSvc
Interface for a component that manages application configuration options.
Definition: IOptionsSvc.h:46
ISvcLocator::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: ISvcLocator.h:118
ISvcLocator::service
StatusCode service(std::string_view type, std::string_view name, T *&svc, bool createIf=true)
Templated method to access a service by type and name.
Definition: ISvcLocator.h:108
ISvcLocator::existsService
virtual bool existsService(std::string_view name) const =0
Check the existence of a service given a service name.
GAUDI_API
#define GAUDI_API
Definition: Kernel.h:81
ISvcManager.h