| # Gaudi Framework, version v21r9 |

Home |  | Generated: 3 May 2010

# ServiceLocatorHelper.cpp

Go to the documentation of this file.

    00001 #include "GaudiKernel/IService.h"
    00002 #include "GaudiKernel/ISvcLocator.h"
    00003 #include "GaudiKernel/MsgStream.h"
    00004 #include "GaudiKernel/ThreadGaudi.h"
    00005 #include "GaudiKernel/ServiceLocatorHelper.h"
    00006
    00007 std::string
    00008 ServiceLocatorHelper::threadName() const {
    00009   return getGaudiThreadIDfromName(requesterName());
    00010 }
    00011
    00012 std::string
    00013 ServiceLocatorHelper::threadedName(const std::string& name) const {
    00014   return ( isInThread() ? name + threadName() : name );
    00015 }
    00016
    00017 bool
    00018 ServiceLocatorHelper::isInThread() const {
    00019   return isGaudiThreaded(requesterName());
    00020 }
    00021
    00022 StatusCode
    00023 ServiceLocatorHelper::locateService(const std::string& name,
    00024                                     const InterfaceID& iid,
    00025                                     void** ppSvc,
    00026                                     bool quiet) const {
    00027   SmartIF<IService> theSvc = service(name, quiet, false);
    00028   if (!theSvc.isValid()) return StatusCode::FAILURE;
    00029   StatusCode sc = theSvc->queryInterface(iid, ppSvc);
    00030   if (!sc.isSuccess()) {
    00031     *ppSvc = 0;
    00032     if (!quiet) log() << MSG::ERROR
    00033       << "ServiceLocatorHelper::locateService: wrong interface id "
    00034       << iid << " for service " << name << endmsg;
    00035   }
    00036   return sc;
    00037 }
    00038
    00039 StatusCode
    00040 ServiceLocatorHelper::createService(const std::string& name,
    00041                                     const InterfaceID& iid,
    00042                                     void** ppSvc) const {
    00043   SmartIF<IService> theSvc = service(name, false, true);
    00044   if (!theSvc.isValid()) return StatusCode::FAILURE;
    00045   StatusCode sc = theSvc->queryInterface(iid, ppSvc);
    00046   if (!sc.isSuccess()) {
    00047     *ppSvc = 0;
    00048     log() << MSG::ERROR
    00049       << "ServiceLocatorHelper::createService: wrong interface id "
    00050       << iid << " for service " << name << endmsg;
    00051   }
    00052   return sc;
    00053 }
    00054
    00055 StatusCode
    00056 ServiceLocatorHelper::createService(const std::string& type,
    00057                                     const std::string& name,
    00058                                     const InterfaceID& iid,
    00059                                     void** ppSvc) const {
    00060   return createService(type + "/" + name, iid, ppSvc);
    00061 }
    00062
    00063 SmartIF<IService> ServiceLocatorHelper::service(const std::string &name, const bool quiet, const bool createIf) const {
    00064   SmartIF<IService> theSvc;
    00065   if (isInThread()) {
    00066     //first we look for  a thread-specific version of the service
    00067     theSvc = serviceLocator()->service(name + threadName(), createIf);
    00068   }
    00069   // if not, try to find the common, single-threaded version of the service
    00070   if (!theSvc.isValid()){
    00071     theSvc = serviceLocator()->service(name, createIf);
    00072   }
    00073
    00074   if (theSvc.isValid()) {
    00075     if (!quiet) {
    00076       log() << MSG::VERBOSE
    00077             << "ServiceLocatorHelper::service: found service " << name <<endmsg;
    00078     }
    00079   } else {
    00080     // if not return an error
    00081     if (!quiet) {
    00082       log() << MSG::ERROR
    00083             << "ServiceLocatorHelper::service: can not locate service "
    00084             << name;
    00085       if (isInThread()) log() << MSG::ERROR << " or " << name + threadName();
    00086       log() << MSG::ERROR << endmsg;
    00087     }
    00088   }
    00089   return theSvc;
    00090 }