00001 #ifndef GAUDIKERNEL_TOOLHANDLE_H
00002 #define GAUDIKERNEL_TOOLHANDLE_H
00003
00004
00005 #include "GaudiKernel/GaudiHandle.h"
00006 #include "GaudiKernel/ServiceHandle.h"
00007 #include "GaudiKernel/IToolSvc.h"
00008 #include "GaudiKernel/INamedInterface.h"
00009
00010 #include <string>
00011 #include <vector>
00012 #include <stdexcept>
00013
00014
00015 class IInterface;
00016 class IAlgTool;
00017 class IToolSvc;
00018
00020 class ToolHandleInfo {
00021 protected:
00022 ToolHandleInfo( const IInterface* parent = 0, bool createIf = true )
00023 : m_parent(parent), m_createIf(createIf)
00024 {}
00025
00026 public:
00027 virtual ~ToolHandleInfo() {};
00028
00029 bool isPublic() const {
00030 return !m_parent;
00031 }
00032
00033 bool createIf() const {
00034 return m_createIf;
00035 }
00036
00037 const IInterface* parent() const {
00038 return m_parent;
00039 }
00040
00041
00042
00043
00044 const std::string toolComponentType( const IInterface* parent ) const {
00045 return parent ? "PrivateTool" : "PublicTool";
00046 }
00047
00048 const std::string toolParentName( const IInterface* parent ) const {
00049 if (parent) {
00050 const INamedInterface* pNamed = dynamic_cast<const INamedInterface*>(parent);
00051 if (pNamed) {
00052 return pNamed->name();
00053 } else {
00054 return "";
00055 }
00056 } else {
00057 return "ToolSvc";
00058 }
00059 }
00060
00061 private:
00062 const IInterface* m_parent;
00063 bool m_createIf;
00064 };
00065
00076 template< class T >
00077 class ToolHandle : public ToolHandleInfo, public GaudiHandle<T> {
00078 public:
00079
00080
00081
00085 ToolHandle( const IInterface* parent = 0, bool createIf = true )
00086 : ToolHandleInfo(parent,createIf),
00087 GaudiHandle<T>( GaudiHandle<T>::getDefaultType(),
00088 ToolHandleInfo::toolComponentType(parent),
00089 ToolHandleInfo::toolParentName(parent) ),
00090 m_pToolSvc( "ToolSvc", GaudiHandleBase::parentName() )
00091 {}
00092
00110 ToolHandle( const std::string& toolTypeAndName, const IInterface* parent = 0, bool createIf = true )
00111 : ToolHandleInfo(parent,createIf),
00112 GaudiHandle<T>( toolTypeAndName,
00113 ToolHandleInfo::toolComponentType(parent),
00114 ToolHandleInfo::toolParentName(parent) ),
00115 m_pToolSvc( "ToolSvc", GaudiHandleBase::parentName() )
00116 {}
00117
00120 StatusCode retrieve() const {
00121 return GaudiHandle<T>::retrieve();
00122 }
00123
00126 StatusCode release() const {
00127 return GaudiHandle<T>::release();
00128 }
00129
00131 virtual StatusCode retrieve( T*& algTool ) const {
00132 return m_pToolSvc->retrieve( GaudiHandleBase::typeAndName(), T::interfaceID(),
00133 (IAlgTool*&)(algTool),
00134 ToolHandleInfo::parent(), ToolHandleInfo::createIf() );
00135 }
00136
00138 virtual StatusCode release( T* algTool ) const {
00139 return m_pToolSvc->releaseTool( algTool );
00140 }
00141
00142 private:
00143
00144
00145
00146 mutable ServiceHandle<IToolSvc> m_pToolSvc;
00147 };
00148
00159 template < class T >
00160 class ToolHandleArray : public ToolHandleInfo, public GaudiHandleArray< ToolHandle<T> > {
00161 public:
00162
00163
00164
00171 ToolHandleArray( const std::vector< std::string >& myTypesAndNames,
00172 const IInterface* parent = 0, bool createIf = true )
00173 : ToolHandleInfo( parent, createIf ),
00174 GaudiHandleArray< ToolHandle<T> >( myTypesAndNames,
00175 ToolHandleInfo::toolComponentType(parent),
00176 ToolHandleInfo::toolParentName(parent) )
00177 {}
00178
00183 ToolHandleArray( const IInterface* parent = 0, bool createIf = true )
00184 : ToolHandleInfo( parent, createIf ),
00185 GaudiHandleArray< ToolHandle<T> >( ToolHandleInfo::toolComponentType(parent),
00186 ToolHandleInfo::toolParentName(parent) )
00187 { }
00188
00193 virtual bool push_back( const std::string& toolTypeAndName ) {
00194 ToolHandle<T> handle( toolTypeAndName,
00195 ToolHandleInfo::parent(),
00196 ToolHandleInfo::createIf() );
00197 GaudiHandleArray< ToolHandle<T> >::push_back( handle );
00198 return true;
00199 }
00200
00202 virtual bool push_back( const ToolHandle<T>& myHandle ) {
00203 return push_back( myHandle.typeAndName() );
00204 }
00205
00206 };
00207
00208
00209 template <class T>
00210 inline std::ostream& operator<<( std::ostream& os, const ToolHandle<T>& handle ) {
00211 return operator<<(os, static_cast<const GaudiHandleInfo&>(handle) );
00212 }
00213
00214
00215 template <class T>
00216 inline std::ostream& operator<<( std::ostream& os, const ToolHandleArray<T>& handle ) {
00217 return operator<<(os, static_cast<const GaudiHandleInfo&>(handle) );
00218 }
00219
00220
00221 #endif // ! GAUDIKERNEL_TOOLHANDLE_H