ServiceLocatorHelper Class Reference

an helper to share the implementation of service() among the various kernel base classes More...

#include <GaudiKernel/ServiceLocatorHelper.h>

Collaboration diagram for ServiceLocatorHelper:

Public Member Functions

 ServiceLocatorHelper (ISvcLocator &svcLoc, const INamedInterface &requester)
 
 ServiceLocatorHelper (ISvcLocator &svcLoc, const std::string &loggedName, const std::string &requesterName)
 
 ServiceLocatorHelper (ISvcLocator &svcLoc, const std::string &requesterName)
 
 ServiceLocatorHelper (ISvcLocator &svcLoc, const MsgStream &log, const std::string &requesterName)
 
StatusCode getService (const std::string &name, bool createIf, const InterfaceID &iid, void **ppSvc) const
 
StatusCode locateService (const std::string &name, const InterfaceID &iid, void **ppSvc, bool quiet=false) const
 
StatusCode createService (const std::string &name, const InterfaceID &iid, void **ppSvc) const
 
StatusCode createService (const std::string &type, const std::string &name, const InterfaceID &iid, void **ppSvc) const
 
SmartIF< IServiceservice (const std::string &name, const bool quiet=false, const bool createIf=true) const
 
template<typename T >
SmartIF< T > service (const std::string &name, const bool quiet=false, const bool createIf=true) const
 

Private Member Functions

std::string threadName () const
 
std::string threadedName (const std::string &name) const
 
bool isInThread () const
 
ISvcLocatorserviceLocator () const
 
MsgStreamlog () const
 
const std::stringrequesterName () const
 

Private Attributes

ISvcLocatorm_svcLoc
 
MsgStream m_msgLog
 
std::string m_requesterName
 

Detailed Description

an helper to share the implementation of service() among the various kernel base classes

Definition at line 17 of file ServiceLocatorHelper.h.

Constructor & Destructor Documentation

ServiceLocatorHelper::ServiceLocatorHelper ( ISvcLocator svcLoc,
const INamedInterface requester 
)
inline

Definition at line 19 of file ServiceLocatorHelper.h.

19  :
20  m_svcLoc(svcLoc),
21  m_msgLog(SmartIF<IMessageSvc>(&svcLoc), requester.name()), //use requester msg level
22  m_requesterName(requester.name()) {
23  }
virtual const std::string & name() const =0
Retrieve the name of the instance.
ServiceLocatorHelper::ServiceLocatorHelper ( ISvcLocator svcLoc,
const std::string loggedName,
const std::string requesterName 
)
inline

Definition at line 24 of file ServiceLocatorHelper.h.

26  :
27  m_svcLoc(svcLoc),
28  m_msgLog(SmartIF<IMessageSvc>(&svcLoc), loggedName), //use requester msg level
29  m_requesterName(requesterName) {
30  }
ServiceLocatorHelper::ServiceLocatorHelper ( ISvcLocator svcLoc,
const std::string requesterName 
)
inline

Definition at line 31 of file ServiceLocatorHelper.h.

31  :
32  m_svcLoc(svcLoc),
33  m_msgLog(SmartIF<IMessageSvc>(&svcLoc), requesterName), //use requester msg level
34  m_requesterName(requesterName) {
35  }
ServiceLocatorHelper::ServiceLocatorHelper ( ISvcLocator svcLoc,
const MsgStream log,
const std::string requesterName 
)
inline

Definition at line 37 of file ServiceLocatorHelper.h.

39  :
40  m_svcLoc(svcLoc), m_msgLog(log), m_requesterName(requesterName) {}

Member Function Documentation

StatusCode ServiceLocatorHelper::createService ( const std::string name,
const InterfaceID iid,
void **  ppSvc 
) const

Definition at line 40 of file ServiceLocatorHelper.cpp.

42  {
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 }
bool isSuccess() const
Test for a status code of SUCCESS.
Definition: StatusCode.h:74
MsgStream & log() const
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:26
SmartIF< IService > service(const std::string &name, const bool quiet=false, const bool createIf=true) const
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:244
StatusCode ServiceLocatorHelper::createService ( const std::string type,
const std::string name,
const InterfaceID iid,
void **  ppSvc 
) const

