Sign in to follow this  
Dragon_Strike

automaticly calling functions for variables in struct

Recommended Posts

ive got like 20 different nested structures like this and more are to be made... struct Mesh { explicit Mesh(TiXmlElement* node) { LOAD_ELEMENT(node, source); LOAD_ELEMENT(node, vertices); LOAD_ELEMENT(node, triangles); } std::vector<Source> source; std::vector<Vertices> vertices; std::vector<Triangles> triangles; }; struct Geometry { explicit Geometry(TiXmlElement* node) { LOAD_ELEMENT(node, id); LOAD_ELEMENT(node, name); LOAD_ELEMENT(node, mesh); } std::string id; std::string name; std::vector<Mesh> mesh; }; all have exactly the same layout... the variables are defined and a function LOAD_ELEMENT(node, ...) is called for all the variables... is there anyway to do this automatically, without having to define it for every struct... using a base struct or templates or somethin. im using C++ EDIT::: edited the text [Edited by - Dragon_Strike on January 29, 2008 3:46:12 PM]

Share this post


Link to post
Share on other sites
I think something like this could do it:


struct nil_t {} nil;

template <typename car_t, typename cdr_t>
void loadPairFromElement(TiXmlElement* node, std::pair<car_t, cdr_t>& thing) {
LOAD_ELEMENT(node, thing.first);
loadPairFromElement(node, thing.second);
}

void loadPairFromElement(TiXmlElement* node, nil_t& thing) {}

struct Mesh: std::pair<std::vector<Source>, std::pair<std::vector<Vertices>, std::pair<std::vector<Triangles>, nil_t> > > {
// There is a funky syntax you can use with static references to alias the
// member names to members of the pair structs, but I don't remember how it
// worked, and I'm not sure it can be made to descend recursively into the
// nested pairs...
};

struct Geometry: std::pair<std::string, std::pair<std::string, std::pair<std::vector<Mesh>, nil_t> > > {
// similarly here
};



But you might want to consider just using something like Boost::preprocessor.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this