views:

93

answers:

2

I need start off with code because I am not sure what terminology to use. Lets say I have the following code:

 class Node
 { 
 public:
  void Parse(rapidxml::xml_node<> *node) 
  {
   for (rapidxml::xml_attribute<> *attr = node->first_attribute();
        attr;
        attr = attr->next_attribute())
   {
    std::stringstream converter;
    converter << attr->value();

    if( !strcmp(attr->name(), "x") ) converter >> x;
    else if( !strcmp(attr->name(),"y") ) converter >> y;
    else if( !strcmp(attr->name(), "z") ) converter >> z;
   }
  }

 private:
  float x;
  float y;
  float z;
 };

What I can't stand is the repetition of if( !strcmp(attr->name(), "x") ) converter >> x; I feel that this is error prone and monotonous, but I cannot think of another way to map a string value to a member assignment. What are some other approaches one can take to avoid code such as this? The only other possible alternative I could think of was to use a hashmap, but that runs into problems with callbacks

This is the best I could up with but it's not as flexible as I'd like:

 class Node
 {
  Node() : x(0.0f), y(0.0f), z(0.0f) 
  {
   assignmentMap["x"] = &x;
   assignmentMap["y"] = &y;
   assignmentMap["z"] = &z;
  }

 public:
  void Parse(rapidxml::xml_node<> *node) 
  {
   for (rapidxml::xml_attribute<> *attr = node->first_attribute();
        attr;
        attr = attr->next_attribute())
   {
    map<std::string, float*>::iterator member = assignmentMap.find(attr->name());
    //check for a pre-existing entry
    if( member == assignmentMap.end()) continue;

    std::stringstream converter;
    converter << attr->value();
    converter >> *(member->second);
   }
  }

 private:
  float x;
  float y;
  float z;

  std::map<std::string, float*> assignmentMap;
 };
+4  A: 

For the implementation with a map, you could use pointers-to-members. Then you won't need a deep copy of the map (when you copy it, the pointers in the map still point into the original Node), and it will also allow you to make the whole thing static (this map is unnecessary at per-instance basis).

For example:

class Node {
    //...
    static std::map<std::string, float Node::*> initVarMap();
    static float Node::* varFromName(const std::string& name);
};

std::map<std::string, float Node::*> Node::initVarMap()
{
    std::map<std::string, float Node::*> varMap;
    varMap["x"] = &Node::x;
    varMap["y"] = &Node::y;
    varMap["z"] = &Node::z;
    return varMap;
}

float Node::* Node::varFromName(const std::string& name)
{
    static std::map<std::string, float Node::*> varMap = initVarMap();
    std::map<std::string, float Node::*>::const_iterator it = varMap.find(name);
    return it != varMap.end() ? it->second : NULL;
}

Usage:

    float Node::* member(varFromName(s));
    if (member)
        this->*member = xyz;

This isn't any more flexible, though.

To support different types of members, you might modify the above to use a map of string to "variant of all supported member types".

For example so. The member setter visitor should be reusable, and the only change to the code, to add or change member types, should be done to the typedef.

 #include <map>
 #include <string>
 #include <iostream>
 #include <boost/variant.hpp>

template <class Obj, class T>
struct MemberSetter: boost::static_visitor<void>
{
    Obj* obj;
    const T* value;
public:
    MemberSetter(Obj* obj, const T* value): obj(obj), value(value) {}

    void operator()(T Obj::*member) const
    {
        obj->*member = *value;
    }
    template <class U>
    void operator()(U Obj::*) const
    {
        //type mismatch: handle error (or attempt conversion?)
    }
};

class Node
{
public:
    Node() : i(0), f(0.0f), d(0.0f)
    {
    }

    template <class T>
    void set(const std::string& s, T value)
    {
        std::map<std::string, MemberTypes>::const_iterator it = varMap.find(s);
        if (it != varMap.end()) {
            boost::apply_visitor(MemberSetter<Node, T>(this, &value), it->second);
        } //else handle error
    }
    void report() const
    {
        std::cout << i << ' ' << f << ' ' << d << '\n';
    }
private:
    int i;
    float f;
    double d;

    typedef boost::variant<int Node::*, float Node::*, double Node::*> MemberTypes;
    static std::map<std::string, MemberTypes> initVarMap();
    static std::map<std::string, MemberTypes> varMap;
};

int main()
{
    Node a;
    a.set("i", 3);
    a.set("d", 4.5);
    a.set("f", 1.5f);
    a.report();
}

std::map<std::string, Node::MemberTypes> Node::initVarMap()
{
    std::map<std::string, Node::MemberTypes> varMap;
    varMap["i"] = &Node::i;
    varMap["f"] = &Node::f;
    varMap["d"] = &Node::d;
    return varMap;
}

std::map<std::string, Node::MemberTypes> Node::varMap = Node::initVarMap();

This is naturally just an example of what you can do. You can write a static_visitor to do what you want. E.g storing a stream and attempting to extract a value of the right type for the given member.

UncleBens
Thank you, now I need to go research the boost libraries and find out exactly how this works.
Apeiron
A: 

Use an array. An alternative to this union would be to let x, y, and z be references (float&) to array elements 0, 1, 2 — or (my preference) always call them by number not by name.

class Node
 { 
 public:
  void Parse(rapidxml::xml_node<> *node) 
  {
   std::stringstream converter;

   for (rapidxml::xml_attribute<> *attr = node->first_attribute();
        attr;
        attr = attr->next_attribute())
   {
    if ( strlen( attr->name() ) != 1
     || *attr->name() < 'x' || *attr->name() > 'z' )
        throw rapidxml::parse_error; // or whatever

    converter << attr->value() >> ary[ *attr->name() - 'x' ];
   }
  }

 private:
  union {
    float ary[3]; // this can come in handy elsewhere
    struct {
      float x;
      float y;
      float z;
    } dim;
 };
Potatoswatter