RTTR测试,反射测试说明

由于课程项目要求,要能够在程序运行时获取场景中对象的属性与方法,在网上了解到了反射机制,但C++都未能支持反射机制,在C++中要实现类似Java等语言的反射机制需要另外写代码保存类型相关信息,然后在运行时使用。所以了解到了RTTR, rttr提供对象的成员数据类型,通过rttr获取每一个对象的的属性,读取起数据类型和值,然后利用这些信息自动保存数据,也能利用这些信息自动解析数据。

在官网下载完成后,cmake即可。

rttr的使用 

1、首先写一些类或结构体


  
  1. // 颜色类
  2. enum class color
  3. {
  4. red,
  5. green,
  6. blue
  7. };
  8. // (x,y)点
  9. struct point2d
  10. {
  11. point2d() {}
  12. point2d( int x_, int y_) : x(x_), y(y_) {}
  13. int x = 0;
  14. int y = 0;
  15. };
  16. // 形状类
  17. struct shape
  18. {
  19. shape(std::string n) : name(n) {}
  20. void set_visible(bool v) { visible = v; }
  21. bool get_visible() const { return visible; }
  22. color color_ = color::blue;
  23. std::string name = "";
  24. point2d position;
  25. std::map<color, point2d> dictionary;
  26. RTTR_ENABLE()
  27. private:
  28. bool visible = false;
  29. };
  30. // 圆
  31. struct circle : shape
  32. {
  33. circle(std::string n) : shape(n) {}
  34. double radius = 5.2;
  35. std::vector<point2d> points;
  36. int no_serialize = 100;
  37. RTTR_ENABLE(shape)
  38. };

 2、手动注册属性方法和构造函数


  
  1. RTTR_REGISTRATION
  2. {
  3. rttr::registration:: class_<shape>( "shape")
  4. . property( "visible", &shape::get_visible, &shape::set_visible)
  5. . property( "color", &shape::color_)
  6. . property( "name", &shape::name)
  7. . property( "position", &shape::position)
  8. . property( "dictionary", &shape::dictionary)
  9. ;
  10. rttr::registration:: class_<circle>( "circle")
  11. . property( "radius", &circle::radius)
  12. . property( "points", &circle::points)
  13. . property( "no_serialize", &circle::no_serialize)
  14. (
  15. metadata( "NO_SERIALIZE", true)
  16. )
  17. ;
  18. rttr::registration:: class_<point2d>( "point2d")
  19. . constructor()(rttr::policy::ctor::as_object)
  20. . property( "x", &point2d::x)
  21. . property( "y", &point2d::y)
  22. ;
  23. rttr::registration:: enumeration<color>( "color")
  24. (
  25. value( "red", color::red),
  26. value( "blue", color::blue),
  27. value( "green", color::green)
  28. );
  29. }

3、使用1——将对象转为json数据

①先创建一个circle的对象


  
  1. circle c_1("Circle #1");
  2. shape& my_shape = c_1;
  3. c_1. set_visible( true);
  4. c_1.points = std:: vector<point2d>( 2, point2d( 1, 1));
  5. c_1.points[ 1].x = 23;
  6. c_1.points[ 1].y = 42;
  7. c_1.position.x = 12;
  8. c_1.position.y = 66;
  9. c_1.radius = 5.123;
  10. c_1.color_ = color::red;
  11. c_1.dictionary = { { {color::green, { 1, 2} }, {color::blue, { 3, 4} }, {color::red, { 5, 6} } } };
  12. c_1.no_serialize = 12345;

②转为json数据,只需要一句话

json_string = io::to_json(my_shape); 
  

结果:

 4、使用2——利用已有的json数据,新生成一个对象

接下来创建一个新的圆circle2,name="Circle #2"

circle c_2("Circle #2");
  

将circle1的json数据,反序列化后的内容用于给circle2的属性赋值

io::from_json(json_string, c_2);
  

输出circle2的json数据后,也能验证和circle的数据内容是一致的。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值