The Gaudi Framework  v30r3 (a5ef0a68)
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 = 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

 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 (const std::string &componentType)
 The component type. More...
 
void setParentName (const 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

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

Constructor & Destructor Documentation

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

Definition at line 176 of file GaudiHandle.h.

177  : GaudiHandleBase( std::move( myTypeAndName ), std::move( myComponentType ), std::move( myParentName ) )
178  {
179  }
T move(T...args)
GaudiHandleBase(std::string myTypeAndName, std::string myComponentType, std::string myParentName)
Create a handle (&#39;smart pointer&#39;) to a gaudi component.
Definition: GaudiHandle.h:111
template<class T>
template<typename CT = T, typename NCT = typename std::remove_const<T>::type>
GaudiHandle< T >::GaudiHandle ( const GaudiHandle< NCT > &  other,
typename std::enable_if< std::is_const< CT >::value &&!std::is_same< CT, NCT >::value >::type = nullptr 
)
inline

Copy constructor needed for correct ref-counting.

Definition at line 184 of file GaudiHandle.h.

186  : GaudiHandleBase( other )
187  {
188  m_pObject = other.get();
189  if ( m_pObject ) details::nonConst( m_pObject )->addRef();
190  }
std::remove_const_t< T > * nonConst(T *p)
Cast a pointer to a non const type.
Definition: GaudiHandle.h:21
T * get()
Return the wrapped pointer, not calling retrieve() if null.
Definition: GaudiHandle.h:264
GaudiHandleBase(std::string myTypeAndName, std::string myComponentType, std::string myParentName)
Create a handle (&#39;smart pointer&#39;) to a gaudi component.
Definition: GaudiHandle.h:111
template<class T>
GaudiHandle< T >::GaudiHandle ( const GaudiHandle< T > &  other)
inline

Copy constructor needed for correct ref-counting.

Definition at line 193 of file GaudiHandle.h.

193  : GaudiHandleBase( other )
194  {
195  m_pObject = other.m_pObject;
196  if ( m_pObject ) details::nonConst( m_pObject )->addRef();
197  }
std::remove_const_t< T > * nonConst(T *p)
Cast a pointer to a non const type.
Definition: GaudiHandle.h:21
GaudiHandleBase(std::string myTypeAndName, std::string myComponentType, std::string myParentName)
Create a handle (&#39;smart pointer&#39;) to a gaudi component.
Definition: GaudiHandle.h:111

Member Function Documentation

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

334  { // not really const, because it may update m_pObject
335  if ( UNLIKELY( !isValid() ) ) {
336  throw GaudiException( "Failed to retrieve " + componentType() + ": " + typeAndName(),
337  componentType() + " retrieve", StatusCode::FAILURE );
338  }
339  }
#define UNLIKELY(x)
Definition: Kernel.h:122
constexpr static const auto FAILURE
Definition: StatusCode.h:88
Define general base for Gaudi exception.
bool isValid() const
Check if the handle is valid (try to retrive the object is not done yet).
Definition: GaudiHandle.h:251
std::string typeAndName() const
The full type and name: "type/name".
Definition: GaudiHandle.h:122
const std::string & componentType() const
Definition: GaudiHandle.h:49
template<class T>
T* GaudiHandle< T >::get ( )
inline

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

Definition at line 264 of file GaudiHandle.h.

264 { return m_pObject; }
template<class T>
std::add_const<T>::type* GaudiHandle< T >::get ( ) const
inline

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

Definition at line 267 of file GaudiHandle.h.

267 { return m_pObject; }
template<class T>
std::string GaudiHandle< T >::getDefaultName ( )
inline

Definition at line 301 of file GaudiHandle.h.

302  {
303  const auto defName = GaudiHandleBase::type();
304  return ( defName.empty() ? getDefaultType() : defName );
305  }
std::string getDefaultType()
Helper function to get default type string from the class type.
Definition: GaudiHandle.h:299
std::string type() const
The concrete component class name: the part before the &#39;/&#39;.
Definition: GaudiHandle.cpp:11
template<class T>
std::string GaudiHandle< T >::getDefaultType ( )
inline

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

Definition at line 299 of file GaudiHandle.h.

299 { 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:332
template<class T>
bool GaudiHandle< T >::isSet ( ) const
inline

True if the wrapped pointer is not null.

Definition at line 270 of file GaudiHandle.h.

270 { return get(); }
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 251 of file GaudiHandle.h.

252  { // not really const, because it may update m_pObject
253  return m_pObject || retrieve().isSuccess();
254  }
bool isSuccess() const
Definition: StatusCode.h:287
StatusCode retrieve() const
Retrieve the component.
Definition: GaudiHandle.h:226
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 258 of file GaudiHandle.h.

259  { // not really const, because it may update m_pObject
260  return isValid();
261  }
bool isValid() const
Check if the handle is valid (try to retrive the object is not done yet).
Definition: GaudiHandle.h:251
template<class T>
T& GaudiHandle< T >::operator* ( )
inline

Definition at line 272 of file GaudiHandle.h.

273  {
274  assertObject();
275  return *m_pObject;
276  }
void assertObject() const
Load the pointer to the component.
Definition: GaudiHandle.h:333
template<class T>
std::add_const<T>::type& GaudiHandle< T >::operator* ( ) const
inline

Definition at line 284 of file GaudiHandle.h.

285  {
286  // not really const, because it may update m_pObject
287  assertObject();
288  return *m_pObject;
289  }
void assertObject() const
Load the pointer to the component.
Definition: GaudiHandle.h:333
template<class T>
T* GaudiHandle< T >::operator-> ( )
inline

Definition at line 278 of file GaudiHandle.h.

279  {
280  assertObject();
281  return m_pObject;
282  }
void assertObject() const
Load the pointer to the component.
Definition: GaudiHandle.h:333
template<class T>
std::add_const<T>::type* GaudiHandle< T >::operator-> ( ) const
inline

Definition at line 291 of file GaudiHandle.h.

292  {
293  // not really const, because it may update m_pObject
294  assertObject();
295  return m_pObject;
296  }
void assertObject() const
Load the pointer to the component.
Definition: GaudiHandle.h:333
template<class T>
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 GaudiHandle< T >::operator= ( const GaudiHandle< NCT > &  other)
inline

Assignment operator for correct ref-counting.

Definition at line 202 of file GaudiHandle.h.

203  {
205  // release any current tool
206  release().ignore();
207  m_pObject = other.get();
208  // update ref-counting
209  if ( m_pObject ) details::nonConst( m_pObject )->addRef();
210  return *this;
211  }
StatusCode release() const
Release the component.
Definition: GaudiHandle.h:240
PropertyMgr & operator=(const PropertyMgr &)=delete
std::remove_const_t< T > * nonConst(T *p)
Cast a pointer to a non const type.
Definition: GaudiHandle.h:21
T * get()
Return the wrapped pointer, not calling retrieve() if null.
Definition: GaudiHandle.h:264
const StatusCode & ignore() const
Ignore/check StatusCode.
Definition: StatusCode.h:165
template<class T>
GaudiHandle& GaudiHandle< T >::operator= ( const GaudiHandle< T > &  other)
inline

Assignment operator for correct ref-counting.

Definition at line 214 of file GaudiHandle.h.

215  {
217  // release any current tool
218  release().ignore();
219  m_pObject = other.m_pObject;
220  // update ref-counting
221  if ( m_pObject ) details::nonConst( m_pObject )->addRef();
222  return *this;
223  }
StatusCode release() const
Release the component.
Definition: GaudiHandle.h:240
PropertyMgr & operator=(const PropertyMgr &)=delete
std::remove_const_t< T > * nonConst(T *p)
Cast a pointer to a non const type.
Definition: GaudiHandle.h:21
const StatusCode & ignore() const
Ignore/check StatusCode.
Definition: StatusCode.h:165
template<class T>
StatusCode GaudiHandle< T >::release ( ) const
inline

Release the component.

Definition at line 240 of file GaudiHandle.h.

241  { // not really const, because it updates m_pObject
243  if ( m_pObject ) {
244  sc = release( m_pObject );
245  m_pObject = nullptr;
246  }
247  return sc;
248  }
StatusCode release() const
Release the component.
Definition: GaudiHandle.h:240
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:51
constexpr static const auto SUCCESS
Definition: StatusCode.h:87
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< IThreadInitTool >.

Definition at line 313 of file GaudiHandle.h.

314  { // not really const, because it updates m_pObject
315  // const cast to support T being a const type
316  details::nonConst( comp )->release();
317  return StatusCode::SUCCESS;
318  }
std::remove_const_t< T > * nonConst(T *p)
Cast a pointer to a non const type.
Definition: GaudiHandle.h:21
constexpr static const auto SUCCESS
Definition: StatusCode.h:87
template<class T>
StatusCode GaudiHandle< T >::retrieve ( ) const
inline

Retrieve the component.

Release existing component if needed.

Definition at line 226 of file GaudiHandle.h.

227  { // not really const, because it updates m_pObject
229  if ( m_pObject && release().isFailure() ) {
230  sc = StatusCode::FAILURE;
231  }
232  if ( sc && retrieve( m_pObject ).isFailure() ) {
233  m_pObject = nullptr;
234  sc = StatusCode::FAILURE;
235  }
236  return sc;
237  }
constexpr static const auto FAILURE
Definition: StatusCode.h:88
StatusCode release() const
Release the component.
Definition: GaudiHandle.h:240
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:51
StatusCode retrieve() const
Retrieve the component.
Definition: GaudiHandle.h:226
constexpr static const auto SUCCESS
Definition: StatusCode.h:87
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 ToolHandle< T >, ToolHandle< IThreadInitTool >, ServiceHandle< T >, and ServiceHandle< IToolSvc >.

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

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

Definition at line 329 of file GaudiHandle.h.

std::string getDefaultType()
Helper function to get default type string from the class type.
Definition: GaudiHandle.h:299
void setTypeAndName(std::string myTypeAndName)
The component "type/name" string.
Definition: GaudiHandle.cpp:9
template<class T>
void GaudiHandle< T >::setDefaultTypeAndName ( )
inlineprivate

Helper function to set default name and type.

Definition at line 322 of file GaudiHandle.h.

323  {
324  const std::string& myType = getDefaultType();
325  GaudiHandleBase::setTypeAndName( myType + '/' + myType );
326  }
std::string getDefaultType()
Helper function to get default type string from the class type.
Definition: GaudiHandle.h:299
void setTypeAndName(std::string myTypeAndName)
The component "type/name" string.
Definition: GaudiHandle.cpp:9
STL class.

Member Data Documentation

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

Definition at line 345 of file GaudiHandle.h.


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