Loading [MathJax]/extensions/tex2jax.js
The Gaudi Framework  v31r0 (aeb156f0)
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
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. More...
 
template<typename CT = T, typename NCT = typename std::remove_const<T>::type>
 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. 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<IProperty, OWNER>::value>>
 ToolHandle (OWNER *owner, std::string propName, std::string toolType, std::string doc="")
 Autodeclaring constructor with property propName, tool type/name and documentation. More...
 
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 >::typeget () 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 (const 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 = typename std::remove_const<T>::type>
 GaudiHandle (const GaudiHandle< NCT > &other, typename std::enable_if< std::is_const< CT >::value &&!std::is_same< CT, NCT >::value >::type *=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 = typename std::remove_const<T>::type>
std::enable_if< std::is_const< CT >::value &&!std::is_same< CT, NCT >::value, GaudiHandle & >::type 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 >::typeget () 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 >::typeoperator* () const
 
std::add_const< T >::typeoperator-> () 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
std::string typeAndName () 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 (const std::string &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 ()=default
 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 (const std::string &componentType)
 The component type. More...
 
void setParentName (const std::string &parent)
 The name of the parent. More...
 

Private Attributes

ServiceHandle< IToolSvcm_pToolSvc
 

Friends

class Gaudi::Algorithm
 
class AlgTool
 
class Service
 

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

Constructor & Destructor Documentation

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

136  : BaseToolHandle( parent, createIf )
137  , GaudiHandle<T>( "", toolComponentType( parent ), toolParentName( parent ) )
138  , m_pToolSvc( "ToolSvc", GaudiHandleBase::parentName() ) {}
Handle to be used in lieu of naked pointers to gaudis.
Definition: GaudiHandle.h:163
ServiceHandle< IToolSvc > m_pToolSvc
Definition: ToolHandle.h:301
static std::string toolParentName(const IInterface *parent)
Definition: ToolHandle.h:52
const std::string & parentName() const
The name of the parent.
Definition: GaudiHandle.h:53
BaseToolHandle(const IInterface *parent=nullptr, bool createIf=true)
Definition: ToolHandle.h:72
static std::string toolComponentType(const IInterface *parent)
Definition: ToolHandle.h:50
bool createIf() const noexcept
Definition: ToolHandle.h:42
template<class T>
template<typename CT = T, typename NCT = typename std::remove_const<T>::type>
ToolHandle< T >::ToolHandle ( const ToolHandle< NCT > &  other,
typename std::enable_if< std::is_const< CT >::value &&!std::is_same< CT, NCT >::value >::type = nullptr 
)
inline

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

Definition at line 142 of file ToolHandle.h.

144  : BaseToolHandle( other.parent(), other.createIf() )
145  , GaudiHandle<CT>( other )
146  , m_pToolSvc( "ToolSvc", GaudiHandleBase::parentName() ) {}
Handle to be used in lieu of naked pointers to gaudis.
Definition: GaudiHandle.h:163
const IInterface * parent() const noexcept
Definition: ToolHandle.h:44
ServiceHandle< IToolSvc > m_pToolSvc
Definition: ToolHandle.h:301
const std::string & parentName() const
The name of the parent.
Definition: GaudiHandle.h:53
BaseToolHandle(const IInterface *parent=nullptr, bool createIf=true)
Definition: ToolHandle.h:72
bool createIf() const noexcept
Definition: ToolHandle.h:42
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 179 of file ToolHandle.h.

180  : BaseToolHandle( parent, createIf )
181  , GaudiHandle<T>( toolTypeAndName, toolComponentType( parent ), toolParentName( parent ) )
182  , m_pToolSvc( "ToolSvc", GaudiHandleBase::parentName() ) {
183  }
Handle to be used in lieu of naked pointers to gaudis.
Definition: GaudiHandle.h:163
ServiceHandle< IToolSvc > m_pToolSvc
Definition: ToolHandle.h:301
static std::string toolParentName(const IInterface *parent)
Definition: ToolHandle.h:52
const std::string & parentName() const
The name of the parent.
Definition: GaudiHandle.h:53
BaseToolHandle(const IInterface *parent=nullptr, bool createIf=true)
Definition: ToolHandle.h:72
static std::string toolComponentType(const IInterface *parent)
Definition: ToolHandle.h:50
bool createIf() const noexcept
Definition: ToolHandle.h:42
template<class T>
template<class OWNER , typename = std::enable_if_t<std::is_base_of<IProperty, OWNER>::value>>
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 188 of file ToolHandle.h.

189  : ToolHandle( owner ) {
190  // convert name and type to a valid type/name string
191  // - if type does not contain '/' use type/type
192  // - otherwise type is already a type/name string
193  if ( !toolType.empty() and toolType.find( '/' ) == std::string::npos ) { toolType += '/' + toolType; }
194  owner->declareTool( *this, std::move( toolType ) ).ignore();
195  auto p = owner->OWNER::PropertyHolderImpl::declareProperty( std::move( propName ), *this, std::move( doc ) );
196  p->template setOwnerType<OWNER>();
197  }
ToolHandle(const IInterface *parent=nullptr, bool createIf=true)
Constructor for a tool with default tool type and name.
Definition: ToolHandle.h:135
T empty(T...args)
T move(T...args)
T find(T...args)

Member Function Documentation

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

Definition at line 262 of file ToolHandle.h.

262 { return GaudiHandle<T>::get(); }
T * get()
Return the wrapped pointer, not calling retrieve() if null.
Definition: GaudiHandle.h:248
template<class T>
T* ToolHandle< T >::get ( )
inline

Definition at line 264 of file ToolHandle.h.

264 { return GaudiHandle<T>::get(); }
T * get()
Return the wrapped pointer, not calling retrieve() if null.
Definition: GaudiHandle.h:248
template<class T>
const IAlgTool* ToolHandle< T >::getAsIAlgTool ( ) const
inlineoverrideprotectedvirtual

Implements BaseToolHandle.

Definition at line 282 of file ToolHandle.h.

282  {
283  // const cast to support T being const
284  return GaudiHandle<T>::get();
285  }
T * get()
Return the wrapped pointer, not calling retrieve() if null.
Definition: GaudiHandle.h:248
template<class T>
IAlgTool* ToolHandle< T >::getAsIAlgTool ( )
inlineoverrideprotectedvirtual

Implements BaseToolHandle.

Definition at line 287 of file ToolHandle.h.

287  {
288  // const cast to support T being const
290  }
Handle to be used in lieu of naked pointers to gaudis.
Definition: GaudiHandle.h:163
std::remove_const_t< T > * nonConst(T *p)
Cast a pointer to a non const type.
Definition: GaudiHandle.h:20
template<class T>
StatusCode ToolHandle< T >::i_retrieve ( IAlgTool *&  algTool) const
inlineoverrideprotectedvirtual

Implements BaseToolHandle.

Definition at line 292 of file ToolHandle.h.

292  {
295  }
virtual StatusCode retrieve(const std::string &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.
const IInterface * parent() const noexcept
Definition: ToolHandle.h:44
ServiceHandle< IToolSvc > m_pToolSvc
Definition: ToolHandle.h:301
std::string typeAndName() const override
Definition: ToolHandle.h:260
static const InterfaceID & interfaceID()
Return an instance of InterfaceID identifying the interface.
Definition: IInterface.h:253
bool createIf() const noexcept
Definition: ToolHandle.h:42
template<class T>
StatusCode ToolHandle< T >::initialize ( const std::string toolTypeAndName,
const IInterface parent = nullptr,
bool  createIf = true 
)
inline

Definition at line 200 of file ToolHandle.h.

201  {
202 
203  GaudiHandleBase::setTypeAndName( toolTypeAndName );
206 
207  m_parent = parent;
209 
210  return m_pToolSvc.initialize( "ToolSvc", GaudiHandleBase::parentName() );
211  }
const IInterface * parent() const noexcept
Definition: ToolHandle.h:44
ServiceHandle< IToolSvc > m_pToolSvc
Definition: ToolHandle.h:301
void setTypeAndName(std::string myTypeAndName)
The component "type/name" string.
Definition: GaudiHandle.cpp:9
void setComponentType(const std::string &componentType)
The component type.
Definition: GaudiHandle.h:68
static std::string toolParentName(const IInterface *parent)
Definition: ToolHandle.h:52
const std::string & parentName() const
The name of the parent.
Definition: GaudiHandle.h:53
void setParentName(const std::string &parent)
The name of the parent.
Definition: GaudiHandle.h:71
static std::string toolComponentType(const IInterface *parent)
Definition: ToolHandle.h:50
StatusCode initialize(const std::string &serviceName, const std::string &theParentName)
Definition: ServiceHandle.h:61
const IInterface * m_parent
Definition: ToolHandle.h:58
bool createIf() const noexcept
Definition: ToolHandle.h:42
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 239 of file ToolHandle.h.

239  { // not really const, because it updates m_pObject
240  return GaudiHandle<T>::release();
241  }
StatusCode release() const
Release the component.
Definition: GaudiHandle.h:224
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 258 of file ToolHandle.h.

258 { return m_pToolSvc->releaseTool( ::details::nonConst( algTool ) ); }
ServiceHandle< IToolSvc > m_pToolSvc
Definition: ToolHandle.h:301
std::remove_const_t< T > * nonConst(T *p)
Cast a pointer to a non const type.
Definition: GaudiHandle.h:20
virtual StatusCode releaseTool(IAlgTool *tool)=0
Release the tool.
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 215 of file ToolHandle.h.

215  { // not really const, because it updates m_pObject
216  return GaudiHandle<T>::retrieve();
217  }
StatusCode retrieve() const
Retrieve the component.
Definition: GaudiHandle.h:212
template<class T>
StatusCode ToolHandle< T >::retrieve ( DisableTool  sd)
inlineoverridevirtual

Implements BaseToolHandle.

Definition at line 219 of file ToolHandle.h.

219  {
220  if ( isEnabled() && sd == DisableTool{false} ) {
221  return GaudiHandle<T>::retrieve();
222  } else {
223  disable();
224  return StatusCode::SUCCESS;
225  }
226  }
void disable()
Definition: ToolHandle.h:98
bool isEnabled() const
Helper to check if the ToolHandle should be retrieved.
Definition: ToolHandle.h:90
constexpr static const auto SUCCESS
Definition: StatusCode.h:85
StatusCode retrieve() const
Retrieve the component.
Definition: GaudiHandle.h:212
Gaudi::tagged_bool< class DisableTool_tag > DisableTool
Definition: ToolHandle.h:27
template<class T>
StatusCode ToolHandle< T >::retrieve ( EnableTool  sd)
inlineoverridevirtual

Implements BaseToolHandle.

Definition at line 228 of file ToolHandle.h.

228  {
229  if ( isEnabled() && sd == EnableTool{true} ) {
230  return GaudiHandle<T>::retrieve();
231  } else {
232  disable();
233  return StatusCode::SUCCESS;
234  }
235  }
void disable()
Definition: ToolHandle.h:98
bool isEnabled() const
Helper to check if the ToolHandle should be retrieved.
Definition: ToolHandle.h:90
constexpr static const auto SUCCESS
Definition: StatusCode.h:85
Gaudi::tagged_bool< class EnableTool_tag > EnableTool
Definition: ToolHandle.h:28
StatusCode retrieve() const
Retrieve the component.
Definition: GaudiHandle.h:212
template<class T>
StatusCode ToolHandle< T >::retrieve ( T *&  algTool) const
inlineoverridevirtual

Do the real retrieval of the AlgTool.

Implements GaudiHandle< T >.

Definition at line 244 of file ToolHandle.h.

244  {
245  IAlgTool* iface = nullptr;
246  if ( i_retrieve( iface ).isFailure() ) { return StatusCode::FAILURE; }
247 
248  algTool = dynamic_cast<T*>( iface );
249  if ( algTool == nullptr ) {
250  throw GaudiException( "unable to dcast AlgTool " + typeAndName() + " to interface " +
251  System::typeinfoName( typeid( T ) ),
252  typeAndName() + " retrieve", StatusCode::FAILURE );
253  }
254  return StatusCode::SUCCESS;
255  }
Define general base for Gaudi exception.
GAUDI_API const std::string typeinfoName(const std::type_info &)
Get platform independent information about the class type.
Definition: System.cpp:309
constexpr static const auto SUCCESS
Definition: StatusCode.h:85
StatusCode i_retrieve(IAlgTool *&algTool) const override
Definition: ToolHandle.h:292
std::string typeAndName() const override
Definition: ToolHandle.h:260
The interface implemented by the AlgTool base class.
Definition: IAlgTool.h:23
constexpr static const auto FAILURE
Definition: StatusCode.h:86
template<class T>
std::string ToolHandle< T >::typeAndName ( ) const
inlineoverridevirtual

Implements BaseToolHandle.

Definition at line 260 of file ToolHandle.h.

260 { return GaudiHandleBase::typeAndName(); }
std::string typeAndName() const
The full type and name: "type/name".
Definition: GaudiHandle.h:115

Friends And Related Function Documentation

template<class T>
friend class AlgTool
friend

Definition at line 128 of file ToolHandle.h.

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

Definition at line 127 of file ToolHandle.h.

template<class T>
friend class Service
friend

Definition at line 129 of file ToolHandle.h.

Member Data Documentation

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

Definition at line 301 of file ToolHandle.h.


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