The Gaudi Framework  v36r9p1 (5c15b2bb)
GaudiHandle< T > Class Template Referenceabstract

#include <GaudiKernel/GaudiHandle.h>

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

Public Member Functions

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
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 (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 ()=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

 GaudiHandle (std::string myTypeAndName, std::string myComponentType, std::string myParentName)
 
virtual StatusCode retrieve (T *&) const =0
 Retrieve the component. More...
 
virtual StatusCode release (T *comp) const
 Release the component. More...
 
- 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

void setDefaultTypeAndName ()
 Helper function to set default name and type. More...
 
void setDefaultType ()
 Helper function to set default type from the class type T. More...
 
void assertObject () const
 Load the pointer to the component. More...
 

Private Attributes

std::atomic< T * > m_pObject = nullptr
 

Additional Inherited Members

- Public Types inherited from GaudiHandleBase
using PropertyType = GaudiHandleProperty
 

Detailed Description

template<class T>
class GaudiHandle< T >

Handle to be used in lieu of naked pointers to gaudis. This allows better control through the framework of gaudi loading and usage. T is the type of the component interface (or concrete class).

Author
Marti.nosp@m.n.Wo.nosp@m.udstr.nosp@m.a@ce.nosp@m.rn.ch

Definition at line 173 of file GaudiHandle.h.

Constructor & Destructor Documentation

◆ GaudiHandle() [1/3]

template<class T >
GaudiHandle< T >::GaudiHandle ( std::string  myTypeAndName,
std::string  myComponentType,
std::string  myParentName 
)
inlineprotected

Definition at line 178 of file GaudiHandle.h.

179  : GaudiHandleBase( std::move( myTypeAndName ), std::move( myComponentType ), std::move( myParentName ) ) {}

◆ GaudiHandle() [2/3]

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

Copy constructor needed for correct ref-counting.

Definition at line 184 of file GaudiHandle.h.

186  : GaudiHandleBase( other ) {
187  m_pObject = other.get();
188  if ( m_pObject ) ::details::nonConst( m_pObject.load() )->addRef();
189  }

◆ GaudiHandle() [3/3]

template<class T >
GaudiHandle< T >::GaudiHandle ( const GaudiHandle< T > &  other)
inline

Copy constructor needed for correct ref-counting.

Definition at line 192 of file GaudiHandle.h.

192  : GaudiHandleBase( other ) {
193  m_pObject = other.m_pObject.load();
194  if ( m_pObject ) ::details::nonConst( m_pObject.load() )->addRef();
195  }

Member Function Documentation

◆ assertObject()

template<class T >
void GaudiHandle< T >::assertObject ( ) const
inlineprivate

Load the pointer to the component.

Do a retrieve if needed. Throw an exception if retrieval fails.

Definition at line 322 of file GaudiHandle.h.

322  { // not really const, because it may update m_pObject
323  if ( !isValid() ) {
324  throw GaudiException( "Failed to retrieve " + componentType() + ": " + typeAndName(),
325  componentType() + " retrieve", StatusCode::FAILURE );
326  }
327  }

◆ get() [1/2]

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

Return the wrapped pointer, not calling retrieve() if null.

Definition at line 260 of file GaudiHandle.h.

260 { return m_pObject; }

◆ get() [2/2]

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

Return the wrapped pointer, not calling retrieve() if null.

Definition at line 263 of file GaudiHandle.h.

263 { return m_pObject; }

◆ getDefaultName()

template<class T >
std::string GaudiHandle< T >::getDefaultName ( )
inline

Definition at line 293 of file GaudiHandle.h.

293  {
294  const auto defName = GaudiHandleBase::type();
295  return ( defName.empty() ? getDefaultType() : defName );
296  }

◆ getDefaultType()

template<class T >
std::string GaudiHandle< T >::getDefaultType ( )
inline

Helper function to get default type string from the class type.

Definition at line 291 of file GaudiHandle.h.

291 { return System::typeinfoName( typeid( T ) ); }

◆ isSet()

template<class T >
bool GaudiHandle< T >::isSet ( ) const
inline

True if the wrapped pointer is not null.

Definition at line 266 of file GaudiHandle.h.

266 { return get(); }

◆ isValid()

template<class T >
bool GaudiHandle< T >::isValid ( ) const
inline

Check if the handle is valid (try to retrive the object is not done yet).

Definition at line 247 of file GaudiHandle.h.

247  {
248  // not really const, because it may update m_pObject
249  return m_pObject || retrieve().isSuccess();
250  }

◆ operator bool()

template<class T >
GaudiHandle< T >::operator bool ( ) const
inline

For testing if handle has component.

Does retrieve() if needed. If this returns false, the component could not be retrieved.

Definition at line 254 of file GaudiHandle.h.

254  {
255  // not really const, because it may update m_pObject
256  return isValid();
257  }

◆ operator*() [1/2]

template<class T >
T& GaudiHandle< T >::operator* ( )
inline

Definition at line 268 of file GaudiHandle.h.

268  {
269  assertObject();
270  return *m_pObject;
271  }

◆ operator*() [2/2]

template<class T >
std::add_const_t<T>& GaudiHandle< T >::operator* ( ) const
inline

Definition at line 278 of file GaudiHandle.h.

278  {
279  // not really const, because it may update m_pObject
280  assertObject();
281  return *m_pObject;
282  }

◆ operator->() [1/2]

template<class T >
T* GaudiHandle< T >::operator-> ( )
inline

Definition at line 273 of file GaudiHandle.h.

273  {
274  assertObject();
275  return m_pObject;
276  }

◆ operator->() [2/2]

template<class T >
std::add_const_t<T>* GaudiHandle< T >::operator-> ( ) const
inline

Definition at line 284 of file GaudiHandle.h.

284  {
285  // not really const, because it may update m_pObject
286  assertObject();
287  return m_pObject;
288  }

◆ operator=() [1/2]

template<class T >
GaudiHandle& GaudiHandle< T >::operator= ( const GaudiHandle< T > &  other)
inline

Assignment operator for correct ref-counting.

Definition at line 211 of file GaudiHandle.h.

211  {
212  GaudiHandleBase::operator=( other );
213  // release any current tool
214  release().ignore();
215  m_pObject = other.m_pObject.load();
216  // update ref-counting
217  if ( m_pObject ) ::details::nonConst( m_pObject.load() )->addRef();
218  return *this;
219  }

◆ operator=() [2/2]

template<class T >
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&> GaudiHandle< T >::operator= ( const GaudiHandle< NCT > &  other)
inline

Assignment operator for correct ref-counting.

Definition at line 200 of file GaudiHandle.h.

200  {
201  GaudiHandleBase::operator=( other );
202  // release any current tool
203  release().ignore();
204  m_pObject = other.get();
205  // update ref-counting
206  if ( m_pObject ) ::details::nonConst( m_pObject.load() )->addRef();
207  return *this;
208  }

◆ release() [1/2]

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

Release the component.

Definition at line 236 of file GaudiHandle.h.

236  {
237  // not really const, because it updates m_pObject
239  if ( m_pObject ) {
240  sc = release( m_pObject );
241  m_pObject = nullptr;
242  }
243  return sc;
244  }

◆ release() [2/2]

template<class T >
virtual StatusCode GaudiHandle< T >::release ( T *  comp) const
inlineprotectedvirtual

Release the component.

Default implementation calls release() on the component. Can be overridden by the derived class if something else is needed.

Reimplemented in ToolHandle< T >, ToolHandle< IThreadInitTool >, ToolHandle< IMyTool >, ToolHandle< IMyOtherTool >, ToolHandle< IAlgTool >, ToolHandle< GaudiTesting::ITestTool >, ToolHandle< Gaudi::Tests::Histograms::Directories::HistoGroupsTool >, ToolHandle< Gaudi::Examples::FloatTool >, and ToolHandle< const IMyTool >.

Definition at line 304 of file GaudiHandle.h.

304  { // not really const, because it updates m_pObject
305  // const cast to support T being a const type
306  ::details::nonConst( comp )->release();
307  return StatusCode::SUCCESS;
308  }

◆ retrieve() [1/2]

template<class T >
StatusCode GaudiHandle< T >::retrieve ( ) const
inline

Retrieve the component.

Release existing component if needed.

Definition at line 222 of file GaudiHandle.h.

222  {
223  // not really const, because it updates m_pObject
224  // Do the lookup into a temporary pointer.
225  T* p = nullptr;
226  if ( retrieve( p ).isFailure() ) { return StatusCode::FAILURE; }
227 
228  // If m_pObject is null, then copy p to m_pObject.
229  // Otherwise, release p.
230  T* old = nullptr;
231  if ( m_pObject.compare_exchange_strong( old, p ) ) { return StatusCode::SUCCESS; }
232  return release( p );
233  }

◆ retrieve() [2/2]

◆ setDefaultType()

template<class T >
void GaudiHandle< T >::setDefaultType ( )
inlineprivate

Helper function to set default type from the class type T.

Definition at line 318 of file GaudiHandle.h.

◆ setDefaultTypeAndName()

template<class T >
void GaudiHandle< T >::setDefaultTypeAndName ( )
inlineprivate

Helper function to set default name and type.

Definition at line 312 of file GaudiHandle.h.

312  {
313  const std::string& myType = getDefaultType();
314  GaudiHandleBase::setTypeAndName( myType + '/' + myType );
315  }

Member Data Documentation

◆ m_pObject

template<class T >
std::atomic<T*> GaudiHandle< T >::m_pObject = nullptr
mutableprivate

Definition at line 333 of file GaudiHandle.h.


The documentation for this class was generated from the following file:
std::string
STL class.
std::move
T move(T... args)
StatusCode::isSuccess
bool isSuccess() const
Definition: StatusCode.h:314
GaudiHandle::getDefaultType
std::string getDefaultType()
Helper function to get default type string from the class type.
Definition: GaudiHandle.h:291
GaudiException
Definition: GaudiException.h:31
System::typeinfoName
GAUDI_API const std::string typeinfoName(const std::type_info &)
Get platform independent information about the class type.
Definition: System.cpp:310
GaudiHandleInfo::componentType
const std::string & componentType() const
Definition: GaudiHandle.h:54
std::atomic::compare_exchange_strong
T compare_exchange_strong(T... args)
GaudiHandle::get
T * get()
Return the wrapped pointer, not calling retrieve() if null.
Definition: GaudiHandle.h:260
details::nonConst
std::remove_const_t< T > * nonConst(T *p)
Cast a pointer to a non const type.
Definition: GaudiHandle.h:30
StatusCode
Definition: StatusCode.h:65
std::atomic::load
T load(T... args)
GaudiHandleBase::typeAndName
std::string typeAndName() const
The full type and name: "type/name".
Definition: GaudiHandle.h:125
GaudiHandle::m_pObject
std::atomic< T * > m_pObject
Definition: GaudiHandle.h:333
StatusCode::ignore
const StatusCode & ignore() const
Allow discarding a StatusCode without warning.
Definition: StatusCode.h:139
GaudiHandle::isValid
bool isValid() const
Check if the handle is valid (try to retrive the object is not done yet).
Definition: GaudiHandle.h:247
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
GaudiHandleBase::GaudiHandleBase
GaudiHandleBase(std::string myTypeAndName, std::string myComponentType, std::string myParentName)
Create a handle ('smart pointer') to a gaudi component.
Definition: GaudiHandle.h:115
StatusCode::FAILURE
constexpr static const auto FAILURE
Definition: StatusCode.h:101
GaudiHandle::assertObject
void assertObject() const
Load the pointer to the component.
Definition: GaudiHandle.h:322
GaudiHandle::retrieve
StatusCode retrieve() const
Retrieve the component.
Definition: GaudiHandle.h:222
GaudiHandle::release
StatusCode release() const
Release the component.
Definition: GaudiHandle.h:236
GaudiHandleBase::setTypeAndName
void setTypeAndName(std::string myTypeAndName)
The component "type/name" string.
Definition: GaudiHandle.cpp:19