c++ so 反射_C++反射实现

本文档介绍了一种在C++中实现SO动态库的反射机制,包括Value类用于存储任意类型数据,reflectBase基类,Attribute类用于表示成员属性,memberFunction类表示成员函数,以及相应的注册宏,如DECLARE_REFLECT_CLASS、IMPLEMENT_REFLECT_CLASS等,用于实现类的反射功能。
摘要由CSDN通过智能技术生成

#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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值