• 【无标题】


    由于课程项目要求,要能够在程序运行时获取场景中对象的属性与方法,在网上了解到了反射机制,但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 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 points;
    36. int no_serialize = 100;
    37. RTTR_ENABLE(shape)
    38. };
    • 1

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

    
      
      
    1. RTTR_REGISTRATION
    2. {
    3. rttr::registration:: class_( "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")
    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")
    19. . constructor()(rttr::policy::ctor::as_object)
    20. . property( "x", &point2d::x)
    21. . property( "y", &point2d::y)
    22. ;
    23. rttr::registration:: enumeration( "color")
    24. (
    25. value( "red", color::red),
    26. value( "blue", color::blue),
    27. value( "green", color::green)
    28. );
    29. }
    • 1

    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( 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;
    • 1

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

    json_string = io::to_json(my_shape); 
      
      
    • 1

    结果:

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

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

    circle c_2("Circle #2");
      
      
    • 1

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

    io::from_json(json_string, c_2);
      
      
    • 1

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

  • 相关阅读:
    图鸟使用阿
    Xilinx IP 10G Ethernet PCS/PMA IP Core
    js echarts踩坑记录
    XAF新手入门 - 类型子系统(Types Info Subsystem)
    【论文翻译】增强复制状态机的两阶段提交协议
    【备忘】navicat mysql导入导出小经验
    10-QNX与Android双系统通讯之FDBUS(1)
    字符串总结
    idea搭建ssm项目全过程详解:
    Spring+SpringMVC+Mybatis的回顾
  • 原文地址:https://blog.csdn.net/sway913/article/details/127665233