CLASS_CREFCOUNT
发布日期:2021-06-30 22:07:55
浏览次数:3
分类:技术文章
本文共 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 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
哈哈,博客排版真的漂亮呢~
[***.90.31.176]2024年04月26日 12时11分57秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
送一个闲置显示器!
2021-07-03
Oracle 行转列 pivot函数基本用法
2021-07-03
Oracle字符串分隔符替换(替换奇数个或偶数个)
2021-07-03
Oracle 利用 UTL_SMTP 包发送邮件
2021-07-03
Oracle 的循环中的异常捕捉和处理
2021-07-03
Oracle通过pivot和unpivot配合实现行列转换
2021-07-03
给Oracle数据库换一个1522端口的监听
2021-07-03
Excel表格数据生成ECharts图表
2021-07-03
阿里云短信服务python版,pyinstaller打包运行时缺少文件
2021-07-03
Oracle的pfile和spfile的一点理解和笔记
2021-07-03
WebService的简单案例记录(Java)
2021-07-03
Html利用PHP与MySQL交互
2021-07-03
dos简单命令
2021-07-03
mysql的安装与卸载与Navicat远程连接
2021-07-03
java实现稀疏数组及将稀疏数组存入硬盘中
2021-07-03
2021-05-18
2021-07-03
Flutter 使用插件打开相册、相机
2021-07-03
libuv实现tcp代理服务器
2021-07-03
libuv使用不当导致的内存泄漏
2021-07-03