00001 #ifndef GAUDIKERNEL_SERVICEHANDLE_H
00002 #define GAUDIKERNEL_SERVICEHANDLE_H
00003
00004
00005 #include "GaudiKernel/GaudiHandle.h"
00006 #include "GaudiKernel/IMessageSvc.h"
00007 #include "GaudiKernel/ISvcLocator.h"
00008 #include "GaudiKernel/Bootstrap.h"
00009 #include "GaudiKernel/GaudiException.h"
00010 #include "GaudiKernel/MsgStream.h"
00011 #include "GaudiKernel/ServiceLocatorHelper.h"
00012
00013 #include <string>
00014 #include <stdexcept>
00015
00016
00017 class IAlgTool;
00018 class IToolSvc;
00019 class ServiceHandleProperty;
00020
00021
00029 template< class T >
00030 class ServiceHandle : public GaudiHandle<T> {
00031 public:
00032
00033
00034
00042 ServiceHandle( const std::string& serviceName, const std::string& theParentName )
00043 : GaudiHandle<T>(serviceName, "Service", theParentName),
00044 m_pSvcLocator(0), m_pMessageSvc(0)
00045 {}
00046
00049 StatusCode retrieve() const {
00050 return GaudiHandle<T>::retrieve();
00051 }
00052
00053
00054
00055
00056
00057
00058
00059 protected:
00061 StatusCode retrieve( T*& service ) const {
00062 MsgStream log(messageSvc(), GaudiHandleBase::messageName());
00063 ServiceLocatorHelper helper(*serviceLocator(), log, this->parentName());
00064 return helper.getService(GaudiHandleBase::typeAndName(), true, T::interfaceID(), (void**)&service);
00065 }
00066
00067
00068
00069
00070
00071
00072 private:
00073
00074
00075
00076 ISvcLocator* serviceLocator() const {
00077 if ( !m_pSvcLocator ) {
00078 m_pSvcLocator = Gaudi::svcLocator();
00079 if ( !m_pSvcLocator ) {
00080 throw GaudiException("SvcLocator not found", "Core component not found", StatusCode::FAILURE);
00081 }
00082 }
00083 return m_pSvcLocator;
00084 }
00085
00086 IMessageSvc* messageSvc() const {
00087 if ( !m_pMessageSvc ) {
00088 StatusCode sc = serviceLocator()->service( "MessageSvc", m_pMessageSvc, true );
00089 if( sc.isFailure() ) {
00090 throw GaudiException("Service [MessageSvc] not found",
00091 this->parentName(), sc);
00092 }
00093 }
00094 return m_pMessageSvc;
00095 }
00096
00097
00098
00099 mutable ISvcLocator* m_pSvcLocator;
00100 mutable IMessageSvc* m_pMessageSvc;
00101 };
00102
00103 template <class T>
00104 inline std::ostream& operator<<( std::ostream& os, const ServiceHandle<T>& handle ) {
00105 return operator<<(os, static_cast<const GaudiHandleInfo&>(handle) );
00106 }
00107
00108 #endif // ! GAUDIKERNEL_SERVICEHANDLE_H