The Gaudi Framework  master (d98a2936)
SmartIF Class Reference

#include <GaudiKernel/SmartIF.h>

Public Member Functions

 SmartIF ()=default
 
 SmartIF (TYPE *ptr)
 Standard constructor from pointer. More...
 
template<class OTHER >
 SmartIF (OTHER *ptr)
 Standard constructor from any (IInterface-derived) pointer. More...
 
 SmartIF (const SmartIF &rhs)
 
 SmartIF (SmartIF &&rhs)
 
SmartIFoperator= (SmartIF &&rhs)
 
template<class T >
 SmartIF (const SmartIF< T > &rhs)
 Constructor from another SmartIF, with a different type. More...
 
 ~SmartIF ()
 
template<class T >
 SmartIF (std::unique_ptr< T > &&rhs)
 
bool isValid () const
 Allow for check if smart pointer is valid. More...
 
 operator bool () const
 
bool operator! () const
 
 operator TYPE * () const
 Automatic conversion to pointer. More...
 
TYPE * operator-> () const
 Dereference operator. More...
 
TYPE & operator* () const
 Dereference operator. More...
 
TYPE * get () const
 Get interface pointer. More...
 
void reset (TYPE *ptr=nullptr)
 Set the internal pointer to the passed one disposing of the old one. More...
 
template<class OTHER >
void reset (OTHER *ptr)
 Set the internal pointer to the passed one disposing of the old one. More...
 
template<typename IFace >
SmartIF< IFace > as () const
 return a new SmartIF instance to another interface More...
 
SmartIFoperator= (IInterface *ptr)
 Assignment operator from IInterface pointer. More...
 
SmartIFoperator= (const SmartIF &rhs)
 Assignment operator. More...
 
template<class T >
SmartIFoperator= (const SmartIF< T > &rhs)
 Assignment operator from a different SmartIF. More...
 

Private Attributes

TYPE * m_interface = nullptr
 Pointer to the instance. More...
 

Detailed Description

Small smart pointer class with automatic reference counting for IInterface.

SmartIF simplifies the interaction with components in Gaudi by implementing an automatic reference counting and the casting (via IInterface::queryInterface).

Author
Markus Frank
Sebastien Ponce
Marco Clemencic

Definition at line 22 of file IConverter.h.

Constructor & Destructor Documentation

◆ SmartIF() [1/7]

SmartIF::SmartIF ( )
inlinedefault

◆ SmartIF() [2/7]

SmartIF::SmartIF ( TYPE *  ptr)
inline

Standard constructor from pointer.

Definition at line 36 of file SmartIF.h.

36  : m_interface( ptr ) {
37  if ( m_interface ) m_interface->addRef();
38  }

◆ SmartIF() [3/7]

template<class OTHER >
SmartIF::SmartIF ( OTHER *  ptr)
inline

Standard constructor from any (IInterface-derived) pointer.

Definition at line 41 of file SmartIF.h.

41  {
42  if ( ptr ) reset( ptr );
43  }

◆ SmartIF() [4/7]

SmartIF::SmartIF ( const SmartIF rhs)
inline

Definition at line 44 of file SmartIF.h.

44  : m_interface( rhs.get() ) {
45  if ( m_interface ) m_interface->addRef();
46  }

◆ SmartIF() [5/7]

SmartIF::SmartIF ( SmartIF &&  rhs)
inline

Definition at line 47 of file SmartIF.h.

47 : m_interface( rhs.m_interface ) { rhs.m_interface = nullptr; }

◆ SmartIF() [6/7]

template<class T >
SmartIF::SmartIF ( const SmartIF< T > &  rhs)
inlineexplicit

Constructor from another SmartIF, with a different type.

Note
it cannot replace the copy constructor.

Definition at line 58 of file SmartIF.h.

58  {
59  reset( rhs.get() );
60  }

◆ ~SmartIF()

SmartIF::~SmartIF ( )
inline

Definition at line 61 of file SmartIF.h.

61 { reset(); }

◆ SmartIF() [7/7]

template<class T >
SmartIF::SmartIF ( std::unique_ptr< T > &&  rhs)
inline

Definition at line 64 of file SmartIF.h.

64  {
65  reset( rhs.release() );
66  }

Member Function Documentation

◆ as()

template<typename IFace >
SmartIF<IFace> SmartIF::as ( ) const
inline

return a new SmartIF instance to another interface

Definition at line 110 of file SmartIF.h.

