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/IAlgTool.h"
10 
11 #include <string>
12 #include <vector>
13 #include <stdexcept>
14 #include <type_traits>
15 
16 // forward declarations
17 class IInterface;
18 class IToolSvc;
19 
20 class Algorithm;
21 class AlgTool;
22 class Service;
23 
26 protected:
27  ToolHandleInfo(const IInterface* parent = nullptr, bool createIf = true )
29  {}
30 
31 public:
32  virtual ~ToolHandleInfo() = default;
33 
34  bool isPublic() const { return !m_parent; }
35 
36  bool createIf() const { return m_createIf; }
37 
38  const IInterface* parent() const { return m_parent; }
39 
40  //
41  // Some helper functions
42  //
44  return parent ? "PrivateTool" : "PublicTool";
45  }
46 
48  if (!parent) return "ToolSvc";
49  const INamedInterface* pNamed = dynamic_cast<const INamedInterface*>(parent);
50  return pNamed ? pNamed->name() : "";
51  }
52 
53 protected:
55  bool m_createIf;
56 
57 };
58 
67 
68 protected:
69  BaseToolHandle(const IInterface* parent = nullptr, bool createIf = true )
71  {}
72 
73  virtual StatusCode i_retrieve(IAlgTool*&) const = 0;
74 
75 public:
76  virtual ~BaseToolHandle() {}
77 
78  StatusCode retrieve(IAlgTool*& tool) const {
79  return i_retrieve(tool);
80  }
81 
82  virtual IAlgTool* get() const = 0;
83  virtual std::string typeAndName() const = 0;
84 };
85 
96 template< class T >
97 class ToolHandle : public BaseToolHandle, public GaudiHandle<T> {
98 
99  friend class Algorithm;
100  friend class AlgTool;
101  friend class Service;
102 
103 public:
107  ToolHandle(const IInterface* parent = nullptr, bool createIf = true)
112  m_pToolSvc( "ToolSvc", GaudiHandleBase::parentName() )
113  { }
114 
115  public:
116  //
117  // Constructors etc.
118  //
119 
139 #if defined(TOOLHANDLE_DEPR_WARN)
140  //warn about using deprecated explicit ToolHandle construction
141 #pragma message("Untracked ToolHandle: Migrate explicit DataHandle constructor to declareTool Algorithm Property")
142 
143  __attribute__ ((deprecated))
144 
145 #endif
146  ToolHandle(const std::string& toolTypeAndName,
147  const IInterface* parent = nullptr, bool createIf = true )
149  GaudiHandle<T>( toolTypeAndName,
152  m_pToolSvc( "ToolSvc", GaudiHandleBase::parentName() )
153  { }
154 
155 public:
156 
157  StatusCode initialize(const std::string& toolTypeAndName,
158  const IInterface* parent = nullptr, bool createIf = true){
159 
160  GaudiHandleBase::setTypeAndName(toolTypeAndName);
163 
164  m_parent = parent;
166 
168 
169  return sc;
170  }
171 
174  StatusCode retrieve() const { // not really const, because it updates m_pObject
175  return GaudiHandle<T>::retrieve();
176  }
177 
180  StatusCode release() const { // not really const, because it updates m_pObject
181  return GaudiHandle<T>::release();
182  }
183 
185  StatusCode retrieve( T*& algTool ) const override {
186  IAlgTool* iface = nullptr;
187  algTool = i_retrieve(iface) ? dynamic_cast<T*>(iface) : nullptr;
188  return algTool ? StatusCode::SUCCESS : StatusCode::FAILURE;
189  }
190 
192  StatusCode release( T* algTool ) const override {
193  return m_pToolSvc->releaseTool( algTool );
194  }
195 
196  IAlgTool *get() const override {
197  return GaudiHandle<T>::get();
198  }
199  std::string typeAndName() const override {
201  }
202 
203 protected:
204  StatusCode i_retrieve(IAlgTool*& algTool) const override {
206  algTool,
208  }
209 
210 private:
211  //
212  // Private data members
213  //
215 };
216 
217 //-------------------------------------------------------------------------//
218 
219 
220 
231 template < class T >
232 class ToolHandleArray : public ToolHandleInfo, public GaudiHandleArray< ToolHandle<T> > {
233 public:
234  //
235  // Constructors
236  //
244  const IInterface* parent = 0, bool createIf = true )
246  GaudiHandleArray< ToolHandle<T> >( myTypesAndNames,
249  {}
250 
255  ToolHandleArray( const IInterface* parent = 0, bool createIf = true )
259  { }
260 
265  virtual bool push_back( const std::string& toolTypeAndName ) {
266  ToolHandle<T> handle( toolTypeAndName,
270  return true;
271  }
272 
274  virtual bool push_back( const ToolHandle<T>& myHandle ) {
275  return push_back( myHandle.typeAndName() );
276  }
277 
278 };
279 
280 
281 template <class T>
282 inline std::ostream& operator<<( std::ostream& os, const ToolHandle<T>& handle ) {
283  return operator<<(os, static_cast<const GaudiHandleInfo&>(handle) );
284 }
285 
286 
287 template <class T>
288 inline std::ostream& operator<<( std::ostream& os, const ToolHandleArray<T>& handle ) {
289  return operator<<(os, static_cast<const GaudiHandleInfo&>(handle) );
290 }
291 
292 
293 #endif // ! GAUDIKERNEL_TOOLHANDLE_H
std::string typeAndName() const override
Definition: ToolHandle.h:199
ToolHandle(const IInterface *parent=nullptr, bool createIf=true)
Constructor for a tool with default tool type and name.
Definition: ToolHandle.h:107
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:274
The interface implemented by the IToolSvc base class.
Definition: IToolSvc.h:18
Handle to be used in lieu of naked pointers to gaudis.
Definition: GaudiHandle.h:171
#define __attribute__(x)
Definition: System.cpp:70
std::string getDefaultType()
Helper function to get default type string from the class type.
Definition: GaudiHandle.h:266
ServiceHandle< IToolSvc > m_pToolSvc
Definition: ToolHandle.h:214
void setTypeAndName(std::string myTypeAndName)
The component "type/name" string.
Definition: GaudiHandle.cpp:9
virtual StatusCode i_retrieve(IAlgTool *&) const =0
StatusCode retrieve(T *&algTool) const override
Do the real retrieval of the AlgTool.
Definition: ToolHandle.h:185
void setComponentType(const std::string &componentType)
The component type.
Definition: GaudiHandle.h:67
static std::string toolParentName(const IInterface *parent)
Definition: ToolHandle.h:47
virtual std::string typeAndName() const =0
Non-templated base class for actual ToolHandle.
Definition: ToolHandle.h:66
Array of Handles to be used in lieu of vector of naked pointers to tools.
Definition: PropertyMgr.h:24
const std::string & parentName() const
The name of the parent.
Definition: GaudiHandle.h:49
BaseToolHandle(const IInterface *parent=nullptr, bool createIf=true)
Definition: ToolHandle.h:69
void setParentName(const std::string &parent)
The name of the parent.
Definition: GaudiHandle.h:72
static std::string toolComponentType(const IInterface *parent)
Definition: ToolHandle.h:43
StatusCode release() const
Release the component.
Definition: GaudiHandle.h:215
StatusCode i_retrieve(IAlgTool *&algTool) const override
Definition: ToolHandle.h:204
StatusCode initialize(const std::string &serviceName, const std::string &theParentName)
Definition: ServiceHandle.h:46
virtual const std::string & name() const =0
Retrieve the name of the instance.
STL class.
StatusCode release(T *algTool) const override
Do the real release of the AlgTool.
Definition: ToolHandle.h:192
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:26
const IInterface * parent() const
Definition: ToolHandle.h:38
Definition of the basic interface.
Definition: IInterface.h:234
StatusCode retrieve() const
Retrieve the component.
Definition: GaudiHandle.h:205
StatusCode retrieve() const
Retrieve the Service.
Definition: ServiceHandle.h:56
StatusCode retrieve(IAlgTool *&tool) const
Definition: ToolHandle.h:78
bool createIf() const
Definition: ToolHandle.h:36
StatusCode retrieve() const
Retrieve the AlgTool.
Definition: ToolHandle.h:174
bool isPublic() const
Definition: ToolHandle.h:34
Handle to be used in lieu of naked pointers to tools.
Definition: PropertyMgr.h:22
StatusCode initialize(const std::string &toolTypeAndName, const IInterface *parent=nullptr, bool createIf=true)
Definition: ToolHandle.h:157
Base class from which all concrete algorithm classes should be derived.
Definition: Algorithm.h:74
ToolHandleArray(const IInterface *parent=0, bool createIf=true)
Constructor which creates and empty list.
Definition: ToolHandle.h:255
IInterface compliant class extending IInterface with the name() method.
const IInterface * m_parent
Definition: ToolHandle.h:54
Base class from which all the concrete tool classes should be derived.
Definition: AlgTool.h:45
The interface implemented by the AlgTool base class.
Definition: IAlgTool.h:23
virtual ~ToolHandleInfo()=default
virtual bool push_back(const std::string &toolTypeAndName)
Add a handle to the array with given tool type and name.
Definition: ToolHandle.h:265
Base class to handles to be used in lieu of naked pointers to various Gaudi components.
Definition: GaudiHandle.h:93
std::string typeAndName() const
The full type and name: "type/name".
Definition: GaudiHandle.h:120
ToolHandleArray(const std::vector< std::string > &myTypesAndNames, const IInterface *parent=0, bool createIf=true)
Generic constructor.
Definition: ToolHandle.h:243
Base class for all services.
Definition: Service.h:36
STL class.
static const InterfaceID & interfaceID()
Return an instance of InterfaceID identifying the interface.
Definition: IInterface.h:243
virtual ~BaseToolHandle()
Definition: ToolHandle.h:76
ToolHandle(const std::string &toolTypeAndName, const IInterface *parent=nullptr, bool createIf=true)
Create a handle ('smart pointer') to a tool.
Definition: ToolHandle.h:146
T * get() const
Return the wrapped pointer, not calling retrieve() if null.
Definition: GaudiHandle.h:236
General info and helper functions for toolhandles and arrays.
Definition: ToolHandle.h:25
StatusCode release() const
Release the AlgTool.
Definition: ToolHandle.h:180
ToolHandleInfo(const IInterface *parent=nullptr, bool createIf=true)
Definition: ToolHandle.h:27
T is the concrete handle type, e.g.
Definition: GaudiHandle.h:384