rttr icon indicating copy to clipboard operation
rttr copied to clipboard

Recusive traversal of structs

Open perara opened this issue 5 years ago • 0 comments

Hi,

I'm trying to do a rather "simple" travels through nested structs, defined as follows:

struct State{
    float time = 0.0;
	int sequence = 0;
	int version = 1;
	Player player = Player();
    State(){};
};
struct Player{
	int level = 0;
    Player(){};
};

whereas I've defined the following registration


RTTR_REGISTRATION
{


    rttr::registration::class_<State>("State")
        .constructor<>()
        .property("time", &State::time)
		.property("sequence", &State::sequence)
		.property("version", &State::version)
		.property("player", &State::player)

    rttr::registration::class_<Player>("Player")
        .constructor<>()
       .property("level", &Player::level);

}
        

Now, I would like to traverse from State and visit all properties along the way:

1. State.sequence
2. State.version
3. State.player
4. Player.level

I am able to do this at "type level" but I would also like to set values along the way. How can I achieve this?

So far, I've been able to set up the following, which successfully traverse as I want, however, I also want to access properties of the objects along the way and set/get values.

 std::vector<std::string> types = {};
    std::list<rttr::type> queue = {rttr::type::get(state)};
    while(!queue.empty()){
        auto& cls = queue.front();
        queue.pop_front();

        for (auto& prop : cls.get_properties()){

            if(prop.get_type().is_class()){
                queue.push_back(rttr::type::get(prop));
                 // Here I find that the type is a class and I then wish to visit this and set values
            }else{
                types.emplace_back(prop.get_type().get_name());
               // Here I would like to set values. First check its type, then set values accordingly.
            }
        }

    }


perara avatar Oct 11 '20 13:10 perara