rttrorg / rttr

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

How to register properties of nested class #347

Closed sdaheng closed 1 year ago

sdaheng commented 1 year ago

I have three classes looks like below:


namespace ns {
namespace np {
class C;
class D;

class A {
public:
    int a;
    int b;
    C c;
    D d;
};

class C {
public:
    float e;
    float f;
};

class D {
public:
    double g;
    int h;
};
}
}

I only register the generic type property of A right now, I have no idea how to register properties of class C and D.

The registration code looks like below:

registration::class_<ns::np::A>("ns::np::A")
    .constructor<>()
    .property("a", &ns::np::A::a)
    .property("b", &ns::np::A::b)
    ;

Can I register property like this?:

.property("c.e", &ns::np::C)
auto instance = ns::np::A();
type aType = type::get(instance);

aType.get_property("a").set_value(instance, (int)22); // ok
aType.get_property("b").set_value(instance, (int)23); // ok

aType.get_property("c.e").set_value(rttr::instance(aType.get_property_value("c", instance)), (float)33.33) // ok, **but not actually setting the value to c.e**