Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 667679
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T23:57:18+00:00 2026-05-13T23:57:18+00:00

I need start off with code because I am not sure what terminology to

  • 0

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;
 };
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T23:57:19+00:00Added an answer on May 13, 2026 at 11:57 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Normally to attach a debuger to a running jvm you would need start the
I need to start messing with a vb.net(vs2008) project file and I'd like a
I need to start and stop SQL Server from the command line. I am
I need to start developing in FORTRAN, any recommendations on learning\developing FORTRAN coming from
For RMI on server-side, do we need to start rmiregistry program, or just call
i need to fire an event (or start a workflow) when the permissions of
I need to list all files whose names start with 'SomeLongString'. But the case
I need a way to tell ASP.NET Kill the current session and start over
How does one start development in Silverlight? Does one need a new IDE? or
I really don't like the VS2008 Start Page. I don't need the RSS reader,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.