| The Gaudi Framework v29r0 (ff2e7097)

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

ServiceLocatorHelper.cpp
Go to the documentation of this file.
1 #include "GaudiKernel/ServiceLocatorHelper.h"
2 #include "GaudiKernel/IService.h"
3 #include "GaudiKernel/ISvcLocator.h"
4 #include "GaudiKernel/MsgStream.h"
5 #include "GaudiKernel/ThreadGaudi.h"
6
7 std::string ServiceLocatorHelper::threadName() const { return getGaudiThreadIDfromName( requesterName() ); }
8
9 std::string ServiceLocatorHelper::threadedName( const std::string& name ) const
10 {
11 return ( isInThread() ? name + threadName() : name );
12 }
13
14 bool ServiceLocatorHelper::isInThread() const { return isGaudiThreaded( requesterName() ); }
15
16 StatusCode ServiceLocatorHelper::locateService( const std::string& name, const InterfaceID& iid, void** ppSvc,
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
31 StatusCode ServiceLocatorHelper::createService( const std::string& name, const InterfaceID& iid, void** ppSvc ) const
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
44 StatusCode ServiceLocatorHelper::createService( const std::string& type, const std::string& name,
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 }
ServiceLocatorHelper::serviceLocator
ISvcLocator * serviceLocator() const
Definition: ServiceLocatorHelper.h:74
UNLIKELY
#define UNLIKELY(x)
Definition: Kernel.h:128
SmartIF< IService >
gaudiComponentHelp.name
name
Definition: gaudiComponentHelp.py:32
StatusCode::FAILURE
Definition: StatusCode.h:31
MsgStream.h
StatusCode::isSuccess
bool isSuccess() const
Test for a status code of SUCCESS.
Definition: StatusCode.h:75
ServiceLocatorHelper::threadName
std::string threadName() const
Definition: ServiceLocatorHelper.cpp:7
MSG::VERBOSE
Definition: IMessageSvc.h:16
ThreadGaudi.h
isGaudiThreaded
GAUDI_API bool isGaudiThreaded(const std::string &name)
test if current Gaudi object is running /will run in a thread
Definition: ThreadGaudi.cpp:76
std::string
STL class.
ServiceLocatorHelper::log
MsgStream & log() const
Definition: ServiceLocatorHelper.h:75
ISvcLocator::service
StatusCode service(const Gaudi::Utils::TypeNameString &name, T *&svc, bool createIf=true)
Templated method to access a service by name.
Definition: ISvcLocator.h:79
InterfaceID
Interface ID class.
Definition: IInterface.h:29
StatusCode
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:28
gaudirun.type
type
Definition: gaudirun.py:133
ServiceLocatorHelper::isInThread
bool isInThread() const
Definition: ServiceLocatorHelper.cpp:14
ServiceLocatorHelper::locateService
StatusCode locateService(const std::string &name, const InterfaceID &iid, void **ppSvc, bool quiet=false) const
Definition: ServiceLocatorHelper.cpp:16
IService.h
ServiceLocatorHelper::threadedName
std::string threadedName(const std::string &name) const
Definition: ServiceLocatorHelper.cpp:9
ServiceLocatorHelper.h
MSG::ERROR
Definition: IMessageSvc.h:16
gaudirun.level
level
Definition: gaudirun.py:271
ServiceLocatorHelper::service
SmartIF< IService > service(const std::string &name, const bool quiet=false, const bool createIf=true) const
Definition: ServiceLocatorHelper.cpp:50
ServiceLocatorHelper::requesterName
const std::string & requesterName() const
Definition: ServiceLocatorHelper.h:76
getGaudiThreadIDfromName
GAUDI_API std::string getGaudiThreadIDfromName(const std::string &name)
helper function to extract Gaudi Thread ID from thread copy name
Definition: ThreadGaudi.cpp:27
ISvcLocator.h
endmsg
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:209
ServiceLocatorHelper::createService
StatusCode createService(const std::string &name, const InterfaceID &iid, void **ppSvc) const
Definition: ServiceLocatorHelper.cpp:31