I have a problem that I would like to solve in the most generic non-intrusive way possible (such as boost serialization). What I want is to access class members both via it's initially designed accessors but also via a "const char *"  key. In addition, I want to pass list of key's (via a vector of const char * or a single delimited multikey char *) to get nested member access.

For example:
...
// these should be equivalent
Point p;
int val;
val = p.x;
val = p.getX();
p.getMemberValue("x", val);

// there also
Rectangle r;
int val;
Point pVal;
r.getMemberValue("corner:x", val);
r.getMemberValue("corner", pVal);


I thought about having a templated function similar to the serialize() function called 'getMemberValue()', where each 'if' block below could be wrapped into a MACRO taking just the member name.

template< class T >
bool getMemberValue(const char *key, T & value)
{
        if(!strcmp(key, "member"))
        {
            value = this->member;
            return true;
        }
        return false;
}

This appears to work fine for one level deep but if I want the behavior such as the above rectangle example, I run into trouble. I end up with a function implementation like this for the 'Rectangle' class

template< Point >
bool getMemberValue(const char *key, Point & value)
{
        if(!strcmp(key, "corner"))
        {
            value = this->corner;  // Point
            return true;
        }
        if(!strcmp(key, "width"))
        {
            value = this->width;  // int
            return true;
        }
        if(!strcmp(key, "height"))
        {
            value = this->height; // int
            return true;
        }
        return false;
}

This fails because it ends up trying to set an integer value to a Point variable.

Anybody have any suggestions?


Joe