cppiface.h

来自「设计模式模板源码」· C头文件 代码 · 共 91 行

H
91
字号
/////////////////////////////////////////////////////////////////////////////
// CppIface.h : Definitions and templates for C++ interfaces.
//
// $Header: $

#ifndef _XP_CPPIFACE_H__
#define _XP_CPPIFACE_H__


#include "GuidCast.h"
#include "RefCount.h"

/////////////////////////////////////////////////////////////////////////////
// CIQueryGuidImpl
//
// CIUnknownImpl is derived from this and QueryInterface is implemented in
// terms if guid_cast. This is for backward compatibility.
template<class base, const XPIID *piid = &(base::IID())>
class CIQueryGuidImpl : public base
{
public:
	virtual XPBool QueryGuid(XPREFIID guid, void **ppvObj)
	{
		*ppvObj = 0;

		if (guid == *piid) {
			*ppvObj = static_cast<base*>(this);
		}

		return (*ppvObj != 0);
	}
};


/////////////////////////////////////////////////////////////////////////////
// RefCountImpl_T

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

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

//@doc RefCountImpl_T
//@mfunc XPUint32 | RefCountImpl_T | 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 RefCountImpl_T : public base_t
{
public:

	//@cmember
	/* Constructor to set reference count to zero. */
	RefCountImpl_T()
	{
		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;
};


#endif   // _XP_CPPIFACE_H__

⌨️ 快捷键说明

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