⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 refcount.h

📁 设计模式模板源码
💻 H
字号:
#ifndef _XP_REFCOUNT_H__
#define _XP_REFCOUNT_H__

#include "xptypes.h"
/////////////////////////////////////////////////////////////////////////////
//@doc IRefCount
//
//@class IRefCount | This class provides methods for reference counting
// objects. It is used in order to do interface based programming in C++.
// The AddRef and Release methods it defines have the same signature as
// IUnknown.

#define XP_IREFCOUNT_IID     \
{ 0x8a189175, 0xb5e8, 0x4c7e, { 0x97, 0xfc, 0xa2, 0x2e, 0x57, 0x7, 0xe6, 0x5f } };

class IRefCount
{
public:
	//@cmember,mfunc
	// Add a reference to this object.
	//@@rdesc New reference count value.
	//@@end
	/* Add a reference to this object*/
	virtual XPUint32 XPSTDMETHODCALLTYPE AddRef() = 0;
	//@cmember,mfunc
	// Release a reference to this object.
	//@@rdesc New reference count value.
	//@@end
	/* Release a reference to this object*/
	virtual XPUint32 XPSTDMETHODCALLTYPE Release() = 0;
	XP_IMPL_IID(XP_IREFCOUNT_IID)
};

/////////////////////////////////////////////////////////////////////////////
//@doc CRefCountImpl
//
//@mfunc | CRefCountImpl | CRefCountImpl | Default constructor sets reference
// count to zero.

//@doc CRefCountImpl
//@mfunc XPUint32 | CRefCountImpl | AddRef | Add a reference to this object.
//@rdesc New reference count value.

//@doc CRefCountImpl
//@mfunc XPUint32 | CRefCountImpl | Release | Release a reference to this object.
//@rdesc New reference count value.

//@class Template to implement reference counting for an interface. This class
// derives itself from the template parameter passed in and overrides AddRef
// and Release.

template<class base_t>
class CRefCountImpl : public base_t
{
public:

	//@cmember
	/* Constructor to set reference count to zero. */
	CRefCountImpl()
	{
		m_ulRefCount = 0;
	}

	//@cmember
	/* Constructor to set reference count to zero. */
	XPUint32 XPSTDMETHODCALLTYPE AddRef()
	{
		return ++m_ulRefCount;
	}

	//@cmember
	/* Constructor to set reference count to zero. */
	XPUint32 XPSTDMETHODCALLTYPE Release()
	{
		XPUint32 ulRefCount = --m_ulRefCount;
		if (m_ulRefCount == 0)
			delete this;
		return ulRefCount;
	}

protected:
	//@cmember
	/* Stores reference count for object. */
	XPUint32 m_ulRefCount;
};

#define IRefCountImpl_T CRefCountImpl

template<class base_t>
class CNoRefCountImpl : public base_t
{
public:
	//@cmember
	/* Constructor to set reference count to zero. */
	XPUint32 XPSTDMETHODCALLTYPE AddRef()
	{
		return 1L;
	}

	//@cmember
	/* Constructor to set reference count to zero. */
	XPUint32 XPSTDMETHODCALLTYPE Release()
	{
		return 1L;
	}
};


#endif   // _XP_REFCOUNT_H__

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -