The Gaudi Framework  master (37c0b60a)
ServiceLocatorHelper.h
Go to the documentation of this file.
1 /***********************************************************************************\
2 * (c) Copyright 1998-2024 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_SERVICELOCATORHELPER_H
12 #define GAUDIKERNEL_SERVICELOCATORHELPER_H
13 
14 #include <GaudiKernel/IService.h>
16 #include <GaudiKernel/SmartIF.h>
17 #include <GaudiKernel/StatusCode.h>
18 #include <string>
19 
20 class InterfaceID;
21 class MsgStream;
22 
28 public:
29  ServiceLocatorHelper( ISvcLocator& svcLoc, const INamedInterface& requester )
30  : m_svcLoc( svcLoc )
31  , m_msgLog( SmartIF<IMessageSvc>( &svcLoc ), requester.name() )
32  , // use requester msg level
33  m_requesterName( requester.name() ) {}
34  ServiceLocatorHelper( ISvcLocator& svcLoc, std::string loggedName, std::string requesterName )
35  : m_svcLoc( svcLoc )
36  , m_msgLog( SmartIF<IMessageSvc>( &svcLoc ), std::move( loggedName ) )
37  , // use requester msg level
38  m_requesterName( std::move( requesterName ) ) {}
39  ServiceLocatorHelper( ISvcLocator& svcLoc, std::string requesterName )
40  : m_svcLoc( svcLoc )
41  , m_msgLog( SmartIF<IMessageSvc>( &svcLoc ), requesterName )
42  , // use requester msg level
43  m_requesterName( std::move( requesterName ) ) {}
44 #if !defined( GAUDI_V22_API ) || defined( G22_NEW_SVCLOCATOR )
45  [[deprecated( "use ServiceLocatorHelper(svcLoc, requesterName) instead" )]] ServiceLocatorHelper(
46  ISvcLocator& svcLoc,
47  const MsgStream& log, // use requester msg level
48  std::string requesterName )
49  : m_svcLoc( svcLoc ), m_msgLog( log ), m_requesterName( std::move( requesterName ) ) {}
50 #endif
51 
52  StatusCode getService( std::string_view name, bool createIf, const InterfaceID& iid, void** ppSvc ) const {
53  return createIf ? createService( name, iid, ppSvc ) : locateService( name, iid, ppSvc, true );
54  }
55 
56  StatusCode locateService( std::string_view name, const InterfaceID& iid, void** ppSvc, bool quiet = false ) const;
57 
58  StatusCode createService( std::string_view name, const InterfaceID& iid, void** ppSvc ) const;
59 
60  StatusCode createService( std::string_view type, std::string_view name, const InterfaceID& iid, void** ppSvc ) const;
61 
62  SmartIF<IService> service( std::string_view name, const bool quiet = false, const bool createIf = true ) const;
63 
64  template <typename T>
65  SmartIF<T> service( std::string_view name, const bool quiet = false, const bool createIf = true ) const {
66  return service( name, quiet, createIf ).as<T>();
67  }
68 
69 private:
70  ISvcLocator* serviceLocator() const { return &m_svcLoc; }
71  MsgStream& log() const { return m_msgLog; }
72  const std::string& requesterName() const { return m_requesterName; }
76 };
77 #endif
ServiceLocatorHelper::service
SmartIF< T > service(std::string_view name, const bool quiet=false, const bool createIf=true) const
Definition: ServiceLocatorHelper.h:65
ServiceLocatorHelper::m_svcLoc
ISvcLocator & m_svcLoc
Definition: ServiceLocatorHelper.h:73
IService.h
std::string
STL class.
IMessageSvc
Definition: IMessageSvc.h:47
Gaudi.Configuration.log
log
Definition: Configuration.py:28
ISvcLocator
Definition: ISvcLocator.h:46
ServiceLocatorHelper::m_requesterName
std::string m_requesterName
Definition: ServiceLocatorHelper.h:75
ServiceLocatorHelper::m_msgLog
MsgStream m_msgLog
Definition: ServiceLocatorHelper.h:74
ServiceLocatorHelper::ServiceLocatorHelper
ServiceLocatorHelper(ISvcLocator &svcLoc, const MsgStream &log, std::string requesterName)
Definition: ServiceLocatorHelper.h:45
ServiceLocatorHelper::log
MsgStream & log() const
Definition: ServiceLocatorHelper.h:71
ServiceLocatorHelper::ServiceLocatorHelper
ServiceLocatorHelper(ISvcLocator &svcLoc, const INamedInterface &requester)
Definition: ServiceLocatorHelper.h:29
StatusCode.h
ServiceLocatorHelper::requesterName
const std::string & requesterName() const
Definition: ServiceLocatorHelper.h:72
SmartIF.h
ServiceLocatorHelper::ServiceLocatorHelper
ServiceLocatorHelper(ISvcLocator &svcLoc, std::string requesterName)
Definition: ServiceLocatorHelper.h:39
ServiceLocatorHelper::ServiceLocatorHelper
ServiceLocatorHelper(ISvcLocator &svcLoc, std::string loggedName, std::string requesterName)
Definition: ServiceLocatorHelper.h:34
StatusCode
Definition: StatusCode.h:65
ServiceLocatorHelper
an helper to share the implementation of service() among the various kernel base classes
Definition: ServiceLocatorHelper.h:27
ServiceLocatorHelper::getService
StatusCode getService(std::string_view name, bool createIf, const InterfaceID &iid, void **ppSvc) const
Definition: ServiceLocatorHelper.h:52
SmartIF
Definition: IConverter.h:25
MsgStream
Definition: MsgStream.h:33
gaudirun.type
type
Definition: gaudirun.py:160
INamedInterface
Definition: INamedInterface.h:25
ConditionsStallTest.name
name
Definition: ConditionsStallTest.py:77
std
STL namespace.
InterfaceID
Definition: IInterface.h:39
ServiceLocatorHelper::serviceLocator
ISvcLocator * serviceLocator() const
Definition: ServiceLocatorHelper.h:70
ISvcLocator.h
GAUDI_API
#define GAUDI_API
Definition: Kernel.h:81