Boost logo

Boost :

Subject: Re: [boost] [property] interest in C# like properties for C++?
From: vicente.botet (vicente.botet_at_[hidden])
Date: 2009-10-21 18:55:58


Hi,
----- Original Message -----
From: "Gennadiy Rozental" <rogeeff_at_[hidden]>
To: <boost_at_[hidden]>
Sent: Wednesday, October 21, 2009 10:50 PM
Subject: Re: [boost] [property] interest in C# like properties for C++?

>
>> A.X() = B.X() = 5 isn't horrible, but A.X = B.X = 5 is what one expects when
> one thinks of "properties."
>
> Sorry, if I jump in in the middle of the discussion.
>
> Boost.Test uses properties with syntax like this:
>
> int i = a.p_X;
>
> a.p_X.value = 1;
>
> This syntax IMO has couple advantages:
>
> 1. From encapsulation standpoint this is as good as having explicit setter and
> getter.
>
> 2. I do not like to allow a.p_X = 1. ".value" syntax allows me to grep easily
> for all the encounters of property modification. This is especially useful in
> situations where there are myriads of read-only access and only couple mutating
> ones.
>
> 3. Single line definition:
>
> readwrite_property<int> p_X;
>
> Instead of 3 lines required by data member, getter and setter.
>
> 4. I also have readonly_property, which can restrict mutating access to the
> property value for all classes (in which case property initialized in a
> constructor once and never changes). Or I can have readonly property with fixed
> list of classes allowed mutating access.

I would name your class readwrite_data<> and read_only_data<> as there is no possibility to provide accessors, or are there? The main aim of a Property is exactly that, ability to provide read/write accessors to some property.

I don't like too much your use of the .value to repere assignements, as the user can also use it to get the value. If you want to repare assignation to a data you should use a explicit function.

I see one advantage to wrap some data type in a readwrite<T> or readonly<T> is that these classes can provide several styles:
* Behave like builtin types (transparent): use assignement and conversion operators
        a.p_X = 1;
        i = a.p_X
* Function like syntax for both setters and getters: use operator() to get reference or value
        a.p_X()=1;
        i = a.p_X();
* Different setter/getter methods names
        a.p_X.set_value(1);
        i = a.p_X.get_value();

Whether a single class should provide all the styles or only a particular need to be analysed, as each style has its own advantage and liabilities. Note for example that, set_value is reperable and can not be used to get the value and get_value is not ambiguous.

Another way to repare the assignements is to use a free function assign_to, like we use swap, but this is out of the scope of this thread, so I will start a new one.

Best,
Vicente


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