File indexing completed on 2024-11-15 09:38:41
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011 #ifndef GAUDI_SMARTIF_H
0012 #define GAUDI_SMARTIF_H 1
0013
0014
0015 #include "GaudiKernel/IInterface.h"
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028 template <class TYPE>
0029 class SmartIF {
0030 private:
0031
0032 TYPE* m_interface = nullptr;
0033
0034 public:
0035
0036
0037 inline SmartIF() = default;
0038
0039 inline SmartIF( TYPE* ptr ) : m_interface( ptr ) {
0040 if ( m_interface ) m_interface->addRef();
0041 }
0042
0043 template <class OTHER>
0044 inline SmartIF( OTHER* ptr ) {
0045 if ( ptr ) reset( ptr );
0046 }
0047
0048 inline SmartIF( const SmartIF& rhs ) : m_interface( rhs.get() ) {
0049 if ( m_interface ) m_interface->addRef();
0050 }
0051
0052 inline SmartIF( SmartIF&& rhs ) : m_interface( rhs.m_interface ) { rhs.m_interface = nullptr; }
0053
0054 inline SmartIF& operator=( SmartIF&& rhs ) {
0055 if ( m_interface ) m_interface->release();
0056 m_interface = rhs.m_interface;
0057 rhs.m_interface = nullptr;
0058 return *this;
0059 }
0060
0061
0062
0063 template <class T>
0064 inline explicit SmartIF( const SmartIF<T>& rhs ) {
0065 reset( rhs.get() );
0066 }
0067
0068 inline ~SmartIF() { reset(); }
0069
0070
0071
0072 inline bool isValid() const { return m_interface != nullptr; }
0073
0074 inline explicit operator bool() const { return isValid(); }
0075 inline bool operator!() const { return !isValid(); }
0076
0077
0078
0079
0080 inline operator TYPE*() const { return m_interface; }
0081
0082 inline TYPE* operator->() const { return m_interface; }
0083
0084 inline TYPE& operator*() const { return *m_interface; }
0085
0086 inline TYPE* get() const { return m_interface; }
0087 #if !defined( GAUDI_V22_API ) && !defined( NEW_SMARTIF )
0088
0089 inline TYPE*& pRef() { return m_interface; }
0090 #endif
0091
0092
0093
0094
0095
0096 inline void reset( TYPE* ptr = nullptr ) {
0097 if ( ptr == m_interface ) return;
0098 if ( m_interface ) m_interface->release();
0099 m_interface = ptr;
0100 if ( m_interface ) m_interface->addRef();
0101 }
0102
0103
0104 template <class OTHER>
0105 inline void reset( OTHER* ptr ) {
0106 if ( static_cast<IInterface*>( ptr ) == static_cast<IInterface*>( m_interface ) ) return;
0107 if ( m_interface ) m_interface->release();
0108 if ( ptr ) {
0109 ptr->queryInterface( TYPE::interfaceID(), pp_cast<void>( &m_interface ) ).ignore();
0110 } else {
0111 m_interface = nullptr;
0112 }
0113 }
0114
0115
0116 template <typename IFace>
0117 SmartIF<IFace> as() const {
0118 return SmartIF<IFace>{ *this };
0119 }
0120
0121
0122
0123
0124
0125
0126
0127
0128 inline SmartIF& operator=( IInterface* ptr ) {
0129 reset( ptr );
0130 return *this;
0131 }
0132
0133 inline SmartIF& operator=( const SmartIF& rhs ) {
0134 reset( rhs.get() );
0135 return *this;
0136 }
0137
0138
0139 template <class T>
0140 inline SmartIF& operator=( const SmartIF<T>& rhs ) {
0141 reset( rhs.get() );
0142 return *this;
0143 }
0144 };
0145
0146
0147
0148
0149 template <typename IFace>
0150 SmartIF<IFace> make_SmartIF( IFace* iface ) {
0151 return SmartIF<IFace>{ iface };
0152 }
0153
0154 #endif