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的数据内容是一致的。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以使用反射技术来获取类成员中的属性并以字符串方式输出。反射是一种在运行时检查、访问和修改程序元素的能力。在C++11中,可以使用一些库来实现反射,例如Boost.Reflection和RTTR(Run Time Type Reflection)。 以下是一个使用Boost.Reflection库的示例代码,可以获取类成员中的属性并以字符串方式输出: ```c++ #include <iostream> #include <boost/reflection.hpp> class MyClass { public: MyClass(int id, const std::string& name) : id_(id), name_(name) {} int get_id() const { return id_; } const std::string& get_name() const { return name_; } private: int id_; std::string name_; }; int main() { MyClass obj(1, "Alice"); boost::reflection::Object obj_ref(obj); boost::reflection::Type obj_type = obj_ref.GetType(); std::vector<boost::reflection::Property> properties = obj_type.GetProperties(); for (const auto& property : properties) { std::cout << property.GetName() << ": " << property.GetValue(obj_ref) << std::endl; } return 0; } ``` 在上面的示例代码中,我们定义了一个名为MyClass的类,并在其中定义了成员变量id_和name_,以及成员函数get_id()和get_name()。接下来,在main函数中,我们创建了一个MyClass对象,并使用boost::reflection::Object类来获取对象的引用。然后,我们使用boost::reflection::Type类来获取对象的类型,并使用GetProperties()函数获取对象的所有属性。最后,我们使用属性的名称和GetValue()函数来输出属性的值。 需要注意的是,Boost.Reflection是一个第三方库,需要在项目中添加相应的头文件和链接库。如果不想使用第三方库,也可以手动实现反射功能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值