静态类型识别实现的无接口ID 的 QueryInterface,dynamic_cast()
//感谢提供静态类型识别的大师
#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;
}