Boost logo

Boost :

From: Hamish Mackenzie (boost_at_[hidden])
Date: 2002-02-14 09:45:19


On Wed, 2002-02-13 at 09:01, Geurt Vos wrote:
> For the declaration I'm thinking of something like:
>
> boost::property<
> boost::read<int,A,&A::get_it>,
> boost::write<int,A,&A::set_it>
> > value;
In order to avoid the storage overhead how about calling it
property_proxy and returning it from a member function ie.

class A
{
public:
  typedef boost::property_proxy<
     boost::read<int,A,&A::get_value>,
     boost::write<int,A,&A::set_value>
> value_proxy;

  value_proxy value()
    { return value_proxy( this ); }
  typename value_proxy::const_proxy value() const
    { return typename value_proxy::const_proxy( this ); }
};

Where const_proxy is a version that stores a const A * and only allows
calls to the read function.

You could make this sorter one of two ways

a) Use multiple inheritance

class value {};

class A : public
  property_owner< A, int, value, &A::get_value, &A::set_value >,
  property_lookup< A >
{
public:
} x;

property_lookup would have provide a member function "property" which
could be used like this

A x;
x.property< value >() = 7

b) Use a macro. Yuk but it might be the simplest solution.

Should it use boost::noncopyable?

Another question is whether or not the property proxy should be
copyable. If the proxy can be copied then it would allow delayed calls
to get_it and set_it. This may be counter intuitive and complicate
threading issues but it also might be very powerful.

I personaly think it should be posible but only if the user explicitly
requests it. This could be accomplished as follows

1) Make boost::property_proxy non copyable

2) Introduce a boost::property_ref that is copyable and can be
explicitly constructed from property_proxy

3) Have a common property class for constructing proxy, ref, const_proxy
and const_ref.

class A
{
public:
  typedef boost::property<
     boost::read<int,A,&A::get_value>,
     boost::write<int,A,&A::set_value>
> value_propery;

  typename value_propery::proxy value()
    { return value_propery::proxy( this ); }
  typename value_propery::const_proxy value() const
    { return value_propery::const_proxy( this ); }
};

User of the class can then store a reference to a property using

A x;
typename A::value_property::ref r( x.value() );
...
int v = r; // calls get_value on x

Hamish Mackenzie


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