静态类型识别实现的无接口ID 的 QueryInterface,dynamic_cast()

LiuYinChina 2006-01-11 12:34:05

//感谢提供静态类型识别的大师

#ifndef CsUtility_H
#define CsUtility_H

#ifndef interface
#define interface struct
#endif

#ifndef for
#define for if(0); else for
#endif

/*
* Copyright (c) 2006
* Author: RobertBaker
* DECLARE_SINGLETON Ver 2.0
*/

#define DECLARE_SINGLETON(CLASSNAME) \
public: \
static CLASSNAME * const & GetSingleton(bool bCreate = true) { \
static CLASSNAME *s_pSingleton = 0; \
static Close##CLASSNAME close##CLASSNAME; \
\
if (s_pSingleton == 0 && bCreate) { \
s_pSingleton = new CLASSNAME; \
} \
\
return s_pSingleton; \
} \
\
static bool IsCreatedSingleton() { return GetSingleton(false) != 0; } \
static void CloseSingleton() { \
CLASSNAME *&p##CLASSNAME = const_cast<CLASSNAME *&>(GetSingleton()); \
\
if (p##CLASSNAME != 0) { \
delete p##CLASSNAME; \
p##CLASSNAME = 0; \
} \
} \
private: \
struct Close##CLASSNAME { \
Close##CLASSNAME() {} \
~Close##CLASSNAME() { CloseSingleton(); } \
}; \

//静态类型识别
template <class Derive, class Base>
class IsTypeOf
{
private:
typedef char value_type;
struct type_1 {
value_type value;
};

struct type_2 { value_type value[2]; };
static type_2 fun(...);
static type_1 fun(Base *);

public:
enum { is = (sizeof(fun((Derive *) 0)) == sizeof(type_1)) };
};
//静态类型识别

template <typename T>
bool CreateInstance(const T *&pT) {
if (pT != NULL) {
return false;
}

pT = new T;
return pT != NULL;
}

template <typename Type_Destination, typename Type_Source>
bool Dynamic_Cast(const Type_Destination * &pType_Destination, const Type_Source * const pType_Source)
{
if (pType_Destination != 0 || pType_Source == 0) {
return false;
}

if (IsTypeOf<Type_Source, Type_Destination>::is == 0) {
return false;
}

pType_Destination = static_cast<Type_Destination *>(pType_Source);
return true;
}

#endif //CsUtility_H

#include "CsUtility.h"

class A
{
public:
void show() { cout << "A" << endl; m = 0; }
protected:
int m;
};

struct I_A {};

class B : public A, public I_A
{
public:
void show() { cout << "B" << endl; m = 0; }
protected:
private:
};

class C
{
public:
void show() { cout << "C" << endl; m = 0; }
protected:
private:
int m;
};

class D : public B
{
public:
void show() { cout << "D" << endl; m = 0; }
protected:
private:
};

int _tmain()
{
cout << "A is A = " << IsTypeOf<A, A>::is << endl;
cout << "B is A = " << IsTypeOf<B, A>::is << endl;
cout << "C is A = " << IsTypeOf<C, A>::is << endl;
cout << "D is A = " << IsTypeOf<D, A>::is << endl;
cout << "A is D = " << IsTypeOf<A, D>::is << endl;
cout << "D is B = " << IsTypeOf<D, B>::is << endl;
cout << "B is I_A = " << IsTypeOf<B, I_A>::is << endl;
cout << "D is I_A = " << IsTypeOf<D, I_A>::is << endl;
cout << endl;

D *pD = 0;
if (!CreateInstance(pD)) {
return 0;
}

cout << "D to A ";
A *pA = 0;
if (QueryInterface<A, D>(pA, pD)) {
pA->show();
}
cout << endl;

cout << "D to B ";
B *pB = 0;
if (QueryInterface<B, D>(pB, pD)) {
pB->show();
}
cout << endl;

cout << "D to C ";
C *pC = 0;
if (QueryInterface<C, D>(pC, pD)) {
pC->show();
}
cout << endl;

cout << "B to A ";
pA = 0;
if (QueryInterface<A, B>(pA, pD)) {
pA->show();
}
cout << endl;
delete pB;

return 0;
}
...全文
71 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

64,682

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