基于lockfree实现的跨平台智能指针

SmartPointer.hpp :


/** 
 * @file SmartPointer.hpp 
 * @brief 智能指针实现
 * 
 * 修订记录  
 * @author   jack3z 
 * @version  1.00 
 * @date 2014-10-30 
 * 
 */  

#ifndef __SMARTPOINTER_HPP__
#define __SMARTPOINTER_HPP__


#if defined(__GNUC__)

#elif defined(_MSC_VER)
#  include <windows.h>
#endif // _DEBUG


#ifndef __CURRENT_FUNCTION__
# if defined(__GNUC__)
#  define __CURRENT_FUNCTION__ __PRETTY_FUNCTION__
# elif defined(_MSC_VER)
#  define __CURRENT_FUNCTION__ __FUNCSIG__
# else
#  error unsupported compiler!
# endif
#endif


template<typename T>
class SmartPointer
{
public:
	SmartPointer()
		: m_ptr(NULL), m_pRef(NULL)
	{
	}

	explicit SmartPointer(T* p)
		: m_ptr(p)
	{
		InitRef();

		printf("%s>>> m_ptr:%p, m_pRef:%p, *m_pRef:%ld \n", __CURRENT_FUNCTION__, 
			m_ptr, m_pRef, m_pRef ? *m_pRef : -1);
		
	}

	SmartPointer(const SmartPointer& other)
		: m_ptr(other.m_ptr), m_pRef(other.m_pRef)
	{
		this->IncreaseRef();

		printf("%s>>> m_ptr:%p, m_pRef:%p, *m_pRef:%ld \n", __CURRENT_FUNCTION__, 
			m_ptr, m_pRef, m_pRef ? *m_pRef : -1);
	}

	~SmartPointer()
	{
		this->DecreaseRef();

		if (m_ptr)
		{
			printf("%s>>> m_ptr:%p, m_pRef:%p, *m_pRef:%ld \n", __CURRENT_FUNCTION__, 
				m_ptr, m_pRef, m_pRef ? *m_pRef : -1);
		}
	}

	SmartPointer& operator=(SmartPointer& other)
	{
		if (this == &other)
		{
			return *this;
		}

		other.IncreaseRef();

		this->DecreaseRef();

		this->m_ptr = other.m_ptr;
		this->m_pRef = other.m_pRef;

		return *this;
	}

	T& operator*() const
	{
		return *m_ptr;
	}

	T* operator->() const
	{
		return m_ptr;
	}

	T* get() const
	{
		return m_ptr;
	}

	void reset()
	{
		this->DecreaseRef();

		this->m_ptr = NULL;
		this->m_pRef = NULL;
	}

	void reset(T* p)
	{
		if (p != m_ptr)
		{
			this->DecreaseRef();

			this->m_ptr = p;

			InitRef();
		}
	}

	// 重载==操作符.
	friend bool operator==(const SmartPointer& a, const SmartPointer& b)
	{
		return a.m_ptr == b.m_ptr;
	}
	friend bool operator==(const SmartPointer& a, const T* p)
	{
		printf("%s>>> a.m_ptr:%p, a.m_pRef:%p, *a.m_pRef:%ld \n", __CURRENT_FUNCTION__, 
			a.m_ptr, a.m_pRef, a.m_pRef ? *a.m_pRef: -1);

		return a.m_ptr == p;
	}
	friend bool operator==(const T* p, const SmartPointer& b)
	{
		printf("%s>>> b.m_ptr:%p, b.m_pRef:%p, *b.m_pRef:%ld \n", __CURRENT_FUNCTION__, 
			b.m_ptr, b.m_pRef, b.m_pRef ? *b.m_pRef : -1);

		return p == b.m_ptr;
	}

	// 重载!=操作符.
	friend bool operator!=(const SmartPointer& a, const SmartPointer& b)
	{
		return a.m_ptr != b.m_ptr;
	}
	friend bool operator!=(const SmartPointer& a, const T* p)
	{
		printf("%s>>> a.m_ptr:%p, a.m_pRef:%p, *a.m_pRef:%ld \n", __CURRENT_FUNCTION__, 
			a.m_ptr, a.m_pRef, a.m_pRef ? *a.m_pRef: -1);

		return a.m_ptr != p;
	}
	friend bool operator!=(const T* p, const SmartPointer& b)
	{
		printf("%s>>> b.m_ptr:%p, b.m_pRef:%p, *b.m_pRef:%ld \n", __CURRENT_FUNCTION__, 
			b.m_ptr, b.m_pRef, b.m_pRef ? *b.m_pRef : -1);

		return p != b.m_ptr;
	}

protected:
	void InitRef()
	{
		m_pRef = m_ptr ? new long(1) : NULL;
	}

	void IncreaseRef()
	{
		if (m_ptr)
		{
#if defined(__GNUC__)
			__sync_fetch_and_add(m_pRef, 1);
#elif defined(_MSC_VER)
			::InterlockedIncrement(m_pRef);
#endif
		}
	}

	void DecreaseRef()
	{
		if (m_ptr)
		{
#if defined(__GNUC__)
			if (1 == __sync_fetch_and_sub(m_pRef, 1))
#elif defined(_MSC_VER)
			if (0 == ::InterlockedDecrement(m_pRef))
#endif
			{
				printf("%s>>> delete m_ptr:%p, delete m_pRef:%p, *m_pRef:%ld \n", __CURRENT_FUNCTION__, 
					m_ptr, m_pRef, *m_pRef);

				delete m_ptr;
				m_ptr = NULL;

				delete m_pRef;
				m_pRef = NULL;
			}
		}
	}

protected:

	T* m_ptr;
	long* m_pRef;
	
};

#endif


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值