| # Gaudi Framework, version v20r2
Generated: 18 Jul 2008

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

- Search for |

- File List
- File Members

# 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       m_pSvcLocator(0), m_pMessageSvc(0)
    00045   {}
    00046
    00049   StatusCode retrieve() const { // not really const, because it updates m_pObject
    00050     return GaudiHandle<T>::retrieve();
    00051   }
    00052
    00053 //  /** Release the Service.
    00054 //    Function must be repeated here to avoid hiding the function release( T*& ) */
    00055 //   StatusCode release() const { // not really const, because it updates m_pObject
    00056 //     return GaudiHandle<T>::release();
    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 //   /** Do the real release of the Service */
    00068 //   virtual StatusCode release( T* service ) const {
    00069 //     return service->release();
    00070 //   }
    00071
    00072 private:
    00073   //
    00074   // Private helper functions
    00075   //
    00076   ISvcLocator* serviceLocator() const { // not really const, because it may change m_pSvcLocator
    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 { // not really const, because it may change m_pMessageSvc
    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   // private data members
    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