C++的反射开源代码

链接地址:http://www.codeproject.com/KB/library/libreflection.aspx

Introduction

LibReflection is a little library (well, a header to be specific) that gives reflection capabilities to C++ classes. When we talk about reflection, we don't mean just RTTI, but a rich palette of capabilities useful in every day programming:

  • specify and examine class inheritance
  • declare and examine normal and static fields
  • declare and examine normal, virtual, and static methods
  • declare and use properties and events
  • set and get field values
  • call methods and get results
  • create instances without having the headers at hand, by using a class registry

And all the above almost happen automatically, with very few macros that the programmer has to put in the application's classes...and you also get the added benefit of class properties and events, something that C++ does not provide by default.

Demo

Using LibReflection is very easy. The following piece of code shows a class with fields, properties and methods, all reflected in the class' Class object:

Collapse
//what you need to include

#include "reflection.hpp"



//namespace usage

using namespace agm::reflection;


//example base class

class Base {
public:
//needed so as that the class gets reflection capabilities

CLASS(Base, NullClass);

//a reflected property

PROPERTY(int, length);

//a reflected method

METHOD(public, bool, processLength, (int l));

private:
//a reflected field

FIELD(private, int, m_length);

//property getter

int get_length() const {
return m_length;
}

//property setter

void set_length(int l) {
m_length = l;
}
};


//a reflected method implementation

bool Base::processLength(int l)
{
return l == m_length;
}


//a derived class

class Derived : public Base {
public:
//derived reflected class

CLASS(Derived, Base);
};


//for the demo

#include <iostream>

using namespace std;


int main()
{
//a class instance

Derived derived;

//get the class of the Derived class

const Class &derived_class = derived.getClass();

//print the class name

cout << derived_class.getName() << endl;

//print the the m_length field

const Field &length_field = derived_class.getField("m_length");
cout << length_field.getType() << " "
<< length_field.getName() << endl;

//print the the length property

const Property &length_prop = derived_class.getProperty("length");
cout << length_prop.getType() << " "
<< length_prop.getName() << endl;

//print the 'processLength()' method

const Method &process_length_method =
derived_class.getMethod("processLength");
cout << process_length_method.getType() << " "
<< process_length_method.getName()
<< process_length_method.getArgs()
<< endl;

//set the length property

cout << "using length property" << endl;
length_prop.set(&derived, 10);
int i;
length_prop.get(i, &derived);
cout << "length = " << i << endl;

//calling the length method

cout << "calling bool Base::processLength(int)" << endl;
bool b;
process_length_method.invoke(b, (Base *)&derived, 10);
cout << "processLength=" << b << endl;

getchar();
return 0;
}

Documentation

For more information, you can check out my little site here.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Achilleas Margaritis



Occupation: Web Developer
Location: Greece Greece

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值