The Gaudi Framework  v30r0 (c919700c)
ToolHandle.h
Go to the documentation of this file.
1 #ifndef GAUDIKERNEL_TOOLHANDLE_H
2 #define GAUDIKERNEL_TOOLHANDLE_H
3 
4 // Includes
6 #include "GaudiKernel/IAlgTool.h"
8 #include "GaudiKernel/IToolSvc.h"
10 
11 #include <stdexcept>
12 #include <string>
13 #include <type_traits>
14 #include <vector>
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  ToolHandleInfo( const IInterface* parent = nullptr, bool createIf = true )
31  {
32  }
33 
34 public:
35  virtual ~ToolHandleInfo() = default;
36 
37  bool isPublic() const noexcept { return !m_parent; }
38 
39  bool createIf() const noexcept { return m_createIf; }
40 
41  const IInterface* parent() const noexcept { return m_parent; }
42 
43  //
44  // Some helper functions
45  //
46 
47  static std::string toolComponentType( const IInterface* parent ) { return parent ? "PrivateTool" : "PublicTool"; }
48 
50  {
51  auto* pNamed = ( parent ? dynamic_cast<const INamedInterface*>( parent ) : nullptr );
52  return ( !parent ? "ToolSvc" : ( pNamed ? pNamed->name() : "" ) );
53  }
54 
55 protected:
56  const IInterface* m_parent = nullptr;
57  bool m_createIf{true};
58 };
59 
68 {
69 
70 protected:
71  BaseToolHandle( const IInterface* parent = nullptr, bool createIf = true ) : ToolHandleInfo( parent, createIf ) {}
72 
73  virtual StatusCode i_retrieve( IAlgTool*& ) const = 0;
74 
75 public:
76  StatusCode retrieve( IAlgTool*& tool ) const { return i_retrieve( tool ); }
77 
78  virtual StatusCode retrieve() const = 0;
79 
80  const IAlgTool* get() const { return getAsIAlgTool(); }
81 
82  IAlgTool* get() { return getAsIAlgTool(); }
83 
84  virtual std::string typeAndName() const = 0;
85 
87  inline bool isEnabled() const
88  {
89  // the handle is considered enabled if the type/name is valid and the
90  // enabled flag was set to true, or it was already retrieved
91  return ( m_enabled && !typeAndName().empty() ) || get();
92  }
93 
94  inline void enable() { m_enabled = true; }
95 
96  inline void disable() { m_enabled = false; }
97 
98  inline bool setEnabled( const bool flag )
99  {
100  auto old = m_enabled;
101  m_enabled = flag;
102  return old;
103  }
104 
105 protected:
106  virtual const IAlgTool* getAsIAlgTool() const = 0;
107 
108  virtual IAlgTool* getAsIAlgTool() = 0;
109 
110  bool m_enabled = true;
111 };
112 
123 template <class T>
124 class ToolHandle : public BaseToolHandle, public GaudiHandle<T>
125 {
126 
127  friend class Algorithm;
128  friend class AlgTool;
129  friend class Service;
130 
131 public:
135  ToolHandle( const IInterface* parent = nullptr, bool createIf = true )
138  , m_pToolSvc( "ToolSvc", GaudiHandleBase::parentName() )
139  {
140  }
141 
146  : BaseToolHandle( other.parent(), other.createIf() )
147  , GaudiHandle<CT>( other )
148  , m_pToolSvc( "ToolSvc", GaudiHandleBase::parentName() )
149  {
150  }
151 
152 public:
153 //
154 // Constructors etc.
155 //
156 
176 #if defined( TOOLHANDLE_DEPR_WARN )
177 // warn about using deprecated explicit ToolHandle construction
178 #pragma message( "Untracked ToolHandle: Migrate explicit DataHandle constructor to declareTool Algorithm Property" )
179 
180  __attribute__( ( deprecated ) )
181 
182 #endif
183  ToolHandle( const std::string& toolTypeAndName, const IInterface* parent = nullptr, bool createIf = true )
185  , GaudiHandle<T>( toolTypeAndName, toolComponentType( parent ), toolParentName( parent ) )
186  , m_pToolSvc( "ToolSvc", GaudiHandleBase::parentName() )
187  {
188  }
189 
192  template <class OWNER, typename = std::enable_if_t<std::is_base_of<IProperty, OWNER>::value>>
193  inline ToolHandle( OWNER* owner, std::string name, std::string toolType, std::string doc = "" ) : ToolHandle( owner )
194  {
195  // convert name and type to a valid type/name string
196  // - if type does not contain '/' use type/name
197  // - otherwise type is already a type/name string
198  if ( !toolType.empty() and toolType.find( '/' ) == std::string::npos ) {
199  toolType += '/';
200  toolType += name;
201  }
202  owner->declareTool( *this, std::move( toolType ) ).ignore();
203  auto p = owner->OWNER::PropertyHolderImpl::declareProperty( std::move( name ), *this, std::move( doc ) );
204  p->template setOwnerType<OWNER>();
205  }
206 
207 public:
208  StatusCode initialize( const std::string& toolTypeAndName, const IInterface* parent = nullptr, bool createIf = true )
209  {
210 
211  GaudiHandleBase::setTypeAndName( toolTypeAndName );
214 
215  m_parent = parent;
217 
218  return m_pToolSvc.initialize( "ToolSvc", GaudiHandleBase::parentName() );
219  }
220 
223  StatusCode retrieve() const override
224  { // not really const, because it updates m_pObject
225  return GaudiHandle<T>::retrieve();
226  }
227 
231  { // not really const, because it updates m_pObject
232  return GaudiHandle<T>::release();
233  }
234 
236  StatusCode retrieve( T*& algTool ) const override
237  {
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 { return m_pToolSvc->releaseTool( details::nonConst( algTool ) ); }
245 
246  std::string typeAndName() const override { return GaudiHandleBase::typeAndName(); }
247 
248  typename std::add_const<T>::type* get() const { return GaudiHandle<T>::get(); }
249 
250  T* get() { return GaudiHandle<T>::get(); }
251 
252 // Allow access to non-const Tool methods of const ToolHandle
253 #ifdef ALLOW_TOOLHANDLE_NONCONSTNESS
254  T* operator->() { return GaudiHandle<T>::operator->(); }
255  T& operator*() { return *( GaudiHandle<T>::operator->() ); }
256 
257  T* operator->() const { return details::nonConst( GaudiHandle<T>::operator->() ); }
258  T& operator*() const { return *( details::nonConst( GaudiHandle<T>::operator->() ) ); }
259 #endif
260 
261 #ifdef ATLAS
262  [[deprecated( "FIXME!! should not call non-const method from a const ToolHandle" )]] ToolHandle<T>& unConst() const
263  {
264  return const_cast<ToolHandle<T>&>( *this );
265  }
266 #endif
267 
268 protected:
269  const IAlgTool* getAsIAlgTool() const override
270  {
271  // const cast to support T being const
272  return GaudiHandle<T>::get();
273  }
274 
276  {
277  // const cast to support T being const
279  }
280 
281  StatusCode i_retrieve( IAlgTool*& algTool ) const override
282  {
283  return m_pToolSvc->retrieve( typeAndName(), IAlgTool::interfaceID(), algTool, ToolHandleInfo::parent(),
285  }
286 
287 private:
288  //
289  // Private data members
290  //
292 };
293 
297 template <class T>
298 class PublicToolHandle : public ToolHandle<T>
299 {
300 public:
301  PublicToolHandle( bool createIf = true ) : ToolHandle<T>( nullptr, createIf ) {}
302  PublicToolHandle( const char* toolTypeAndName, bool createIf = true )
303  : PublicToolHandle{std::string{toolTypeAndName}, createIf}
304  {
305  }
306  PublicToolHandle( const std::string& toolTypeAndName, bool createIf = true )
307  : ToolHandle<T>( toolTypeAndName, nullptr, createIf )
308  {
309  }
310 
314  std::enable_if_t<std::is_const<CT>::value && !std::is_same<CT, NCT>::value>* = nullptr )
315  : ToolHandle<T>( static_cast<const ToolHandle<NCT>&>( other ) )
316  {
317  }
318 
321  template <class OWNER, typename = typename std::enable_if<std::is_base_of<IProperty, OWNER>::value>::type>
322  inline PublicToolHandle( OWNER* owner, std::string name, std::string toolType, std::string doc = "" )
323  : PublicToolHandle()
324  {
325  // convert name and type to a valid type/name string
326  // - if type does not contain '/' use type/name
327  // - otherwise type is already a type/name string
328  if ( !toolType.empty() and toolType.find( '/' ) == std::string::npos ) {
329  toolType += '/';
330  toolType += name;
331  }
332  owner->declareTool( *this, std::move( toolType ) ).ignore();
333  auto p = owner->OWNER::PropertyHolderImpl::declareProperty( std::move( name ), *this, std::move( doc ) );
334  p->template setOwnerType<OWNER>();
335  }
336 };
337 
338 //-------------------------------------------------------------------------//
339 
350 template <class T>
351 class ToolHandleArray : public ToolHandleInfo, public GaudiHandleArray<ToolHandle<T>>
352 {
353 public:
354  //
355  // Constructors
356  //
363  ToolHandleArray( const std::vector<std::string>& myTypesAndNames, const IInterface* parent = nullptr,
364  bool createIf = true )
368  {
369  }
370 
375  ToolHandleArray( const IInterface* parent = nullptr, bool createIf = true )
379  {
380  }
381 
386  bool push_back( const std::string& toolTypeAndName ) override
387  {
388  ToolHandle<T> handle( toolTypeAndName, ToolHandleInfo::parent(), ToolHandleInfo::createIf() );
390  return true;
391  }
392 
394  bool push_back( const ToolHandle<T>& myHandle ) override { return push_back( myHandle.typeAndName() ); }
395 
398  template <class OWNER, typename = std::enable_if_t<std::is_base_of<IProperty, OWNER>::value>>
399  inline ToolHandleArray( OWNER* owner, std::string name, const std::vector<std::string>& typesAndNames = {},
400  std::string doc = "" )
401  : ToolHandleArray( typesAndNames, owner )
402  {
403  owner->addToolsArray( *this );
404  auto p = owner->OWNER::PropertyHolderImpl::declareProperty( std::move( name ), *this, std::move( doc ) );
405  p->template setOwnerType<OWNER>();
406  }
407 };
408 
412 template <class T>
414 {
415 public:
416  PublicToolHandleArray( bool createIf = true ) : ToolHandleArray<T>( nullptr, createIf ) {}
417  PublicToolHandleArray( const std::vector<std::string>& typesAndNames, bool createIf = true )
418  : ToolHandleArray<T>( typesAndNames, nullptr, createIf )
419  {
420  }
421 
424  template <class OWNER, typename = typename std::enable_if<std::is_base_of<IProperty, OWNER>::value>::type>
425  inline PublicToolHandleArray( OWNER* owner, std::string name, const std::vector<std::string>& typesAndNames = {},
426  std::string doc = "" )
427  : PublicToolHandleArray( typesAndNames )
428  {
429  owner->addToolsArray( *this );
430  auto p = owner->OWNER::PropertyHolderImpl::declareProperty( std::move( name ), *this, std::move( doc ) );
431  p->template setOwnerType<OWNER>();
432  }
433 };
434 
435 template <class T>
436 inline std::ostream& operator<<( std::ostream& os, const ToolHandle<T>& handle )
437 {
438  return operator<<( os, static_cast<const GaudiHandleInfo&>( handle ) );
439 }
440 
441 template <class T>
442 inline std::ostream& operator<<( std::ostream& os, const ToolHandleArray<T>& handle )
443 {
444  return operator<<( os, static_cast<const GaudiHandleInfo&>( handle ) );
445 }
446 
447 #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:135
Helper class to construct ToolHandle instances for public tools via the auto registering constructor...
Definition: ToolHandle.h:413
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:167
#define __attribute__(x)
Definition: System.cpp:89
T empty(T...args)
const IInterface * parent() const noexcept
Definition: ToolHandle.h:41
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:399
ServiceHandle< IToolSvc > m_pToolSvc
Definition: ToolHandle.h:291
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
StatusCode retrieve() const override
Retrieve the AlgTool.
Definition: ToolHandle.h:223
bool setEnabled(const bool flag)
Definition: ToolHandle.h:98
bool isPublic() const noexcept
Definition: ToolHandle.h:37
void disable()
Definition: ToolHandle.h:96
void setComponentType(const std::string &componentType)
The component type.
Definition: GaudiHandle.h:72
bool isEnabled() const
Helper to check if the ToolHandle should be retrieved.
Definition: ToolHandle.h:87
static std::string toolParentName(const IInterface *parent)
Definition: ToolHandle.h:49
PublicToolHandle(const PublicToolHandle< NCT > &other, std::enable_if_t< std::is_const< CT >::value &&!std::is_same< CT, NCT >::value > *=nullptr)
Copy constructor from a non const T to const T tool handle.
Definition: ToolHandle.h:313
Non-templated base class for actual ToolHandle<T>.
Definition: ToolHandle.h:67
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:57
PublicToolHandleArray(const std::vector< std::string > &typesAndNames, bool createIf=true)
Definition: ToolHandle.h:417
BaseToolHandle(const IInterface *parent=nullptr, bool createIf=true)
Definition: ToolHandle.h:71
void setParentName(const std::string &parent)
The name of the parent.
Definition: GaudiHandle.h:75
bool push_back(const std::string &toolTypeAndName) override
Add a handle to the array with given tool type and name.
Definition: ToolHandle.h:386
static std::string toolComponentType(const IInterface *parent)
Definition: ToolHandle.h:47
StatusCode release() const
Release the component.
Definition: GaudiHandle.h:237
void push_back(Container &c, const Value &v, std::true_type)
PublicToolHandle(const std::string &toolTypeAndName, bool createIf=true)
Definition: ToolHandle.h:306
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:144
StatusCode i_retrieve(IAlgTool *&algTool) const override
Definition: ToolHandle.h:281
PublicToolHandle(const char *toolTypeAndName, bool createIf=true)
Definition: ToolHandle.h:302
STL class.
ToolHandleArray(const IInterface *parent=nullptr, bool createIf=true)
Constructor which creates and empty list.
Definition: ToolHandle.h:375
std::remove_const_t< T > * nonConst(T *p)
Cast a pointer to a non const type.
Definition: GaudiHandle.h:20
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:26
std::string typeAndName() const override
Definition: ToolHandle.h:246
Definition of the basic interface.
Definition: IInterface.h:277
T * get()
Return the wrapped pointer, not calling retrieve() if null.
Definition: GaudiHandle.h:261
StatusCode retrieve() const
Retrieve the component.
Definition: GaudiHandle.h:223
StatusCode retrieve(IAlgTool *&tool) const
Definition: ToolHandle.h:76
void enable()
Definition: ToolHandle.h:94
T move(T...args)
PublicToolHandle(bool createIf=true)
Definition: ToolHandle.h:301
IAlgTool * getAsIAlgTool() override
Definition: ToolHandle.h:275
T * operator->()
Definition: GaudiHandle.h:275
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:208
Base class from which all concrete algorithm classes should be derived.
Definition: Algorithm.h:79
IInterface compliant class extending IInterface with the name() method.
T find(T...args)
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:322
const IInterface * m_parent
Definition: ToolHandle.h:56
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:269
Helper class to construct ToolHandle instances for public tools via the auto registering constructor...
Definition: ToolHandle.h:298
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:193
virtual ~ToolHandleInfo()=default
StatusCode retrieve(T *&algTool) const override
Do the real retrieval of the AlgTool.
Definition: ToolHandle.h:236
ToolHandleArray(const std::vector< std::string > &myTypesAndNames, const IInterface *parent=nullptr, bool createIf=true)
Generic constructor.
Definition: ToolHandle.h:363
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:121
Base class for all services.
Definition: Service.h:36
PublicToolHandleArray(bool createIf=true)
Definition: ToolHandle.h:416
STL class.
static const InterfaceID & interfaceID()
Return an instance of InterfaceID identifying the interface.
Definition: IInterface.h:287
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:183
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:425
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:394
StatusCode release() const
Release the AlgTool.
Definition: ToolHandle.h:230
ToolHandleInfo(const IInterface *parent=nullptr, bool createIf=true)
Definition: ToolHandle.h:29
T is the concrete handle type, e.g.
Definition: GaudiHandle.h:416
bool createIf() const noexcept
Definition: ToolHandle.h:39