Boost logo

Boost :

Subject: Re: [boost] [property] interest in C# like properties for C++?
From: David Brownstein (dbrownstein_at_[hidden])
Date: 2009-10-21 17:17:45


Hi,
You have nailed several important points. Yes, for "private" setters and
getters a class name is required so that the class can be a friend. I also
share your belief that using function-style accessors rather than assignment
is preferable.

I'm attaching an include file that contains a Property class implementation
that supports this example notation:

struct TestClass
{
  TestClass():
    Alpha( 'a' ),
    Amount( 1001 ),
    AmountSetOnly( 1024 ),
    NumberGetOnly( 4096 )
  {};

  Property<bool> IsCreditCard;
  Property<int> Count;
  Property<int> Index;
  Property<char> Alpha;

  Property<int, reveal, conceal, TestClass> Number;
  Property<int, reveal, not_used> Amount;
  Property<int, not_used, reveal > AmountSetOnly;
  Property<int, conceal, not_used, TestClass> NumberGetOnly;
};

Here "reveal" is analogous to "public" and "conceal" to private. I'm also
attaching a test file, so it should be easy to build and see that this
works.

David

-----Original Message-----
From: boost-bounces_at_[hidden] [mailto:boost-bounces_at_[hidden]]
On Behalf Of Stefan Strasser
Sent: Tuesday, October 20, 2009 8:16 PM
To: boost_at_[hidden]
Subject: Re: [boost] [property] interest in C# like properties for C++?

Am Wednesday 21 October 2009 02:07:19 schrieb David Brownstein:
> Hi,
> I've been thinking about adding properties to C++ for some time, and I've
> written several different Property<T> classes, so I've been following this
> discussion with great interest. IMO C# properties are not as useful as
they
> should be; I think that ideally a property is a declarative expression
that
> automatically generates getter and/or setter code.
>
> For example I've been experimenting with a syntax that looks like this:
>
> struct Example
> {
> explicit Example( std::string& strName):
> name( strName ),
> count( 0 )
> {
> }
>
> Property< std::string, public_get, private_set> name;
> Property< int, public_get, public_set> count;
> };

ok, I can see the benefit of that, and it solves the
trivial-property-problem
much better than my TRIVIAL_PROPERTY macro. but wouldn't you need an
additional template argument "Example" so you could be-friend "Example" to
enable private access?

however, I would keep accessing the property compatible to the established
C++
property syntax.
so instead of...

> x.count = 5;
>
> int I = x.count;

...Example::count would be a function object and you'd write...

        x.count(5);
        int I = x.count();

...so you can exchange the property object with accessor functions at a
later
time, when you need non-trivial properties.
_______________________________________________
Unsubscribe & other changes:
http://lists.boost.org/mailman/listinfo.cgi/boost





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