110  {
111  return SmartIF<IFace>{ *this };
112  }

◆ get()

TYPE* SmartIF::get ( ) const
inline

Get interface pointer.

Definition at line 82 of file SmartIF.h.

82 { return m_interface; }

◆ isValid()

bool SmartIF::isValid ( ) const
inline

Allow for check if smart pointer is valid.

Definition at line 69 of file SmartIF.h.

69 { return m_interface != nullptr; }

◆ operator bool()

SmartIF::operator bool ( ) const
inlineexplicit

Definition at line 71 of file SmartIF.h.

71 { return isValid(); }

◆ operator TYPE *()

SmartIF::operator TYPE * ( ) const
inline

Automatic conversion to pointer.

It is also used by the compiler for automatic conversion to boolean.

Definition at line 76 of file SmartIF.h.

76 { return m_interface; }

◆ operator!()

bool SmartIF::operator! ( ) const
inline

Definition at line 72 of file SmartIF.h.

72 { return !isValid(); }

◆ operator*()

TYPE& SmartIF::operator* ( ) const
inline

Dereference operator.

Definition at line 80 of file SmartIF.h.

80 { return *m_interface; }

◆ operator->()

TYPE* SmartIF::operator-> ( ) const
inline

Dereference operator.

Definition at line 78 of file SmartIF.h.

78 { return m_interface; }

◆ operator=() [1/4]

SmartIF& SmartIF::operator= ( const SmartIF rhs)
inline

Assignment operator.

Definition at line 126 of file SmartIF.h.

126  {
127  reset( rhs.get() );
128  return *this;
129  }

◆ operator=() [2/4]

template<class T >
SmartIF& SmartIF::operator= ( const SmartIF< T > &  rhs)
inline

Assignment operator from a different SmartIF.

Note
it cannot replace the assignment operator.

Definition at line 133 of file SmartIF.h.

133  {
134  reset( rhs.get() );
135  return *this;
136  }

◆ operator=() [3/4]

SmartIF& SmartIF::operator= ( IInterface ptr)
inline

Assignment operator from IInterface pointer.

It allows things like SmartIF<T> x; x = 0;

Definition at line 121 of file SmartIF.h.

121  {
122  reset( ptr );
123  return *this;
124  }

◆ operator=() [4/4]

SmartIF& SmartIF::operator= ( SmartIF &&  rhs)
inline

Definition at line 48 of file SmartIF.h.

48  {
49  if ( m_interface ) m_interface->release();
51  rhs.m_interface = nullptr;
52  return *this;
53  }

◆ reset() [1/2]

template<class OTHER >
void SmartIF::reset ( OTHER *  ptr)
inline

Set the internal pointer to the passed one disposing of the old one.

Version for pointers of types inheriting from IInterface.

Definition at line 97 of file SmartIF.h.

97  {
98  if ( static_cast<const IInterface*>( ptr ) == static_cast<const IInterface*>( m_interface ) ) return;
99  if ( m_interface ) m_interface->release();
100  if ( ptr ) {
101  m_interface = ptr->template cast<TYPE>();
102  if ( m_interface ) m_interface->addRef();
103  } else {
104  m_interface = nullptr;
105  }
106  }

◆ reset() [2/2]

void SmartIF::reset ( TYPE *  ptr = nullptr)
inline

Set the internal pointer to the passed one disposing of the old one.

Version for pointers of the same type of the managed ones (no call to queryInterface needed).

Definition at line 88 of file SmartIF.h.

88  {
89  if ( ptr == m_interface ) return;
90  if ( m_interface ) m_interface->release();
91  m_interface = ptr;
92  if ( m_interface ) m_interface->addRef();
93  }

Member Data Documentation

◆ m_interface

TYPE* SmartIF::m_interface = nullptr
private

Pointer to the instance.

Definition at line 31 of file SmartIF.h.


The documentation for this class was generated from the following files:
SmartIF::reset
void reset(TYPE *ptr=nullptr)
Set the internal pointer to the passed one disposing of the old one.
Definition: SmartIF.h:88
SmartIF::isValid
bool isValid() const
Allow for check if smart pointer is valid.
Definition: SmartIF.h:69
SmartIF::m_interface
TYPE * m_interface
Pointer to the instance.
Definition: SmartIF.h:31
SmartIF
Definition: IConverter.h:22
SmartIF::get
TYPE * get() const
Get interface pointer.
Definition: SmartIF.h:82
IInterface
Definition: IInterface.h:225