这里以Linux为例,我使用的机器的gcc版本是4.9.2
使用的RTTR的版本是0.9.5
编译&安装
首先在官网将rttr的0.9.5版本下载下来。
Home |RTTR
按照官方的安装流程:
但这里可能会出现一个问题:
按照解答,切换成root用户,在rttr-0.9.5目录下直接cmake . 即可:
最后再进行make编译即可:
需不需要make install就看你们自己了,我没有install,后面的测试程序直接拷贝编译好的so进行处理。
使用
这里我使用了Qt的pro管理项目,qmake写起来比makefile简单点。
此处完全是参考官方给的例子:
1.手动注册:
#include <rttr/registration>
using namespace rttr;
struct MyStruct { MyStruct() {}; void func(double) {}; int data; };
RTTR_REGISTRATION
{
registration::class_<MyStruct>("MyStruct")
.constructor<>()
.property("data", &MyStruct::data)
.method("func", &MyStruct::func);
}
2.遍历成员变量和成员函数:
type t = type::get<MyStruct>();
for (auto& prop : t.get_properties())
std::cout << "name: " << prop.get_name() << std::endl;
for (auto& meth : t.get_methods())
std::cout << "name: " << meth.get_name() << std::endl;
3.构造类型:
type t = type::get_by_name("MyStruct");
variant var = t.create(); // will invoke the previously registered ctor
constructor ctor = t.get_constructor(); // 2nd way with the constructor class
var = ctor.invoke();
std::cout << var.get_type().get_name(); // prints 'MyStruct'
4.Set/Get属性:
MyStruct obj;
property prop = type::get(obj).get_property("data");
prop.set_value(obj, 23);
variant var_prop = prop.get_value(obj);
std::cout << var_prop.to_int(); // prints '23'
5.调用方法:
MyStruct obj;
method meth = type::get(obj).get_method("func");
meth.invoke(obj, 42.0);
variant var = type::get(obj).create();
meth.invoke(var, 42.0);
源码如下:
main.cpp
#include <rttr/registration>
#include <rttr/type.h>
#include <QDebug>
#include <iostream>
using namespace std;
using namespace rttr;
struct MyStruct{
MyStruct(){};
void func(double value){cout << "value: " << value << endl;};
int data;
};
int main(int argc, char *argv[]){
qDebug() << "RTTR Demo";
//manual registration
registration::class_<MyStruct>("MyStruct")
.constructor<>()
.property("data", &MyStruct::data)
.method("func", &MyStruct::func);
//iterate over members
{
type t = type::get<MyStruct>();
for(auto &prop : t.get_properties())
cout << "name: " << prop.get_name() << endl;
for(auto &meth : t.get_methods())
cout << "name: " << meth.get_name() << endl;
}
qDebug() << "---------------------------";
//constructing type
{
type t = type::get_by_name("MyStruct");
variant var = t.create();
constructor ctor = t.get_constructor();
var = ctor.invoke();
cout << var.get_type().get_name() << endl;
}
qDebug() << "---------------------------";
//Set/get properties
{
MyStruct obj;
property prop = type::get(obj).get_property("data");
prop.set_value(obj, 23);
variant var_prop = prop.get_value(obj);
cout << var_prop.to_int() << endl;
}
qDebug() << "---------------------------";
//Invoke methods
{
MyStruct obj;
method meth = type::get(obj).get_method("func");
meth.invoke(obj, 42.0);
variant var = type::get(obj).create();
meth.invoke(var, 43.0);
}
return 0;
}
RTTRDemo.pro
QT += core
QT -= gui
CONFIG += c++11
TARGET = RTTRDemo
CONFIG += console
CONFIG -= app_bundle
TEMPLATE = app
SOURCES += main.cpp
INCLUDEPATH += /xxxx/xxxx/CFFPro/rttr-0.9.5-src/src
LIBS += -L/xxxx/xxxx/CFFPro/rttr-0.9.5-src/bin -lrttr_core
运行截图如下:
源码打包下载地址:
https://github.com/fengfanchen/Qt/tree/master/RTTRDemo