The Gaudi Framework  v40r0 (475e45c1)
ServiceLocatorHelper.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 <GaudiKernel/IService.h>
15 #include <GaudiKernel/SmartIF.h>
16 #include <GaudiKernel/StatusCode.h>
17 #include <string>
18 
19 class InterfaceID;
20 class MsgStream;
21 
27 public:
28  ServiceLocatorHelper( ISvcLocator& svcLoc, const INamedInterface& requester )
29  : m_svcLoc( svcLoc )
30  , m_msgLog( SmartIF<IMessageSvc>( &svcLoc ), requester.name() )
31  , // use requester msg level
32  m_requesterName( requester.name() ) {}
33  ServiceLocatorHelper( ISvcLocator& svcLoc, std::string loggedName, std::string requesterName )
34  : m_svcLoc( svcLoc )
35  , m_msgLog( SmartIF<IMessageSvc>( &svcLoc ), std::move( loggedName ) )
36  , // use requester msg level
37  m_requesterName( std::move( requesterName ) ) {}
38  ServiceLocatorHelper( ISvcLocator& svcLoc, std::string requesterName )
39  : m_svcLoc( svcLoc )
40  , m_msgLog( SmartIF<IMessageSvc>( &svcLoc ), requesterName )
41  , // use requester msg level
42  m_requesterName( std::move( requesterName ) ) {}
43 
44  StatusCode getService( std::string_view name, bool createIf, const InterfaceID& iid, void** ppSvc ) const {
45  return createIf ? createService( name, iid, ppSvc ) : locateService( name, iid, ppSvc, true );
46  }
47 
48  StatusCode locateService( std::string_view name, const InterfaceID& iid, void** ppSvc, bool quiet = false ) const;
49 
50  StatusCode createService( std::string_view name, const InterfaceID& iid, void** ppSvc ) const;
51 
52  StatusCode createService( std::string_view type, std::string_view name, const InterfaceID& iid, void** ppSvc ) const;
53 
54  SmartIF<IService> service( std::string_view name, const bool quiet = false, const bool createIf = true ) const;
55 
56  template <typename T>
57  SmartIF<T> service( std::string_view name, const bool quiet = false, const bool createIf = true ) const {
58  return service( name, quiet, createIf ).as<T>();
59  }
60 
61 private:
62  ISvcLocator* serviceLocator() const { return &m_svcLoc; }
63  MsgStream& log() const { return m_msgLog; }
64  const std::string& requesterName() const { return m_requesterName; }
67  std::string m_requesterName;
68 };
ServiceLocatorHelper::service
SmartIF< T > service(std::string_view name, const bool quiet=false, const bool createIf=true) const
Definition: ServiceLocatorHelper.h:57
ServiceLocatorHelper::m_svcLoc
ISvcLocator & m_svcLoc
Definition: ServiceLocatorHelper.h:65
IService.h
IMessageSvc
Definition: IMessageSvc.h:44
GaudiPartProp.decorators.std
std
Definition: decorators.py:32
ISvcLocator
Definition: ISvcLocator.h:42
ServiceLocatorHelper::m_requesterName
std::string m_requesterName
Definition: ServiceLocatorHelper.h:67
ServiceLocatorHelper::m_msgLog
MsgStream m_msgLog
Definition: ServiceLocatorHelper.h:66
ServiceLocatorHelper::log
MsgStream & log() const
Definition: ServiceLocatorHelper.h:63
ServiceLocatorHelper::ServiceLocatorHelper
ServiceLocatorHelper(ISvcLocator &svcLoc, const INamedInterface &requester)
Definition: ServiceLocatorHelper.h:28
StatusCode.h
ServiceLocatorHelper::requesterName
const std::string & requesterName() const
Definition: ServiceLocatorHelper.h:64
SmartIF.h
ServiceLocatorHelper::ServiceLocatorHelper
ServiceLocatorHelper(ISvcLocator &svcLoc, std::string requesterName)
Definition: ServiceLocatorHelper.h:38
ServiceLocatorHelper::ServiceLocatorHelper
ServiceLocatorHelper(ISvcLocator &svcLoc, std::string loggedName, std::string requesterName)
Definition: ServiceLocatorHelper.h:33
StatusCode
Definition: StatusCode.h:64
ServiceLocatorHelper
an helper to share the implementation of service() among the various kernel base classes
Definition: ServiceLocatorHelper.h:26
ServiceLocatorHelper::getService
StatusCode getService(std::string_view name, bool createIf, const InterfaceID &iid, void **ppSvc) const
Definition: ServiceLocatorHelper.h:44
SmartIF
Definition: IConverter.h:22
MsgStream
Definition: MsgStream.h:29
gaudirun.type
type
Definition: gaudirun.py:160
INamedInterface
Definition: INamedInterface.h:23
ConditionsStallTest.name
name
Definition: ConditionsStallTest.py:77
InterfaceID
Definition: IInterface.h:38
ServiceLocatorHelper::serviceLocator
ISvcLocator * serviceLocator() const
Definition: ServiceLocatorHelper.h:62
ISvcLocator.h
GAUDI_API
#define GAUDI_API
Definition: Kernel.h:83