All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
ServiceLocatorHelper.cpp
Go to the documentation of this file.
1 #include "GaudiKernel/IService.h"
6 
7 std::string
10 }
11 
12 std::string
13 ServiceLocatorHelper::threadedName(const std::string& name) const {
14  return ( isInThread() ? name + threadName() : name );
15 }
16 
17 bool
20 }
21 
23 ServiceLocatorHelper::locateService(const std::string& name,
24  const InterfaceID& iid,
25  void** ppSvc,
26  bool quiet) const {
27  SmartIF<IService> theSvc = service(name, quiet, false);
28  if (!theSvc.isValid()) return StatusCode::FAILURE;
29  StatusCode sc = theSvc->queryInterface(iid, ppSvc);
30  if (!sc.isSuccess()) {
31  *ppSvc = 0;
32  if (!quiet) log() << MSG::ERROR
33  << "ServiceLocatorHelper::locateService: wrong interface id "
34  << iid << " for service " << name << endmsg;
35  }
36  return sc;
37 }
38 
40 ServiceLocatorHelper::createService(const std::string& name,
41  const InterfaceID& iid,
42  void** ppSvc) const {
43  SmartIF<IService> theSvc = service(name, false, true);
44  if (!theSvc.isValid()) return StatusCode::FAILURE;
45  StatusCode sc = theSvc->queryInterface(iid, ppSvc);
46  if (!sc.isSuccess()) {
47  *ppSvc = 0;
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.isValid()){
71  theSvc = serviceLocator()->service(name, createIf);
72  }
73 
74  if (theSvc.isValid()) {
75  if (!quiet) {
76  if (UNLIKELY(log().level() <= MSG::VERBOSE))
77  log() << MSG::VERBOSE
78  << "ServiceLocatorHelper::service: found service " << name <<endmsg;
79  }
80  } else {
81  // if not return an error
82  if (!quiet) {
83  log() << MSG::ERROR
84  << "ServiceLocatorHelper::service: can not locate service "
85  << name;
86  if (isInThread()) log() << MSG::ERROR << " or " << name + threadName();
87  log() << MSG::ERROR << endmsg;
88  }
89  }
90  return theSvc;
91 }
ISvcLocator * serviceLocator() const
#define UNLIKELY(x)
Definition: Kernel.h:127
bool isSuccess() const
Test for a status code of SUCCESS.
Definition: StatusCode.h:62
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:75
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:82
Interface ID class.
Definition: IInterface.h:55
string type
Definition: gaudirun.py:126
bool isValid() const
Allow for check if smart pointer is valid.
Definition: SmartIF.h:51
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:30
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:28
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:243
StatusCode createService(const std::string &name, const InterfaceID &iid, void **ppSvc) const