| The Gaudi Framework v30r0 (c919700c)

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

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 {
21 private:
23 TYPE* m_interface = nullptr;
24
25 public:
26 // ---------- Construction and destruction ----------
28 inline SmartIF() = default;
30 inline SmartIF( TYPE* ptr ) : m_interface( ptr )
31 {
32 if ( m_interface ) m_interface->addRef();
33 }
35 template <class OTHER>
36 inline SmartIF( OTHER* ptr )
37 {
38 if ( ptr ) reset( ptr );
39 }
41 inline SmartIF( const SmartIF& rhs ) : m_interface( rhs.get() )
42 {
43 if ( m_interface ) m_interface->addRef();
44 }
46 inline SmartIF( SmartIF&& rhs ) : m_interface( rhs.m_interface ) { rhs.m_interface = nullptr; }
48 inline SmartIF& operator=( SmartIF&& rhs )
49 {
50 if ( m_interface ) m_interface->release();
51 m_interface = rhs.m_interface;
52 rhs.m_interface = nullptr;
53 return *this;
54 }
55
58 template <class T>
59 inline explicit SmartIF( const SmartIF<T>& rhs )
60 {
61 reset( rhs.get() );
62 }
64 inline ~SmartIF() { reset(); }
65
66 // ---------- Boolean and comparison methods ----------
68 inline bool isValid() const { return m_interface != nullptr; }
69
70 inline explicit operator bool() const { return isValid(); }
71 inline bool operator!() const { return !isValid(); }
72
73 // ---------- Pointer access methods ----------
76 inline operator TYPE*() const { return m_interface; }
78 inline TYPE* operator->() const { return m_interface; }
80 inline TYPE& operator*() const { return *m_interface; }
82 inline TYPE* get() const { return m_interface; }
83 #if !defined( GAUDI_V22_API ) && !defined( NEW_SMARTIF )
84 inline TYPE*& pRef() { return m_interface; }
86 #endif
87
88 // ---------- Cast methods ----------
92 inline void reset( TYPE* ptr = nullptr )
93 {
94 if ( ptr == m_interface ) return;
95 if ( m_interface ) m_interface->release();
96 m_interface = ptr;
97 if ( m_interface ) m_interface->addRef();
98 }
101 template <class OTHER>
102 inline void reset( OTHER* ptr )
103 {
104 if ( static_cast<IInterface*>( ptr ) == static_cast<IInterface*>( m_interface ) ) return;
105 if ( m_interface ) m_interface->release();
106 if ( ptr ) {
107 ptr->queryInterface( TYPE::interfaceID(), pp_cast<void>( &m_interface ) ).ignore();
108 } else {
109 m_interface = nullptr;
110 }
111 }
112
114 template <typename IFace>
115 SmartIF<IFace> as() const
116 {
117 return SmartIF<IFace>{*this};
118 }
119
120 // ---------- Special hacks ----------
127 inline SmartIF& operator=( IInterface* ptr )
128 {
129 reset( ptr );
130 return *this;
131 }
133 inline SmartIF& operator=( const SmartIF& rhs )
134 {
135 reset( rhs.get() );
136 return *this;
137 }
140 template <class T>
141 inline SmartIF& operator=( const SmartIF<T>& rhs )
142 {
143 reset( rhs.get() );
144 return *this;
145 }
146 };
147
148 // helper function to turn a pointer to an interface into
149 // the corresponding SmartIF -- this avoids having to type
150 // the typename twice, and thus insures consistency
151 template <typename IFace>
152 SmartIF<IFace> make_SmartIF( IFace* iface )
153 {
154 return SmartIF<IFace>{iface};
155 }
156
157 #endif // GAUDI_SMARTIF_H
SmartIF::SmartIF
SmartIF(SmartIF &&rhs)
Move constructor.
Definition: SmartIF.h:46
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:102
SmartIF::operator*
TYPE & operator*() const
Dereference operator.
Definition: SmartIF.h:80
SmartIF::~SmartIF
~SmartIF()
Standard Destructor.
Definition: SmartIF.h:64
SmartIF::operator->
TYPE * operator->() const
Dereference operator.
Definition: SmartIF.h:78
SmartIF::get
TYPE * get() const
Get interface pointer.
Definition: SmartIF.h:82
SmartIF::SmartIF
SmartIF()=default
Default constructor.
SmartIF::as
SmartIF< IFace > as() const
return a new SmartIF instance to another interface
Definition: SmartIF.h:115
SmartIF::m_interface
TYPE * m_interface
Pointer to the instance.
Definition: SmartIF.h:23
SmartIF::operator=
SmartIF & operator=(IInterface *ptr)
Assignment operator from IInterface pointer.
Definition: SmartIF.h:127
IInterface
Definition of the basic interface.
Definition: IInterface.h:277
SmartIF::operator=
SmartIF & operator=(const SmartIF< T > &rhs)
Assignment operator from a different SmartIF.
Definition: SmartIF.h:141
SmartIF::SmartIF
SmartIF(const SmartIF &rhs)
Copy constructor.
Definition: SmartIF.h:41
SmartIF::SmartIF
SmartIF(TYPE *ptr)
Standard constructor from pointer.
Definition: SmartIF.h:30
SmartIF::isValid
bool isValid() const
Allow for check if smart pointer is valid.
Definition: SmartIF.h:68
SmartIF::operator=
SmartIF & operator=(const SmartIF &rhs)
Assignment operator.
Definition: SmartIF.h:133
SmartIF::operator=
SmartIF & operator=(SmartIF &&rhs)
Move assignement.
Definition: SmartIF.h:48
SmartIF::SmartIF
SmartIF(OTHER *ptr)
Standard constructor from any (IInterface-derived) pointer.
Definition: SmartIF.h:36
SmartIF::SmartIF
SmartIF(const SmartIF< T > &rhs)
Constructor from another SmartIF, with a different type.
Definition: SmartIF.h:59
make_SmartIF
SmartIF< IFace > make_SmartIF(IFace *iface)
Definition: SmartIF.h:152
SmartIF::reset
void reset(TYPE *ptr=nullptr)
Set the internal pointer to the passed one disposing of the old one.
Definition: SmartIF.h:92
SmartIF::pRef
TYPE *& pRef()
Get reference to the pointer.
Definition: SmartIF.h:85
IInterface.h
SmartIF::operator!
bool operator!() const
Definition: SmartIF.h:71