The Gaudi Framework  master (37c0b60a)
ToolHandle< T > Class Template Reference

#include <GaudiKernel/ToolHandle.h>

Inheritance diagram for ToolHandle< T >:
Collaboration diagram for ToolHandle< T >:

Public Member Functions

 ToolHandle (const IInterface *parent=nullptr, bool createIf=true)
 Constructor for a tool with default tool type and name. More...
 
template<typename CT = T, typename NCT = std::remove_const_t<T>, typename = std::enable_if_t<std::is_const_v<CT> && !std::is_same_v<CT, NCT>>>
 ToolHandle (const ToolHandle< NCT > &other)
 Copy constructor from a non const T to const T tool handle. More...
 
 ToolHandle (const std::string &toolTypeAndName, const IInterface *parent=nullptr, bool createIf=true)
 Create a handle ('smart pointer') to a tool. More...
 
template<class OWNER , typename = std::enable_if_t<std::is_base_of_v<IProperty, OWNER>>>
 ToolHandle (OWNER *owner, std::string propName, std::string toolType, std::string doc="")
 Autodeclaring constructor with property propName, tool type/name and documentation. More...
 
template<typename... Args>
 ToolHandle (std::tuple< Args... > &&args)
 
StatusCode initialize (const std::string &toolTypeAndName, const IInterface *parent=nullptr, bool createIf=true)
 
StatusCode retrieve () const override
 Retrieve the AlgTool. More...
 
StatusCode retrieve (DisableTool sd) override
 
StatusCode retrieve (EnableTool sd) override
 
StatusCode release () const
 Release the AlgTool. More...
 
StatusCode retrieve (T *&algTool) const override
 Do the real retrieval of the AlgTool. More...
 
StatusCode release (T *algTool) const override
 Do the real release of the AlgTool. More...
 
std::string typeAndName () const override
 
std::add_const_t< T > * get () const
 
T * get ()
 
- Public Member Functions inherited from BaseToolHandle
StatusCode retrieve (IAlgTool *&tool) const
 
const IAlgToolget () const
 
IAlgToolget ()
 
bool isEnabled () const
 Helper to check if the ToolHandle should be retrieved. More...
 
void enable ()
 
void disable ()
 
bool setEnabled (bool flag)
 
- Public Member Functions inherited from ToolHandleInfo
virtual ~ToolHandleInfo ()=default
 
bool isPublic () const noexcept
 
bool createIf () const noexcept
 
const IInterfaceparent () const noexcept
 
- Public Member Functions inherited from GaudiHandle< T >
template<typename CT = T, typename NCT = std::remove_const_t<T>>
 GaudiHandle (const GaudiHandle< NCT > &other, std::enable_if_t< std::is_const_v< CT > &&!std::is_same_v< CT, NCT >> *=nullptr)
 Copy constructor needed for correct ref-counting. More...
 
 GaudiHandle (const GaudiHandle &other)
 Copy constructor needed for correct ref-counting. More...
 
template<typename CT = T, typename NCT = std::remove_const_t<T>>
std::enable_if_t< std::is_const_v< CT > &&!std::is_same_v< CT, NCT >, GaudiHandle & > operator= (const GaudiHandle< NCT > &other)
 Assignment operator for correct ref-counting. More...
 
GaudiHandleoperator= (const GaudiHandle &other)
 Assignment operator for correct ref-counting. More...
 
StatusCode retrieve () const
 Retrieve the component. More...
 
StatusCode release () const
 Release the component. More...
 
bool isValid () const
 Check if the handle is valid (try to retrive the object is not done yet). More...
 
 operator bool () const
 For testing if handle has component. More...
 
T * get ()
 Return the wrapped pointer, not calling retrieve() if null. More...
 
std::add_const_t< T > * get () const
 Return the wrapped pointer, not calling retrieve() if null. More...
 
bool isSet () const
 True if the wrapped pointer is not null. More...
 
T & operator* ()
 
T * operator-> ()
 
std::add_const_t< T > & operator* () const
 
std::add_const_t< T > * operator-> () const
 
std::string getDefaultType ()
 Helper function to get default type string from the class type. More...
 
std::string getDefaultName ()
 
- Public Member Functions inherited from GaudiHandleBase
const std::stringtypeAndName () const
 The full type and name: "type/name". More...
 
std::string type () const
 The concrete component class name: the part before the '/'. More...
 
