Loading [MathJax]/extensions/tex2jax.js
The Gaudi Framework  v28r2p1 (f1a77ff4)
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules 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/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 {
27 
28 protected:
29 
30  ToolHandleInfo(const IInterface* parent = nullptr, bool createIf = true )
32  {}
33 
34 public:
35 
36  virtual ~ToolHandleInfo() = default;
37 
38  bool isPublic() const noexcept { return !m_parent; }
39 
40  bool createIf() const noexcept { return m_createIf; }
41 
42  const IInterface* parent() const noexcept { return m_parent; }
43 
44  //
45  // Some helper functions
46  //
47 
49  {
50  return parent ? "PrivateTool" : "PublicTool";
51  }
52 
54  {
55  auto * pNamed = ( parent ? dynamic_cast<const INamedInterface*>(parent) : nullptr );
56  return ( !parent ? "ToolSvc" : ( pNamed ? pNamed->name() : "" ) );
57  }
58 
59 protected:
60 
61  const IInterface* m_parent = nullptr;
62  bool m_createIf{true};
63 
64 };
65 
74 {
75 
76 protected:
77 
78  BaseToolHandle(const IInterface* parent = nullptr, bool createIf = true )
80  {}
81 
82  virtual StatusCode i_retrieve(IAlgTool*&) const = 0;
83 
84 public:
85 
86  StatusCode retrieve(IAlgTool*& tool) const {
87  return i_retrieve(tool);
88  }
89 
90  const IAlgTool * get() const { return getAsIAlgTool(); }
91 
92  IAlgTool * get() { return getAsIAlgTool(); }
93 
94  virtual std::string typeAndName() const = 0;
95 
96 protected:
97 
98  virtual const IAlgTool * getAsIAlgTool() const = 0;
99 
100  virtual IAlgTool * getAsIAlgTool() = 0;
101 
102 };
103 
114 template< class T >
115 class ToolHandle : public BaseToolHandle, public GaudiHandle<T>
116 {
117 
118  friend class Algorithm;
119  friend class AlgTool;
120  friend class Service;
121 
122 public:
123 
127  ToolHandle(const IInterface* parent = nullptr, bool createIf = true)
129  GaudiHandle<T>( GaudiHandle<T>::getDefaultType(),
132  m_pToolSvc( "ToolSvc", GaudiHandleBase::parentName() )
133  { }
134 
136  template< typename CT = T,
137  typename NCT = typename std::remove_const<T>::type >
140  !std::is_same<CT,NCT>::value >::type * = nullptr )
141  : BaseToolHandle( other.parent(), other.createIf() ),
142  GaudiHandle<CT>( other ),
143  m_pToolSvc( "ToolSvc", GaudiHandleBase::parentName() )
144  { }
145 
146  public:
147  //
148  // Constructors etc.
149  //
150 
170 #if defined(TOOLHANDLE_DEPR_WARN)
171  //warn about using deprecated explicit ToolHandle construction
172 #pragma message("Untracked ToolHandle: Migrate explicit DataHandle constructor to declareTool Algorithm Property")
173 
175 
176 #endif
177  ToolHandle(const std::string& toolTypeAndName,
178  const IInterface* parent = nullptr, bool createIf = true )
180  GaudiHandle<T>( toolTypeAndName,
183  m_pToolSvc( "ToolSvc", GaudiHandleBase::parentName() )
184  { }
185 
186 public:
187 
188  StatusCode initialize(const std::string& toolTypeAndName,
189  const IInterface* parent = nullptr, bool createIf = true)
190  {
191 
192  GaudiHandleBase::setTypeAndName(toolTypeAndName);
195 
196  m_parent = parent;
198 
199  return m_pToolSvc.initialize("ToolSvc", GaudiHandleBase::parentName());
200  }
201 
204  StatusCode retrieve() const { // not really const, because it updates m_pObject
205  return GaudiHandle<T>::retrieve();
206  }
207 
210  StatusCode release() const { // not really const, because it updates m_pObject
211  return GaudiHandle<T>::release();
212  }
213 
215  StatusCode retrieve( T*& algTool ) const override {
216  IAlgTool* iface = nullptr;
217  algTool = i_retrieve(iface) ? dynamic_cast<T*>(iface) : nullptr;
218  return algTool ? StatusCode::SUCCESS : StatusCode::FAILURE;
219  }
220 
222  StatusCode release( T* algTool ) const override
223  {
224  return m_pToolSvc->releaseTool( this->nonConst(algTool) );
225  }
226 
227  std::string typeAndName() const override {
229  }
230 
231  typename std::add_const<T>::type * get() const { return GaudiHandle<T>::get(); }
232 
233  T * get() { return GaudiHandle<T>::get(); }
234 
235  // Allow access to non-const Tool methods of const ToolHandle
236  #ifdef ALLOW_TOOLHANDLE_NONCONSTNESS
237  T * operator->() {
238  return GaudiHandle<T>::operator->();
239  }
240  T & operator*() {
241  return * ( GaudiHandle<T>::operator->() );
242  }
243 
244  T * operator->() const {
246  }
247  T & operator*() const {
249  }
250  #endif
251 
252  #ifdef ATLAS
253 [[deprecated("FIXME!! should not call non-const method from a const ToolHandle")]]
254  ToolHandle<T>& unConst() const {
255  return const_cast<ToolHandle<T>&> (*this);
256  }
257  #endif
258 
259 protected:
260 
261  const IAlgTool * getAsIAlgTool() const override
262  {
263  // const cast to support T being const
264  return GaudiHandle<T>::get();
265  }
266 
267  IAlgTool * getAsIAlgTool() override
268  {
269  // const cast to support T being const
270  return this->nonConst( GaudiHandle<T>::get() );
271  }
272 
273  StatusCode i_retrieve(IAlgTool*& algTool) const override
274  {
275  return m_pToolSvc->retrieve( typeAndName(), IAlgTool::interfaceID(),
276  algTool,
278  }
279 
280 private:
281  //
282  // Private data members
283  //
285 };
286 
287 //-------------------------------------------------------------------------//
288 
289 
290 
301 template < class T >
302 class ToolHandleArray : public ToolHandleInfo, public GaudiHandleArray< ToolHandle<T> > {
303 public:
304  //
305  // Constructors
306  //
314  const IInterface* parent = 0, bool createIf = true )
316  GaudiHandleArray< ToolHandle<T> >( myTypesAndNames,
319  {}
320 
325  ToolHandleArray( const IInterface* parent = 0, bool createIf = true )
329  { }
330 
335  bool push_back( const std::string& toolTypeAndName ) override {
336  ToolHandle<T> handle( toolTypeAndName,
340  return true;
341  }
342 
344  bool push_back( const ToolHandle<T>& myHandle ) override {
345  return push_back( myHandle.typeAndName() );
346  }
347 
348 };
349 
350 
351 template <class T>
352 inline std::ostream& operator<<( std::ostream& os, const ToolHandle<T>& handle ) {
353  return operator<<(os, static_cast<const GaudiHandleInfo&>(handle) );
354 }
355 
356 
357 template <class T>
358 inline std::ostream& operator<<( std::ostream& os, const ToolHandleArray<T>& handle ) {
359  return operator<<(os, static_cast<const GaudiHandleInfo&>(handle) );
360 }
361 
362 #endif // ! GAUDIKERNEL_TOOLHANDLE_H
ToolHandle(const IInterface *parent=nullptr, bool createIf=true)
Constructor for a tool with default tool type and name.
Definition: ToolHandle.h:127
The interface implemented by the IToolSvc base class.
Definition: IToolSvc.h:19
Handle to be used in lieu of naked pointers to gaudis.
Definition: GaudiHandle.h:172
#define __attribute__(x)
Definition: System.cpp:72
const IInterface * parent() const noexcept
Definition: ToolHandle.h:42
std::remove_const< CLASS >::type * nonConst(CLASS *p) const
Cast a pointer to a non const type.
Definition: GaudiHandle.h:321
ServiceHandle< IToolSvc > m_pToolSvc
Definition: ToolHandle.h:284
void setTypeAndName(std::string myTypeAndName)
The component "type/name" string.
Definition: GaudiHandle.cpp:9
StatusCode release(T *algTool) const override
Do the real release of the AlgTool.
Definition: ToolHandle.h:222
bool isPublic() const noexcept
Definition: ToolHandle.h:38
void setComponentType(const std::string &componentType)
The component type.
Definition: GaudiHandle.h:68
static std::string toolParentName(const IInterface *parent)
Definition: ToolHandle.h:53
struct deprecated("use MergingTransformer instead")]] ListTransformer
Non-templated base class for actual ToolHandle<T>.
Definition: ToolHandle.h:73
Array of Handles to be used in lieu of vector of naked pointers to tools.
const std::string & parentName() const
The name of the parent.
Definition: GaudiHandle.h:50
BaseToolHandle(const IInterface *parent=nullptr, bool createIf=true)
Definition: ToolHandle.h:78
void setParentName(const std::string &parent)
The name of the parent.
Definition: GaudiHandle.h:73
bool push_back(const std::string &toolTypeAndName) override
Add a handle to the array with given tool type and name.
Definition: ToolHandle.h:335
static std::string toolComponentType(const IInterface *parent)
Definition: ToolHandle.h:48
StatusCode release() const
Release the component.
Definition: GaudiHandle.h:240
void push_back(Container &c, const Value &v, std::true_type)
ToolHandle(const ToolHandle< NCT > &other, typename std::enable_if< std::is_const< CT >::value &&!std::is_same< CT, NCT >::value >::type *=nullptr)
Copy constructor from a non const T to const T tool handle.
Definition: ToolHandle.h:138
StatusCode i_retrieve(IAlgTool *&algTool) const override
Definition: ToolHandle.h:273
STL class.
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:26
std::string typeAndName() const override
Definition: ToolHandle.h:227
Definition of the basic interface.
Definition: IInterface.h:234
T * get()
Return the wrapped pointer, not calling retrieve() if null.
Definition: GaudiHandle.h:262
StatusCode retrieve() const
Retrieve the component.
Definition: GaudiHandle.h:228
StatusCode retrieve(IAlgTool *&tool) const
Definition: ToolHandle.h:86
IAlgTool * getAsIAlgTool() override
Definition: ToolHandle.h:267
T * operator->()
Definition: GaudiHandle.h:279
StatusCode retrieve() const
Retrieve the AlgTool.
Definition: ToolHandle.h:204
Handle to be used in lieu of naked pointers to tools.
StatusCode initialize(const std::string &toolTypeAndName, const IInterface *parent=nullptr, bool createIf=true)
Definition: ToolHandle.h:188
Base class from which all concrete algorithm classes should be derived.
Definition: Algorithm.h:78
ToolHandleArray(const IInterface *parent=0, bool createIf=true)
Constructor which creates and empty list.
Definition: ToolHandle.h:325
IInterface compliant class extending IInterface with the name() method.
const IInterface * m_parent
Definition: ToolHandle.h:61
Base class from which all the concrete tool classes should be derived.
Definition: AlgTool.h:48
The interface implemented by the AlgTool base class.
Definition: IAlgTool.h:23
const IAlgTool * getAsIAlgTool() const override
Definition: ToolHandle.h:261
virtual ~ToolHandleInfo()=default
StatusCode retrieve(T *&algTool) const override
Do the real retrieval of the AlgTool.
Definition: ToolHandle.h:215
Base class to handles to be used in lieu of naked pointers to various Gaudi components.
Definition: GaudiHandle.h:94
std::string typeAndName() const
The full type and name: "type/name".
Definition: GaudiHandle.h:121
ToolHandleArray(const std::vector< std::string > &myTypesAndNames, const IInterface *parent=0, bool createIf=true)
Generic constructor.
Definition: ToolHandle.h:313
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
ToolHandle(const std::string &toolTypeAndName, const IInterface *parent=nullptr, bool createIf=true)
Create a handle (&#39;smart pointer&#39;) to a tool.
Definition: ToolHandle.h:177
General info and helper functions for toolhandles and arrays.
Definition: ToolHandle.h:25
bool push_back(const ToolHandle< T > &myHandle) override
Ensure that for added handles the parent and creatIf are taken from this array.
Definition: ToolHandle.h:344
StatusCode release() const
Release the AlgTool.
Definition: ToolHandle.h:210
ToolHandleInfo(const IInterface *parent=nullptr, bool createIf=true)
Definition: ToolHandle.h:30
T is the concrete handle type, e.g.
Definition: GaudiHandle.h:428
bool createIf() const noexcept
Definition: ToolHandle.h:40