Re: C++ Templates - passing a pointer to the member to that member's base class
<jonas.andero@hotmail.com> wrote:
I'm writing a little framework for interface programming, where the
interfaces are represented by template classes with modern features
like properties ? la C#. While these properties are member variables
with Get and Set operations, it is the implementing class that shall
handle these calls and hence I want Get and Set to be members of the
class that implements the interface.
The folowing code is an example what I want to achive (simplfied).
First the interface class and then an example with a class that
declares a property "time_pos".
=>
You could do something like:
template <class C,class T,class Tag>
class Property
{
T C::*pmp;
C *c;
public:
void init(C *a, T C::*b,Tag)
{
pmp = b;
c = a;
}
T get() const { return c->*pmp;}
void set(T x) { c->*pmp;}
};
template <class C,class T,class Tag>
inline
void do_init(C *c,T C::*pmp,Tag tag)
{
Property<C,T,Tag> *p=static_cast<Property<C,T,Tag> *>(c);
p->init(c,pmp,tag);
}
struct tag{};
class implimentation:public Property<implimentation,int,tag>
{
int time_pos;
public:
implimentation()
{
do_init(this,&implimentation::time_pos,tag());
}
};
struct tag2{};
class B:public Property<B,int,tag>,public Property<B,int,tag2>
{
int x;
int y;
public:
B()
{
do_init(this,&B::x,tag());
do_init(this,&B::y,tag2());
}
};
int main()
{
implimentation a;
B b;
}
the tag args are just to differ the Property classes for same T C::*
types.
free do_get and do_set functions with the same arguments as do_init and
similiar bodies, can be writtten. but still it looks like a pain to me.
the free functions eliminate ambiguities that using the member functions
directly seem to have.
Boost fusion probably can be used with some type list manipulation to
make this more automatic, but the old compiler I used to test this did
not take member data pointers as template args, and does not like
boost::mpl or boost::fusion,whicn would create a tuple like structure
and iterate in more like stl for_each.
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]