The Gaudi Framework  master (bb95dfce)
Loading...
Searching...
No Matches
ToolHandle< T > Class Template Reference

Handle to be used in lieu of naked pointers to tools. More...

#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.
template<typename CT = T, typename NCT = std::remove_const_t<T>>
 ToolHandle (const ToolHandle< NCT > &other)
 Copy constructor from a non const T to const T tool handle.
 ToolHandle (const std::string &toolTypeAndName, const IInterface *parent=nullptr, bool createIf=true)
 Create a handle ('smart pointer') to a tool.
template<std::derived_from< IProperty > OWNER>
 ToolHandle (OWNER *owner, std::string propName, std::string toolType, std::string doc="")
 Autodeclaring constructor with property propName, tool type/name and documentation.
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.
StatusCode retrieve (DisableTool sd) override
StatusCode retrieve (EnableTool sd) override
StatusCode release () const override
 Release the AlgTool.
StatusCode retrieve (T *&algTool) const override
 Do the real retrieval of the AlgTool.
StatusCode release (T *algTool) const override
 Do the real release of the AlgTool.
std::string typeAndName () const override
std::add_const_t< T > * get () const
T * get ()
GaudiHandleBaseoperator= (const std::string &typeAndName)
 assignment operator from base class
GaudiHandleBaseoperator= (std::string &&typeAndName)
 assignment operator from base class
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.
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>>
requires ( std::is_const_v<CT> && !std::is_same_v<CT, NCT> )
 GaudiHandle (const GaudiHandle< NCT > &other)
 Copy constructor needed for correct ref-counting.
 GaudiHandle (const GaudiHandle &other)
 Copy constructor needed for correct ref-counting.
template<typename CT = T, typename NCT = std::remove_const_t<T>>
GaudiHandleoperator= (const GaudiHandle< NCT > &other)
 Assignment operator for correct ref-counting.
GaudiHandleoperator= (const GaudiHandle &other)
 Assignment operator for correct ref-counting.
StatusCode retrieve () const
 Retrieve the component.
StatusCode release () const
 Release the component.
bool isValid () const
 Check if the handle is valid (try to retrive the object is not done yet).
 operator bool () const
 For testing if handle has component.
T * get ()
 Return the wrapped pointer, not calling retrieve() if null.
std::add_const_t< T > * get () const
 Return the wrapped pointer, not calling retrieve() if null.
bool isSet () const
 True if the wrapped pointer is not null.
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.
std::string getDefaultName ()
Public Member Functions inherited from GaudiHandleBase
const std::string & typeAndName () const
 The full type and name: "type/name".
std::string type () const
 The concrete component class name: the part before the '/'.
std::string name () const
 The instance name: the part after the '/'.
bool empty () const
 Check if the handle has been set to empty string (i.e.
void setTypeAndName (std::string myTypeAndName)
 The component "type/name" string.
void setName (std::string_view myName)
 Set the instance name (part after the '/') without changing the class type.
std::string pythonPropertyClassName () const override
 Name of the componentType with "Handle" appended.
std::string messageName () const
 name used for printing messages
std::string pythonRepr () const override
 Python representation of handle, i.e.
GaudiHandleBaseoperator= (const std::string &typeAndName)
 Set "type/name" from string.
GaudiHandleBaseoperator= (std::string &&typeAndName)
 Set "type/name" from string.
Public Member Functions inherited from GaudiHandleInfo
virtual ~GaudiHandleInfo ()
 virtual destructor so that derived class destructor is called.
const std::string & componentType () const
const std::string & propertyName () const
 name as used in declareProperty(name,gaudiHandle)
void setPropertyName (std::string propName)
 set name as used in declareProperty(name,gaudiHandle).
const std::string & parentName () const
 The name of the parent.

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.
Protected Member Functions inherited from GaudiHandleInfo
 GaudiHandleInfo (std::string myComponentType, std::string myParentName)
 Some basic information and helper functions shared between various handles/arrays.
void setComponentType (std::string componentType)
 The component type.
void setParentName (std::string parent)
 The name of the parent.

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::ostream & operator<< (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 ) )... ) {}
Handle to be used in lieu of naked pointers to tools.
Definition ToolHandle.h:132
ToolHandle(std::tuple< Args... > &&args, std::index_sequence< Is... >)
Definition ToolHandle.h:139

◆ 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.

149 , m_pToolSvc( "ToolSvc", GaudiHandleBase::parentName() ) {}
BaseToolHandle(const IInterface *parent=nullptr, bool createIf=true)
Definition ToolHandle.h:81
GaudiHandle(std::string myTypeAndName, std::string myComponentType, std::string myParentName)
ServiceHandle< IToolSvc > m_pToolSvc
Definition ToolHandle.h:306
bool createIf() const noexcept
Definition ToolHandle.h:51
const IInterface * parent() const noexcept
Definition ToolHandle.h:53
static std::string toolParentName(const IInterface *parent)
Definition ToolHandle.h:61
static std::string toolComponentType(const IInterface *parent)
Definition ToolHandle.h:59

