The Gaudi Framework  master (cc9a61f4)
Loading...
Searching...
No Matches
GaudiHandle< T > Class Template Referenceabstract

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

#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>>
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

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

void assertObject () const
 Load the pointer to the component.

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 188 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 193 of file GaudiHandle.h.

GaudiHandleBase(std::string myTypeAndName, std::string myComponentType, std::string myParentName)
Create a handle ('smart pointer') to a gaudi component.
Handle to be used in lieu of naked pointers to gaudis.

◆ GaudiHandle() [2/3]

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

Copy constructor needed for correct ref-counting.

Definition at line 199 of file GaudiHandle.h.

202 if ( m_pObject ) ::details::nonConst( m_pObject.load() )->addRef();
203 }
T * get()
Return the wrapped pointer, not calling retrieve() if null.
std::atomic< T * > m_pObject
std::remove_const_t< T > * nonConst(T *p)
Cast a pointer to a non const type.
Definition GaudiHandle.h:26

◆ GaudiHandle() [3/3]

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

Copy constructor needed for correct ref-counting.

Definition at line 206 of file GaudiHandle.h.

207 if ( m_pObject ) ::details::nonConst( m_pObject.load() )->addRef();
208 }

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 326 of file GaudiHandle.h.

326 { // not really const, because it may update m_pObject
327 if ( !isValid() ) {
328 throw GaudiException( "Failed to retrieve " + componentType() + ": " + typeAndName(),
329 componentType() + " retrieve", StatusCode::FAILURE );
330 }
331 }
const std::string & typeAndName() const
The full type and name: "type/name".
bool isValid() const
Check if the handle is valid (try to retrive the object is not done yet).
const std::string & componentType() const
Definition GaudiHandle.h:53

◆ get() [1/2]

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

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

Definition at line 273 of file GaudiHandle.h.

273{ 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 276 of file GaudiHandle.h.

276{ return m_pObject; }

◆ getDefaultName()

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

Definition at line 306 of file GaudiHandle.h.

306 {
307 const auto defName = GaudiHandleBase::type();
308 return ( defName.empty() ? getDefaultType() : defName );
309 }
bool empty() const
Check if the handle has been set to empty string (i.e.
std::string type() const
The concrete component class name: the part before the '/'.
std::string getDefaultType()
Helper function to get default type string from the class type.

◆ getDefaultType()

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

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

Definition at line 304 of file GaudiHandle.h.

304{ return System::typeinfoName( typeid( T ) ); }
GAUDI_API const std::string typeinfoName(const std::type_info &)
Get platform independent information about the class type.
Definition System.cpp:254

◆ isSet()

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

True if the wrapped pointer is not null.

Definition at line 279 of file GaudiHandle.h.

279{ 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 260 of file GaudiHandle.h.

260 {
261 // not really const, because it may update m_pObject
262 return m_pObject || retrieve().isSuccess();
263 }
StatusCode retrieve() const
Retrieve the component.
bool isSuccess() const
Definition StatusCode.h:302

◆ 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 267 of file GaudiHandle.h.

267 {
268 // not really const, because it may update m_pObject
269 return isValid();
270 }

◆ operator*() [1/2]

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

Definition at line 281 of file GaudiHandle.h.

281 {
282 assertObject();
283 return *m_pObject;
284 }
void assertObject() const
Load the pointer to the component.

◆ operator*() [2/2]

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

Definition at line 291 of file GaudiHandle.h.

291 {
292 // not really const, because it may update m_pObject
293 assertObject();
294 return *m_pObject;
295 }

◆ operator->() [1/2]

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

Definition at line 286 of file GaudiHandle.h.

286 {
287 assertObject();
288 return m_pObject;
289 }

◆ operator->() [2/2]

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

Definition at line 297 of file GaudiHandle.h.

297 {
298 // not really const, because it may update m_pObject
299 assertObject();
300 return m_pObject;
301 }

◆ operator=() [1/2]

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

Assignment operator for correct ref-counting.

Definition at line 224 of file GaudiHandle.h.

224 {
226 // release any current tool
227 release().ignore();
228 m_pObject = other.m_pObject.load();
229 // update ref-counting
230 if ( m_pObject ) ::details::nonConst( m_pObject.load() )->addRef();
231 return *this;
232 }
GaudiHandleBase & operator=(const std::string &typeAndName)
Set "type/name" from string.
StatusCode release() const
Release the component.
void ignore() const
Allow discarding a StatusCode without warning.
Definition StatusCode.h:128

◆ operator=() [2/2]

template<class T>
template<typename CT = T, typename NCT = std::remove_const_t<T>>
GaudiHandle & GaudiHandle< T >::operator= ( const GaudiHandle< NCT > & other)
inline

Assignment operator for correct ref-counting.

Definition at line 213 of file GaudiHandle.h.

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

◆ release() [1/2]

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

Release the component.

Definition at line 249 of file GaudiHandle.h.

249 {
250 // not really const, because it updates m_pObject
252 if ( m_pObject ) {
253 sc = release( m_pObject );
254 m_pObject = nullptr;
255 }
256 return sc;
257 }

◆ 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 >, and ToolHandle< Gaudi::Interface::Bind::IBinder< std::decay_t< T > > >.

Definition at line 317 of file GaudiHandle.h.

317 { // not really const, because it updates m_pObject
318 // const cast to support T being a const type
319 ::details::nonConst( comp )->release();
320 return StatusCode::SUCCESS;
321 }

◆ retrieve() [1/2]

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

Retrieve the component.

Release existing component if needed.

Definition at line 235 of file GaudiHandle.h.

235 {
236 // not really const, because it updates m_pObject
237 // Do the lookup into a temporary pointer.
238 T* p = nullptr;
239 if ( retrieve( p ).isFailure() ) { return StatusCode::FAILURE; }
240
241 // If m_pObject is null, then copy p to m_pObject.
242 // Otherwise, release p.
243 T* old = nullptr;
244 if ( m_pObject.compare_exchange_strong( old, p ) ) { return StatusCode::SUCCESS; }
245 return release( p );
246 }

◆ retrieve() [2/2]

template<class T>
virtual StatusCode GaudiHandle< T >::retrieve ( T *& ) const
protectedpure virtual

Retrieve the component.

To be implemented by the derived class. It will pass the pointer

Implemented in ServiceHandle< T >, ToolHandle< T >, and ToolHandle< Gaudi::Interface::Bind::IBinder< std::decay_t< T > > >.

Member Data Documentation

◆ m_pObject

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

Definition at line 337 of file GaudiHandle.h.


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