CLASS_CREFCOUNT
发布日期:2021-06-30 22:07:55 浏览次数:2 分类:技术文章

本文共 7537 字,大约阅读时间需要 25 分钟。

用宏实现一个引用计数类, 可以对不同的所有者类, 产生不同的引用计数类.

/// @file exam_x_x.cpp/// @brief 测试引用计数类#include 
#include
#include "RefCount.h"#include "RefOwner.h"using namespace std;void clear_cin();/// 用宏实现的类中不能带行注释, 只能带块注释/// 用CLASS_CREFCOUNT宏实现的参考计数类, 可以对不同的资源, 产生不同的参考计数类CLASS_CREFCOUNT(CRefCount, CRefOwner)CLASS_CREFCOUNT(CRefCntForCRefOwner1, CRefOwner1)void fnTestRefCount(){ CRefCount ref1(new CRefOwner("ref1")); CRefCount ref2(ref1); CRefCount ref3(new CRefOwner("ref3")); CRefCount ref4(ref3); CRefCntForCRefOwner1 ref01(new CRefOwner1("ref01")); CRefCntForCRefOwner1 ref02(ref01); CRefCntForCRefOwner1 ref03(new CRefOwner1("ref03")); CRefCntForCRefOwner1 ref04(ref03);}int main(int argc, char** argv, char** envp){ fnTestRefCount(); /** run result CRefOwner::CRefOwner ref1 ref_class_name::ref_class_name this = 0018FED8 pOwner = 0x00833C40 ref_class_name::addRef this = 0018FED8 *m_piRefCount = 1 ref_class_name::addRef this = 0018FECC *m_piRefCount = 2 CRefOwner::CRefOwner ref3 ref_class_name::ref_class_name this = 0018FEC0 pOwner = 0x00830F28 ref_class_name::addRef this = 0018FEC0 *m_piRefCount = 1 ref_class_name::addRef this = 0018FEB4 *m_piRefCount = 2 CRefOwner1::CRefOwner1 ref01 ref_class_name::ref_class_name this = 0018FEA8 pOwner = 0x008310D0 ref_class_name::addRef this = 0018FEA8 *m_piRefCount = 1 ref_class_name::addRef this = 0018FE9C *m_piRefCount = 2 CRefOwner1::CRefOwner1 ref03 ref_class_name::ref_class_name this = 0018FE90 pOwner = 0x008311A8 ref_class_name::addRef this = 0018FE90 *m_piRefCount = 1 ref_class_name::addRef this = 0018FE84 *m_piRefCount = 2 ref_class_name::releaseRef this = 0018FE84 *m_piRefCount = 1 ref_class_name::releaseRef this = 0018FE90 *m_piRefCount = 0 ref_class_name::~ref_class_name() this = 0018FE90 0 == releaseRef() ref_class_name::~ref_class_name() this = 0018FE90 delete m_pOwner 008311A8 CRefOwner1::~CRefOwner1 ref03 ref_class_name::~ref_class_name() this = 0018FE90 delete m_piRefCount 00831238 ref_class_name::releaseRef this = 0018FE9C *m_piRefCount = 1 ref_class_name::releaseRef this = 0018FEA8 *m_piRefCount = 0 ref_class_name::~ref_class_name() this = 0018FEA8 0 == releaseRef() ref_class_name::~ref_class_name() this = 0018FEA8 delete m_pOwner 008310D0 CRefOwner1::~CRefOwner1 ref01 ref_class_name::~ref_class_name() this = 0018FEA8 delete m_piRefCount 00831160 ref_class_name::releaseRef this = 0018FEB4 *m_piRefCount = 1 ref_class_name::releaseRef this = 0018FEC0 *m_piRefCount = 0 ref_class_name::~ref_class_name() this = 0018FEC0 0 == releaseRef() ref_class_name::~ref_class_name() this = 0018FEC0 delete m_pOwner 00830F28 CRefOwner::~CRefOwner ref3 ref_class_name::~ref_class_name() this = 0018FEC0 delete m_piRefCount 00831088 ref_class_name::releaseRef this = 0018FECC *m_piRefCount = 1 ref_class_name::releaseRef this = 0018FED8 *m_piRefCount = 0 ref_class_name::~ref_class_name() this = 0018FED8 0 == releaseRef() ref_class_name::~ref_class_name() this = 0018FED8 delete m_pOwner 00833C40 CRefOwner::~CRefOwner ref1 ref_class_name::~ref_class_name() this = 0018FED8 delete m_piRefCount 00833F08 */ cout << "END, press any key to quit" << endl; clear_cin(); getchar(); return 0;}void clear_cin(){ cin.clear(); cin.sync();}
// RefCount.h: interface for the ref_class_name class.////#if !defined(AFX_REFCOUNT_H__2F7675D6_BDC0_4BE6_A0FD_14F51476D8ED__INCLUDED_)#define AFX_REFCOUNT_H__2F7675D6_BDC0_4BE6_A0FD_14F51476D8ED__INCLUDED_#if _MSC_VER > 1000#pragma once#endif // _MSC_VER > 1000#include 
#include
using namespace std;#define CLASS_CREFCOUNT(ref_class_name, owner_class_name)\class ref_class_name\{\public:\ ref_class_name(owner_class_name* pOwner)\ :m_piRefCount(NULL)\ ,m_pOwner(NULL)\ {\ cout << "ref_class_name::ref_class_name this = " << this << " pOwner = 0x" << pOwner << endl;\ m_piRefCount = new int(0);\ m_pOwner = pOwner;\ addRef();\ }\\ ref_class_name(const ref_class_name* pObj)\ {\ cpyAndAddRef(pObj);\ }\\ ref_class_name(const ref_class_name& Obj)\ {\ cpyAndAddRef(&Obj);\ }\\ virtual ~ref_class_name()\ {\ if (0 == releaseRef())\ {\ cout << "ref_class_name::~ref_class_name() this = " << this << " 0 == releaseRef()" << endl;\ \ if (NULL != m_pOwner)\ {\ cout << "ref_class_name::~ref_class_name() this = " << this << " delete m_pOwner " << m_pOwner << endl;\ delete m_pOwner;\ m_pOwner = NULL;\ }\ \ if (NULL != m_piRefCount)\ {\ cout << "ref_class_name::~ref_class_name() this = " << this << " delete m_piRefCount " << m_piRefCount << endl;\ delete m_piRefCount;\ m_piRefCount = NULL;\ }\ }\ }\ \ void addRef()\ {\ /** 引用计数++ */\ int iRc = 0;\ \ if (NULL != m_piRefCount)\ {\ (*m_piRefCount)++;\ iRc = *m_piRefCount;\ \ cout << "ref_class_name::addRef this = " << this << " *m_piRefCount = " << *m_piRefCount << endl;\ }\ }\\ int releaseRef()\ {\ /** 引用计数-- */\ int iRc = 0;\ \ iRc = (NULL != *m_piRefCount) ? (--(*m_piRefCount)) : 0;\ cout << "ref_class_name::releaseRef this = " << this << " *m_piRefCount = " << iRc << endl;\ return iRc;\ }\ \private:\ void cpyAndAddRef(const ref_class_name* pObj)\ {\ if ((NULL == pObj) || (this == pObj))\ {\ return;\ }\ \ m_piRefCount = pObj->m_piRefCount;\ m_pOwner = pObj->m_pOwner;\ addRef();\ }\ \private:\ int* m_piRefCount; /** 引用计数点数指针 */\ owner_class_name* m_pOwner; /** 引用计数所有者指针 */\};#endif // !defined(AFX_REFCOUNT_H__2F7675D6_BDC0_4BE6_A0FD_14F51476D8ED__INCLUDED_)
// RefOwner.h: interface for the CRefOwner class.////#if !defined(AFX_REFOWNER_H__C08C0436_4EE1_4AE6_9EC2_B6E320280FAE__INCLUDED_)#define AFX_REFOWNER_H__C08C0436_4EE1_4AE6_9EC2_B6E320280FAE__INCLUDED_#if _MSC_VER > 1000#pragma once#endif // _MSC_VER > 1000class CRefOwner  {public:	CRefOwner(const char* pszName);	virtual ~CRefOwner();private:    char *m_pszName;};class CRefOwner1{public:    CRefOwner1(const char* pszName);    virtual ~CRefOwner1();    private:    char *m_pszName;};#endif // !defined(AFX_REFOWNER_H__C08C0436_4EE1_4AE6_9EC2_B6E320280FAE__INCLUDED_)
// RefOwner.cpp: implementation of the CRefOwner class.////#include 
#include
#include "RefOwner.h"using namespace std;//// Construction/Destruction//CRefOwner::CRefOwner(const char* pszName){ cout << "CRefOwner::CRefOwner " << pszName << " " << endl; m_pszName = new char[strlen(pszName) + 1]; strcpy(m_pszName, pszName);}CRefOwner::~CRefOwner(){ if (NULL != m_pszName) { cout << "CRefOwner::~CRefOwner " << m_pszName << " " << endl; delete[] m_pszName; m_pszName = NULL; }}CRefOwner1::CRefOwner1(const char* pszName){ cout << "CRefOwner1::CRefOwner1 " << pszName << " " << endl; m_pszName = new char[strlen(pszName) + 1]; strcpy(m_pszName, pszName);}CRefOwner1::~CRefOwner1(){ if (NULL != m_pszName) { cout << "CRefOwner1::~CRefOwner1 " << m_pszName << " " << endl; delete[] m_pszName; m_pszName = NULL; }}

转载地址:https://lostspeed.blog.csdn.net/article/details/50340111 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:(ZT) Is it legal (and moral) for a member function to say delete this?
下一篇:CRefCount

发表评论

最新留言

逛到本站,mark一下
[***.202.152.39]2024年04月22日 00时57分01秒