Definition at line 56 of file ServiceLocatorHelper.cpp.

59  {
60  return createService(type + "/" + name, iid, ppSvc);
61 }
StatusCode createService(const std::string &name, const InterfaceID &iid, void **ppSvc) const
StatusCode ServiceLocatorHelper::getService ( const std::string name,
bool  createIf,
const InterfaceID iid,
void **  ppSvc 
) const
inline

Definition at line 43 of file ServiceLocatorHelper.h.

46  {
47  return createIf ? createService(name, iid, ppSvc)
48  : locateService(name, iid, ppSvc, true);
49  }
StatusCode locateService(const std::string &name, const InterfaceID &iid, void **ppSvc, bool quiet=false) const
StatusCode createService(const std::string &name, const InterfaceID &iid, void **ppSvc) const
bool ServiceLocatorHelper::isInThread ( ) const
private

Definition at line 18 of file ServiceLocatorHelper.cpp.

18  {
20 }
GAUDI_API bool isGaudiThreaded(const std::string &name)
test if current Gaudi object is running /will run in a thread
Definition: ThreadGaudi.cpp:73
const std::string & requesterName() const
StatusCode ServiceLocatorHelper::locateService ( const std::string name,
const InterfaceID iid,
void **  ppSvc,
bool  quiet = false 
) const

Definition at line 23 of file ServiceLocatorHelper.cpp.

26  {
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 }
bool isSuccess() const
Test for a status code of SUCCESS.
Definition: StatusCode.h:74
MsgStream & log() const
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:26
SmartIF< IService > service(const std::string &name, const bool quiet=false, const bool createIf=true) const
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:244
MsgStream& ServiceLocatorHelper::log ( ) const
inlineprivate

Definition at line 77 of file ServiceLocatorHelper.h.

77 { return m_msgLog; }
const std::string& ServiceLocatorHelper::requesterName ( ) const
inlineprivate

Definition at line 78 of file ServiceLocatorHelper.h.

78 { return m_requesterName; }
SmartIF< IService > ServiceLocatorHelper::service ( const std::string name,
const bool  quiet = false,
const bool  createIf = true 
) const

Definition at line 63 of file ServiceLocatorHelper.cpp.

63  {
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
#define UNLIKELY(x)
Definition: Kernel.h:126
std::string threadName() const
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:78
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:244
template<typename T >
SmartIF<T> ServiceLocatorHelper::service ( const std::string name,
const bool  quiet = false,
const bool  createIf = true 
) const
inline

Definition at line 68 of file ServiceLocatorHelper.h.

68  {
69  return service(name,quiet,createIf).as<T>();
70  }
SmartIF< IFace > as() const
return a new SmartIF instance to another interface
Definition: SmartIF.h:110
SmartIF< IService > service(const std::string &name, const bool quiet=false, const bool createIf=true) const
ISvcLocator* ServiceLocatorHelper::serviceLocator ( ) const
inlineprivate

Definition at line 76 of file ServiceLocatorHelper.h.

76 { return &m_svcLoc; }
std::string ServiceLocatorHelper::threadedName ( const std::string name) const
private

Definition at line 13 of file ServiceLocatorHelper.cpp.

13  {
14  return ( isInThread() ? name + threadName() : name );
15 }
std::string threadName() const
std::string ServiceLocatorHelper::threadName ( ) const
private

Definition at line 8 of file ServiceLocatorHelper.cpp.

8  {
10 }
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

Member Data Documentation

MsgStream ServiceLocatorHelper::m_msgLog
mutableprivate

Definition at line 80 of file ServiceLocatorHelper.h.

std::string ServiceLocatorHelper::m_requesterName
private

Definition at line 81 of file ServiceLocatorHelper.h.

ISvcLocator& ServiceLocatorHelper::m_svcLoc
private

Definition at line 79 of file ServiceLocatorHelper.h.


The documentation for this class was generated from the following files: