| # Gaudi Framework, version v21r7p1 |

Home |  | Generated: 15 Feb 2010

# ToolHandle.h

Go to the documentation of this file.

    00001 #ifndef GAUDIKERNEL_TOOLHANDLE_H
    00002 #define GAUDIKERNEL_TOOLHANDLE_H
    00003
    00004 //Includes
    00005 #include "GaudiKernel/GaudiHandle.h"
    00006 #include "GaudiKernel/ServiceHandle.h"
    00007 #include "GaudiKernel/IToolSvc.h"
    00008 #include "GaudiKernel/INamedInterface.h"
    00009 //#include "GaudiKernel/SmartIF.h"
    00010
    00011 #include <string>
    00012 #include <vector>
    00013 #include <stdexcept>
    00014
    00015 // forward declarations
    00016 class IInterface;
    00017 class IAlgTool;
    00018 class IToolSvc;
    00019
    00021 class ToolHandleInfo {
    00022 protected:
    00023   ToolHandleInfo( const IInterface* parent = 0, bool createIf = true )
    00024     : m_parent(parent), m_createIf(createIf)
    00025     {}
    00026
    00027 public:
    00028   virtual ~ToolHandleInfo() {};
    00029
    00030   bool isPublic() const {
    00031      return !m_parent;
    00032   }
    00033
    00034   bool createIf() const {
    00035      return m_createIf;
    00036   }
    00037
    00038   const IInterface* parent() const {
    00039      return m_parent;
    00040   }
    00041
    00042   //
    00043   // Some helper functions
    00044   //
    00045   const std::string toolComponentType( const IInterface* parent ) const {
    00046     return parent ? "PrivateTool" : "PublicTool";
    00047   }
    00048
    00049   const std::string toolParentName( const IInterface* parent ) const {
    00050     if (parent) {
    00051       //SmartIF<INamedInterface> pNamed(const_cast<IInterface*>(parent));
    00052       //if (pNamed.isValid()) {
    00053       const INamedInterface* pNamed = dynamic_cast<const INamedInterface*>(parent);
    00054       if (pNamed) {
    00055         return pNamed->name();
    00056       } else {
    00057         return "";
    00058       }
    00059     } else {
    00060       return "ToolSvc";
    00061     }
    00062   }
    00063
    00064 private:
    00065   const IInterface* m_parent;
    00066   bool m_createIf;
    00067 };
    00068
    00079 template< class T >
    00080 class ToolHandle : public ToolHandleInfo, public GaudiHandle<T> {
    00081 public:
    00082   //
    00083   // Constructors etc.
    00084   //
    00088   ToolHandle( const IInterface* parent = 0, bool createIf = true )
    00089     : ToolHandleInfo(parent,createIf),
    00090       GaudiHandle<T>( GaudiHandle<T>::getDefaultType(),
    00091                       ToolHandleInfo::toolComponentType(parent),
    00092                       ToolHandleInfo::toolParentName(parent) ),
    00093       m_pToolSvc( "ToolSvc", GaudiHandleBase::parentName() )
    00094   {}
    00095
    00113   ToolHandle( const std::string& toolTypeAndName, const IInterface* parent = 0, bool createIf = true )
    00114     : ToolHandleInfo(parent,createIf),
    00115       GaudiHandle<T>( toolTypeAndName,
    00116                       ToolHandleInfo::toolComponentType(parent),
    00117                       ToolHandleInfo::toolParentName(parent) ),
    00118       m_pToolSvc( "ToolSvc", GaudiHandleBase::parentName() )
    00119   {}
    00120
    00123   StatusCode retrieve() const { // not really const, because it updates m_pObject
    00124     return GaudiHandle<T>::retrieve();
    00125   }
    00126
    00129   StatusCode release() const { // not really const, because it updates m_pObject
    00130     return GaudiHandle<T>::release();
    00131   }
    00132
    00134   virtual StatusCode retrieve( T*& algTool ) const {
    00135     return m_pToolSvc->retrieve( GaudiHandleBase::typeAndName(), T::interfaceID(),
    00136                                  (IAlgTool*&)(algTool),
    00137                                  ToolHandleInfo::parent(), ToolHandleInfo::createIf() );
    00138   }
    00139
    00141   virtual StatusCode release( T* algTool ) const {
    00142     return m_pToolSvc->releaseTool( algTool );
    00143   }
    00144
    00145 private:
    00146   //
    00147   // Private data members
    00148   //
    00149   mutable ServiceHandle<IToolSvc> m_pToolSvc;
    00150 };
    00151
    00162 template < class T >
    00163 class ToolHandleArray : public ToolHandleInfo, public GaudiHandleArray< ToolHandle<T> > {
    00164 public:
    00165   //
    00166   // Constructors
    00167   //
    00174   ToolHandleArray( const std::vector< std::string >& myTypesAndNames,
    00175                    const IInterface* parent = 0, bool createIf = true )
    00176     : ToolHandleInfo( parent, createIf ),
    00177       GaudiHandleArray< ToolHandle<T> >( myTypesAndNames,
    00178                                          ToolHandleInfo::toolComponentType(parent),
    00179                                          ToolHandleInfo::toolParentName(parent) )
    00180   {}
    00181
    00186   ToolHandleArray( const IInterface* parent = 0, bool createIf = true )
    00187     : ToolHandleInfo( parent, createIf ),
    00188       GaudiHandleArray< ToolHandle<T> >( ToolHandleInfo::toolComponentType(parent),
    00189                                          ToolHandleInfo::toolParentName(parent) )
    00190   { }
    00191
    00196   virtual bool push_back( const std::string& toolTypeAndName ) {
    00197     ToolHandle<T> handle( toolTypeAndName,
    00198                           ToolHandleInfo::parent(),
    00199                           ToolHandleInfo::createIf() );
    00200     GaudiHandleArray< ToolHandle<T> >::push_back( handle );
    00201     return true;
    00202   }
    00203
    00205   virtual bool push_back( const ToolHandle<T>& myHandle ) {
    00206     return push_back( myHandle.typeAndName() );
    00207   }
    00208
    00209 };
    00210
    00211
    00212 template <class T>
    00213 inline std::ostream& operator<<( std::ostream& os, const ToolHandle<T>& handle ) {
    00214   return operator<<(os, static_cast<const GaudiHandleInfo&>(handle) );
    00215 }
    00216
    00217
    00218 template <class T>
    00219 inline std::ostream& operator<<( std::ostream& os, const ToolHandleArray<T>& handle ) {
    00220   return operator<<(os, static_cast<const GaudiHandleInfo&>(handle) );
    00221 }
    00222
    00223
    00224 #endif // ! GAUDIKERNEL_TOOLHANDLE_H