| # Gaudi Framework, version v20r3
Generated: 24 Nov 2008

# 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(requestorName());
    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(requestorName());
    00020 }
    00021
    00022 StatusCode
    00023 ServiceLocatorHelper::locateService(const std::string& name,
    00024                                     const InterfaceID& iid,
    00025                                     void** ppSvc,
    00026                                     bool quiet) const {
    00027   IService* theSvc(0);
    00028   StatusCode sc(StatusCode::FAILURE);
    00029   if (isInThread()) {
    00030     //first we look for  a thread-specific version of the service
    00031     sc=serviceLocator()->getService(name + threadName(), theSvc, false);
    00032   };
    00033   // if not, try to find the common, single-threaded version of the service
    00034   if (!sc.isSuccess()) sc=serviceLocator()->getService(name, theSvc,false);
    00035
    00036   if ( sc.isSuccess() ) {
    00037     if (!quiet) log() << MSG::VERBOSE
    00038           << "ServiceLocatorHelper::locateService: found service "
    00039           << name <<endreq;
    00040     sc = theSvc->queryInterface(iid, ppSvc);
    00041     if (!sc.isSuccess()) {
    00042       *ppSvc=0;
    00043       if (!quiet) log() << MSG::ERROR
    00044             << "ServiceLocatorHelper::locateService: wrong interface id "
    00045             << iid << " for service " << name << endreq;
    00046     }
    00047   } else {
    00048     // if not return an error
    00049     if (!quiet) {
    00050       log() << MSG::ERROR
    00051             << "ServiceLocatorHelper::locateService: can not locate service "
    00052             << name;
    00053       if (isInThread()) log() << MSG::ERROR << " or " << name + threadName();
    00054       log() << MSG::ERROR << endreq;
    00055     }
    00056     *ppSvc = 0;
    00057   }
    00058   return sc;
    00059 }
    00060
    00061 StatusCode
    00062 ServiceLocatorHelper::createService(const std::string& name,
    00063                                    const InterfaceID& iid,
    00064                                    void** ppSvc) const {
    00065
    00066   IService* theSvc(0);
    00067   StatusCode sc(StatusCode::FAILURE);
    00068   if (isInThread()) {
    00069     //first we look for  a thread-specific version of the service
    00070     sc=serviceLocator()->getService(threadedName(name), theSvc, true);
    00071   };
    00072   // if not, try to find the common, single-threaded version of the service
    00073   if (!sc.isSuccess()) sc=serviceLocator()->getService(name, theSvc, true);
    00074
    00075   if ( sc.isSuccess() ) {
    00076     sc = theSvc->queryInterface(iid, ppSvc);
    00077     if (!sc.isSuccess()) {
    00078       *ppSvc=0;
    00079     }
    00080   } else {
    00081     *ppSvc = 0;
    00082   }
    00083
    00084   if (sc.isSuccess()) {
    00085 #ifndef NDEBUG
    00086     log() << MSG::VERBOSE
    00087           << "ServiceLocatorHelper::createService: found service "
    00088           << threadedName(name) <<endreq;
    00089 #endif
    00090   } else {
    00091     log() << MSG::ERROR
    00092           << "ServiceLocatorHelper::createService: can not create service "
    00093           << threadedName(name) << endreq;
    00094   }
    00095   return sc;
    00096 }
    00097
    00098 StatusCode
    00099 ServiceLocatorHelper::createService(const std::string& type,
    00100                                     const std::string& name,
    00101                                     const InterfaceID& iid,
    00102                                     void** ppSvc) const {
    00103
    00104
    00105   IService* theSvc(0);
    00106   StatusCode sc(StatusCode::FAILURE);
    00107
    00108   //first we need to declare it
    00109   try {
    00110     dynamic_cast<ISvcManager&>(*serviceLocator()).declareSvcType(threadedName(name),type).ignore();
    00111   }
    00112   catch(...) {
    00113     log() << MSG::ERROR
    00114           << "ServiceLocatorHelper::createService: can not declare service "
    00115           << threadedName(name) << " of type " << type << endreq;
    00116     return StatusCode::FAILURE;
    00117   }
    00118
    00119   if (isInThread()) {
    00120     //first we look for  a thread-specific version of the service
    00121     sc=serviceLocator()->getService(threadedName(name), theSvc, true);
    00122   };
    00123   // if not, try to find the common, single-threaded version of the service
    00124   if (!sc.isSuccess()) sc=serviceLocator()->getService(name, theSvc, true);
    00125
    00126
    00127   if ( sc.isSuccess() ) {
    00128     sc = theSvc->queryInterface(iid, ppSvc);
    00129     if (!sc.isSuccess()) {
    00130       *ppSvc=0;
    00131     }
    00132   } else {
    00133     *ppSvc = 0;
    00134   }
    00135
    00136   if (sc.isSuccess()) {
    00137 #ifndef NDEBUG
    00138     log() << MSG::VERBOSE
    00139           << "ServiceLocatorHelper::createService: found service "
    00140           << threadedName(name) <<endreq;
    00141 #endif
    00142   } else {
    00143     log() << MSG::ERROR
    00144           << "ServiceLocatorHelper::createService: can not create service "
    00145           << threadedName(name) << " of type " << type << endreq;
    00146   }
    00147   return sc;
    00148
    00149 }
    00150