The Gaudi Framework  v36r1 (3e2fb5a8)
ToolHandle.h
Go to the documentation of this file.
1 /***********************************************************************************\
2 * (c) Copyright 1998-2019 CERN for the benefit of the LHCb and ATLAS collaborations *
3 * *
4 * This software is distributed under the terms of the Apache version 2 licence, *
5 * copied verbatim in the file "LICENSE". *
6 * *
7 * In applying this licence, CERN does not waive the privileges and immunities *
8 * granted to it by virtue of its status as an Intergovernmental Organization *
9 * or submit itself to any jurisdiction. *
10 \***********************************************************************************/
11 #ifndef GAUDIKERNEL_TOOLHANDLE_H
12 #define GAUDIKERNEL_TOOLHANDLE_H
13 
14 // Includes
16 #include "GaudiKernel/IAlgTool.h"
18 #include "GaudiKernel/IToolSvc.h"
20 #include "GaudiKernel/TaggedBool.h"
21 
22 #include <stdexcept>
23 #include <string>
24 #include <type_traits>
25 #include <vector>
26 
27 // forward declarations
28 class IInterface;
29 class IToolSvc;
30 
31 namespace Gaudi {
32  class Algorithm;
33 }
34 class AlgTool;
35 class Service;
36 
39 
42 
43 protected:
44  ToolHandleInfo( const IInterface* parent = nullptr, bool createIf = true )
46 
47 public:
48  virtual ~ToolHandleInfo() = default;
49 
50  bool isPublic() const noexcept { return !m_parent; }
51 
52  bool createIf() const noexcept { return m_createIf; }
53 
54  const IInterface* parent() const noexcept { return m_parent; }
55 
56  //
57  // Some helper functions
58  //
59 
60  static std::string toolComponentType( const IInterface* parent ) { return parent ? "PrivateTool" : "PublicTool"; }
61 
63  auto* pNamed = ( parent ? dynamic_cast<const INamedInterface*>( parent ) : nullptr );
64  return ( !parent ? "ToolSvc" : ( pNamed ? pNamed->name() : "" ) );
65  }
66 
67 protected:
68  const IInterface* m_parent = nullptr;
69  bool m_createIf{true};
70 };
71 
80 
81 protected:
82  BaseToolHandle( const IInterface* parent = nullptr, bool createIf = true ) : ToolHandleInfo( parent, createIf ) {}
83 
84  virtual StatusCode i_retrieve( IAlgTool*& ) const = 0;
85 
86 public:
87  StatusCode retrieve( IAlgTool*& tool ) const { return i_retrieve( tool ); }
88 
89  virtual StatusCode retrieve() const = 0;
90  virtual StatusCode retrieve( DisableTool sd ) = 0;
91  virtual StatusCode retrieve( EnableTool sd ) = 0;
92 
93  const IAlgTool* get() const { return getAsIAlgTool(); }
94 
95  IAlgTool* get() { return getAsIAlgTool(); }
96 
97  virtual std::string typeAndName() const = 0;
98 
100  inline bool isEnabled() const {
101  // the handle is considered enabled if the type/name is valid and the
102  // enabled flag was set to true, or it was already retrieved
103  return ( m_enabled && !typeAndName().empty() ) || get();
104  }
105 
106  inline void enable() { m_enabled = true; }
107 
108  inline void disable() { m_enabled = false; }
109 
110  inline bool setEnabled( const bool flag ) {
111  auto old = m_enabled;
112  m_enabled = flag;
113  return old;
114  }
115 
116 protected:
117  virtual const IAlgTool* getAsIAlgTool() const = 0;
118 
119  virtual IAlgTool* getAsIAlgTool() = 0;
120 
121  bool m_enabled = true;
122 };
123 
134 template <class T>
135 class ToolHandle : public BaseToolHandle, public GaudiHandle<T> {
136 
137  friend class Gaudi::Algorithm;
138  friend class AlgTool;
139  friend class Service;
140 
141 public:
145  ToolHandle( const IInterface* parent = nullptr, bool createIf = true )
148  , m_pToolSvc( "ToolSvc", GaudiHandleBase::parentName() ) {}
149 
151  template <typename CT = T, typename NCT = std::remove_const_t<T>,
152  typename = std::enable_if_t<std::is_const_v<CT> && !std::is_same_v<CT, NCT>>>
153  ToolHandle( const ToolHandle<NCT>& other )
154  : BaseToolHandle( other.parent(), other.createIf() )
155  , GaudiHandle<CT>( other )
156  , m_pToolSvc( "ToolSvc", GaudiHandleBase::parentName() ) {}
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 
197  template <class OWNER, typename = std::enable_if_t<std::is_base_of_v<IProperty, OWNER>>>
198  inline ToolHandle( OWNER* owner, std::string propName, std::string toolType, std::string doc = "" )
199  : ToolHandle( owner ) {
200  // convert name and type to a valid type/name string
201  // - if type does not contain '/' use type/type
202  // - otherwise type is already a type/name string
203  if ( !toolType.empty() and toolType.find( '/' ) == std::string::npos ) { toolType += '/' + toolType; }
204  owner->declareTool( *this, std::move( toolType ) ).ignore();
205  auto p = owner->OWNER::PropertyHolderImpl::declareProperty( std::move( propName ), *this, std::move( doc ) );
206  p->template setOwnerType<OWNER>();
207  }
208 
209 public:
210  StatusCode initialize( const std::string& toolTypeAndName, const IInterface* parent = nullptr,
211  bool createIf = true ) {
212 
213  GaudiHandleBase::setTypeAndName( toolTypeAndName );
216 
217  m_parent = parent;
219 
220  return m_pToolSvc.initialize( "ToolSvc", GaudiHandleBase::parentName() );
221  }
222 
225  StatusCode retrieve() const override { // not really const, because it updates m_pObject
226  return GaudiHandle<T>::retrieve();
227  }
228 
229  StatusCode retrieve( DisableTool sd ) override {
230  if ( isEnabled() && sd == DisableTool{false} ) {
231  return GaudiHandle<T>::retrieve();
232  } else {
233  disable();
234  return StatusCode::SUCCESS;
235  }
236  }
237 
238  StatusCode retrieve( EnableTool sd ) override {
239  if ( isEnabled() && sd == EnableTool{true} ) {
240  return GaudiHandle<T>::retrieve();
241  } else {
242  disable();
243  return StatusCode::SUCCESS;
244  }
245  }
246 
249  StatusCode release() const { // not really const, because it updates m_pObject
250  return GaudiHandle<T>::release();
251  }
252 
254  StatusCode retrieve( T*& algTool ) const override {
255  IAlgTool* iface = nullptr;
256  if ( i_retrieve( iface ).isFailure() ) { return StatusCode::FAILURE; }
257 
258  algTool = dynamic_cast<T*>( iface );
259  if ( algTool == nullptr ) {
260  throw GaudiException( "unable to dcast AlgTool " + typeAndName() + " to interface " +
261  System::typeinfoName( typeid( T ) ),
262  typeAndName() + " retrieve", StatusCode::FAILURE );
263  }
264  return StatusCode::SUCCESS;
265  }
266 
268  StatusCode release( T* algTool ) const override { return m_pToolSvc->releaseTool( ::details::nonConst( algTool ) ); }
269 
270  std::string typeAndName() const override { return GaudiHandleBase::typeAndName(); }
271 
272  std::add_const_t<T>* get() const { return GaudiHandle<T>::get(); }
273 
274  T* get() { return GaudiHandle<T>::get(); }
275 
276 protected:
277  const IAlgTool* getAsIAlgTool() const override {
278  // const cast to support T being const
279  return GaudiHandle<T>::get();
280  }
281 
282  IAlgTool* getAsIAlgTool() override {
283  // const cast to support T being const
285  }
286 
287  StatusCode i_retrieve( IAlgTool*& algTool ) const override {
290  }
291 
292 private:
293  //
294  // Private data members
295  //
297 };
298 
302 template <class T>
303 class PublicToolHandle : public ToolHandle<T> {
304 public:
306  PublicToolHandle( const char* toolTypeAndName, bool createIf = true )
307  : PublicToolHandle{std::string{toolTypeAndName}, createIf} {}
308  PublicToolHandle( const std::string& toolTypeAndName, bool createIf = true )
309  : ToolHandle<T>( toolTypeAndName, nullptr, createIf ) {}
310 
312  template <typename CT = T, typename NCT = std::remove_const_t<T>>
314  std::enable_if_t<std::is_const_v<CT> && !std::is_same_v<CT, NCT>>* = nullptr )
315  : ToolHandle<T>( static_cast<const ToolHandle<NCT>&>( other ) ) {}
316 
319  template <class OWNER, typename = std::enable_if_t<std::is_base_of_v<IProperty, OWNER>>>
320  inline PublicToolHandle( OWNER* owner, std::string propName, std::string toolType, std::string doc = "" )
321  : PublicToolHandle() {
322  // convert name and type to a valid type/name string
323  // - if type does not contain '/' use type/type
324  // - otherwise type is already a type/name string
325  if ( !toolType.empty() and toolType.find( '/' ) == std::string::npos ) { toolType += '/' + toolType; }
326  owner->declareTool( *this, std::move( toolType ) ).ignore();
327  auto p = owner->OWNER::PropertyHolderImpl::declareProperty( std::move( propName ), *this, std::move( doc ) );
328  p->template setOwnerType<OWNER>();
329  }
330 };
331 
332 //-------------------------------------------------------------------------//
333 
344 template <class T>
345 class ToolHandleArray : public ToolHandleInfo, public GaudiHandleArray<ToolHandle<T>> {
346 public:
347  //
348  // Constructors
349  //
356  ToolHandleArray( const std::vector<std::string>& myTypesAndNames, const IInterface* parent = nullptr,
357  bool createIf = true )
361 
366  ToolHandleArray( const IInterface* parent = nullptr, bool createIf = true )
370 
375  bool push_back( const std::string& toolTypeAndName ) override {
376  ToolHandle<T> handle( toolTypeAndName, ToolHandleInfo::parent(), ToolHandleInfo::createIf() );
378  return true;
379  }
380 
382  bool push_back( const ToolHandle<T>& myHandle ) override { return push_back( myHandle.typeAndName() ); }
383 
386  template <class OWNER, typename = std::enable_if_t<std::is_base_of_v<IProperty, OWNER>>>
388  std::string doc = "" )
389  : ToolHandleArray( owner ) {
390  owner->addToolsArray( *this );
392  auto p = owner->OWNER::PropertyHolderImpl::declareProperty( std::move( name ), *this, std::move( doc ) );
393  p->template setOwnerType<OWNER>();
394  }
395 };
396 
400 template <class T>
402 public:
406 
409  template <class OWNER, typename = std::enable_if_t<std::is_base_of_v<IProperty, OWNER>>>
411  std::string doc = "" )
413  owner->addToolsArray( *this );
415  auto p = owner->OWNER::PropertyHolderImpl::declareProperty( std::move( name ), *this, std::move( doc ) );
416  p->template setOwnerType<OWNER>();
417  }
418 };
419 
420 template <class T>
421 inline std::ostream& operator<<( std::ostream& os, const ToolHandle<T>& handle ) {
422  return operator<<( os, static_cast<const GaudiHandleInfo&>( handle ) );
423 }
424 
425 template <class T>
427  return operator<<( os, static_cast<const GaudiHandleInfo&>( handle ) );
428 }
429 
430 #endif // ! GAUDIKERNEL_TOOLHANDLE_H
ToolHandle::retrieve
StatusCode retrieve() const override
Retrieve the AlgTool.
Definition: ToolHandle.h:225
GaudiHandleInfo::parentName
const std::string & parentName() const
The name of the parent.
Definition: GaudiHandle.h:63
PublicToolHandle::PublicToolHandle
PublicToolHandle(const std::string &toolTypeAndName, bool createIf=true)
Definition: ToolHandle.h:308
GaudiHandle.h
std::string
STL class.
IAlgTool
Definition: IAlgTool.h:33
BaseToolHandle::setEnabled
bool setEnabled(const bool flag)
Definition: ToolHandle.h:110
std::move
T move(T... args)
BaseToolHandle::disable
void disable()
Definition: ToolHandle.h:108
BaseToolHandle::BaseToolHandle
BaseToolHandle(const IInterface *parent=nullptr, bool createIf=true)
Definition: ToolHandle.h:82
GaudiHandleArray< ToolHandle< T > >::push_back
virtual bool push_back(const std::string &myHandleTypeAndName)=0
Add a handle with given type and name.
ServiceHandle< IToolSvc >
PublicToolHandle::PublicToolHandle
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:320
BaseToolHandle::typeAndName
virtual std::string typeAndName() const =0
ToolHandle::ToolHandle
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:198
std::vector< std::string >
std::string::find
T find(T... args)
GaudiException
Definition: GaudiException.h:31
Algorithm
Alias for backward compatibility.
Definition: Algorithm.h:58
ServiceHandle::initialize
StatusCode initialize(const std::string &serviceName, const std::string &theParentName)
Definition: ServiceHandle.h:70
ToolHandleArray::push_back
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:382
ToolHandleInfo::toolComponentType
static std::string toolComponentType(const IInterface *parent)
Definition: ToolHandle.h:60
System::typeinfoName
GAUDI_API const std::string typeinfoName(const std::type_info &)
Get platform independent information about the class type.
Definition: System.cpp:308
ServiceHandle.h
BaseToolHandle::i_retrieve
virtual StatusCode i_retrieve(IAlgTool *&) const =0
ToolHandle::ToolHandle
ToolHandle(const ToolHandle< NCT > &other)
Copy constructor from a non const T to const T tool handle.
Definition: ToolHandle.h:153
BaseToolHandle::isEnabled
bool isEnabled() const
Helper to check if the ToolHandle should be retrieved.
Definition: ToolHandle.h:100
GaudiHandleInfo::setParentName
void setParentName(std::string parent)
The name of the parent.
Definition: GaudiHandle.h:81
Service
Definition: Service.h:46
GaudiHandleBase
Definition: GaudiHandle.h:99
PublicToolHandle::PublicToolHandle
PublicToolHandle(bool createIf=true)
Definition: ToolHandle.h:305
GaudiHandle
Definition: GaudiHandle.h:173
ToolHandleInfo::toolParentName
static std::string toolParentName(const IInterface *parent)
Definition: ToolHandle.h:62
ToolHandleArray::ToolHandleArray
ToolHandleArray(const IInterface *parent=nullptr, bool createIf=true)
Constructor which creates and empty list.
Definition: ToolHandle.h:366
PublicToolHandleArray::PublicToolHandleArray
PublicToolHandleArray(const std::vector< std::string > &typesAndNames, bool createIf=true)
Definition: ToolHandle.h:404
ToolHandle::retrieve
StatusCode retrieve(T *&algTool) const override
Do the real retrieval of the AlgTool.
Definition: ToolHandle.h:254
IToolSvc.h
operator<<
std::ostream & operator<<(std::ostream &os, const ToolHandle< T > &handle)
Definition: ToolHandle.h:421
ToolHandle
Definition: ToolHandle.h:135
INamedInterface.h
ToolHandleArray::ToolHandleArray
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:387
GaudiHandle::get
T * get()
Return the wrapped pointer, not calling retrieve() if null.
Definition: GaudiHandle.h:260
bug_34121.tool
tool
Definition: bug_34121.py:17
BaseToolHandle::retrieve
virtual StatusCode retrieve(DisableTool sd)=0
details::nonConst
std::remove_const_t< T > * nonConst(T *p)
Cast a pointer to a non const type.
Definition: GaudiHandle.h:30
PublicToolHandleArray::PublicToolHandleArray
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:410
TimingHistograms.name
name
Definition: TimingHistograms.py:23
StatusCode
Definition: StatusCode.h:65
Gaudi::tagged_bool_ns::tagged_bool
Definition: TaggedBool.h:16
ToolHandle::ToolHandle
ToolHandle(const IInterface *parent=nullptr, bool createIf=true)
Constructor for a tool with default tool type and name.
Definition: ToolHandle.h:145
TaggedBool.h
IInterface::interfaceID
static const InterfaceID & interfaceID()
Return an instance of InterfaceID identifying the interface.
Definition: IInterface.h:246
PublicToolHandle::PublicToolHandle
PublicToolHandle(const char *toolTypeAndName, bool createIf=true)
Definition: ToolHandle.h:306
std::ostream
STL class.
ToolHandleInfo
General info and helper functions for toolhandles and arrays.
Definition: ToolHandle.h:41
ToolHandle::get
T * get()
Definition: ToolHandle.h:274
IAlgTool.h
Gaudi::Algorithm
Base class from which all concrete algorithm classes should be derived.
Definition: Algorithm.h:90
ToolHandleArray
Definition: ToolHandle.h:345
ToolHandle::ToolHandle
ToolHandle(const std::string &toolTypeAndName, const IInterface *parent=nullptr, bool createIf=true)
Create a handle ('smart pointer') to a tool.
Definition: ToolHandle.h:189
PublicToolHandleArray
Helper class to construct ToolHandle instances for public tools via the auto registering constructor.
Definition: ToolHandle.h:401
BaseToolHandle::retrieve
StatusCode retrieve(IAlgTool *&tool) const
Definition: ToolHandle.h:87
ToolHandle::i_retrieve
StatusCode i_retrieve(IAlgTool *&algTool) const override
Definition: ToolHandle.h:287
PublicToolHandle
Helper class to construct ToolHandle instances for public tools via the auto registering constructor.
Definition: ToolHandle.h:303
GaudiHandleBase::typeAndName
std::string typeAndName() const
The full type and name: "type/name".
Definition: GaudiHandle.h:125
GaudiPython.Bindings.nullptr
nullptr
Definition: Bindings.py:74
IToolSvc::retrieve
virtual StatusCode retrieve(std::string_view type, const InterfaceID &iid, IAlgTool *&tool, const IInterface *parent=0, bool createIf=true)=0
Retrieve tool with tool dependent part of the name automatically assigned.
ToolHandle::release
StatusCode release() const
Release the AlgTool.
Definition: ToolHandle.h:249
BaseToolHandle::retrieve
virtual StatusCode retrieve(EnableTool sd)=0
ToolHandle::getAsIAlgTool
const IAlgTool * getAsIAlgTool() const override
Definition: ToolHandle.h:277
Gaudi
Header file for std:chrono::duration-based Counters.
Definition: __init__.py:1
ToolHandle::m_pToolSvc
ServiceHandle< IToolSvc > m_pToolSvc
Definition: ToolHandle.h:296
ToolHandleArray::ToolHandleArray
ToolHandleArray(const std::vector< std::string > &myTypesAndNames, const IInterface *parent=nullptr, bool createIf=true)
Generic constructor.
Definition: ToolHandle.h:356
BaseToolHandle::enable
void enable()
Definition: ToolHandle.h:106
ToolHandleInfo::isPublic
bool isPublic() const noexcept
Definition: ToolHandle.h:50
GaudiHandleArrayBase::setTypesAndNames
bool setTypesAndNames(const std::vector< std::string > &myTypesAndNamesList)
Set the array of handles from list of "type/name" strings in <myTypesAndNamesList>.
Definition: GaudiHandle.cpp:63
BaseToolHandle::get
IAlgTool * get()
Definition: ToolHandle.h:95
INamedInterface
Definition: INamedInterface.h:25
BaseToolHandle::retrieve
virtual StatusCode retrieve() const =0
StatusCode::SUCCESS
constexpr static const auto SUCCESS
Definition: StatusCode.h:100
ToolHandle::retrieve
StatusCode retrieve(DisableTool sd) override
Definition: ToolHandle.h:229
AlgTool
Definition: AlgTool.h:62
BaseToolHandle
Definition: ToolHandle.h:79
ToolHandleInfo::m_parent
const IInterface * m_parent
Definition: ToolHandle.h:68
ToolHandleInfo::~ToolHandleInfo
virtual ~ToolHandleInfo()=default
IInterface
Definition: IInterface.h:237
BaseToolHandle::get
const IAlgTool * get() const
Definition: ToolHandle.h:93
IToolSvc::releaseTool
virtual StatusCode releaseTool(IAlgTool *tool)=0
Release the tool.
ToolHandleInfo::m_createIf
bool m_createIf
Definition: ToolHandle.h:69
ToolHandle::retrieve
StatusCode retrieve(EnableTool sd) override
Definition: ToolHandle.h:238
GaudiHandleInfo::setComponentType
void setComponentType(std::string componentType)
The component type.
Definition: GaudiHandle.h:78
ToolHandle::getAsIAlgTool
IAlgTool * getAsIAlgTool() override
Definition: ToolHandle.h:282
std::string::empty
T empty(T... args)
PublicToolHandleArray::PublicToolHandleArray
PublicToolHandleArray(bool createIf=true)
Definition: ToolHandle.h:403
ToolHandle::initialize
StatusCode initialize(const std::string &toolTypeAndName, const IInterface *parent=nullptr, bool createIf=true)
Definition: ToolHandle.h:210
PublicToolHandle::PublicToolHandle
PublicToolHandle(const PublicToolHandle< NCT > &other, std::enable_if_t< std::is_const_v< CT > &&!std::is_same_v< CT, NCT >> *=nullptr)
Copy constructor from a non const T to const T tool handle.
Definition: ToolHandle.h:313
ToolHandleInfo::ToolHandleInfo
ToolHandleInfo(const IInterface *parent=nullptr, bool createIf=true)
Definition: ToolHandle.h:44
GaudiHandleArrayBase::typesAndNames
const std::vector< std::string > typesAndNames() const
Return a vector with "type/name" strings of all handles in the array.
Definition: GaudiHandle.cpp:78
ToolHandle::typeAndName
std::string typeAndName() const override
Definition: ToolHandle.h:270
StatusCode::FAILURE
constexpr static const auto FAILURE
Definition: StatusCode.h:101
BaseToolHandle::m_enabled
bool m_enabled
Definition: ToolHandle.h:121
IToolSvc
Definition: IToolSvc.h:29
GaudiHandleArray
T is the concrete handle type, e.g.
Definition: GaudiHandle.h:405
BaseToolHandle::getAsIAlgTool
virtual IAlgTool * getAsIAlgTool()=0
BaseToolHandle::getAsIAlgTool
virtual const IAlgTool * getAsIAlgTool() const =0
ToolHandleInfo::parent
const IInterface * parent() const noexcept
Definition: ToolHandle.h:54
GaudiHandle::retrieve
StatusCode retrieve() const
Retrieve the component.
Definition: GaudiHandle.h:222
ToolHandleInfo::createIf
bool createIf() const noexcept
Definition: ToolHandle.h:52
ToolHandleArray::push_back
bool push_back(const std::string &toolTypeAndName) override
Add a handle to the array with given tool type and name.
Definition: ToolHandle.h:375
GaudiHandle::release
StatusCode release() const
Release the component.
Definition: GaudiHandle.h:236
ToolHandle::release
StatusCode release(T *algTool) const override
Do the real release of the AlgTool.
Definition: ToolHandle.h:268
ToolHandle::get
std::add_const_t< T > * get() const
Definition: ToolHandle.h:272
GaudiHandleBase::setTypeAndName
void setTypeAndName(std::string myTypeAndName)
The component "type/name" string.
Definition: GaudiHandle.cpp:19