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

📄 guidcast.h

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

#include "xptypes.h"
///////////////////////////////////////////////////////////////////
//@doc IQueryGuid
//
//@class IQueryGuid | This interface provides a method very similar
// to QueryInterface. The <mf IQueryGuid::QueryGuid> function is
// more generic and can be used to query an object for any type of
// pointer based given a XPIID. QueryInterface can be thought of as
// a subset of QueryGuid.  QueryGuid is simply a replacement for
// compiler RTTI.

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


class IQueryGuid
{
public:
	//@cmember,mfunc Cast object to a pointer type given a XPIID.
	//@@rdesc XP_TRUE if object is successfully cast to the new data type, otherwise XP_FALSE.
	//@@parm XPREFIID | guid | Input XPIID that determines the data type to cast to.
	//@@parm void ** | ppvObj | Output parameter to receive the result of the cast.
	//@@comm
	// This function is very similar to IUnknown's QueryInterface, but it is more
	// generic. It can be used to cast to any C++ data type using a XPIID. It provides
	// a more flexible and efficient way to dynamically cast objects and doesn't
	// incur the overhead of C++ RTTI.
	//@@end
	/* Cast object to a pointer type given a XPIID*/
	virtual XPBool QueryGuid(XPREFIID guid, void **ppvObj) = 0;
	XP_IMPL_IID(XP_IQUERYGUID_IID)	
};


///////////////////////////////////////////////////////////////////
// guid_cast is a function template that calls QueryGuid on the
// object passed in.

template<class to_t, class from_t>
to_t guid_cast(from_t* pFrom)
{
	to_t pTo = NULL;
	if (pFrom) {
		pFrom->QueryGuid(to_t::IID(), (void**) &pTo);
	}
	return pTo;
}


template<class to_t, const XPIID* guid, class from_t>
to_t guid_cast(from_t* pFrom)
{
	to_t pTo = NULL;
	if (pFrom) {
		pFrom->QueryGuid(*guid, (void**) &pTo);
	}
	return pTo;
}
///////////////////////////////////////////////////////////////////
// XPIID map macros for implementing the QueryGuid() function.

#define BEGIN_GUID_MAP(CLASSNAME)  \
	virtual XPBool QueryGuid(XPREFIID guid, void **ppvObj) {  *ppvObj = 0; \

#define GUID_ENTRY(T)  \
	if (guid == T::IID()) { *ppvObj=static_cast<T*>(this); return XP_TRUE; } \

#define GUID_ENTRY2(T,X)  \
	if (guid == T::IID()) { *ppvObj=static_cast<X*>(this); return XP_TRUE; } \

#define GUID_ENTRY_IID(iid, T)  \
	if (guid == iid) { *ppvObj=static_cast<T*>(this); return XP_TRUE; } \

#define GUID_CHAIN_ENTRY(T)  \
	if (T::QueryGuid(guid, ppvObj)) return XP_TRUE; \

#define END_GUID_MAP  return XP_FALSE; }

#endif  // _XP_GUIDCAST_H__

⌨️ 快捷键说明

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