| # Gaudi Framework, version v21r11 |

Home |  | Generated: 30 Sep 2010

# SmartIF.h

Go to the documentation of this file.

    00001 // $Id: SmartIF.h,v 1.10 2008/10/27 19:22:20 marcocle Exp $
    00002 #ifndef GAUDI_SMARTIF_H
    00003 #define GAUDI_SMARTIF_H 1
    00004
    00005 // Framework include files
    00006 #include "GaudiKernel/IInterface.h"
    00007
    00019 template <class TYPE> class SmartIF {
    00020 private:
    00022   TYPE* m_interface;
    00023 public:
    00024   // ---------- Construction and destruction ----------
    00026   inline SmartIF(): m_interface(0) {}
    00028   inline SmartIF(TYPE* ptr): m_interface(ptr) {
    00029     if (m_interface) m_interface->addRef();
    00030   }
    00032   template <class OTHER>
    00033   inline SmartIF(OTHER* ptr): m_interface(0) {
    00034     if (ptr) reset(ptr);
    00035   }
    00037   inline SmartIF(const SmartIF& rhs): m_interface(rhs.get()) {
    00038     if (m_interface) m_interface->addRef();
    00039   }
    00042   template <class T>
    00043   inline explicit SmartIF(const SmartIF<T>& rhs): m_interface(0) {
    00044     reset(rhs.get());
    00045   }
    00047   inline ~SmartIF() { reset(); }
    00048
    00049   // ---------- Boolean and comparison methods ----------
    00051   inline bool isValid() const { return m_interface != 0; }
    00052
    00053   // ---------- Pointer access methods ----------
    00056   inline operator TYPE* () const { return m_interface; }
    00058   inline TYPE* operator->() const { return m_interface; }
    00060   inline TYPE& operator *() const { return *m_interface; }
    00062   inline TYPE* get() const { return m_interface; }
    00063 #if !defined(GAUDI_V22_API) && !defined(NEW_SMARTIF)
    00065   inline TYPE*& pRef() {
    00066     return m_interface;
    00067   }
    00068 #endif
    00069
    00070   // ---------- Cast methods ----------
    00074   inline void reset(TYPE* ptr = 0) {
    00075     if (ptr == m_interface) return;
    00076     if (m_interface) m_interface->release();
    00077     if (ptr) {
    00078       m_interface = ptr;
    00079       m_interface->addRef();
    00080     } else {
    00081       m_interface = 0;
    00082     }
    00083   }
    00086   template <class OTHER>
    00087   inline void reset(OTHER* ptr) {
    00088     if (static_cast<IInterface*>(ptr) == static_cast<IInterface*>(m_interface)) return;
    00089     if (m_interface) m_interface->release();
    00090     if (ptr) {
    00091       ptr->queryInterface(TYPE::interfaceID(), pp_cast<void>(&m_interface)).ignore();
    00092     } else {
    00093       m_interface = 0;
    00094     }
    00095   }
    00096
    00097   // ---------- Special hacks ----------
    00104   inline SmartIF& operator = (IInterface* ptr) {
    00105     reset(ptr);
    00106     return *this;
    00107   }
    00109   inline SmartIF& operator = (const SmartIF& rhs) {
    00110     reset(rhs.get());
    00111     return *this;
    00112   }
    00115   template <class T>
    00116   inline SmartIF& operator = (const SmartIF<T>& rhs) {
    00117     reset(rhs.get());
    00118     return *this;
    00119   }
    00120 };
    00121
    00122 #if defined(_MSC_VER) && (_MSC_VER < 1500)
    00130 template <class TYPE>
    00131 inline bool operator == (long lhs, const SmartIF<TYPE> &rhs) {
    00132   return rhs == reinterpret_cast<TYPE*>(lhs);
    00133 }
    00141 template <class TYPE>
    00142 inline bool operator != (long lhs, const SmartIF<TYPE> &rhs) {
    00143   return rhs != reinterpret_cast<TYPE*>(lhs);
    00144 }
    00145 #endif
    00146 #endif // GAUDI_SMARTIF_H