The Gaudi Framework  v33r1 (b1225454)
ServiceLocatorHelper.cpp
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 \***********************************************************************************/
12 
13 #include "GaudiKernel/IService.h"
15 #include "GaudiKernel/MsgStream.h"
16 
17 StatusCode ServiceLocatorHelper::locateService( std::string_view name, const InterfaceID& iid, void** ppSvc,
18  bool quiet ) const {
19  auto theSvc = service( name, quiet, false );
20  if ( !theSvc ) return StatusCode::FAILURE;
21  StatusCode sc = theSvc->queryInterface( iid, ppSvc );
22  if ( !sc.isSuccess() ) {
23  *ppSvc = nullptr;
24  if ( !quiet )
25  log() << MSG::ERROR << "ServiceLocatorHelper::locateService: wrong interface id " << iid << " for service "
26  << name << endmsg;
27  }
28  return sc;
29 }
30 
31 StatusCode ServiceLocatorHelper::createService( std::string_view name, const InterfaceID& iid, void** ppSvc ) const {
32  auto theSvc = service( name, false, true );
33  if ( !theSvc ) return StatusCode::FAILURE;
34  StatusCode sc = theSvc->queryInterface( iid, ppSvc );
35  if ( !sc.isSuccess() ) {
36  *ppSvc = nullptr;
37  log() << MSG::ERROR << "ServiceLocatorHelper::createService: wrong interface id " << iid << " for service " << name
38  << endmsg;
39  }
40  return sc;
41 }
42 
43 StatusCode ServiceLocatorHelper::createService( std::string_view type, std::string_view name, const InterfaceID& iid,
44  void** ppSvc ) const {
45  return createService( std::string{type}.append( "/" ).append( name ), iid, ppSvc );
46 }
47 
48 SmartIF<IService> ServiceLocatorHelper::service( std::string_view name, const bool quiet, const bool createIf ) const {
49  SmartIF<IService> theSvc = serviceLocator()->service( name, createIf );
50 
51  if ( theSvc ) {
52  if ( !quiet ) {
53  if ( UNLIKELY( log().level() <= MSG::VERBOSE ) )
54  log() << MSG::VERBOSE << "ServiceLocatorHelper::service: found service " << name << endmsg;
55  }
56  } else {
57  // if not return an error
58  if ( !quiet ) { log() << MSG::ERROR << "ServiceLocatorHelper::service: can not locate service " << name << endmsg; }
59  }
60  return theSvc;
61 }
#define UNLIKELY(x)
Definition: Kernel.h:106
SmartIF< IService > service(std::string_view name, const bool quiet=false, const bool createIf=true) const
StatusCode locateService(std::string_view name, const InterfaceID &iid, void **ppSvc, bool quiet=false) const
STL class.
StatusCode service(const Gaudi::Utils::TypeNameString &name, T *&svc, bool createIf=true)
Templated method to access a service by name.
Definition: ISvcLocator.h:86
Interface ID class.
Definition: IInterface.h:39
MsgStream & log() const
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:61
bool isSuccess() const
Definition: StatusCode.h:365
ISvcLocator * serviceLocator() const
StatusCode createService(std::string_view name, const InterfaceID &iid, void **ppSvc) const
constexpr static const auto FAILURE
Definition: StatusCode.h:101
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:202