The Gaudi Framework  master (37c0b60a)
ServiceLocatorHelper.cpp
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 \***********************************************************************************/
12 
13 #include <GaudiKernel/IService.h>
15 #include <GaudiKernel/MsgStream.h>
16 
17 #include <fmt/format.h>
18 
19 StatusCode ServiceLocatorHelper::locateService( std::string_view name, const InterfaceID& iid, void** ppSvc,
20  bool quiet ) const {
21  auto theSvc = service( name, quiet, false );
22  if ( !theSvc ) return StatusCode::FAILURE;
23  StatusCode sc = theSvc->queryInterface( iid, ppSvc );
24  if ( !sc.isSuccess() ) {
25  *ppSvc = nullptr;
26  if ( !quiet )
27  log() << MSG::ERROR << "ServiceLocatorHelper::locateService: wrong interface id " << iid << " for service "
28  << name << endmsg;
29  }
30  return sc;
31 }
32 
33 StatusCode ServiceLocatorHelper::createService( std::string_view name, const InterfaceID& iid, void** ppSvc ) const {
34  auto theSvc = service( name, false, true );
35  if ( !theSvc ) return StatusCode::FAILURE;
36  StatusCode sc = theSvc->queryInterface( iid, ppSvc );
37  if ( !sc.isSuccess() ) {
38  *ppSvc = nullptr;
39  log() << MSG::ERROR << "ServiceLocatorHelper::createService: wrong interface id " << iid << " for service " << name
40  << endmsg;
41  }
42  return sc;
43 }
44 
45 StatusCode ServiceLocatorHelper::createService( std::string_view type, std::string_view name, const InterfaceID& iid,
46  void** ppSvc ) const {
47  return createService( fmt::format( "{}/{}", type, name ), iid, ppSvc );
48 }
49 
50 SmartIF<IService> ServiceLocatorHelper::service( std::string_view name, const bool quiet, const bool createIf ) const {
51  SmartIF<IService> theSvc = serviceLocator()->service( name, createIf );
52 
53  if ( theSvc ) {
54  if ( !quiet ) {
55  if ( log().level() <= MSG::VERBOSE )
56  log() << MSG::VERBOSE << "ServiceLocatorHelper::service: found service " << name << endmsg;
57  }
58  } else {
59  // if not return an error
60  if ( !quiet ) { log() << MSG::ERROR << "ServiceLocatorHelper::service: can not locate service " << name << endmsg; }
61  }
62  return theSvc;
63 }
ServiceLocatorHelper::service
SmartIF< IService > service(std::string_view name, const bool quiet=false, const bool createIf=true) const
Definition: ServiceLocatorHelper.cpp:50
IService.h
StatusCode::isSuccess
bool isSuccess() const
Definition: StatusCode.h:314
ServiceLocatorHelper::log
MsgStream & log() const
Definition: ServiceLocatorHelper.h:71
ServiceLocatorHelper::locateService
StatusCode locateService(std::string_view name, const InterfaceID &iid, void **ppSvc, bool quiet=false) const
Definition: ServiceLocatorHelper.cpp:19
StatusCode
Definition: StatusCode.h:65
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:98
SmartIF< IService >
format
GAUDI_API std::string format(const char *,...)
MsgStream format utility "a la sprintf(...)".
Definition: MsgStream.cpp:119
endmsg
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:202
gaudirun.level
level
Definition: gaudirun.py:364
gaudirun.type
type
Definition: gaudirun.py:160
MSG::VERBOSE
@ VERBOSE
Definition: IMessageSvc.h:25
ConditionsStallTest.name
name
Definition: ConditionsStallTest.py:77
ServiceLocatorHelper.h
MSG::ERROR
@ ERROR
Definition: IMessageSvc.h:25
InterfaceID
Definition: IInterface.h:39
StatusCode::FAILURE
constexpr static const auto FAILURE
Definition: StatusCode.h:101
ServiceLocatorHelper::serviceLocator
ISvcLocator * serviceLocator() const
Definition: ServiceLocatorHelper.h:70
ISvcLocator.h
ServiceLocatorHelper::createService
StatusCode createService(std::string_view name, const InterfaceID &iid, void **ppSvc) const
Definition: ServiceLocatorHelper.cpp:33
MsgStream.h