Boost logo

Boost :

From: Stefan Strasser (sstrasser_at_[hidden])
Date: 2005-04-09 18:15:51


Hi,

I'd like to know if there is interest in developing a small library for
inclusion in boost to emulate properties, something like:

struct A{
   void SetVar(int a){ cerr << "set " << a << endl; }
   int GetVar() const{ cerr << "get" << endl; return 0; }
   BOOST_PROPERTY(A,int,Var,SetVar,GetVar);
};

A a;
a.Var=3; //calls SetVar

here's a proof-of-concept implementation which works with gcc 3.4 and
intel cc:

#define BOOST_PROPERTY(ClassT,T,name,setter,getter) \
   struct _##name##_offset; \
   property<T,ClassT,_##name##_offset,&ClassT::setter,&ClassT::getter>
name; \
   struct _##name##_offset{ \
     size_t operator()() const{ \
       return (size_t) &((ClassT *)0)->name; \
     } \
   }

template<typename T,typename ClassT,typename offset,void
(ClassT::*setter)(T),T (ClassT::*getter)() const>
class property{
public:
   property &operator=(T const &t){
     unsigned int temp=(unsigned int)this;
     temp-=offset()();
     ClassT *this_=(ClassT *)temp;
     (this_->*setter)(t);
     return *this;
   }
};

Regards,

-- 
Stefan Strasser

Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk