The Gaudi Framework  v30r3 (a5ef0a68)
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 #include "GaudiKernel/TaggedBool.h"
11 
12 #include <stdexcept>
13 #include <string>
14 #include <type_traits>
15 #include <vector>
16 
17 // forward declarations
18 class IInterface;
19 class IToolSvc;
20 
21 class Algorithm;
22 class AlgTool;
23 class Service;
24 
25 using DisableTool = Gaudi::tagged_bool<class DisableTool_tag>;
26 using EnableTool = Gaudi::tagged_bool<class EnableTool_tag>;
27 
30 {
31 
32 protected:
33  ToolHandleInfo( const IInterface* parent = nullptr, bool createIf = true )
35  {
36  }
37 
38 public:
39  virtual ~ToolHandleInfo() = default;
40 
41  bool isPublic() const noexcept { return !m_parent; }
42 
43  bool createIf() const noexcept { return m_createIf; }
44 
45  const IInterface* parent() const noexcept { return m_parent; }
46 
47  //
48  // Some helper functions
49  //
50 
51  static std::string toolComponentType( const IInterface* parent ) { return parent ? "PrivateTool" : "PublicTool"; }
52 
54  {
55  auto* pNamed = ( parent ? dynamic_cast<const INamedInterface*>( parent ) : nullptr );
56  return ( !parent ? "ToolSvc" : ( pNamed ? pNamed->name() : "" ) );
57  }
58 
59 protected:
60  const IInterface* m_parent = nullptr;
61  bool m_createIf{true};
62 };
63 
72 {
73 
74 protected:
75  BaseToolHandle( const IInterface* parent = nullptr, bool createIf = true ) : ToolHandleInfo( parent, createIf ) {}
76 
77  virtual StatusCode i_retrieve( IAlgTool*& ) const = 0;
78 
79 public:
80  StatusCode retrieve( IAlgTool*& tool ) const { return i_retrieve( tool ); }
81 
82  virtual StatusCode retrieve() const = 0;
83  virtual StatusCode retrieve( DisableTool sd ) = 0;
84  virtual StatusCode retrieve( EnableTool sd ) = 0;
85 
86  const IAlgTool* get() const { return getAsIAlgTool(); }
87 
88  IAlgTool* get() { return getAsIAlgTool(); }
89 
90  virtual std::string typeAndName() const = 0;
91 
93  inline bool isEnabled() const
94  {
95  // the handle is considered enabled if the type/name is valid and the
96  // enabled flag was set to true, or it was already retrieved
97  return ( m_enabled && !typeAndName().empty() ) || get();
98  }
99 
100  inline void enable() { m_enabled = true; }
101 
102  inline void disable() { m_enabled = false; }
103 
104  inline bool setEnabled( const bool flag )
105  {
106  auto old = m_enabled;
107  m_enabled = flag;
108  return old;
109  }
110 
111 protected:
112  virtual const IAlgTool* getAsIAlgTool() const = 0;
113 
114  virtual IAlgTool* getAsIAlgTool() = 0;
115 
116  bool m_enabled = true;
117 };
118 
129 template <class T>
130 class ToolHandle : public BaseToolHandle, public GaudiHandle<T>
131 {
132 
133  friend class Algorithm;
134  friend class AlgTool;
135  friend class Service;
136 
137 public:
141  ToolHandle( const IInterface* parent = nullptr, bool createIf = true )
144  , m_pToolSvc( "ToolSvc", GaudiHandleBase::parentName() )
145  {
146  }
147 
152  : BaseToolHandle( other.parent(), other.createIf() )
153  , GaudiHandle<CT>( other )
154  , m_pToolSvc( "ToolSvc", GaudiHandleBase::parentName() )
155  {
156  }
157 
158 public:
159 //
160 // Constructors etc.
161 //
162 
182 #if defined( TOOLHANDLE_DEPR_WARN )
183 // warn about using deprecated explicit ToolHandle construction
184 #pragma message( "Untracked ToolHandle: Migrate explicit DataHandle constructor to declareTool Algorithm Property" )
185 
186  __attribute__( ( deprecated ) )
187 
188 #endif
189  ToolHandle( const std::string& toolTypeAndName, const IInterface* parent = nullptr, bool createIf = true )
191  , GaudiHandle<T>( toolTypeAndName, toolComponentType( parent ), toolParentName( parent ) )
192  , m_pToolSvc( "ToolSvc", GaudiHandleBase::parentName() )
193  {
194  }
195 
198  template <class OWNER, typename = std::enable_if_t<std::is_base_of<IProperty, OWNER>::value>>
199  inline ToolHandle( OWNER* owner, std::string propName, std::string toolType, std::string doc = "" )
200  : ToolHandle( owner )
201  {
202  // convert name and type to a valid type/name string
203  // - if type does not contain '/' use type/type
204  // - otherwise type is already a type/name string
205  if ( !toolType.empty() and toolType.find( '/' ) == std::string::npos ) {
206  toolType += '/' + toolType;
207  }
208  owner->declareTool( *this, std::move( toolType ) ).ignore();
209  auto p = owner->OWNER::PropertyHolderImpl::declareProperty( std::move( propName ), *this, std::move( doc ) );
210  p->template setOwnerType<OWNER>();
211  }
212 
213 public:
214  StatusCode initialize( const std::string& toolTypeAndName, const IInterface* parent = nullptr, bool createIf = true )
215  {
216 
217  GaudiHandleBase::setTypeAndName( toolTypeAndName );
220 
221  m_parent = parent;
223 
224  return m_pToolSvc.initialize( "ToolSvc", GaudiHandleBase::parentName() );
225  }
226 
229  StatusCode retrieve() const override
230  { // not really const, because it updates m_pObject
231  return GaudiHandle<T>::retrieve();
232  }
233 
235  {
236  if ( isEnabled() && sd == DisableTool{false} ) {
237  return GaudiHandle<T>::retrieve();
238  } else {
239  disable();
240  return StatusCode::SUCCESS;
241  }
242  }
243 
245  {
246  if ( isEnabled() && sd == EnableTool{true} ) {
247  return GaudiHandle<T>::retrieve();
248  } else {
249  disable();
250  return StatusCode::SUCCESS;
251  }
252  }
253 
257  { // not really const, because it updates m_pObject
258  return GaudiHandle<T>::release();
259  }
260 
262  StatusCode retrieve( T*& algTool ) const override
263  {
264  IAlgTool* iface = nullptr;
265  if ( i_retrieve( iface ).isFailure() ) {
266  return StatusCode::FAILURE;
267  }
268 
269  algTool = dynamic_cast<T*>( iface );
270  if ( algTool == nullptr ) {
271  throw GaudiException( "unable to dcast AlgTool " + typeAndName() + " to interface " +
272  System::typeinfoName( typeid( T ) ),
273  typeAndName() + " retrieve", StatusCode::FAILURE );
274  }
275  return StatusCode::SUCCESS;
276  }
277 
279  StatusCode release( T* algTool ) const override { return m_pToolSvc->releaseTool( details::nonConst( algTool ) ); }
280 
281  std::string typeAndName() const override { return GaudiHandleBase::typeAndName(); }
282 
283  typename std::add_const<T>::type* get() const { return GaudiHandle<T>::get(); }
284 
285  T* get() { return GaudiHandle<T>::get(); }
286 
287 // Allow access to non-const Tool methods of const ToolHandle
288 #ifdef ALLOW_TOOLHANDLE_NONCONSTNESS
289  T* operator->() { return GaudiHandle<T>::operator->(); }
290  T& operator*() { return *( GaudiHandle<T>::operator->() ); }
291 
292  T* operator->() const { return details::nonConst( GaudiHandle<T>::operator->() ); }
293  T& operator*() const { return *( details::nonConst( GaudiHandle<T>::operator->() ) ); }
294 #endif
295 
296 #ifdef ATLAS
297  [[deprecated( "FIXME!! should not call non-const method from a const ToolHandle" )]] ToolHandle<T>& unConst() const
298  {
299  return const_cast<ToolHandle<T>&>( *this );
300  }
301 #endif
302 
303 protected:
304  const IAlgTool* getAsIAlgTool() const override
305  {
306  // const cast to support T being const
307  return GaudiHandle<T>::get();
308  }
309 
311  {
312  // const cast to support T being const
314  }
315 
316  StatusCode i_retrieve( IAlgTool*& algTool ) const override
317  {
318  return m_pToolSvc->retrieve( typeAndName(), IAlgTool::interfaceID(), algTool, ToolHandleInfo::parent(),
320  }
321 
322 private:
323  //
324  // Private data members
325  //
327 };
328 
332 template <class T>
333 class PublicToolHandle : public ToolHandle<T>
334 {
335 public:
336  PublicToolHandle( bool createIf = true ) : ToolHandle<T>( nullptr, createIf ) {}
337  PublicToolHandle( const char* toolTypeAndName, bool createIf = true )
338  : PublicToolHandle{std::string{toolTypeAndName}, createIf}
339  {
340  }
341  PublicToolHandle( const std::string& toolTypeAndName, bool createIf = true )
342  : ToolHandle<T>( toolTypeAndName, nullptr, createIf )
343  {
344  }
345 
349  std::enable_if_t<std::is_const<CT>::value && !std::is_same<CT, NCT>::value>* = nullptr )
350  : ToolHandle<T>( static_cast<const ToolHandle<NCT>&>( other ) )
351  {
352  }
353 
356  template <class OWNER, typename = typename std::enable_if<std::is_base_of<IProperty, OWNER>::value>::type>
357  inline PublicToolHandle( OWNER* owner, std::string propName, std::string toolType, std::string doc = "" )
358  : PublicToolHandle()
359  {
360  // convert name and type to a valid type/name string
361  // - if type does not contain '/' use type/type
362  // - otherwise type is already a type/name string
363  if ( !toolType.empty() and toolType.find( '/' ) == std::string::npos ) {
364  toolType += '/' + toolType;
365  }
366  owner->declareTool( *this, std::move( toolType ) ).ignore();
367  auto p = owner->OWNER::PropertyHolderImpl::declareProperty( std::move( propName ), *this, std::move( doc ) );
368  p->template setOwnerType<OWNER>();
369  }
370 };
371 
372 //-------------------------------------------------------------------------//
373 
384 template <class T>
385 class ToolHandleArray : public ToolHandleInfo, public GaudiHandleArray<ToolHandle<T>>
386 {
387 public:
388  //
389  // Constructors
390  //
397  ToolHandleArray( const std::vector<std::string>& myTypesAndNames, const IInterface* parent = nullptr,
398  bool createIf = true )
402  {
403  }
404 
409  ToolHandleArray( const IInterface* parent = nullptr, bool createIf = true )
413  {
414  }
415 
420  bool push_back( const std::string& toolTypeAndName ) override
421  {
422  ToolHandle<T> handle( toolTypeAndName, ToolHandleInfo::parent(), ToolHandleInfo::createIf() );
424  return true;
425  }
426 
428  bool push_back( const ToolHandle<T>& myHandle ) override { return push_back( myHandle.typeAndName() ); }
429 
432  template <class OWNER, typename = std::enable_if_t<std::is_base_of<IProperty, OWNER>::value>>
433  inline ToolHandleArray( OWNER* owner, std::string name, const std::vector<std::string>& typesAndNames = {},
434  std::string doc = "" )
435  : ToolHandleArray( typesAndNames, owner )
436  {
437  owner->addToolsArray( *this );
438  auto p = owner->OWNER::PropertyHolderImpl::declareProperty( std::move( name ), *this, std::move( doc ) );
439  p->template setOwnerType<OWNER>();
440  }
441 };
442 
446 template <class T>
448 {
449 public:
450  PublicToolHandleArray( bool createIf = true ) : ToolHandleArray<T>( nullptr, createIf ) {}
451  PublicToolHandleArray( const std::vector<std::string>& typesAndNames, bool createIf = true )
452  : ToolHandleArray<T>( typesAndNames, nullptr, createIf )
453  {
454  }
455 
458  template <class OWNER, typename = typename std::enable_if<std::is_base_of<IProperty, OWNER>::value>::type>
459  inline PublicToolHandleArray( OWNER* owner, std::string name, const std::vector<std::string>& typesAndNames = {},
460  std::string doc = "" )
461  : PublicToolHandleArray( typesAndNames )
462  {
463  owner->addToolsArray( *this );
464  auto p = owner->OWNER::PropertyHolderImpl::declareProperty( std::move( name ), *this, std::move( doc ) );
465  p->template setOwnerType<OWNER>();
466  }
467 };
468 
469 template <class T>
470 inline std::ostream& operator<<( std::ostream& os, const ToolHandle<T>& handle )
471 {
472  return operator<<( os, static_cast<const GaudiHandleInfo&>( handle ) );
473 }
474 
475 template <class T>
476 inline std::ostream& operator<<( std::ostream& os, const ToolHandleArray<T>& handle )
477 {
478  return operator<<( os, static_cast<const GaudiHandleInfo&>( handle ) );
479 }
480 
481 #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:141
Helper class to construct ToolHandle instances for public tools via the auto registering constructor...
Definition: ToolHandle.h:447
The interface implemented by the IToolSvc base class.
Definition: IToolSvc.h:19
constexpr static const auto FAILURE
Definition: StatusCode.h:88
Handle to be used in lieu of naked pointers to gaudis.
Definition: GaudiHandle.h:170
#define __attribute__(x)
Definition: System.cpp:89
T empty(T...args)
const IInterface * parent() const noexcept
Definition: ToolHandle.h:45
Define general base for Gaudi exception.
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:433
ServiceHandle< IToolSvc > m_pToolSvc
Definition: ToolHandle.h:326
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:279
StatusCode retrieve() const override
Retrieve the AlgTool.
Definition: ToolHandle.h:229
GAUDI_API const std::string typeinfoName(const std::type_info &)
Get platform independent information about the class type.
Definition: System.cpp:332
bool setEnabled(const bool flag)
Definition: ToolHandle.h:104
bool isPublic() const noexcept
Definition: ToolHandle.h:41
void disable()
Definition: ToolHandle.h:102
void setComponentType(const std::string &componentType)
The component type.
Definition: GaudiHandle.h:73
bool isEnabled() const
Helper to check if the ToolHandle should be retrieved.
Definition: ToolHandle.h:93
static std::string toolParentName(const IInterface *parent)
Definition: ToolHandle.h:53
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:348
Non-templated base class for actual ToolHandle<T>.
Definition: ToolHandle.h:71
Array of Handles to be used in lieu of vector of naked pointers to tools.
Definition: ToolHandle.h:385
const std::string & parentName() const
The name of the parent.
Definition: GaudiHandle.h:58
PublicToolHandleArray(const std::vector< std::string > &typesAndNames, bool createIf=true)
Definition: ToolHandle.h:451
BaseToolHandle(const IInterface *parent=nullptr, bool createIf=true)
Definition: ToolHandle.h:75
void setParentName(const std::string &parent)
The name of the parent.
Definition: GaudiHandle.h:76
bool push_back(const std::string &toolTypeAndName) override
Add a handle to the array with given tool type and name.
Definition: ToolHandle.h:420
static std::string toolComponentType(const IInterface *parent)
Definition: ToolHandle.h:51
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:341
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:150
StatusCode i_retrieve(IAlgTool *&algTool) const override
Definition: ToolHandle.h:316
PublicToolHandle(const char *toolTypeAndName, bool createIf=true)
Definition: ToolHandle.h:337
STL class.
ToolHandleArray(const IInterface *parent=nullptr, bool createIf=true)
Constructor which creates and empty list.
Definition: ToolHandle.h:409
PublicToolHandle(OWNER *owner, std::string propName, std::string toolType, std::string doc="")
Autodeclaring constructor with property propName, tool type/name and documentation.
Definition: ToolHandle.h:357
std::remove_const_t< T > * nonConst(T *p)
Cast a pointer to a non const type.
Definition: GaudiHandle.h:21
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:51
std::string typeAndName() const override
Definition: ToolHandle.h:281
Definition of the basic interface.
Definition: IInterface.h:277
StatusCode retrieve(DisableTool sd) override
Definition: ToolHandle.h:234
T * get()
Return the wrapped pointer, not calling retrieve() if null.
Definition: GaudiHandle.h:264
Gaudi::tagged_bool< class EnableTool_tag > EnableTool
Definition: ToolHandle.h:26
StatusCode retrieve() const
Retrieve the component.
Definition: GaudiHandle.h:226
StatusCode retrieve(IAlgTool *&tool) const
Definition: ToolHandle.h:80
T move(T...args)
Gaudi::tagged_bool< class DisableTool_tag > DisableTool
Definition: ToolHandle.h:25
constexpr static const auto SUCCESS
Definition: StatusCode.h:87
PublicToolHandle(bool createIf=true)
Definition: ToolHandle.h:336
IAlgTool * getAsIAlgTool() override
Definition: ToolHandle.h:310
T * operator->()
Definition: GaudiHandle.h:278
Handle to be used in lieu of naked pointers to tools.
Definition: ToolHandle.h:130
StatusCode initialize(const std::string &toolTypeAndName, const IInterface *parent=nullptr, bool createIf=true)
Definition: ToolHandle.h:214
Base class from which all concrete algorithm classes should be derived.
Definition: Algorithm.h:78
IInterface compliant class extending IInterface with the name() method.
T find(T...args)
const IInterface * m_parent
Definition: ToolHandle.h:60
Base class from which all the concrete tool classes should be derived.
Definition: AlgTool.h:47
The interface implemented by the AlgTool base class.
Definition: IAlgTool.h:23
const IAlgTool * getAsIAlgTool() const override
Definition: ToolHandle.h:304
Helper class to construct ToolHandle instances for public tools via the auto registering constructor...
Definition: ToolHandle.h:333
virtual ~ToolHandleInfo()=default
StatusCode retrieve(T *&algTool) const override
Do the real retrieval of the AlgTool.
Definition: ToolHandle.h:262
ToolHandleArray(const std::vector< std::string > &myTypesAndNames, const IInterface *parent=nullptr, bool createIf=true)
Generic constructor.
Definition: ToolHandle.h:397
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:122
Base class for all services.
Definition: Service.h:36
PublicToolHandleArray(bool createIf=true)
Definition: ToolHandle.h:450
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:189
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:459
ToolHandle(OWNER *owner, std::string propName, std::string toolType, std::string doc="")
Autodeclaring constructor with property propName, tool type/name and documentation.
Definition: ToolHandle.h:199
General info and helper functions for toolhandles and arrays.
Definition: ToolHandle.h:29
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:428
StatusCode release() const
Release the AlgTool.
Definition: ToolHandle.h:256
ToolHandleInfo(const IInterface *parent=nullptr, bool createIf=true)
Definition: ToolHandle.h:33
T is the concrete handle type, e.g.
Definition: GaudiHandle.h:420
StatusCode retrieve(EnableTool sd) override
Definition: ToolHandle.h:244
bool createIf() const noexcept
Definition: ToolHandle.h:43