Jump to content
  • Advertisement
Sign in to follow this  

automaticly calling functions for variables in struct

This topic is 3916 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!