std::string name () const
 The instance name: the part after the '/'. More...
 
bool empty () const
 Check if the handle has been set to empty string (i.e. More...
 
void setTypeAndName (std::string myTypeAndName)
 The component "type/name" string. More...
 
void setName (std::string_view myName)
 Set the instance name (part after the '/') without changing the class type. More...
 
std::string pythonPropertyClassName () const override
 Name of the componentType with "Handle" appended. More...
 
std::string messageName () const
 name used for printing messages More...
 
std::string pythonRepr () const override
 Python representation of handle, i.e. More...
 
- Public Member Functions inherited from GaudiHandleInfo
virtual ~GaudiHandleInfo ()
 virtual destructor so that derived class destructor is called. More...
 
const std::stringcomponentType () const
 
const std::stringpropertyName () const
 name as used in declareProperty(name,gaudiHandle) More...
 
void setPropertyName (std::string propName)
 set name as used in declareProperty(name,gaudiHandle). More...
 
const std::stringparentName () const
 The name of the parent. More...
 

Protected Member Functions

const IAlgToolgetAsIAlgTool () const override
 
IAlgToolgetAsIAlgTool () override
 
StatusCode i_retrieve (IAlgTool *&algTool) const override
 
- Protected Member Functions inherited from BaseToolHandle
 BaseToolHandle (const IInterface *parent=nullptr, bool createIf=true)
 
- Protected Member Functions inherited from ToolHandleInfo
 ToolHandleInfo (const IInterface *parent=nullptr, bool createIf=true)
 
- Protected Member Functions inherited from GaudiHandle< T >
 GaudiHandle (std::string myTypeAndName, std::string myComponentType, std::string myParentName)
 
- Protected Member Functions inherited from GaudiHandleBase
 GaudiHandleBase (std::string myTypeAndName, std::string myComponentType, std::string myParentName)
 Create a handle ('smart pointer') to a gaudi component. More...
 
- Protected Member Functions inherited from GaudiHandleInfo
 GaudiHandleInfo (std::string myComponentType, std::string myParentName)
 Some basic information and helper functions shared between various handles/arrays. More...
 
void setComponentType (std::string componentType)
 The component type. More...
 
void setParentName (std::string parent)
 The name of the parent. More...
 

Private Member Functions

template<typename... Args, std::size_t... Is>
 ToolHandle (std::tuple< Args... > &&args, std::index_sequence< Is... >)
 

Private Attributes

ServiceHandle< IToolSvcm_pToolSvc
 

Friends

class Gaudi::Algorithm
 
class AlgTool
 
class Service
 
std::ostreamoperator<< (std::ostream &os, const ToolHandle< T > &handle)
 

Additional Inherited Members

- Public Types inherited from GaudiHandleBase
using PropertyType = GaudiHandleProperty
 
- Static Public Member Functions inherited from ToolHandleInfo
static std::string toolComponentType (const IInterface *parent)
 
static std::string toolParentName (const IInterface *parent)
 
- Protected Attributes inherited from BaseToolHandle
bool m_enabled = true
 
- Protected Attributes inherited from ToolHandleInfo
const IInterfacem_parent = nullptr
 
bool m_createIf { true }
 

Detailed Description

template<class T>
class ToolHandle< T >

Handle to be used in lieu of naked pointers to tools. This allows better control through the framework of tool loading and usage. @paramater T is the AlgTool interface class (or concrete class) of the tool to use, and must derive from IAlgTool.

Author
Wim Lavrijsen WLavr.nosp@m.ijse.nosp@m.n@lbl.nosp@m..gov
Marti.nosp@m.n.Wo.nosp@m.udstr.nosp@m.a@ce.nosp@m.rn.ch

Definition at line 132 of file ToolHandle.h.

Constructor & Destructor Documentation

◆ ToolHandle() [1/6]

template<class T >
template<typename... Args, std::size_t... Is>
ToolHandle< T >::ToolHandle ( std::tuple< Args... > &&  args,
std::index_sequence< Is... >   
)
inlineprivate

Definition at line 139 of file ToolHandle.h.

140  : ToolHandle( std::get<Is>( std::move( args ) )... ) {}

◆ ToolHandle() [2/6]

template<class T >
ToolHandle< T >::ToolHandle ( const IInterface parent = nullptr,
bool  createIf = true 
)
inline

Constructor for a tool with default tool type and name.

Can be called only if the type T is a concrete tool type (not an interface), and you want to use the default name.

Definition at line 146 of file ToolHandle.h.

◆ ToolHandle() [3/6]

template<class T >
template<typename CT = T, typename NCT = std::remove_const_t<T>, typename = std::enable_if_t<std::is_const_v<CT> && !std::is_same_v<CT, NCT>>>
ToolHandle< T >::ToolHandle ( const ToolHandle< NCT > &  other)
inline

Copy constructor from a non const T to const T tool handle.

Definition at line 154 of file ToolHandle.h.

155  : BaseToolHandle( other.parent(), other.createIf() )
156  , GaudiHandle<CT>( other )
157  , m_pToolSvc( "ToolSvc", GaudiHandleBase::parentName() ) {}

◆ ToolHandle() [4/6]

template<class T >
ToolHandle< T >::ToolHandle ( const std::string toolTypeAndName,
const IInterface parent = nullptr,
bool  createIf = true 
)
inline

Create a handle ('smart pointer') to a tool.

The arguments are passed on to ToolSvc, and have the same meaning:

StatusCode ToolSvc::retrieveTool ( const std::string& type ,
T*& tool ,
const IInterface* parent = 0 ,
bool createIf = true )
Parameters
ownerclass owning the ToolHandle
toolType"MyToolType/MyToolName" "MyToolType" is short for "MyToolType/MyToolType" 'MyToolType' is the name of the class of the concrete tool 'MyToolName' is to distinguish several tool instances of the same class
parentthe parent Algorithm,Tool or Service of which this tool is a member. If non-zero, the the tool is a private tool of the parent, otherwise it is a public (shared) tool.
createIfif true, create tool if not yet existing.

Definition at line 190 of file ToolHandle.h.

192  , GaudiHandle<T>( toolTypeAndName, toolComponentType( parent ), toolParentName( parent ) )
193  , m_pToolSvc( "ToolSvc", GaudiHandleBase::parentName() ) {
194  }

◆ ToolHandle() [5/6]

template<class T >
template<class OWNER , typename = std::enable_if_t<std::is_base_of_v<IProperty, OWNER>>>
ToolHandle< T >::ToolHandle ( OWNER *  owner,
std::string  propName,
std::string  toolType,
std::string  doc = "" 
)
inline

Autodeclaring constructor with property propName, tool type/name and documentation.

Note
the use std::enable_if is required to avoid ambiguities

Definition at line 199 of file ToolHandle.h.

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  }

