ServiceLocatorHelper.cpp
Go to the documentation of this file.
1 #include "GaudiKernel/IService.h"
6 
10 }
11 
14  return ( isInThread() ? name + threadName() : name );
15 }
16 
17 bool
20 }
21 
24  const InterfaceID& iid,
25  void** ppSvc,
26  bool quiet) const {
27  auto theSvc = service(name, quiet, false);
28  if (!theSvc) return StatusCode::FAILURE;
29  StatusCode sc = theSvc->queryInterface(iid, ppSvc);
30  if (!sc.isSuccess()) {
31  *ppSvc = nullptr;
32  if (!quiet) log() << MSG::ERROR
33  << "ServiceLocatorHelper::locateService: wrong interface id "
34  << iid << " for service " << name << endmsg;
35  }
36  return sc;
37 }
38 
41  const InterfaceID& iid,
42  void** ppSvc) const {
43  auto theSvc = service(name, false, true);
44  if (!theSvc) return StatusCode::FAILURE;
45  StatusCode sc = theSvc->queryInterface(iid, ppSvc);
46  if (!sc.isSuccess()) {
47  *ppSvc = nullptr;
48  log() << MSG::ERROR
49  << "ServiceLocatorHelper::createService: wrong interface id "
50  << iid << " for service " << name << endmsg;
51  }
52  return sc;
53 }
54 
57  const std::string& name,
58  const InterfaceID& iid,
59  void** ppSvc) const {
60  return createService(type + "/" + name, iid, ppSvc);
61 }
62 
63 SmartIF<IService> ServiceLocatorHelper::service(const std::string &name, const bool quiet, const bool createIf) const {
64  SmartIF<IService> theSvc;
65  if (isInThread()) {
66  //first we look for a thread-specific version of the service
67  theSvc = serviceLocator()->service(name + threadName(), createIf);
68  }
69  // if not, try to find the common, single-threaded version of the service
70  if (!theSvc) theSvc = serviceLocator()->service(name, createIf);
71 
72  if (theSvc) {
73  if (!quiet) {
74  if (UNLIKELY(log().level() <= MSG::VERBOSE))
75  log() << MSG::VERBOSE
76  << "ServiceLocatorHelper::service: found service " << name <<endmsg;
77  }
78  } else {
79  // if not return an error
80  if (!quiet) {
81  log() << MSG::ERROR
82  << "ServiceLocatorHelper::service: can not locate service "
83  << name;
84  if (isInThread()) log() << MSG::ERROR << " or " << name + threadName();
85  log() << MSG::ERROR << endmsg;
86  }
87  }
88  return theSvc;
89 }
ISvcLocator * serviceLocator() const
bool isSuccess() const
Test for a status code of SUCCESS.
Definition: StatusCode.h:74
#define UNLIKELY(x)
Definition: Kernel.h:126
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:73
STL class.
MsgStream & log() const
StatusCode createService(const std::string &type, const std::string &name, const InterfaceID &iid, void **ppSvc) const
StatusCode service(const Gaudi::Utils::TypeNameString &name, T *&svc, bool createIf=true)
Templated method to access a service by name.
Definition: ISvcLocator.h:78
Interface ID class.
Definition: IInterface.h:30
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:26
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:26
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:244
StatusCode createService(const std::string &name, const InterfaceID &iid, void **ppSvc) const