The Gaudi Framework  v28r3 (cc1cf868)
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 
188  template <class OWNER,
190  inline ToolHandle( OWNER* owner, std::string name, std::string toolType = "", std::string doc = "" )
191  : ToolHandle( owner )
192  {
193  // convert name and type to a valid type/name string
194  // - if type is not specified use only name
195  // - if type does not contain '/' use type/name
196  // - otherwise type is already a type/name string
197  if ( toolType.empty() ) {
198  toolType = name;
199  } else if ( toolType.find( '/' ) == std::string::npos ) {
200  toolType += '/';
201  toolType += name;
202  }
203  owner->declareTool( *this, std::move(toolType) ).ignore();
204  auto p = owner->OWNER::PropertyHolderImpl::declareProperty( std::move(name), *this, std::move(doc) );
205  p->template setOwnerType<OWNER>();
206  }
207 
208 public:
209 
210  StatusCode initialize(const std::string& toolTypeAndName,
211  const IInterface* parent = nullptr, bool createIf = true)
212  {
213 
214  GaudiHandleBase::setTypeAndName(toolTypeAndName);
217 
218  m_parent = parent;
220 
221  return m_pToolSvc.initialize("ToolSvc", GaudiHandleBase::parentName());
222  }
223 
226  StatusCode retrieve() const { // not really const, because it updates m_pObject
227  return GaudiHandle<T>::retrieve();
228  }
229 
232  StatusCode release() const { // not really const, because it updates m_pObject
233  return GaudiHandle<T>::release();
234  }
235 
237  StatusCode retrieve( T*& algTool ) const override {
238  IAlgTool* iface = nullptr;
239  algTool = i_retrieve(iface) ? dynamic_cast<T*>(iface) : nullptr;
240  return algTool ? StatusCode::SUCCESS : StatusCode::FAILURE;
241  }
242 
244  StatusCode release( T* algTool ) const override
245  {
246  return m_pToolSvc->releaseTool( this->nonConst(algTool) );
247  }
248 
249  std::string typeAndName() const override {
251  }
252 
253  typename std::add_const<T>::type * get() const { return GaudiHandle<T>::get(); }
254 
255  T * get() { return GaudiHandle<T>::get(); }
256 
257  // Allow access to non-const Tool methods of const ToolHandle
258  #ifdef ALLOW_TOOLHANDLE_NONCONSTNESS
259  T * operator->() {
261  }
262  T & operator*() {
263  return * ( GaudiHandle<T>::operator->() );
264  }
265 
266  T * operator->() const {
268  }
269  T & operator*() const {
271  }
272  #endif
273 
274  #ifdef ATLAS
275 [[deprecated("FIXME!! should not call non-const method from a const ToolHandle")]]
276  ToolHandle<T>& unConst() const {
277  return const_cast<ToolHandle<T>&> (*this);
278  }
279  #endif
280 
281 protected:
282 
283  const IAlgTool * getAsIAlgTool() const override
284  {
285  // const cast to support T being const
286  return GaudiHandle<T>::get();
287  }
288 
289  IAlgTool * getAsIAlgTool() override
290  {
291  // const cast to support T being const
292  return this->nonConst( GaudiHandle<T>::get() );
293  }
294 
295  StatusCode i_retrieve(IAlgTool*& algTool) const override
296  {
297  return m_pToolSvc->retrieve( typeAndName(), IAlgTool::interfaceID(),
298  algTool,
300  }
301 
302 private:
303  //
304  // Private data members
305  //
307 };
308 
312 template< class T >
313 class PublicToolHandle: public ToolHandle<T> {
314 public:
315  PublicToolHandle(bool createIf = true): ToolHandle<T>(nullptr, createIf) {}
316  PublicToolHandle(const char* toolTypeAndName, bool createIf = true):
317  PublicToolHandle{std::string{toolTypeAndName}, createIf} {}
318  PublicToolHandle(const std::string& toolTypeAndName, bool createIf = true ):
319  ToolHandle<T>(toolTypeAndName, nullptr, createIf) {}
320 
322  template< typename CT = T,
323  typename NCT = typename std::remove_const<T>::type >
326  !std::is_same<CT,NCT>::value >::type * = nullptr ):
327  ToolHandle<T>( static_cast<const ToolHandle<NCT>&>( other ) ) {}
328 
331  template <class OWNER,
333  inline PublicToolHandle( OWNER* owner, std::string name, std::string toolType = "", std::string doc = "" )
334  : PublicToolHandle()
335  {
336  // convert name and type to a valid type/name string
337  // - if type is not specified use only name
338  // - if type does not contain '/' use type/name
339  // - otherwise type is already a type/name string
340  if ( toolType.empty() ) {
341  toolType = name;
342  } else if ( toolType.find( '/' ) == std::string::npos ) {
343  toolType += '/';
344  toolType += name;
345  }
346  owner->declareTool( *this, std::move(toolType) ).ignore();
347  auto p = owner->OWNER::PropertyHolderImpl::declareProperty( std::move(name), *this, std::move(doc) );
348  p->template setOwnerType<OWNER>();
349  }
350 };
351 
352 //-------------------------------------------------------------------------//
353 
354 
355 
366 template < class T >
367 class ToolHandleArray : public ToolHandleInfo, public GaudiHandleArray< ToolHandle<T> > {
368 public:
369  //
370  // Constructors
371  //
379  const IInterface* parent = nullptr, bool createIf = true )
381  GaudiHandleArray< ToolHandle<T> >( myTypesAndNames,
384  {}
385 
390  ToolHandleArray( const IInterface* parent = nullptr, bool createIf = true )
394  { }
395 
400  bool push_back( const std::string& toolTypeAndName ) override {
401  ToolHandle<T> handle( toolTypeAndName,
405  return true;
406  }
407 
409  bool push_back( const ToolHandle<T>& myHandle ) override {
410  return push_back( myHandle.typeAndName() );
411  }
412 
415  template <class OWNER,
417  inline ToolHandleArray( OWNER* owner, std::string name, const std::vector< std::string >& typesAndNames = {}, std::string doc = "" )
418  : ToolHandleArray( typesAndNames, owner )
419  {
420  owner->addToolsArray( *this );
421  auto p = owner->OWNER::PropertyHolderImpl::declareProperty( std::move(name), *this, std::move(doc) );
422  p->template setOwnerType<OWNER>();
423  }
424 
425 };
426 
430 template< class T >
432 public:
434  PublicToolHandleArray(const std::vector< std::string >& typesAndNames, bool createIf = true ):
435  ToolHandleArray<T>(typesAndNames, nullptr, createIf) {}
436 
439  template <class OWNER,
441  inline PublicToolHandleArray( OWNER* owner, std::string name, const std::vector< std::string >& typesAndNames = {}, std::string doc = "" )
442  : PublicToolHandleArray( typesAndNames )
443  {
444  owner->addToolsArray( *this );
445  auto p = owner->OWNER::PropertyHolderImpl::declareProperty( std::move(name), *this, std::move(doc) );
446  p->template setOwnerType<OWNER>();
447  }
448 };
449 
450 
451 template <class T>
452 inline std::ostream& operator<<( std::ostream& os, const ToolHandle<T>& handle ) {
453  return operator<<(os, static_cast<const GaudiHandleInfo&>(handle) );
454 }
455 
456 
457 template <class T>
458 inline std::ostream& operator<<( std::ostream& os, const ToolHandleArray<T>& handle ) {
459  return operator<<(os, static_cast<const GaudiHandleInfo&>(handle) );
460 }
461 
462 #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
Helper class to construct ToolHandle instances for public tools via the auto registering constructor...
Definition: ToolHandle.h:431
The interface implemented by the IToolSvc base class.
Definition: IToolSvc.h:19
ToolHandle(OWNER *owner, std::string name, std::string toolType="", std::string doc="")
Autodeclaring constructor with property name, tool type/name and documentation.
Definition: ToolHandle.h:190
PublicToolHandle(const PublicToolHandle< 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:324
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
ToolHandleArray(OWNER *owner, std::string name, const std::vector< std::string > &typesAndNames={}, std::string doc="")
Autodeclaring constructor with property name, tool type/name and documentation.
Definition: ToolHandle.h:417
ServiceHandle< IToolSvc > m_pToolSvc
Definition: ToolHandle.h:306
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:244
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
PublicToolHandleArray(const std::vector< std::string > &typesAndNames, bool createIf=true)
Definition: ToolHandle.h:434
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:400
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)
PublicToolHandle(const std::string &toolTypeAndName, bool createIf=true)
Definition: ToolHandle.h:318
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:295
PublicToolHandle(const char *toolTypeAndName, bool createIf=true)
Definition: ToolHandle.h:316
STL class.
PublicToolHandle(OWNER *owner, std::string name, std::string toolType="", std::string doc="")
Autodeclaring constructor with property name, tool type/name and documentation.
Definition: ToolHandle.h:333
ToolHandleArray(const IInterface *parent=nullptr, bool createIf=true)
Constructor which creates and empty list.
Definition: ToolHandle.h:390
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:26
std::string typeAndName() const override
Definition: ToolHandle.h:249
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
T move(T...args)
PublicToolHandle(bool createIf=true)
Definition: ToolHandle.h:315
IAlgTool * getAsIAlgTool() override
Definition: ToolHandle.h:289
T * operator->()
Definition: GaudiHandle.h:279
StatusCode retrieve() const
Retrieve the AlgTool.
Definition: ToolHandle.h:226
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:210
Base class from which all concrete algorithm classes should be derived.
Definition: Algorithm.h:78
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:283
Helper class to construct ToolHandle instances for public tools via the auto registering constructor...
Definition: ToolHandle.h:313
virtual ~ToolHandleInfo()=default
StatusCode retrieve(T *&algTool) const override
Do the real retrieval of the AlgTool.
Definition: ToolHandle.h:237
ToolHandleArray(const std::vector< std::string > &myTypesAndNames, const IInterface *parent=nullptr, bool createIf=true)
Generic constructor.
Definition: ToolHandle.h:378
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
Base class for all services.
Definition: Service.h:36
PublicToolHandleArray(bool createIf=true)
Definition: ToolHandle.h:433
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
PublicToolHandleArray(OWNER *owner, std::string name, const std::vector< std::string > &typesAndNames={}, std::string doc="")
Autodeclaring constructor with property name, tool type/name and documentation.
Definition: ToolHandle.h:441
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:409
StatusCode release() const
Release the AlgTool.
Definition: ToolHandle.h:232
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