◆ ToolHandle() [6/6]

template<class T >
template<typename... Args>
ToolHandle< T >::ToolHandle ( std::tuple< Args... > &&  args)
inline

Definition at line 210 of file ToolHandle.h.

210 : ToolHandle( std::move( args ), std::index_sequence_for<Args...>{} ) {}

Member Function Documentation

◆ get() [1/2]

template<class T >
T* ToolHandle< T >::get ( )
inline

Definition at line 277 of file ToolHandle.h.

277 { return GaudiHandle<T>::get(); }

◆ get() [2/2]

template<class T >
std::add_const_t<T>* ToolHandle< T >::get ( ) const
inline

Definition at line 275 of file ToolHandle.h.

275 { return GaudiHandle<T>::get(); }

◆ getAsIAlgTool() [1/2]

template<class T >
const IAlgTool* ToolHandle< T >::getAsIAlgTool ( ) const
inlineoverrideprotectedvirtual

Implements BaseToolHandle.

Definition at line 284 of file ToolHandle.h.

284  {
285  // const cast to support T being const
286  return GaudiHandle<T>::get();
287  }

◆ getAsIAlgTool() [2/2]

template<class T >
IAlgTool* ToolHandle< T >::getAsIAlgTool ( )
inlineoverrideprotectedvirtual

Implements BaseToolHandle.

Definition at line 289 of file ToolHandle.h.

289  {
290  // const cast to support T being const
292  }

◆ i_retrieve()

template<class T >
StatusCode ToolHandle< T >::i_retrieve ( IAlgTool *&  algTool) const
inlineoverrideprotectedvirtual

Implements BaseToolHandle.

Definition at line 294 of file ToolHandle.h.

294  {
297  }

◆ initialize()

template<class T >
StatusCode ToolHandle< T >::initialize ( const std::string toolTypeAndName,
const IInterface parent = nullptr,
bool  createIf = true 
)
inline

