00001
00018 #ifndef INC_MI32_AUTOREFCOUNT_H
00019 #define INC_MI32_AUTOREFCOUNT_H
00020
00024 template <typename _TT> class AUTOREFCOUNT {
00025 public:
00026
00028 AUTOREFCOUNT (
00029 ): m_pInstance(new _TT) { if (m_pInstance->GetRefCount() == 0) m_pInstance->AddRef(); }
00030
00032 AUTOREFCOUNT (
00033 const AUTOREFCOUNT<_TT>& rhs
00034 ): m_pInstance(rhs.m_pInstance) { m_pInstance->AddRef(); }
00035
00037 ~AUTOREFCOUNT (
00038 ) { m_pInstance->Release(); }
00039
00041 AUTOREFCOUNT& operator= (
00042 const AUTOREFCOUNT<_TT>& rhs
00043 ) {
00044 if (this != &rhs) {
00045 m_pInstance->Release();
00046 m_pInstance = rhs.m_pInstance;
00047 m_pInstance->AddRef();
00048 }
00049 return (*this);
00050 }
00051
00053 const _TT& operator* (
00054 ) const { return (*m_pInstance); }
00055
00057 _TT& operator* (
00058 ) { return (*m_pInstance); }
00059
00061 const _TT* operator-> (
00062 ) const { return (&*m_pInstance); }
00063
00065 _TT* operator-> (
00066 ) { return (&*m_pInstance); }
00067
00070 void GetExclusive (
00071 ) {
00072 if (m_pInstance->GetRefCount() > 1) {
00073 m_pInstance = new _TT(*m_pInstance);
00074 if (m_pInstance->GetRefCount() == 0) m_pInstance->AddRef();
00075 }
00076 }
00077
00078 private:
00079 #ifndef GENERATING_DOXYGEN_OUTPUT
00080 _TT *m_pInstance;
00081 #endif // GENERATING_DOXYGEN_OUTPUT
00082 };
00083
00084 #endif // INC_MI32_AUTOREFCOUNT_H