◆ ToolHandle() [3/6]

template<class T>
template<typename CT = T, typename NCT = std::remove_const_t<T>>
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.

◆ ToolHandle() [4/6]

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

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 )
std::string type() const
The concrete component class name: the part before the '/'.
Definition of the basic interface.
Definition IInterface.h:225
This class is used for returning status codes from appropriate routines.
Definition StatusCode.h:64
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.

◆ ToolHandle() [5/6]

template<class T>
template<std::derived_from< 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 of requires 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 }
bool empty() const
Check if the handle has been set to empty string (i.e.

◆ 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.

Member Function Documentation

◆ get() [1/2]

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

Definition at line 280 of file ToolHandle.h.

280{ return GaudiHandle<T>::get(); }
T * get()
Return the wrapped pointer, not calling retrieve() if null.

◆ get() [2/2]

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

Definition at line 278 of file ToolHandle.h.

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

◆ getAsIAlgTool() [1/2]

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

Implements BaseToolHandle.

Definition at line 287 of file ToolHandle.h.

287 {
288 // const cast to support T being const
289 return GaudiHandle<T>::get();
290 }

◆ getAsIAlgTool() [2/2]

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

Implements BaseToolHandle.

Definition at line 292 of file ToolHandle.h.

292 {
293 // const cast to support T being const
295 }

◆ i_retrieve()

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

Implements BaseToolHandle.

Definition at line 297 of file ToolHandle.h.

297 {
300 }
std::string typeAndName() const override
Definition ToolHandle.h:276

◆ initialize()

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

Definition at line 216 of file ToolHandle.h.

217 {
218
222
225
226 return m_pToolSvc.initialize( "ToolSvc", GaudiHandleBase::parentName() );
227 }
void setTypeAndName(std::string myTypeAndName)
The component "type/name" string.
void setParentName(std::string parent)
The name of the parent.
Definition GaudiHandle.h:84
void setComponentType(std::string componentType)
The component type.
Definition GaudiHandle.h:81
const IInterface * m_parent
Definition ToolHandle.h:67

◆ operator=() [1/2]

template<class T>
GaudiHandleBase & GaudiHandleBase::operator= ( const std::string & typeAndName)
inline

assignment operator from base class

Definition at line 159 of file GaudiHandle.h.

159 {
161 return *this;
162 }

◆ operator=() [2/2]

template<class T>
GaudiHandleBase & GaudiHandleBase::operator= ( std::string && typeAndName)
inline

assignment operator from base class

Definition at line 165 of file GaudiHandle.h.

165 {
167 return *this;
168 }

◆ release() [1/2]

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

Release the AlgTool.

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

Implements BaseToolHandle.

Definition at line 255 of file ToolHandle.h.

255 { // not really const, because it updates m_pObject
257 }
StatusCode release() const
Release the component.

◆ 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 274 of file ToolHandle.h.

274{ 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 231 of file ToolHandle.h.

231 { // not really const, because it updates m_pObject
233 }
StatusCode retrieve() const
Retrieve the component.

◆ retrieve() [2/4]

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

Implements BaseToolHandle.

Definition at line 235 of file ToolHandle.h.

235 {
236 if ( isEnabled() && sd == DisableTool{ false } ) {
238 } else {
239 disable();
240 return StatusCode::SUCCESS;
241 }
242 }
bool isEnabled() const
Helper to check if the ToolHandle should be retrieved.
Definition ToolHandle.h:101

◆ retrieve() [3/4]

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

Implements BaseToolHandle.

Definition at line 244 of file ToolHandle.h.

244 {
245 if ( isEnabled() && sd == EnableTool{ true } ) {
247 } else {
248 disable();
249 return StatusCode::SUCCESS;
250 }
251 }

◆ 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 260 of file ToolHandle.h.

260 {
261 IAlgTool* iface = nullptr;
262 if ( i_retrieve( iface ).isFailure() ) { return StatusCode::FAILURE; }
263
264 algTool = dynamic_cast<T*>( iface );
265 if ( !algTool ) {
266 throw GaudiException( "unable to dcast AlgTool " + typeAndName() + " to interface " +
267 System::typeinfoName( typeid( T ) ),
268 typeAndName() + " retrieve", StatusCode::FAILURE );
269 }
270 return StatusCode::SUCCESS;
271 }
StatusCode i_retrieve(IAlgTool *&algTool) const override
Definition ToolHandle.h:297

◆ typeAndName()

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

Implements BaseToolHandle.

Definition at line 276 of file ToolHandle.h.

const std::string & typeAndName() const
The full type and name: "type/name".

◆ 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 282 of file ToolHandle.h.

◆ 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 306 of file ToolHandle.h.


The documentation for this class was generated from the following file: