#ifndef _REFLECT_H#define _REFLECT_H#include"any.h"#include#include
classValue
{public:
Value(const any &value):_value(value){}
Value(const Value &other):_value(other._value)
{
}
Value& operator=(const Value &rhs)
{if(&rhs == this)return *this;else{
_value=rhs._value;return *this;
}
}const any& GetValue() const{return_value;
}void SetValue(const any &value)
{
_value=value;
}private:
any _value;
};classreflectBase;structattribute_op
{
typedef Value (reflectBase::*func_get)();
typedefvoid (reflectBase::*func_set)(const Value&);
func_get _get;
func_set _set;
};classAttribute
{public:
Attribute(){}
Attribute(const std::string &name,attribute_op &op)
:name(name),member_get(op._get),member_set(op._set)
{}
Value GetValue(reflectBase&object)
{return (object.*member_get)();
}void SetValue(reflectBase &object,const Value &value)
{
(object.*member_set)(value);
}const std::string &GetName()
{returnname;
}~Attribute()
{
}private:
std::stringname;
attribute_op::func_get member_get;
attribute_op::func_set member_set;
};classparam
{public:
param(){}
param(const any&p1)
{
m_param.push_back(p1);
}
param(const any& p1,const any &p2)
{
m_param.push_back(p1);
m_param.push_back(p2);
}
std::vectorm_param;
};structmemberFunc_op
{
typedef Value (reflectBase::*func)(const param &);
func _call;
};classreflectBase;classmemberFunction
{public:
typedef memberFunc_op::func methord;
memberFunction(const std::string &name,memberFunc_op &op)
:funcname(name),_call(op._call)//,callret(op._call_ret)
{}
methord GetMethord()
{return_call;
}~memberFunction()
{
}public:
std::stringfuncname;
memberFunc_op::func _call;
};classclassdef;classreflectBase
{public:virtual classdef *GetClassDef() = 0;virtual ~reflectBase(){};
};classclassdef
{public:
typedef reflectBase* (*creator)();
classdef(const std::string&name,classdef::creator _creator):classname(name),instance_creator(_creator){}
reflectBase*CreateInstance()
{returninstance_creator();
}void AddAttribute(const std::string &name,Attribute *attr)
{
attributes.insert(std::make_pair(name,attr));
}void AddMemberFunc(const std::string &name,memberFunction *func)
{
memberfuncs.insert(std::make_pair(name,func));
}
memberFunction*GetMemFunc(const std::string &name)
{
std::map<:string>::iterator it =memberfuncs.find(name);if(it !=memberfuncs.end())return it->second;returnNULL;
}
Attribute* FindAttribute(const std::string &name)
{
std::map<:string>::iterator it =attributes.find(name);if(it !=attributes.end())return it->second;returnNULL;
}public:
std::stringclassname;
std::map<:string>attributes;
std::map<:string>memberfuncs;
creator instance_creator;
};#define DECLARE_REFLECT_CLASS(NAME,CLASSNAME)/
static reflectBase *CreateInstance()/{/
return new NAME;/}/
static classdef *class_def##NAME;/classdef*GetClassDef()/{/
return class_def##NAME;/}/
struct regClass##NAME/{/regClass##NAME(){/NAME::class_def##NAME= new classdef(CLASSNAME,static_cast<:creator>(&NAME::CreateInstance));/}/};/
staticregClass##NAME _regClass##NAME;#define IMPLEMENT_REFLECT_CLASS(NAME)/classdef*NAME::class_def##NAME;/NAME::regClass##NAME _regClass##NAME;#define REGISTER_MEMBER(CLASS,NAME,TYPE,MEMBERNAME)/Value Get##NAME()/{/
return Value(NAME);/}/
void Set##NAME(const Value &value)/{/NAME= any_cast(value.GetValue());/}/
struct reg##NAME{/reg##NAME()/{/attribute_op op;/op._get= static_cast<:func_get>(&CLASS::Get##NAME);/op._set= static_cast<:func_set>(&CLASS::Set##NAME);/Attribute*attr = new Attribute(MEMBERNAME,op);/CLASS::class_def##CLASS->AddAttribute(MEMBERNAME,attr);/}/};/
staticreg##NAME _reg##NAME;#define IMPLEMENT_MEMBER(CLASSNAME,NAME)/CLASSNAME::reg##NAME CLASSNAME::_reg##NAME;#define REGISTER_MEMBERFUNCTION_0(CLASS,NAME,RET,FUNCNAME)/template/
class doCall##NAME/{/
public:/
static Value doCall(CLASS *obj,const param &_param)/{/typedef LOKI_TYPELIST_1(void) voidType;/
return call(obj,_param,Int2Type::value == 0>());/}/
private:/
static Value call(CLASS *obj,const param &_param,Int2Type)/{/obj->NAME();/
return Value(any());/}/
static Value call(CLASS *obj,const param &_param,Int2Type)/{/
return Value(obj->NAME());/}/};/Value call_##NAME(const param &_param = param())/{/
return doCall##NAME::doCall(this,_param);/}/
struct regMemberFunc0##NAME/{/regMemberFunc0##NAME()/{/memberFunc_op op;/op._call= static_cast<:func>(&CLASS::call_##NAME);/memberFunction*memfunc = new memberFunction(FUNCNAME,op);/CLASS::class_def##CLASS->AddMemberFunc(FUNCNAME,memfunc);/}/};/
staticregMemberFunc0##NAME _regMemberFunc0##NAME;#define IMPLEMENT_MEMBERFUNCTION0(CLASSNAME,NAME)/CLASSNAME::regMemberFunc0##NAME CLASSNAME::_regMemberFunc0##NAME;#define REGISTER_MEMBERFUNCTION_1(CLASS,NAME,RET,PARAM1,FUNCNAME)/template/
class doCall##NAME/{/
public:/
static Value doCall(CLASS *obj,const param &_param)/{/typedef LOKI_TYPELIST_1(void) voidType;/
return call(obj,_param,Int2Type::value == 0>());/}/
private:/
static Value call(CLASS *obj,const param &_param,Int2Type)/{/obj->NAME(any_cast(_param.m_param[0].GetValue()));/
return Value(any());/}/
static Value call(CLASS *obj,const param &_param,Int2Type)/{/
return Value(obj->NAME(any_cast(_param.m_param[0].GetValue())));/}/};/Value call_##NAME(const param &_param = param())/{/
return doCall##NAME::doCall(this,_param);/}/
struct regMemberFunc1##NAME/{/regMemberFunc1##NAME()/{/memberFunc_op op;/op._call= static_cast<:func>(&CLASS::call_##NAME);/memberFunction*memfunc = new memberFunction(FUNCNAME,op);/CLASS::class_def##CLASS->AddMemberFunc(FUNCNAME,memfunc);/}/};/
staticregMemberFunc1##NAME _regMemberFunc1##NAME;#define IMPLEMENT_MEMBERFUNCTION1(CLASSNAME,NAME)/CLASSNAME::regMemberFunc1##NAME CLASSNAME::_regMemberFunc1##NAME;#define REGISTER_MEMBERFUNCTION_2(CLASS,NAME,RET,PARAM1,PARAM2,FUNCNAME)/template/
class doCall##NAME/{/
public:/
static Value doCall(CLASS *obj,const param &_param)/{/typedef LOKI_TYPELIST_1(void) voidType;/
return call(obj,_param,Int2Type::value == 0>());/}/
private:/
static Value call(CLASS *obj,const param &_param,Int2Type)/{/obj->NAME(any_cast(_param.m_param[0].GetValue()),any_cast(_param.m_param[1].GetValue()));/
return Value(any());/}/
static Value call(CLASS *obj,const param &_param,Int2Type)/{/
return Value(obj->NAME(any_cast(_param.m_param[0].GetValue()),any_cast(_param.m_param[1].GetValue())));/}/};/Value call_##NAME(const param &_param = param())/{/
return doCall##NAME::doCall(this,_param);/}/
struct regMemberFunc2##NAME/{/regMemberFunc2##NAME()/{/memberFunc_op op;/op._call= static_cast<:func>(&CLASS::call_##NAME);/memberFunction*memfunc = new memberFunction(FUNCNAME,op);/CLASS::class_def##CLASS->AddMemberFunc(FUNCNAME,memfunc);/}/};/
staticregMemberFunc2##NAME _regMemberFunc2##NAME;#define IMPLEMENT_MEMBERFUNCTION2(CLASSNAME,NAME)/CLASSNAME::regMemberFunc2##NAME CLASSNAME::_regMemberFunc2##NAME;#endif