Definition at line 213 of file ToolHandle.h.

214  {
215 
216  GaudiHandleBase::setTypeAndName( toolTypeAndName );
219 
220  m_parent = parent;
222 
223  return m_pToolSvc.initialize( "ToolSvc", GaudiHandleBase::parentName() );
224  }

◆ release() [1/2]

template<class T >
StatusCode ToolHandle< T >::release ( ) const
inline

Release the AlgTool.

Function must be repeated here to avoid hiding the function release( T*& )

Definition at line 252 of file ToolHandle.h.

252  { // not really const, because it updates m_pObject
253  return GaudiHandle<T>::release();
254  }

◆ release() [2/2]

template<class T >
StatusCode ToolHandle< T >::release ( T *  algTool) const
inlineoverridevirtual

Do the real release of the AlgTool.

Reimplemented from GaudiHandle< T >.

Definition at line 271 of file ToolHandle.h.

271 { return m_pToolSvc->releaseTool( ::details::nonConst( algTool ) ); }

◆ retrieve() [1/4]

template<class T >
StatusCode ToolHandle< T >::retrieve ( ) const
inlineoverridevirtual

Retrieve the AlgTool.

Release existing tool if needed. Function must be repeated here to avoid hiding the function retrieve( T*& )

Implements BaseToolHandle.

Definition at line 228 of file ToolHandle.h.

228  { // not really const, because it updates m_pObject
229  return GaudiHandle<T>::retrieve();
230  }

◆ retrieve() [2/4]

template<class T >
StatusCode ToolHandle< T >::retrieve ( DisableTool  sd)
inlineoverridevirtual

Implements BaseToolHandle.

Definition at line 232 of file ToolHandle.h.

232  {
233  if ( isEnabled() && sd == DisableTool{ false } ) {
234  return GaudiHandle<T>::retrieve();
235  } else {
236  disable();
237  return StatusCode::SUCCESS;
238  }
239  }

◆ retrieve() [3/4]

template<class T >
StatusCode ToolHandle< T >::retrieve ( EnableTool  sd)
inlineoverridevirtual

Implements BaseToolHandle.

Definition at line 241 of file ToolHandle.h.

241  {
242  if ( isEnabled() && sd == EnableTool{ true } ) {
243  return GaudiHandle<T>::retrieve();
244  } else {
245  disable();
246  return StatusCode::SUCCESS;
247  }
248  }

◆ retrieve() [4/4]

template<class T >
StatusCode ToolHandle< T >::retrieve ( T *&  algTool) const
inlineoverridevirtual

Do the real retrieval of the AlgTool.

Implements GaudiHandle< T >.

Definition at line 257 of file ToolHandle.h.

257  {
258  IAlgTool* iface = nullptr;
259  if ( i_retrieve( iface ).isFailure() ) { return StatusCode::FAILURE; }
260 
261  algTool = dynamic_cast<T*>( iface );
262  if ( !algTool ) {
263  throw GaudiException( "unable to dcast AlgTool " + typeAndName() + " to interface " +
264  System::typeinfoName( typeid( T ) ),
265  typeAndName() + " retrieve", StatusCode::FAILURE );
266  }
267  return StatusCode::SUCCESS;
268  }

◆ typeAndName()

template<class T >
std::string ToolHandle< T >::typeAndName ( ) const
inlineoverridevirtual

Implements BaseToolHandle.

Definition at line 273 of file ToolHandle.h.

273 { return GaudiHandleBase::typeAndName(); }

Friends And Related Function Documentation

◆ AlgTool

template<class T >
friend class AlgTool
friend

Definition at line 135 of file ToolHandle.h.

◆ Gaudi::Algorithm

template<class T >
friend class Gaudi::Algorithm
friend

Definition at line 134 of file ToolHandle.h.

◆ operator<<

template<class T >
std::ostream& operator<< ( std::ostream os,
const ToolHandle< T > &  handle 
)
friend

Definition at line 279 of file ToolHandle.h.

279  {
280  return os << static_cast<const GaudiHandleInfo&>( handle );
281  }

◆ Service

template<class T >
friend class Service
friend

Definition at line 136 of file ToolHandle.h.

Member Data Documentation

◆ m_pToolSvc

template<class T >
ServiceHandle<IToolSvc> ToolHandle< T >::m_pToolSvc
mutableprivate

Definition at line 303 of file ToolHandle.h.


