#ifndef _tref_H_
#define _tref_H_
class IObjectSingle {
private:
DWORD m_count;
protected:
typedef IObjectSingle QIType;
void Internal_Release()
{
--m_count;
}
public:
IObjectSingle() :
m_count(0)
{
}
virtual ~IObjectSingle()
{
}
DWORD GetCount() const
{
return m_count;
}
virtual bool IsValid()
{
return true;
}
#ifdef _DEBUG
virtual
#endif
DWORD __stdcall AddRef()
{
return ++m_count;
}
#ifdef _DEBUG
virtual
#endif
DWORD __stdcall Release()
{
if (--m_count == 0) {
delete this;
return 0;
}
return m_count;
}
};
class IObject : virtual public IObjectSingle {
public:
};
template <class Type>
class TRef {
private:
Type* m_pt;
public:
TRef() :
m_pt(NULL)
{
}
TRef(const Type* pt)
{
m_pt = (Type*)pt;
if (m_pt)
m_pt->AddRef();
}
TRef(const TRef& tref) :
m_pt(tref.m_pt)
{
if (m_pt)
m_pt->AddRef();
}
template< class Other > TRef( const TRef<Other>& oref ) :
m_pt( static_cast<Other*>(oref) )
{
if (m_pt)
m_pt->AddRef();
}
TRef* Pointer()
{
return this;
}
Type** operator&()
{
if (m_pt) {
m_pt->Release();
m_pt = NULL;
}
return &m_pt;
}
TRef& operator=(const TRef& tref)
{
Type* ptOld = m_pt;
m_pt = tref.m_pt;
if (m_pt)
m_pt->AddRef();
if (ptOld)
ptOld->Release();
return *this;
}
~TRef(void)
{
if (m_pt)
m_pt->Release();
}
operator Type*(void) const { return m_pt; }
Type& operator*(void) const { return *m_pt; }
Type* operator->(void) const { return m_pt; }
friend bool operator==(const TRef& t1, Type* pt)
{
return t1.m_pt == pt;
}
friend bool operator!=(const TRef& t1, Type* pt)
{
return t1.m_pt != pt;
}
inline friend bool operator<(const TRef& t1, Type* pt) { return t1.m_pt < pt; };
inline friend bool operator>(const TRef& t1, Type* pt) { return t1.m_pt > pt; };
inline friend bool operator<=(const TRef& t1, Type* pt) { return t1.m_pt <= pt; };
inline friend bool operator>=(const TRef& t1, Type* pt) { return t1.m_pt >= pt; };
};
#endif