rttrorg / rttr

C++ Reflection Library
https://www.rttr.org
MIT License
3.12k stars 430 forks source link

change the value using property:: set_value(...) #311

Closed NePutin94 closed 3 years ago

NePutin94 commented 3 years ago

i have two classes, one class contains an object of the other. class A { public: float x; }; class B { public: A a; }; I'm trying to do something like this:

B b;
rttr::variant val = b;
for(auto& p : val.get_type().get_properties())
{
   auto p_value = p.get_value(val);
   for(auto& p2 : p_value.get_type().get_properties())
   {
      bool s = p2.set_value(b, 6.f);
   }
}

And it doesn't work. Is it even possible?

NePutin94 commented 3 years ago

As I understood, I have to pass an instance of the type whose field I want to change. If I want to change the x field in class A, I have to do this: p2.set_value(b.a, 6.f);. But how to use it when, for example, an object of some type T is passed to the function and this type contains some object whose field we want to change. That is, we can't pass the instance of this object to set_value because we don't know anything about the type T.

NePutin94 commented 3 years ago

Apparently, variant does not refer to the object that is passed to it in any way. I have found a solution to my problem:

auto val2 = p.get_value(val);
p2.set_value(val2, 12.f);
p.set_value(test, val2);