The Gaudi Framework  master (ff829712)
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>>
requires ( std::is_const_v<CT> && !std::is_same_v<CT, NCT> )
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.
 
- 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 176 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 181 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 187 of file GaudiHandle.h.

190 if ( m_pObject ) ::details::nonConst( m_pObject.load() )->addRef();
191 }
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 194 of file GaudiHandle.h.

195 if ( m_pObject ) ::details::nonConst( m_pObject.load() )->addRef();
196 }

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

314 { // not really const, because it may update m_pObject
315 if ( !isValid() ) {
316 throw GaudiException( "Failed to retrieve " + componentType() + ": " + typeAndName(),
317 componentType() + " retrieve", StatusCode::FAILURE );
318 }
319 }
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 261 of file GaudiHandle.h.

261{ 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 264 of file GaudiHandle.h.

264{ return m_pObject; }

◆ getDefaultName()

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

Definition at line 294 of file GaudiHandle.h.

294 {
295 const auto defName = GaudiHandleBase::type();
296 return ( defName.empty() ? getDefaultType() : defName );
297 }
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 292 of file GaudiHandle.h.

292{ 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:260

◆ isSet()

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

True if the wrapped pointer is not null.

Definition at line 267 of file GaudiHandle.h.

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

248 {
249 // not really const, because it may update m_pObject
250 return m_pObject || retrieve().isSuccess();
251 }
StatusCode retrieve() const
Retrieve the component.
bool isSuccess() const
Definition StatusCode.h:314

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

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

◆ operator*() [1/2]

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

Definition at line 269 of file GaudiHandle.h.

269 {
270 assertObject();
271 return *m_pObject;
272 }
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 279 of file GaudiHandle.h.

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

◆ operator->() [1/2]

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

Definition at line 274 of file GaudiHandle.h.

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

◆ operator->() [2/2]

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

Definition at line 285 of file GaudiHandle.h.

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

◆ operator=() [1/2]

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

Assignment operator for correct ref-counting.

Definition at line 212 of file GaudiHandle.h.

212 {
214 // release any current tool
215 release().ignore();
216 m_pObject = other.m_pObject.load();
217 // update ref-counting
218 if ( m_pObject ) ::details::nonConst( m_pObject.load() )->addRef();
219 return *this;
220 }
StatusCode release() const
Release the component.
const StatusCode & ignore() const
Allow discarding a StatusCode without warning.
Definition StatusCode.h:139

◆ operator=() [2/2]

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

Assignment operator for correct ref-counting.

Definition at line 201 of file GaudiHandle.h.

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

◆ release() [1/2]

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

Release the component.

Definition at line 237 of file GaudiHandle.h.

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

◆ 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< const IMyTool >, ToolHandle< Gaudi::Tests::Histograms::Directories::HistoGroupsTool >, ToolHandle< Gaudi::TestSuite::FloatTool >, ToolHandle< GaudiTesting::ITestTool >, ToolHandle< IAlgTool >, ToolHandle< IMyTool >, ToolHandle< IThreadInitTool >, and ToolHandle< IWrongTool >.

Definition at line 305 of file GaudiHandle.h.

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

◆ retrieve() [1/2]

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

Retrieve the component.

Release existing component if needed.

Definition at line 223 of file GaudiHandle.h.

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

◆ retrieve() [2/2]

Member Data Documentation

◆ m_pObject

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

Definition at line 325 of file GaudiHandle.h.


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