|
Boost : |
From: Douglas Gregor (gregod_at_[hidden])
Date: 2002-02-13 14:38:44
On Wednesday 13 February 2002 12:55 pm, you wrote:
> IMHO, the ONLY advantage of a 'property' in comparison with the good old:
>
> int const& foo() const { return m_foo ; }
> int & foo() { return m_foo ; }
> int m_foo ;
>
> is that the user don't have to write the extra '()'.
>
> I don't think that a property facility could add that much complexity just
> to allow syntactic sugar.
>
> Of course, I can be missing something and I'll be glad to know it.
There's no reason that properties be restricted to 'just' being syntactic
sugar (note: in my world, syntax is _everything_, so that's enough of an
argument for me already).
Since every property will know the address of the object containing it,
properties can also be used for setting values at run-time and for
reflection. Let's say we can do this (some abbreviations in the code):
class Window : public runtime_properties {
public:
union {
Window* self;
property< read<&Window::get_top>, write<&Window::set_top> > top;
property< read<&Window::get_left>, write<&Window::set_left> > left;
property< read<&Window::get_width>, write<&Window::set_width> > width;
property< read<&Window::get_height>, write<&Window::set_height> > height;
}
Window() :
self(this), top("Top", 0), left("Left", 0),
width("Width", 100), height("Height", 100)
{
}
};
The intention here is that the properties can register themselves with the
"runtime_properties" base class of the Window class. They tell the
runtime_properties base class:
1) Their human-readable names
2) Their default values (optional)
3) How to read and/or write their values
Properties such as this have been made language features before (see Delphi
or C++ Builder), and the degree to which one can interact with a component
via its properties is wonderful.
Did I mention that the syntax is nicer? :)
Doug
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk