自己写的share_ptr

#pragma once

namespace WSBoost
{
	template<class T> class shared_ptr
	{
	private:
		typedef int count_type;
	public:
		explicit shared_ptr(T * p = 0): px(p)
		{
			pn = new count_type(1);
		}
		~shared_ptr()
		{
			if(--*pn == 0)
			{
				delete px;
				delete pn;
			}
		}
		shared_ptr(shared_ptr const & r): px(r.px), pn(r.pn)
		{
			++*pn;
		}
		shared_ptr & operator=(shared_ptr const & r)
		{
			if (this != &r)
			{
				pn = r.pn;
				px = r.px;
				++ *pn;
			}
			return *this;
		}
	private:
		T * px; 
		count_type * pn;
	};
}

  

 另外一个版本的CSTring 和 share_ptr

 

#include "MyStudyFile.h"
#include <crtdbg.h>
#include <boost/smart_ptr/shared_ptr.hpp>
int  strlength (const char * str)
{
	const char *eos = str;
	while( *eos++ ) ;
	return( eos - str - 1 );
}
char *  strcapend (char * dst,const char * src)
{
	char * cp = dst;
	while( *cp )cp++;
	while( *cp++ = *src++ ) ;
	return( dst );
}
char * __cdecl strcopy(char * dst, const char * src)
{
	char * cp = dst;
	while( *cp++ = *src++ );
	return( dst );
}


//共享的实现。
template<class T>
class ShareDataImp
{
	typedef T ElementType;

private:
	struct SharedData
	{
		SharedData(ElementType* p = NULL):m_p(p), nRef(1)
		{
		}
		void AddRef()
		{
			++ nRef;
		}
		void Release()
		{
			if (-- nRef == 0)
			{
				delete[] m_p;
				m_p = NULL;
			}
		}
		ElementType* GetPtr()
		{
			return m_p;
		}
		int GetCount()
		{
			return nRef;
		}
	private:
		ElementType* m_p;
		int nRef;
	};

public:
	ShareDataImp(ElementType* pstr = NULL) 
		: m_pData(new SharedData(pstr))
	{
	}
	~ShareDataImp()
	{
		m_pData->Release();

		if (m_pData->GetCount() == 0)
		{
			delete m_pData;
		}

		m_pData = NULL;
	}
	ShareDataImp(const ShareDataImp& _right)
		: m_pData(_right.m_pData)
	{
		m_pData->AddRef();
	}
	//释放对共享数据的所有权。
	void ResetAs(ElementType* p)
	{
		m_pData->Release();

		delete m_pData;

		m_pData = new SharedData(p);
	}
	char* Get() const
	{
		return m_pData->GetPtr();
	}
private:
	SharedData* m_pData;
};


class CString
{
public:
	CString(char* pStr)
	{
		char* pNewStr = new char[strlength(pStr) + 1];

		strcopy(pNewStr, pStr);

		m_shareData.ResetAs(pNewStr);
	}
	~CString()
	{
	}
	const char* cstr() const
	{ 
		return m_shareData.Get();
	}

	CString& Append(CString& strVal)
	{
		CString strRight(strVal.cstr());

		m_shareData.ResetAs(CloneData(GetLength() + strVal.GetLength() + 1));

		strcapend(cstr(), strRight.cstr());

		return *this;
	}

	CString& Assign(const CString& strVal)
	{
		m_shareData.ResetAs(strVal.CloneData(strVal.GetLength() + 1));

		return *this;
	}

	CString& Assign(char* pStr)
	{
		CString strRight(pStr);

		m_shareData.ResetAs(strRight.cstr());

		return *this;
	}

	int GetLength() const
	{
		return strlength(m_shareData.Get());
	}
	
private:
	char* cstr()
	{
		return  m_shareData.Get();
	}

	char* CloneData(int iSize) const
	{
		char* pStr = new char[ iSize];

		strcopy(pStr, cstr());

		return pStr;
	}

	ShareDataImp<char> m_shareData;
};
int main()
{
	CString* pVal = new  CString("1");
	CString* pVa2 = new  CString(*pVal);
	CString* pVa3 = new  CString(*pVa2);
	CString* pVa4 = new  CString(*pVa3);
	delete pVal;
	delete pVa2;
	delete pVa3;
	delete pVa4;
	_CrtDumpMemoryLeaks();
}

 

你可能感兴趣的:(sha)