| The Gaudi Framework v28r2p1 (f1a77ff4)

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

- File List
- File Members

ServiceLocatorHelper.cpp
Go to the documentation of this file.
1 #include "GaudiKernel/IService.h"
2 #include "GaudiKernel/ISvcLocator.h"
3 #include "GaudiKernel/MsgStream.h"
4 #include "GaudiKernel/ThreadGaudi.h"
5 #include "GaudiKernel/ServiceLocatorHelper.h"
6
7 std::string
8 ServiceLocatorHelper::threadName() const {
9 return getGaudiThreadIDfromName(requesterName());
10 }
11
12 std::string
13 ServiceLocatorHelper::threadedName(const std::string& name) const {
14 return ( isInThread() ? name + threadName() : name );
15 }
16
17 bool
18 ServiceLocatorHelper::isInThread() const {
19 return isGaudiThreaded(requesterName());
20 }
21
22 StatusCode
23 ServiceLocatorHelper::locateService(const std::string& name,
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
39 StatusCode
40 ServiceLocatorHelper::createService(const std::string& name,
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
55 StatusCode
56 ServiceLocatorHelper::createService(const std::string& type,
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 }
ServiceLocatorHelper::serviceLocator
ISvcLocator * serviceLocator() const
Definition: ServiceLocatorHelper.h:76
SmartIF< IService >
gaudiComponentHelp.name
name
Definition: gaudiComponentHelp.py:28
StatusCode::FAILURE
Definition: StatusCode.h:29
MsgStream.h
StatusCode::isSuccess
bool isSuccess() const
Test for a status code of SUCCESS.
Definition: StatusCode.h:74
UNLIKELY
#define UNLIKELY(x)
Definition: Kernel.h:126
ServiceLocatorHelper::threadName
std::string threadName() const
Definition: ServiceLocatorHelper.cpp:8
MSG::VERBOSE
Definition: IMessageSvc.h:17
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:73
std::string
STL class.
ServiceLocatorHelper::log
MsgStream & log() const
Definition: ServiceLocatorHelper.h:77
ServiceLocatorHelper::createService
StatusCode createService(const std::string &type, const std::string &name, const InterfaceID &iid, void **ppSvc) const
Definition: ServiceLocatorHelper.cpp:56
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:78
InterfaceID
Interface ID class.
Definition: IInterface.h:30
StatusCode
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:26
gaudirun.type
type
Definition: gaudirun.py:125
ServiceLocatorHelper::isInThread
bool isInThread() const
Definition: ServiceLocatorHelper.cpp:18
ServiceLocatorHelper::locateService
StatusCode locateService(const std::string &name, const InterfaceID &iid, void **ppSvc, bool quiet=false) const
Definition: ServiceLocatorHelper.cpp:23
IService.h
ServiceLocatorHelper::threadedName
std::string threadedName(const std::string &name) const
Definition: ServiceLocatorHelper.cpp:13
ServiceLocatorHelper.h
MSG::ERROR
Definition: IMessageSvc.h:21
gaudirun.level
level
Definition: gaudirun.py:261
ServiceLocatorHelper::service
SmartIF< IService > service(const std::string &name, const bool quiet=false, const bool createIf=true) const
Definition: ServiceLocatorHelper.cpp:63
ServiceLocatorHelper::requesterName
const std::string & requesterName() const
Definition: ServiceLocatorHelper.h:78
getGaudiThreadIDfromName
GAUDI_API std::string getGaudiThreadIDfromName(const std::string &name)
helper function to extract Gaudi Thread ID from thread copy name
Definition: ThreadGaudi.cpp:26
ISvcLocator.h
endmsg
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:244
ServiceLocatorHelper::createService
StatusCode createService(const std::string &name, const InterfaceID &iid, void **ppSvc) const
Definition: ServiceLocatorHelper.cpp:40