#include
#include
#include
#include
#define DECL_CLS(name) static CRuntimeClass gCClass ## name; CRuntimeClass* type(){return &gCClass ## name;}
#define IMPL_CLS(name, base) CRuntimeClass name::gCClass##name(#name, &gCClass##base);
#define IMPL_CLS0(name) CRuntimeClass name::gCClass##name(#name, 0);
class CObjectBase;
class CRuntimeClass
{
const char *clsname;
CRuntimeClass *baseclass;
static std::map<std::string, CRuntimeClass*> classmap;
public:
CRuntimeClass(const char *name, CRuntimeClass *base) :clsname(name), baseclass(base){
classmap[name] = this;
}
static CRuntimeClass* get_by_name(const char *n){
auto it = classmap.find(n);
return it==classmap.end()?0:it->second;
}
bool is_base_of(CRuntimeClass *p)
{
for(;p;p=p->baseclass)
{
if(p==this)
return true;
}
return false;
}
//static CObjectBase* create();
};
std::map<std::string, CRuntimeClass*> CRuntimeClass::classmap;
class CObjectBase{
public:
virtual ~CObjectBase(){}
DECL_CLS(CObjectBase)
};
IMPL_CLS0(CObjectBase)
//CObjectBase* CRuntimeClass::create(){return new ;}
class classA:public CObjectBase
{
public:
DECL_CLS(classA)
};
IMPL_CLS(classA, CObjectBase)
using namespace std;
int main()
{
classA a;
CObjectBase o;
assert(o.type()->is_base_of(a.type()));
//CObjectBase *pa=CRuntimeClass::get_by_name("classA")->
cout << "Hello World!" << endl;
return 0;
}
没有评论:
发表评论