D
DBCB
Unregistered / Unconfirmed
GUEST, unregistred user!
[^] 精彩!
//********************************************************************************************
//* Intelligent Pointer Unit
//********************************************************************************************
//* James Jin<JJams_King@yahoo.com>
//* $Date: 2001/10/22 13:57:04 $
//* $Revision: 1.1 $
//* $State: Exp $
//********************************************************************************************
#ifndef _IPOINTER_J_H_J_
#define _IPOINTER_J_H_J_
#ifdef _DEBUG_J_
#include <iostream.h>
#endif
//********************************************************************************************
//* CReferable_J
//********************************************************************************************
class CReferable_J
{
//*
//* Member variables
//*
protected:
//* m_nRefCount_J;
reference count
int m_nRefCount_J;
//* end protected
//*
//* public methods
//*
public:
//* constructor
CReferable_J(): m_nRefCount_J(0)
{
#ifdef _DEBUG_J_
cout<<"[CReferable] constructor"<<endl;
#endif
}
//* virtual destructor
virtual ~CReferable_J()
{
#ifdef _DEBUG_J_
cout<<"[CReferable] destructor <"<<m_nRefCount_J<<">"<<endl;
#endif
}
//* increaseReferenceCount_J;
increase reference count by one
int increaseReferenceCount_J()
{
#ifdef _DEBUG_J_
cout<<"[CReferable] increaseReferenceCount_J <"<<m_nRefCount_J+1<<">"<<endl;
#endif
return ++m_nRefCount_J;
}
//* decreaseReferenceCount_J;
decrease reference count by one
int decreaseReferenceCount_J()
{
#ifdef _DEBUG_J_
cout<<"[CReferable] decreaseReferenceCount_J <"<<m_nRefCount_J-1<<">"<<endl;
#endif
return --m_nRefCount_J;
}
//* end public
};
//********************************************************************************************
//********************************************************************************************
//* CIPointer_J
//********************************************************************************************
template<class T>
class CIPointer_J
{
//*
//* Member variables
//*
protected:
//* m_pointer;
pointer to instance of T
CReferable_J* m_pointer;
//* end protected
//*
//* helper functions
//*
protected:
void setPointer(CReferable_J* p)
{
if(m_pointer)
{
if(m_pointer->decreaseReferenceCount_J()<=0)
{
delete m_pointer;
}
m_pointer = 0;
}
m_pointer = p;
if(m_pointer)
{
m_pointer->increaseReferenceCount_J();
}
}
//* end protected
//*
//* public methods
//*
public:
//* virtual destructor
virtual ~CIPointer_J()
{
if(m_pointer)
{
if(m_pointer->decreaseReferenceCount_J()<=0)
{
delete m_pointer;
}
m_pointer = 0;
}
#ifdef _DEBUG_J_
cout<<"[CIPointer_J] destructor"<<endl;
#endif
}
//* default constructor
CIPointer_J()
{
m_pointer = 0;
#ifdef _DEBUG_J_
cout<<"[CIPointer_J] construtor()"<<endl;
#endif
}
//* copy constructor
CIPointer_J(CIPointer_J<T>&
p)
{
#ifdef _DEBUG_J_
cout<<"[CIPointer_J] construtor(&)"<<endl;
#endif
m_pointer = 0;
setPointer(p);
}
//* assign constructor
CIPointer_J(T* p)
{
#ifdef _DEBUG_J_
cout<<"[CIPointer_J] construtor(*)"<<endl;
#endif
m_pointer = 0;
setPointer(p);
}
//* operator->
T* operator->()
{
#ifdef _DEBUG_J_
cout<<"[CIPointer_J] operator->"<<endl;
#endif
return (T*)m_pointer;
}
//* operator T*
operator T*()
{
#ifdef _DEBUG_J_
cout<<"[CIPointer_J] operator T*"<<endl;
#endif
return (T*)m_pointer;
}
//* operator=
CIPointer_J<T>&
operator=(CIPointer_J<T>&
p)
{
#ifdef _DEBUG_J_
cout<<"[CIPointer_J] operator="<<endl;
#endif
setPointer(p.m_pointer);
return *this;
}
//* operator=
CIPointer_J<T>&
operator=(T* p)
{
#ifdef _DEBUG_J_
cout<<"[CIPointer_J] operator="<<endl;
#endif
setPointer(p);
return *this;
}
//* operator==
int operator==(CIPointer_J<T>&p)
{
#ifdef _DEBUG_J_
cout<<"[CIPointer_J] operator=="<<endl;
#endif
return m_pointer == p.m_pointer;
}
//* end public
};
//********************************************************************************************
//********************************************************************************************
//* CIPointer_1_J
//********************************************************************************************
template<class T,class T1>
class CIPointer_1_J: public CIPointer_J<T1>
{
//*
//* public methods
//*
public:
//* default constructor
CIPointer_1_J()
{
m_pointer = 0;
#ifdef _DEBUG_J_
cout<<"[CIPointer_1_J] construtor()"<<endl;
#endif
}
//* copy constructor
CIPointer_1_J(CIPointer_1_J<T,T1>&
p)
{
#ifdef _DEBUG_J_
cout<<"[CIPointer_1_J] construtor(&)"<<endl;
#endif
m_pointer = 0;
setPointer(p);
}
//* assign constructor
CIPointer_1_J(T* p)
{
#ifdef _DEBUG_J_
cout<<"[CIPointer_1_J] construtor(*)"<<endl;
#endif
m_pointer = 0;
setPointer(p);
}
//* do
wn cast constructor
CIPointer_1_J(CIPointer_J<T1>&p)
{
#ifdef _DEBUG_J_
cout<<"[CIPointer_1_J] construtor(down-cast)"<<endl;
#endif
m_pointer = 0;
setPointer((T1*)p);
}
//* operator->
T* operator->()
{
return (T*)m_pointer;
}
//* operator T*
operator T*()
{
return (T*)m_pointer;
}
//* operator=
CIPointer_1_J<T,T1>&
operator=(CIPointer_1_J<T,T1>&
p)
{
setPointer(p.m_pointer);
return *this;
}
//* operator=
CIPointer_1_J<T,T1>&
operator=(T* p)
{
setPointer(p);
return *this;
}
//* operator==
int operator==(CIPointer_1_J<T,T1>&p)
{
return m_pointer == p.m_pointer;
}
//* end public
};
//********************************************************************************************
#endif
//* End of file //
//********************************************************************************************
//* Test IPointer_J
//********************************************************************************************
//* James Jin<JJams_King@yahoo.com>
//* $Date: 2001/10/22 13:57:04 $
//* $Revision: 1.1 $
//* $State: Exp $
//********************************************************************************************
//#define _DEBUG_J_
#include <iostream.h>
#include "IPointer_J.h"
class CC1: public CReferable_J
{
protected:
int m_nMember;
//* end protected
public:
CC1(): m_nMember(1){}
void normal_function1()
{
cout<<"normal_1 <"<<m_nMember<<">"<<endl;
}
virtual void virtual_function()
{
cout<<"virtual_1"<<endl;
}
//* end public
};
class CC2: public CC1
{
protected:
int m_nMember2;
//* end protected
public:
CC2(): m_nMember2(2){}
void normal_function2()
{
cout<<"normal_2 <"<<m_nMember2<<">"<<endl;
}
virtual void virtual_function()
{
cout<<"virtual_2"<<endl;
}
//* end public
};
typedef CIPointer_J<CC1> CC1_ptr;
typedef CIPointer_1_J<CC2,CC1> CC2_ptr;
int main(int argc,char**argv)
{
CC1_ptr ptr1 = new CC1();
ptr1->normal_function1();
ptr1->virtual_function();
CC1_ptr ptr1_1 = ptr1;
ptr1_1 = NULL;
ptr1 = NULL;
cout<<"**********************"<<endl;
CC2_ptr ptr2 = new CC2();
ptr2->normal_function1();
ptr2->normal_function2();
ptr2->virtual_function();
cout<<"**********************"<<endl;
ptr1 = new CC2();
ptr1->normal_function1();
ptr1->virtual_function();
ptr1_1 = ptr2;
ptr1->normal_function1();
ptr1->virtual_function();
cout<<"**********************"<<endl;
((CC2_ptr)ptr1)->normal_function2();
((CC2_ptr)ptr1)->normal_function1();
cout<<"**********************"<<endl;
return 0;
}
//* End of file //