File indexing completed on 2025-09-18 09:13:47
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011 #ifndef GAUDIKERNEL_SERVICELOCATORHELPER_H
0012 #define GAUDIKERNEL_SERVICELOCATORHELPER_H
0013
0014 #include <GaudiKernel/IService.h>
0015 #include <GaudiKernel/ISvcLocator.h>
0016 #include <GaudiKernel/SmartIF.h>
0017 #include <GaudiKernel/StatusCode.h>
0018 #include <string>
0019
0020 class InterfaceID;
0021 class MsgStream;
0022
0023
0024
0025
0026
0027 class GAUDI_API ServiceLocatorHelper {
0028 public:
0029 ServiceLocatorHelper( ISvcLocator& svcLoc, const INamedInterface& requester )
0030 : m_svcLoc( svcLoc )
0031 , m_msgLog( SmartIF<IMessageSvc>( &svcLoc ), requester.name() )
0032 ,
0033 m_requesterName( requester.name() ) {}
0034 ServiceLocatorHelper( ISvcLocator& svcLoc, std::string loggedName, std::string requesterName )
0035 : m_svcLoc( svcLoc )
0036 , m_msgLog( SmartIF<IMessageSvc>( &svcLoc ), std::move( loggedName ) )
0037 ,
0038 m_requesterName( std::move( requesterName ) ) {}
0039 ServiceLocatorHelper( ISvcLocator& svcLoc, std::string requesterName )
0040 : m_svcLoc( svcLoc )
0041 , m_msgLog( SmartIF<IMessageSvc>( &svcLoc ), requesterName )
0042 ,
0043 m_requesterName( std::move( requesterName ) ) {}
0044 #if !defined( GAUDI_V22_API ) || defined( G22_NEW_SVCLOCATOR )
0045 [[deprecated( "use ServiceLocatorHelper(svcLoc, requesterName) instead" )]] ServiceLocatorHelper(
0046 ISvcLocator& svcLoc,
0047 const MsgStream& log,
0048 std::string requesterName )
0049 : m_svcLoc( svcLoc ), m_msgLog( log ), m_requesterName( std::move( requesterName ) ) {}
0050 #endif
0051
0052 StatusCode getService( std::string_view name, bool createIf, const InterfaceID& iid, void** ppSvc ) const {
0053 return createIf ? createService( name, iid, ppSvc ) : locateService( name, iid, ppSvc, true );
0054 }
0055
0056 StatusCode locateService( std::string_view name, const InterfaceID& iid, void** ppSvc, bool quiet = false ) const;
0057
0058 StatusCode createService( std::string_view name, const InterfaceID& iid, void** ppSvc ) const;
0059
0060 StatusCode createService( std::string_view type, std::string_view name, const InterfaceID& iid, void** ppSvc ) const;
0061
0062 SmartIF<IService> service( std::string_view name, const bool quiet = false, const bool createIf = true ) const;
0063
0064 template <typename T>
0065 SmartIF<T> service( std::string_view name, const bool quiet = false, const bool createIf = true ) const {
0066 return service( name, quiet, createIf ).as<T>();
0067 }
0068
0069 private:
0070 ISvcLocator* serviceLocator() const { return &m_svcLoc; }
0071 MsgStream& log() const { return m_msgLog; }
0072 const std::string& requesterName() const { return m_requesterName; }
0073 ISvcLocator& m_svcLoc;
0074 mutable MsgStream m_msgLog;
0075 std::string m_requesterName;
0076 };
0077 #endif