The Gaudi Framework  v29r0 (ff2e7097)
ServiceLocatorHelper.cpp
Go to the documentation of this file.
2 #include "GaudiKernel/IService.h"
6 
8 
10 {
11  return ( isInThread() ? name + threadName() : name );
12 }
13 
15 
17  bool quiet ) const
18 {
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 
32 {
33  auto theSvc = service( name, false, true );
34  if ( !theSvc ) return StatusCode::FAILURE;
35  StatusCode sc = theSvc->queryInterface( iid, ppSvc );
36  if ( !sc.isSuccess() ) {
37  *ppSvc = nullptr;
38  log() << MSG::ERROR << "ServiceLocatorHelper::createService: wrong interface id " << iid << " for service " << name
39  << endmsg;
40  }
41  return sc;
42 }
43 
45  const InterfaceID& iid, void** ppSvc ) const
46 {
47  return createService( type + "/" + name, iid, ppSvc );
48 }
49 
50 SmartIF<IService> ServiceLocatorHelper::service( const std::string& name, const bool quiet, const bool createIf ) const
51 {
52  SmartIF<IService> theSvc;
53  if ( isInThread() ) {
54  // first we look for a thread-specific version of the service
55  theSvc = serviceLocator()->service( name + threadName(), createIf );
56  }
57  // if not, try to find the common, single-threaded version of the service
58  if ( !theSvc ) theSvc = serviceLocator()->service( name, createIf );
59 
60  if ( theSvc ) {
61  if ( !quiet ) {
62  if ( UNLIKELY( log().level() <= MSG::VERBOSE ) )
63  log() << MSG::VERBOSE << "ServiceLocatorHelper::service: found service " << name << endmsg;
64  }
65  } else {
66  // if not return an error
67  if ( !quiet ) {
68  log() << MSG::ERROR << "ServiceLocatorHelper::service: can not locate service " << name;
69  if ( isInThread() ) log() << MSG::ERROR << " or " << name + threadName();
70  log() << MSG::ERROR << endmsg;
71  }
72  }
73  return theSvc;
74 }
ISvcLocator * serviceLocator() const
#define UNLIKELY(x)
Definition: Kernel.h:128
bool isSuccess() const
Test for a status code of SUCCESS.
Definition: StatusCode.h:75
std::string threadName() const
GAUDI_API bool isGaudiThreaded(const std::string &name)
test if current Gaudi object is running /will run in a thread
Definition: ThreadGaudi.cpp:76
STL class.
MsgStream & log() const
StatusCode service(const Gaudi::Utils::TypeNameString &name, T *&svc, bool createIf=true)
Templated method to access a service by name.
Definition: ISvcLocator.h:79
Interface ID class.
Definition: IInterface.h:29
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:28
StatusCode locateService(const std::string &name, const InterfaceID &iid, void **ppSvc, bool quiet=false) const
std::string threadedName(const std::string &name) const
SmartIF< IService > service(const std::string &name, const bool quiet=false, const bool createIf=true) const
const std::string & requesterName() const
GAUDI_API std::string getGaudiThreadIDfromName(const std::string &name)
helper function to extract Gaudi Thread ID from thread copy name
Definition: ThreadGaudi.cpp:27
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:209
StatusCode createService(const std::string &name, const InterfaceID &iid, void **ppSvc) const