| # Gaudi Framework, version v25r0 |

Home |  | Generated: Mon Feb 17 2014

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

- File List
- File Members

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"
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 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
39 StatusCode
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
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.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 }