All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
ToolHandle.h
Go to the documentation of this file.
1 #ifndef GAUDIKERNEL_TOOLHANDLE_H
2 #define GAUDIKERNEL_TOOLHANDLE_H
3 
4 //Includes
7 #include "GaudiKernel/IToolSvc.h"
9 //#include "GaudiKernel/SmartIF.h"
10 
11 #include <string>
12 #include <vector>
13 #include <stdexcept>
14 
15 // forward declarations
16 class IInterface;
17 class IAlgTool;
18 class IToolSvc;
19 
22 protected:
23  ToolHandleInfo( const IInterface* parent = 0, bool createIf = true )
25  {}
26 
27 public:
28  virtual ~ToolHandleInfo() {};
29 
30  bool isPublic() const {
31  return !m_parent;
32  }
33 
34  bool createIf() const {
35  return m_createIf;
36  }
37 
38  const IInterface* parent() const {
39  return m_parent;
40  }
41 
42  //
43  // Some helper functions
44  //
45  const std::string toolComponentType( const IInterface* parent ) const {
46  return parent ? "PrivateTool" : "PublicTool";
47  }
48 
49  const std::string toolParentName( const IInterface* parent ) const {
50  if (parent) {
51  //SmartIF<INamedInterface> pNamed(const_cast<IInterface*>(parent));
52  //if (pNamed.isValid()) {
53  const INamedInterface* pNamed = dynamic_cast<const INamedInterface*>(parent);
54  if (pNamed) {
55  return pNamed->name();
56  } else {
57  return "";
58  }
59  } else {
60  return "ToolSvc";
61  }
62  }
63 
64 private:
66  bool m_createIf;
67 };
68 
79 template< class T >
80 class ToolHandle : public ToolHandleInfo, public GaudiHandle<T> {
81 public:
82  //
83  // Constructors etc.
84  //
88  ToolHandle( const IInterface* parent = 0, bool createIf = true )
93  m_pToolSvc( "ToolSvc", GaudiHandleBase::parentName() )
94  {}
95 
113  ToolHandle( const std::string& toolTypeAndName, const IInterface* parent = 0, bool createIf = true )
115  GaudiHandle<T>( toolTypeAndName,
118  m_pToolSvc( "ToolSvc", GaudiHandleBase::parentName() )
119  {}
120 
123  StatusCode retrieve() const { // not really const, because it updates m_pObject
124  return GaudiHandle<T>::retrieve();
125  }
126 
129  StatusCode release() const { // not really const, because it updates m_pObject
130  return GaudiHandle<T>::release();
131  }
132 
134  virtual StatusCode retrieve( T*& algTool ) const {
135  return m_pToolSvc->retrieve( GaudiHandleBase::typeAndName(), T::interfaceID(),
136  (IAlgTool*&)(algTool),
138  }
139 
141  virtual StatusCode release( T* algTool ) const {
142  return m_pToolSvc->releaseTool( algTool );
143  }
144 
145 private:
146  //
147  // Private data members
148  //
150 };
151 
162 template < class T >
163 class ToolHandleArray : public ToolHandleInfo, public GaudiHandleArray< ToolHandle<T> > {
164 public:
165  //
166  // Constructors
167  //
174  ToolHandleArray( const std::vector< std::string >& myTypesAndNames,
175  const IInterface* parent = 0, bool createIf = true )
177  GaudiHandleArray< ToolHandle<T> >( myTypesAndNames,
180  {}
181 
186  ToolHandleArray( const IInterface* parent = 0, bool createIf = true )
190  { }
191 
196  virtual bool push_back( const std::string& toolTypeAndName ) {
197  ToolHandle<T> handle( toolTypeAndName,
201  return true;
202  }
203 
205  virtual bool push_back( const ToolHandle<T>& myHandle ) {
206  return push_back( myHandle.typeAndName() );
207  }
208 
209 };
210 
211 
212 template <class T>
213 inline std::ostream& operator<<( std::ostream& os, const ToolHandle<T>& handle ) {
214  return operator<<(os, static_cast<const GaudiHandleInfo&>(handle) );
215 }
216 
217 
218 template <class T>
219 inline std::ostream& operator<<( std::ostream& os, const ToolHandleArray<T>& handle ) {
220  return operator<<(os, static_cast<const GaudiHandleInfo&>(handle) );
221 }
222 
223 
224 #endif // ! GAUDIKERNEL_TOOLHANDLE_H
virtual bool push_back(const ToolHandle< T > &myHandle)
Ensure that for added handles the parent and creatIf are taken from this array.
Definition: ToolHandle.h:205
The interface implemented by the IToolSvc base class.
Definition: IToolSvc.h:17
Handle to be used in lieu of naked pointers to gaudis.
Definition: GaudiHandle.h:158
std::string getDefaultType()
Helper function to get default type string from the class type.
Definition: GaudiHandle.h:234
ServiceHandle< IToolSvc > m_pToolSvc
Definition: ToolHandle.h:149
const std::string toolParentName(const IInterface *parent) const
Definition: ToolHandle.h:49
ToolHandle(const IInterface *parent=0, bool createIf=true)
Constructor for a tool with default tool type and name.
Definition: ToolHandle.h:88
Array of Handles to be used in lieu of vector of naked pointers to tools.
Definition: PropertyMgr.h:26
const std::string & parentName() const
The name of the parent.
Definition: GaudiHandle.h:48
ToolHandle(const std::string &toolTypeAndName, const IInterface *parent=0, bool createIf=true)
Create a handle ('smart pointer') to a tool.
Definition: ToolHandle.h:113
StatusCode release() const
Release the component.
Definition: GaudiHandle.h:198
virtual const std::string & name() const =0
Retrieve the name of the instance.
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:30
const IInterface * parent() const
Definition: ToolHandle.h:38
Definition of the basic interface.
Definition: IInterface.h:160
const std::string toolComponentType(const IInterface *parent) const
Definition: ToolHandle.h:45
StatusCode retrieve() const
Retrieve the component.
Definition: GaudiHandle.h:188
StatusCode retrieve() const
Retrieve the Service.
Definition: ServiceHandle.h:48
virtual StatusCode retrieve(T *&algTool) const
Do the real retrieval of the AlgTool.
Definition: ToolHandle.h:134
virtual StatusCode release(T *algTool) const
Do the real release of the AlgTool.
Definition: ToolHandle.h:141
bool createIf() const
Definition: ToolHandle.h:34
StatusCode retrieve() const
Retrieve the AlgTool.
Definition: ToolHandle.h:123
bool isPublic() const
Definition: ToolHandle.h:30
Handle to be used in lieu of naked pointers to tools.
Definition: PropertyMgr.h:24
ToolHandleArray(const IInterface *parent=0, bool createIf=true)
Constructor which creates and empty list.
Definition: ToolHandle.h:186
IInterface compliant class extending IInterface with the name() method.
const IInterface * m_parent
Definition: ToolHandle.h:65
The interface implemented by the AlgTool base class.
Definition: IAlgTool.h:23
virtual ~ToolHandleInfo()
Definition: ToolHandle.h:28
virtual bool push_back(const std::string &toolTypeAndName)
Add a handle to the array with given tool type and name.
Definition: ToolHandle.h:196
Base class to handles to be used in lieu of naked pointers to various Gaudi components.
Definition: GaudiHandle.h:80
std::string typeAndName() const
The full type and name: "type/name".
Definition: GaudiHandle.h:107
ToolHandleArray(const std::vector< std::string > &myTypesAndNames, const IInterface *parent=0, bool createIf=true)
Generic constructor.
Definition: ToolHandle.h:174
ToolHandleInfo(const IInterface *parent=0, bool createIf=true)
Definition: ToolHandle.h:23
General info and helper functions for toolhandles and arrays.
Definition: ToolHandle.h:21
StatusCode release() const
Release the AlgTool.
Definition: ToolHandle.h:129
T is the concrete handle type, e.g.
Definition: GaudiHandle.h:353