The documentation for this class was generated from the following file:
GaudiHandleInfo::parentName
const std::string & parentName() const
The name of the parent.
Definition: GaudiHandle.h:65
std::string
STL class.
IAlgTool
Definition: IAlgTool.h:33
std::move
T move(T... args)
BaseToolHandle::disable
void disable()
Definition: ToolHandle.h:109
BaseToolHandle::BaseToolHandle
BaseToolHandle(const IInterface *parent=nullptr, bool createIf=true)
Definition: ToolHandle.h:83
std::string::find
T find(T... args)
GaudiException
Definition: GaudiException.h:31
ServiceHandle::initialize
StatusCode initialize(const std::string &serviceName, const std::string &theParentName)
Definition: ServiceHandle.h:70
ToolHandleInfo::toolComponentType
static std::string toolComponentType(const IInterface *parent)
Definition: ToolHandle.h:61
System::typeinfoName
GAUDI_API const std::string typeinfoName(const std::type_info &)
Get platform independent information about the class type.
Definition: System.cpp:315
BaseToolHandle::isEnabled
bool isEnabled() const
Helper to check if the ToolHandle should be retrieved.
Definition: ToolHandle.h:101
GaudiHandleInfo::setParentName
void setParentName(std::string parent)
The name of the parent.
Definition: GaudiHandle.h:87
GaudiHandle
Definition: GaudiHandle.h:179
ToolHandleInfo::toolParentName
static std::string toolParentName(const IInterface *parent)
Definition: ToolHandle.h:63
GaudiHandle::get
T * get()
Return the wrapped pointer, not calling retrieve() if null.
Definition: GaudiHandle.h:266
bug_34121.tool
tool
Definition: bug_34121.py:18
details::nonConst
std::remove_const_t< T > * nonConst(T *p)
Cast a pointer to a non const type.
Definition: GaudiHandle.h:29
StatusCode
Definition: StatusCode.h:65
Gaudi::tagged_bool_ns::tagged_bool
Definition: TaggedBool.h:16
IInterface::interfaceID
static const InterfaceID & interfaceID()
Return an instance of InterfaceID identifying the interface.
Definition: IInterface.h:248
ToolHandle::ToolHandle
ToolHandle(std::tuple< Args... > &&args, std::index_sequence< Is... >)
Definition: ToolHandle.h:139
ToolHandle::i_retrieve
StatusCode i_retrieve(IAlgTool *&algTool) const override
Definition: ToolHandle.h:294
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::m_pToolSvc
ServiceHandle< IToolSvc > m_pToolSvc
Definition: ToolHandle.h:303
GaudiHandleBase::type
std::string type() const
The concrete component class name: the part before the '/'.
Definition: GaudiHandle.cpp:21
StatusCode::SUCCESS
constexpr static const auto SUCCESS
Definition: StatusCode.h:100
gaudirun.args
args
Definition: gaudirun.py:336
ToolHandleInfo::m_parent
const IInterface * m_parent
Definition: ToolHandle.h:69
IInterface
Definition: IInterface.h:239
IToolSvc::releaseTool
virtual StatusCode releaseTool(IAlgTool *tool)=0
Release the tool.
ToolHandleInfo::m_createIf
bool m_createIf
Definition: ToolHandle.h:70
GaudiHandleInfo::setComponentType
void setComponentType(std::string componentType)
The component type.
Definition: GaudiHandle.h:84
std::string::empty
T empty(T... args)
GaudiHandleBase::typeAndName
const std::string & typeAndName() const
The full type and name: "type/name".
Definition: GaudiHandle.h:131
ToolHandle::typeAndName
std::string typeAndName() const override
Definition: ToolHandle.h:273
StatusCode::FAILURE
constexpr static const auto FAILURE
Definition: StatusCode.h:101
ToolHandleInfo::parent
const IInterface * parent() const noexcept
Definition: ToolHandle.h:55
GaudiHandle::retrieve
StatusCode retrieve() const
Retrieve the component.
Definition: GaudiHandle.h:228
ToolHandleInfo::createIf
bool createIf() const noexcept
Definition: ToolHandle.h:53
GaudiHandle::release
StatusCode release() const
Release the component.
Definition: GaudiHandle.h:242
GaudiHandleBase::setTypeAndName
void setTypeAndName(std::string myTypeAndName)
The component "type/name" string.
Definition: GaudiHandle.cpp:19