Loading [MathJax]/extensions/tex2jax.js
The Gaudi Framework  v29r0 (ff2e7097)
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
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 20 of file ServiceLocatorHelper.h.

21  : m_svcLoc( svcLoc )
22  , m_msgLog( SmartIF<IMessageSvc>( &svcLoc ), requester.name() )
23  , // use requester msg level
24  m_requesterName( requester.name() )
25  {
26  }
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 27 of file ServiceLocatorHelper.h.

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

Definition at line 34 of file ServiceLocatorHelper.h.

35  : m_svcLoc( svcLoc )
36  , m_msgLog( SmartIF<IMessageSvc>( &svcLoc ), requesterName )
37  , // use requester msg level
38  m_requesterName( requesterName )
39  {
40  }
ServiceLocatorHelper::ServiceLocatorHelper ( ISvcLocator svcLoc,
const MsgStream log,
const std::string requesterName 
)
inline

Definition at line 42 of file ServiceLocatorHelper.h.

45  : m_svcLoc( svcLoc ), m_msgLog( log ), m_requesterName( requesterName )
46  {
47  }

Member Function Documentation

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

Definition at line 31 of file ServiceLocatorHelper.cpp.

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 }
bool isSuccess() const
Test for a status code of SUCCESS.
Definition: StatusCode.h:75
MsgStream & log() const
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:28
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:209
StatusCode ServiceLocatorHelper::createService ( const std::string type,
const std::string name,
const InterfaceID iid,
void **  ppSvc 
) const

Definition at line 44 of file ServiceLocatorHelper.cpp.

46 {
47  return createService( type + "/" + name, iid, ppSvc );
48 }
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 50 of file ServiceLocatorHelper.h.

51  {
52  return createIf ? createService( name, iid, ppSvc ) : locateService( name, iid, ppSvc, true );
53  }
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 14 of file ServiceLocatorHelper.cpp.

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

Definition at line 16 of file ServiceLocatorHelper.cpp.

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 }
bool isSuccess() const
Test for a status code of SUCCESS.
Definition: StatusCode.h:75
MsgStream & log() const
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:28
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:209
MsgStream& ServiceLocatorHelper::log ( ) const
inlineprivate

Definition at line 75 of file ServiceLocatorHelper.h.

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

Definition at line 76 of file ServiceLocatorHelper.h.

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

Definition at line 50 of file ServiceLocatorHelper.cpp.

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
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:79
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:209
template<typename T >
SmartIF<T> ServiceLocatorHelper::service ( const std::string name,
const bool  quiet = false,
const bool  createIf = true 
) const
inline

Definition at line 65 of file ServiceLocatorHelper.h.

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

Definition at line 74 of file ServiceLocatorHelper.h.

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

Definition at line 9 of file ServiceLocatorHelper.cpp.

10 {
11  return ( isInThread() ? name + threadName() : name );
12 }
std::string threadName() const
std::string ServiceLocatorHelper::threadName ( ) const
private

Definition at line 7 of file ServiceLocatorHelper.cpp.

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

Member Data Documentation

MsgStream ServiceLocatorHelper::m_msgLog
mutableprivate

Definition at line 78 of file ServiceLocatorHelper.h.

std::string ServiceLocatorHelper::m_requesterName
private

Definition at line 79 of file ServiceLocatorHelper.h.

ISvcLocator& ServiceLocatorHelper::m_svcLoc
private

Definition at line 77 of file ServiceLocatorHelper.h.


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