| # Gaudi Framework, version v21r7 |

Home |  | Generated: 22 Jan 2010

# ServiceHandle.h

Go to the documentation of this file.

    00001 #ifndef GAUDIKERNEL_SERVICEHANDLE_H
    00002 #define GAUDIKERNEL_SERVICEHANDLE_H
    00003
    00004 //Includes
    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 // class predeclarations
    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   // Constructors etc.
    00034   //
    00042   ServiceHandle( const std::string& serviceName, const std::string& theParentName )
    00043     : GaudiHandle<T>(serviceName, "Service", theParentName)
    00044   {}
    00045
    00048   StatusCode retrieve() const { // not really const, because it updates m_pObject
    00049     return GaudiHandle<T>::retrieve();
    00050   }
    00051
    00052 //  /** Release the Service.
    00053 //    Function must be repeated here to avoid hiding the function release( T*& ) */
    00054 //   StatusCode release() const { // not really const, because it updates m_pObject
    00055 //     return GaudiHandle<T>::release();
    00056 //   }
    00057
    00058 protected:
    00060   StatusCode retrieve( T*& service ) const {
    00061     const ServiceLocatorHelper helper(*serviceLocator(), GaudiHandleBase::messageName(), this->parentName());
    00062     return helper.getService(GaudiHandleBase::typeAndName(), true, T::interfaceID(), (void**)&service);
    00063   }
    00064
    00065 //   /** Do the real release of the Service */
    00066 //   virtual StatusCode release( T* service ) const {
    00067 //     return service->release();
    00068 //   }
    00069
    00070 private:
    00071   //
    00072   // Private helper functions
    00073   //
    00074   SmartIF<ISvcLocator>& serviceLocator() const { // not really const, because it may change m_pSvcLocator
    00075     if ( !m_pSvcLocator.isValid() ) {
    00076       m_pSvcLocator = Gaudi::svcLocator();
    00077       if ( !m_pSvcLocator.isValid() ) {
    00078         throw GaudiException("SvcLocator not found", "Core component not found", StatusCode::FAILURE);
    00079       }
    00080     }
    00081     return m_pSvcLocator;
    00082   }
    00083
    00084   SmartIF<IMessageSvc>& messageSvc() const { // not really const, because it may change m_pMessageSvc
    00085     if ( !m_pMessageSvc.isValid() ) {
    00086       m_pMessageSvc = serviceLocator(); // default message service
    00087       if( !m_pMessageSvc.isValid() ) {
    00088         throw GaudiException("Service [MessageSvc] not found",
    00089                              this->parentName(), StatusCode::FAILURE);
    00090       }
    00091     }
    00092     return m_pMessageSvc;
    00093   }
    00094   //
    00095   // private data members
    00096   //
    00097   mutable SmartIF<ISvcLocator> m_pSvcLocator;
    00098   mutable SmartIF<IMessageSvc> m_pMessageSvc;
    00099 };
    00100
    00111 template < class T >
    00112 class ServiceHandleArray : public GaudiHandleArray< ServiceHandle<T> > {
    00113 public:
    00114   //
    00115   // Constructors
    00116   //
    00119   ServiceHandleArray( const std::vector< std::string >& myTypesAndNamesList,
    00120                       const std::string& myComponentType, const std::string& myParentName  ):
    00121     GaudiHandleArray< ServiceHandle<T> >( myTypesAndNamesList,
    00122                                           myComponentType,
    00123                                           myParentName)
    00124   {  }
    00125
    00126   ServiceHandleArray( const std::string& myParentName )
    00127     : GaudiHandleArray< ServiceHandle<T> >( "Service", myParentName)
    00128   { }
    00129
    00130   virtual bool push_back( const std::string& serviceTypeAndName ) {
    00131     ServiceHandle<T> handle( serviceTypeAndName,GaudiHandleInfo::parentName());
    00132     GaudiHandleArray< ServiceHandle<T> >::push_back( handle );
    00133     return true;
    00134   }
    00135
    00136   virtual bool push_back( const ServiceHandle<T>& myHandle ) {
    00137     return push_back( myHandle.typeAndName() );
    00138   }
    00139
    00140 };
    00141
    00142 template <class T>
    00143 inline std::ostream& operator<<( std::ostream& os, const ServiceHandle<T>& handle ) {
    00144   return operator<<(os, static_cast<const GaudiHandleInfo&>(handle) );
    00145 }
    00146
    00147 template <class T>
    00148 inline std::ostream& operator<<( std::ostream& os, const ServiceHandleArray<T>& handle ) {
    00149   return operator<<(os, static_cast<const GaudiHandleInfo&>(handle) );
    00150 }
    00151
    00152 #endif // ! GAUDIKERNEL_SERVICEHANDLE_H