CRefCount

一个单线程的引用计数类, 针对具体类.



/// @file exam_x_x.cpp
/// @brief 测试引用计数类

#include <iostream>
#include <limits>
#include "RefCount.h"
#include "RefOwner.h"

using namespace std;

void clear_cin();
void fnTestRefCount();

int main(int argc, char** argv, char** envp)
{
    fnTestRefCount();

    cout << "END, press any key to quit" << endl;
    clear_cin();
    getchar();

    return 0;
}

void fnTestRefCount()
{
    cout << endl << "fnTestRefCount() ==> CRefCount ref1(new CRefOwner(\"ref1\"));" << endl << endl;
    CRefCount ref1(new CRefOwner("ref1"));

    cout << endl << "fnTestRefCount() ==> CRefCount ref2(ref1);" << endl << endl;
    CRefCount ref2(ref1);
    
    cout << endl <<  "fnTestRefCount() ==> CRefCount ref3(new CRefOwner(\"ref3\"));" << endl << endl;
    CRefCount ref3(new CRefOwner("ref3"));

    cout << endl <<  "fnTestRefCount() ==> CRefCount ref4(ref3);" << endl << endl;
    CRefCount ref4(ref3);

    cout << endl <<  "fnTestRefCount() <==" << endl << endl;
}

void clear_cin()
{
    cin.clear();
    cin.sync();
}

// RefCount.h: interface for the CRefCount 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 "RefOwner.h"

class CRefCount  
{
public:
	CRefCount(CRefOwner* pOwner);
    CRefCount(const CRefCount* pObj);
    CRefCount(const CRefCount& Obj);
	virtual ~CRefCount();

    void addRef(); ///< 引用计数++
    int releaseRef(); ///< 引用计数--

private:
    void cpyAndAddRef(const CRefCount* pObj);

private:
    int* m_piRefCount; ///< 引用计数点数指针
    CRefOwner* m_pOwner; ///< 引用计数所有者指针s
};

#endif // !defined(AFX_REFCOUNT_H__2F7675D6_BDC0_4BE6_A0FD_14F51476D8ED__INCLUDED_)

// RefCount.cpp: implementation of the CRefCount class.
//
//

#include <iostream>
#include <string>
#include <stddef.h>
#include "RefCount.h"

//
// Construction/Destruction
//

using namespace std;

CRefCount::CRefCount(CRefOwner* pOwner)
:m_piRefCount(NULL)
,m_pOwner(NULL)
{
    cout << "CRefCount::CRefCount this = " << this << " pOwner = 0x" << pOwner << endl;
    m_piRefCount = new int(0);
    m_pOwner = pOwner;
    addRef();
}

CRefCount::CRefCount(const CRefCount* pObj)
{
    cpyAndAddRef(pObj);
}

CRefCount::CRefCount(const CRefCount& Obj)
{
    cpyAndAddRef(&Obj);
}

void CRefCount::cpyAndAddRef(const CRefCount* pObj)
{
    if ((NULL == pObj) || (this == pObj))
    {
        return;
    }
    
    m_piRefCount = pObj->m_piRefCount;
    m_pOwner = pObj->m_pOwner;
    addRef();
}

CRefCount::~CRefCount()
{
    if (0 == releaseRef())
    {
        cout << "CRefCount::~CRefCount() this = " << this << " 0 == releaseRef()" << endl;

        if (NULL != m_pOwner)
        {
            cout << "CRefCount::~CRefCount() this = " << this << " delete m_pOwner " << m_pOwner << endl;
            delete m_pOwner;
            m_pOwner = NULL;
        }

        if (NULL != m_piRefCount)
        {
            cout << "CRefCount::~CRefCount() this = " << this << " delete m_piRefCount " << m_piRefCount << endl;
            delete m_piRefCount;
            m_piRefCount = NULL;
        }
    }
}

void CRefCount::addRef()
{
    int iRc = 0;

    if (NULL != m_piRefCount)
    {
        (*m_piRefCount)++;
        iRc = *m_piRefCount;

        cout << "CRefCount::addRef this = " << this << " *m_piRefCount = " << *m_piRefCount << endl;
    }
}

int CRefCount::releaseRef()
{
    int iRc = 0;

    iRc = (NULL != *m_piRefCount) ? (--(*m_piRefCount)) : 0;
    cout << "CRefCount::releaseRef this = " << this << " *m_piRefCount = " << iRc << endl;
    return iRc;
}

// 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 > 1000

class CRefOwner  
{
public:
	CRefOwner(const char* pszName);
	virtual ~CRefOwner();

private:
    char *m_pszName;
};

#endif // !defined(AFX_REFOWNER_H__C08C0436_4EE1_4AE6_9EC2_B6E320280FAE__INCLUDED_)

// RefOwner.cpp: implementation of the CRefOwner class.
//
//

#include <iostream>
#include <string>
#include "RefOwner.h"

using namespace std;

//
// Construction/Destruction
//

CRefOwner::CRefOwner(const char* pszName)
{
    cout << "CRefOwner::CRefOwner this = " << this << " pszName = " << pszName << " " << endl;
    m_pszName = new char[strlen(pszName) + 1];
    strcpy(m_pszName, pszName);
}

CRefOwner::~CRefOwner()
{
    if (NULL != m_pszName)
    {
        cout << "CRefOwner::~CRefOwner this = " << this << " m_pszName = " << m_pszName << endl;
        delete[] m_pszName;
        m_pszName = NULL;
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值