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( int ) = delete;
89  ToolHandle( std::nullptr_t ) = delete;
90 
91  explicit ToolHandle( const IInterface* parent = 0, bool createIf = true )
96  m_pToolSvc( "ToolSvc", GaudiHandleBase::parentName() )
97  {}
98 
116  ToolHandle( const std::string& toolTypeAndName, const IInterface* parent = 0, bool createIf = true )
118  GaudiHandle<T>( toolTypeAndName,
121  m_pToolSvc( "ToolSvc", GaudiHandleBase::parentName() )
122  {}
123 
126  StatusCode retrieve() const { // not really const, because it updates m_pObject
127  return GaudiHandle<T>::retrieve();
128  }
129 
132  StatusCode release() const { // not really const, because it updates m_pObject
133  return GaudiHandle<T>::release();
134  }
135 
137  virtual StatusCode retrieve( T*& algTool ) const {
138  return m_pToolSvc->retrieve( GaudiHandleBase::typeAndName(), T::interfaceID(),
139  (IAlgTool*&)(algTool),
141  }
142 
144  virtual StatusCode release( T* algTool ) const {
145  return m_pToolSvc->releaseTool( algTool );
146  }
147 
148 private:
149  //
150  // Private data members
151  //
153 };
154 
165 template < class T >
166 class ToolHandleArray : public ToolHandleInfo, public GaudiHandleArray< ToolHandle<T> > {
167 public:
168  //
169  // Constructors
170  //
177  ToolHandleArray( const std::vector< std::string >& myTypesAndNames,
178  const IInterface* parent = 0, bool createIf = true )
180  GaudiHandleArray< ToolHandle<T> >( myTypesAndNames,
183  {}
184 
189  ToolHandleArray( const IInterface* parent = 0, bool createIf = true )
193  { }
194 
199  virtual bool push_back( const std::string& toolTypeAndName ) {
200  ToolHandle<T> handle( toolTypeAndName,
204  return true;
205  }
206 
208  virtual bool push_back( const ToolHandle<T>& myHandle ) {
209  return push_back( myHandle.typeAndName() );
210  }
211 
212 };
213 
214 
215 template <class T>
216 inline std::ostream& operator<<( std::ostream& os, const ToolHandle<T>& handle ) {
217  return operator<<(os, static_cast<const GaudiHandleInfo&>(handle) );
218 }
219 
220 
221 template <class T>
222 inline std::ostream& operator<<( std::ostream& os, const ToolHandleArray<T>& handle ) {
223  return operator<<(os, static_cast<const GaudiHandleInfo&>(handle) );
224 }
225 
226 
227 #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:208
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:249
ServiceHandle< IToolSvc > m_pToolSvc
Definition: ToolHandle.h:152
const std::string toolParentName(const IInterface *parent) const
Definition: ToolHandle.h:49
ToolHandle(const IInterface *parent=0, bool createIf=true)
Definition: ToolHandle.h:91
Array of Handles to be used in lieu of vector of naked pointers to tools.
Definition: PropertyMgr.h:22
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:116
StatusCode release() const
Release the component.
Definition: GaudiHandle.h:198
ToolHandle(int)=delete
Constructor for a tool with default tool type and name.
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:137
virtual StatusCode release(T *algTool) const
Do the real release of the AlgTool.
Definition: ToolHandle.h:144
bool createIf() const
Definition: ToolHandle.h:34
StatusCode retrieve() const
Retrieve the AlgTool.
Definition: ToolHandle.h:126
bool isPublic() const
Definition: ToolHandle.h:30
Handle to be used in lieu of naked pointers to tools.
Definition: PropertyMgr.h:20
ToolHandleArray(const IInterface *parent=0, bool createIf=true)
Constructor which creates and empty list.
Definition: ToolHandle.h:189
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:199
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:177
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:132
T is the concrete handle type, e.g.
Definition: GaudiHandle.h:363