| The Gaudi Framework v32r1 (f65d50dc)

SmartIF.h
Go to the documentation of this file.
1 #ifndef GAUDI_SMARTIF_H
2 #define GAUDI_SMARTIF_H 1
3
4 // Framework include files
5 #include "GaudiKernel/IInterface.h"
6
18 template <class TYPE>
19 class SmartIF {
20 private:
22 TYPE* m_interface = nullptr;
23
24 public:
25 // ---------- Construction and destruction ----------
27 inline SmartIF() = default;
29 inline SmartIF( TYPE* ptr ) : m_interface( ptr ) {
30 if ( m_interface ) m_interface->addRef();
31 }
33 template <class OTHER>
34 inline SmartIF( OTHER* ptr ) {
35 if ( ptr ) reset( ptr );
36 }
38 inline SmartIF( const SmartIF& rhs ) : m_interface( rhs.get() ) {
39 if ( m_interface ) m_interface->addRef();
40 }
42 inline SmartIF( SmartIF&& rhs ) : m_interface( rhs.m_interface ) { rhs.m_interface = nullptr; }
44 inline SmartIF& operator=( SmartIF&& rhs ) {
45 if ( m_interface ) m_interface->release();
46 m_interface = rhs.m_interface;
47 rhs.m_interface = nullptr;
48 return *this;
49 }
50
53 template <class T>
54 inline explicit SmartIF( const SmartIF<T>& rhs ) {
55 reset( rhs.get() );
56 }
58 inline ~SmartIF() { reset(); }
59
60 // ---------- Boolean and comparison methods ----------
62 inline bool isValid() const { return m_interface != nullptr; }
63
64 inline explicit operator bool() const { return isValid(); }
65 inline bool operator!() const { return !isValid(); }
66
67 // ---------- Pointer access methods ----------
70 inline operator TYPE*() const { return m_interface; }
72 inline TYPE* operator->() const { return m_interface; }
74 inline TYPE& operator*() const { return *m_interface; }
76 inline TYPE* get() const { return m_interface; }
77 #if !defined( GAUDI_V22_API ) && !defined( NEW_SMARTIF )
78 inline TYPE*& pRef() { return m_interface; }
80 #endif
81
82 // ---------- Cast methods ----------
86 inline void reset( TYPE* ptr = nullptr ) {
87 if ( ptr == m_interface ) return;
88 if ( m_interface ) m_interface->release();
89 m_interface = ptr;
90 if ( m_interface ) m_interface->addRef();
91 }
94 template <class OTHER>
95 inline void reset( OTHER* ptr ) {
96 if ( static_cast<IInterface*>( ptr ) == static_cast<IInterface*>( m_interface ) ) return;
97 if ( m_interface ) m_interface->release();
98 if ( ptr ) {
99 ptr->queryInterface( TYPE::interfaceID(), pp_cast<void>( &m_interface ) ).ignore();
100 } else {
101 m_interface = nullptr;
102 }
103 }
104
106 template <typename IFace>
107 SmartIF<IFace> as() const {
108 return SmartIF<IFace>{*this};
109 }
110
111 // ---------- Special hacks ----------
118 inline SmartIF& operator=( IInterface* ptr ) {
119 reset( ptr );
120 return *this;
121 }
123 inline SmartIF& operator=( const SmartIF& rhs ) {
124 reset( rhs.get() );
125 return *this;
126 }
129 template <class T>
130 inline SmartIF& operator=( const SmartIF<T>& rhs ) {
131 reset( rhs.get() );
132 return *this;
133 }
134 };
135
136 // helper function to turn a pointer to an interface into
137 // the corresponding SmartIF -- this avoids having to type
138 // the typename twice, and thus insures consistency
139 template <typename IFace>
140 SmartIF<IFace> make_SmartIF( IFace* iface ) {
141 return SmartIF<IFace>{iface};
142 }
143
144 #endif // GAUDI_SMARTIF_H
SmartIF::SmartIF
SmartIF(SmartIF &&rhs)
Move constructor.
Definition: SmartIF.h:42
SmartIF
Small smart pointer class with automatic reference counting for IInterface.
Definition: IConverter.h:15
SmartIF::reset
void reset(OTHER *ptr)
Set the internal pointer to the passed one disposing of the old one.
Definition: SmartIF.h:95
SmartIF::~SmartIF
~SmartIF()
Standard Destructor.
Definition: SmartIF.h:58
SmartIF::isValid
bool isValid() const
Allow for check if smart pointer is valid.
Definition: SmartIF.h:62
SmartIF::as
SmartIF< IFace > as() const
return a new SmartIF instance to another interface
Definition: SmartIF.h:107
SmartIF::get
TYPE * get() const
Get interface pointer.
Definition: SmartIF.h:76
SmartIF::SmartIF
SmartIF()=default
Default constructor.
SmartIF::m_interface
TYPE * m_interface
Pointer to the instance.
Definition: SmartIF.h:22
SmartIF::operator=
SmartIF & operator=(IInterface *ptr)
Assignment operator from IInterface pointer.
Definition: SmartIF.h:118
SmartIF::operator!
bool operator!() const
Definition: SmartIF.h:65
IInterface
Definition of the basic interface.
Definition: IInterface.h:244
SmartIF::operator=
SmartIF & operator=(const SmartIF< T > &rhs)
Assignment operator from a different SmartIF.
Definition: SmartIF.h:130
SmartIF::SmartIF
SmartIF(const SmartIF &rhs)
Copy constructor.
Definition: SmartIF.h:38
SmartIF::operator *
TYPE & operator *() const
Dereference operator.
Definition: SmartIF.h:74
SmartIF::operator->
TYPE * operator->() const
Dereference operator.
Definition: SmartIF.h:72
SmartIF::SmartIF
SmartIF(TYPE *ptr)
Standard constructor from pointer.
Definition: SmartIF.h:29
SmartIF::operator=
SmartIF & operator=(const SmartIF &rhs)
Assignment operator.
Definition: SmartIF.h:123
SmartIF::operator=
SmartIF & operator=(SmartIF &&rhs)
Move assignement.
Definition: SmartIF.h:44
SmartIF::SmartIF
SmartIF(OTHER *ptr)
Standard constructor from any (IInterface-derived) pointer.
Definition: SmartIF.h:34
SmartIF::SmartIF
SmartIF(const SmartIF< T > &rhs)
Constructor from another SmartIF, with a different type.
Definition: SmartIF.h:54
make_SmartIF
SmartIF< IFace > make_SmartIF(IFace *iface)
Definition: SmartIF.h:140
SmartIF::reset
void reset(TYPE *ptr=nullptr)
Set the internal pointer to the passed one disposing of the old one.
Definition: SmartIF.h:86
SmartIF::pRef
TYPE *& pRef()
Get reference to the pointer.
Definition: SmartIF.h:79
IInterface.h