Boost logo

Boost Users :

From: Ovanes Markarian (om_boost_at_[hidden])
Date: 2008-01-29 09:45:01


Yes, you can define your types based on const char* and overload template
functions on them.

//using structs to skip explicit public access specifiers

///header
extern const char SomeName[];

template<const char*>
struct TypeId
{};

typedef TypeId<SomeName> MemberX_Accessor;

struct ClassWithMembers
{
    void access(MemberX_Accessor)
    {
      ...
    }

    template<class T>
    void access(T)
    {
        /// issue error here, no accessor specified!!!
    }

   private:
      MemberX x;
};

///cpp
extern const char SomeName[] ="memberX";

I hope that example brings you to some useful ideas.

Regards,
Ovanes

On Jan 29, 2008 3:34 PM, Joseph Fradley <joe.fradley_at_[hidden]> wrote:

>
> 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
>
>
>
>
> _______________________________________________
> Boost-users mailing list
> Boost-users_at_[hidden]
> http://lists.boost.org/mailman/listinfo.cgi/boost-users
>



Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net