The Gaudi Framework  master (ff829712)
Loading...
Searching...
No Matches
SmartIF< TYPE > Class Template Reference

Small smart pointer class with automatic reference counting for IInterface. More...

#include <GaudiKernel/SmartIF.h>

Public Member Functions

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

Private Attributes

TYPE * m_interface = nullptr
 Pointer to the instance.
 

Detailed Description

template<class TYPE>
class SmartIF< TYPE >

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 28 of file SmartIF.h.

Constructor & Destructor Documentation

◆ SmartIF() [1/7]

template<class TYPE>
SmartIF< TYPE >::SmartIF ( )
inlinedefault

◆ SmartIF() [2/7]

template<class TYPE>
SmartIF< TYPE >::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 }
Small smart pointer class with automatic reference counting for IInterface.
Definition SmartIF.h:28
TYPE * m_interface
Pointer to the instance.
Definition SmartIF.h:31

◆ SmartIF() [3/7]

template<class TYPE>
template<class OTHER>
SmartIF< TYPE >::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 }
void reset(TYPE *ptr=nullptr)
Set the internal pointer to the passed one disposing of the old one.
Definition SmartIF.h:88

◆ SmartIF() [4/7]

template<class TYPE>
SmartIF< TYPE >::SmartIF ( const SmartIF< TYPE > & rhs)
inline

Definition at line 44 of file SmartIF.h.

44 : m_interface( rhs.get() ) {
45 if ( m_interface ) m_interface->addRef();
46 }
TYPE * get() const
Get interface pointer.
Definition SmartIF.h:82

◆ SmartIF() [5/7]

template<class TYPE>
SmartIF< TYPE >::SmartIF ( SmartIF< TYPE > && 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 TYPE>
template<class T>
SmartIF< TYPE >::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()

template<class TYPE>
SmartIF< TYPE >::~SmartIF ( )
inline

Definition at line 61 of file SmartIF.h.

61{ reset(); }

◆ SmartIF() [7/7]

template<class TYPE>
template<class T>
SmartIF< TYPE >::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<class TYPE>
template<typename IFace>
SmartIF< IFace > SmartIF< TYPE >::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 }
SmartIF()=default

◆ get()

template<class TYPE>
TYPE * SmartIF< TYPE >::get ( ) const
inline

Get interface pointer.

Definition at line 82 of file SmartIF.h.

82{ return m_interface; }

◆ isValid()

template<class TYPE>
bool SmartIF< TYPE >::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()

template<class TYPE>
SmartIF< TYPE >::operator bool ( ) const
inlineexplicit

Definition at line 71 of file SmartIF.h.

71{ return isValid(); }
bool isValid() const
Allow for check if smart pointer is valid.
Definition SmartIF.h:69

◆ operator TYPE *()

template<class TYPE>
SmartIF< TYPE >::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!()

template<class TYPE>
bool SmartIF< TYPE >::operator! ( ) const
inline

Definition at line 72 of file SmartIF.h.

72{ return !isValid(); }

◆ operator*()

template<class TYPE>
TYPE & SmartIF< TYPE >::operator* ( ) const
inline

Dereference operator.

Definition at line 80 of file SmartIF.h.

80{ return *m_interface; }

◆ operator->()

template<class TYPE>
TYPE * SmartIF< TYPE >::operator-> ( ) const
inline

Dereference operator.

Definition at line 78 of file SmartIF.h.

78{ return m_interface; }

◆ operator=() [1/4]

template<class TYPE>
SmartIF & SmartIF< TYPE >::operator= ( const SmartIF< TYPE > & 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 TYPE>
template<class T>
SmartIF & SmartIF< TYPE >::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]

template<class TYPE>
SmartIF & SmartIF< TYPE >::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]

template<class TYPE>
SmartIF & SmartIF< TYPE >::operator= ( SmartIF< TYPE > && 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 TYPE>
template<class OTHER>
void SmartIF< TYPE >::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]

template<class TYPE>
void SmartIF< TYPE >::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();
92 if ( m_interface ) m_interface->addRef();
93 }

Member Data Documentation

◆ m_interface

template<class TYPE>
TYPE* SmartIF< TYPE >::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: